Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/dodrio/counter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "dodrio-counter"
version = "0.1.0"
authors = ["Nick Fitzgerald <fitzgen@gmail.com>"]
edition = "2018"
edition = "2021"

[lib]
crate-type = ["cdylib"]
Expand Down
2 changes: 1 addition & 1 deletion examples/dodrio/todomvc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
authors = ["Nick Fitzgerald <fitzgen@gmail.com>"]
edition = "2018"
edition = "2021"
name = "dodrio-todomvc"
version = "0.1.0"

Expand Down
2 changes: 1 addition & 1 deletion examples/iron/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "typed-html-iron-test"
version = "0.1.0"
edition = "2018"
edition = "2021"
authors = ["Bodil Stokke <bodil@bodil.org>"]

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion examples/stdweb/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "typed-html-stdweb-test"
version = "0.1.0"
edition = "2018"
edition = "2021"
authors = ["Bodil Stokke <bodil@bodil.org>"]

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "typed-html-macros"
version = "0.2.2"
edition = "2018"
edition = "2021"
authors = ["Bodil Stokke <bodil@bodil.org>"]
build = "build.rs"
license = "MPL-2.0+"
Expand Down
25 changes: 7 additions & 18 deletions macros/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ pub fn global_attrs(span: Span) -> StringyMap<Ident, TokenStream> {

insert("accesskey", "String");
insert("autocapitalize", "String");
insert("contenteditable", "crate::types::Bool");
insert("contenteditable", "crate::types::EnumeratedBool");
insert("contextmenu", "crate::types::Id");
insert("dir", "crate::types::TextDirection");
insert("draggable", "crate::types::Bool");
insert("draggable", "crate::types::EnumeratedBool");
insert("hidden", "crate::types::Bool");
insert("is", "String");
insert("itemprop", "String");
insert("itemscope", "crate::types::Bool");
insert("itemtype", "String");
insert("lang", "crate::types::LanguageTag");
insert("role", "crate::types::Role");
insert("style", "String");
Expand All @@ -39,20 +42,6 @@ pub fn global_attrs(span: Span) -> StringyMap<Ident, TokenStream> {
}

pub static SELF_CLOSING: &[&str] = &[
"area",
"base",
"br",
"col",
"command",
"embed",
"hr",
"img",
"input",
"keygen",
"link",
"meta",
"param",
"source",
"track",
"wbr",
"area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link",
"meta", "param", "source", "track", "wbr",
];
50 changes: 19 additions & 31 deletions macros/src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Declare {
pub attrs: StringyMap<Ident, TokenStream>,
pub req_children: Vec<Ident>,
pub opt_children: Option<TokenStream>,
pub opt_children_dyn: bool,
pub traits: Vec<TokenStream>,
}

Expand All @@ -24,6 +25,7 @@ impl Declare {
attrs: global_attrs(name.span()),
req_children: Vec::new(),
opt_children: None,
opt_children_dyn: false,
traits: Vec::new(),
name,
}
Expand All @@ -34,11 +36,7 @@ impl Declare {
}

fn attr_type_name(&self) -> TokenTree {
Ident::new(
&format!("Attrs_{}", self.name),
self.name.span(),
)
.into()
Ident::new(&format!("Attrs_{}", self.name), self.name.span()).into()
}

fn attrs(&self) -> impl Iterator<Item = (TokenTree, TokenStream, TokenTree)> + '_ {
Expand Down Expand Up @@ -99,7 +97,12 @@ impl Declare {

if let Some(child_constraint) = &self.opt_children {
let child_constraint = child_constraint.clone();
body.extend(quote!(pub children: Vec<Box<#child_constraint<T>>>,));
let child_dyn = if self.opt_children_dyn {
quote!(dyn)
} else {
quote!()
};
body.extend(quote!(pub children: Vec<Box<#child_dyn #child_constraint<T>>>,));
}

quote!(
Expand Down Expand Up @@ -170,7 +173,7 @@ impl Declare {
for (attr_name, _, attr_str) in self.attrs() {
push_attrs.extend(quote!(
if let Some(ref value) = self.attrs.#attr_name {
attributes.push((#attr_str, value.to_string()));
attributes.push((#attr_str, value.to_string_value()));
}
));
}
Expand Down Expand Up @@ -219,7 +222,7 @@ impl Declare {
for (attr_name, _, attr_str) in self.attrs() {
push_attrs.extend(quote!(
if let Some(ref value) = self.attrs.#attr_name {
out.push((#attr_str, value.to_string()));
out.push((#attr_str, value.to_string_value()));
}
));
}
Expand Down Expand Up @@ -303,26 +306,14 @@ impl Declare {
}

let print_children = if self.req_children.is_empty() {
if self.opt_children.is_some() {
if !SELF_CLOSING.contains(&elem_name.to_string().as_str()) {
quote!(
write!(f, ">")?;
#print_opt_children
write!(f, "</{}>", #name)
)
} else {
quote!(if self.children.is_empty() {
write!(f, " />")
} else {
write!(f, ">")?;
#print_opt_children
write!(f, "</{}>", #name)
})
}
} else if !SELF_CLOSING.contains(&elem_name.to_string().as_str()) {
quote!(write!(f, "></{}>", #name))
if SELF_CLOSING.contains(&elem_name.to_string().as_str()) {
quote!(write!(f, ">"))
} else {
quote!(write!(f, "/>"))
quote!(
write!(f, ">")?;
#print_opt_children
write!(f, "</{}>", #name)
)
}
} else {
quote!(
Expand All @@ -337,10 +328,7 @@ impl Declare {
for (attr_name, _, attr_str) in self.attrs() {
print_attrs.extend(quote!(
if let Some(ref value) = self.attrs.#attr_name {
let value = crate::escape_html_attribute(value.to_string());
if !value.is_empty() {
write!(f, " {}=\"{}\"", #attr_str, value)?;
}
value.write_attribute(#attr_str, f)?;
}
));
}
Expand Down
8 changes: 5 additions & 3 deletions macros/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ IdentList = "[" <Separated<Ident, ",">> "]";

Groups = "in" <TypePathList>;

Children: (Vec<Ident>, Option<Vec<Token>>) = "with" <req:IdentList?> <opt:TypePath?> => {
(req.unwrap_or_else(|| Vec::new()), opt)
Children: (Vec<Ident>, bool, Option<Vec<Token>>) = "with" <req:IdentList?> <has_dyn:"dyn"?> <opt:TypePath?> => {
(req.unwrap_or_else(|| Vec::new()), has_dyn.is_some(), opt)
};

Declaration: Declare = <name:HtmlIdent> <attrs:Attributes?> <groups:Groups?> <children:Children?> ";" => {
Expand All @@ -288,8 +288,9 @@ Declaration: Declare = <name:HtmlIdent> <attrs:Attributes?> <groups:Groups?> <ch
decl.traits.push(to_stream(group));
}
}
if let Some((req_children, opt_children)) = children {
if let Some((req_children, has_dyn, opt_children)) = children {
decl.req_children = req_children;
decl.opt_children_dyn = has_dyn;
decl.opt_children = opt_children.map(to_stream);
}
decl
Expand Down Expand Up @@ -321,6 +322,7 @@ extern {
"]" => Token::GroupClose(Delimiter::Bracket, _),
"in" => Token::Keyword(lexer::Keyword::In, _),
"with" => Token::Keyword(lexer::Keyword::With, _),
"dyn" => Token::Keyword(lexer::Keyword::Dyn, _),
IdentToken => Token::Ident(_),
LiteralToken => Token::Literal(_),
ParenGroupToken => Token::Group(Delimiter::Parenthesis, _),
Expand Down
2 changes: 1 addition & 1 deletion macros/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ impl Element {
let key_str = TokenTree::from(Literal::string(&key_str));
builder.extend(quote!(
let attr_value = dodrio::bumpalo::format!(
in &#bump, "{}", element.attrs.#key.unwrap());
in &#bump, "{}", typed_html::types::DisplayAttribute::to_string_value(&element.attrs.#key.unwrap()));
if !attr_value.is_empty() {
attr_list.push(dodrio::builder::attr(#key_str, attr_value.into_bump_str()));
}
Expand Down
3 changes: 3 additions & 0 deletions macros/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ impl From<Group> for Token {
pub enum Keyword {
In,
With,
Dyn,
}

pub fn keywordise(tokens: Vec<Token>) -> Vec<Token> {
Expand All @@ -94,6 +95,8 @@ pub fn keywordise(tokens: Vec<Token>) -> Vec<Token> {
Token::Keyword(Keyword::In, ident)
} else if name == "with" {
Token::Keyword(Keyword::With, ident)
} else if name == "dyn" {
Token::Keyword(Keyword::Dyn, ident)
} else {
Token::Ident(ident)
}
Expand Down
2 changes: 1 addition & 1 deletion typed-html/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "typed-html"
version = "0.2.2"
edition = "2018"
edition = "2021"
authors = ["Bodil Stokke <bodil@bodil.org>"]
license = "MPL-2.0+"
description = "Type checked JSX for Rust"
Expand Down
Loading