feat: full html support

This commit is contained in:
Jonathan Kelley committed 2021-10-19 22:39:27 -04:00
1 parent e86c1d8972
commit 79503f15c5
10 files changed
+343 -73

No files matched your search

+60 -24
View File
@@ -26,7 +26,7 @@ impl Parse for AmbiguousElement<AS_RSX> {
if input.peek(Token![::]) {
return input
.parse::<Component<AS_RSX>>()
.map(|c| AmbiguousElement::Component(c));
.map(AmbiguousElement::Component);
}
// If not an absolute path, then parse the ident and check if it's a valid tag
@@ -35,7 +35,7 @@ impl Parse for AmbiguousElement<AS_RSX> {
if pat.segments.len() > 1 {
return input
.parse::<Component<AS_RSX>>()
.map(|c| AmbiguousElement::Component(c));
.map(AmbiguousElement::Component);
}
}
@@ -47,11 +47,11 @@ impl Parse for AmbiguousElement<AS_RSX> {
if first_char.is_ascii_uppercase() {
input
.parse::<Component<AS_RSX>>()
.map(|c| AmbiguousElement::Component(c))
.map(AmbiguousElement::Component)
} else {
input
.parse::<Element<AS_RSX>>()
.map(|c| AmbiguousElement::Element(c))
.map(AmbiguousElement::Element)
}
} else {
Err(Error::new(input.span(), "Not a valid Html tag"))
@@ -61,26 +61,11 @@ impl Parse for AmbiguousElement<AS_RSX> {
impl Parse for AmbiguousElement<AS_HTML> {
fn parse(input: ParseStream) -> Result<Self> {
// Try to parse as an absolute path and immediately defer to the componetn
if input.peek(Token![::]) {
return input
.parse::<Component<AS_HTML>>()
.map(|c| AmbiguousElement::Component(c));
}
// If not an absolute path, then parse the ident and check if it's a valid tag
if let Ok(pat) = input.fork().parse::<syn::Path>() {
if pat.segments.len() > 1 {
return input
.parse::<Component<AS_HTML>>()
.map(|c| AmbiguousElement::Component(c));
}
}
use syn::ext::IdentExt;
if let Ok(name) = input.fork().call(Ident::parse_any) {
let name_str = name.to_string();
if input.peek(Token![<]) {
let forked = input.fork();
forked.parse::<Token![<]>().unwrap();
let tag = forked.parse::<Ident>()?;
let name_str = tag.to_string();
let first_char = name_str.chars().next().unwrap();
if first_char.is_ascii_uppercase() {
@@ -95,6 +80,57 @@ impl Parse for AmbiguousElement<AS_HTML> {
} else {
Err(Error::new(input.span(), "Not a valid Html tag"))
}
// input.parse::<Token![>]>()?;
// let mut children = Vec::new();
// while !input.peek(Token![<]) {
// children.push(input.parse::<BodyNode<AS_HTML>>()?);
// }
// Ok(AmbiguousElement::Element(Element {
// name,
// key: todo!(),
// attributes: todo!(),
// listeners: todo!(),
// children,
// _is_static: todo!(),
// }))
// // Try to parse as an absolute path and immediately defer to the componetn
// if input.peek(Token![::]) {
// return input
// .parse::<Component<AS_HTML>>()
// .map(AmbiguousElement::Component);
// }
// // If not an absolute path, then parse the ident and check if it's a valid tag
// if let Ok(pat) = input.fork().parse::<syn::Path>() {
// if pat.segments.len() > 1 {
// return input
// .parse::<Component<AS_HTML>>()
// .map(AmbiguousElement::Component);
// }
// }
// use syn::ext::IdentExt;
// if let Ok(name) = input.fork().call(Ident::parse_any) {
// let name_str = name.to_string();
// let first_char = name_str.chars().next().unwrap();
// if first_char.is_ascii_uppercase() {
// input
// .parse::<Component<AS_HTML>>()
// .map(AmbiguousElement::Component)
// } else {
// input
// .parse::<Element<AS_HTML>>()
// .map(AmbiguousElement::Element)
// }
// } else {
// Err(Error::new(input.span(), "Not a valid Html tag"))
// }
}
}
+10 -8
View File
@@ -1,3 +1,4 @@
use bumpalo::Bump;
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, ToTokens, TokenStreamExt};
use syn::{
@@ -7,17 +8,17 @@ use syn::{
use super::*;
pub struct RsxBody<const AS: HtmlOrRsx> {
pub struct CallBody<const AS: HtmlOrRsx> {
custom_context: Option<Ident>,
roots: Vec<BodyNode<AS>>,
}
/// The custom rusty variant of parsing rsx!
impl Parse for RsxBody<AS_RSX> {
impl Parse for CallBody<AS_RSX> {
fn parse(input: ParseStream) -> Result<Self> {
let custom_context = try_parse_custom_context(input)?;
let (_, roots, _) =
BodyParseConfig::<AS_RSX>::new_as_body().parse_component_body(&input)?;
let arena = Bump::new();
let (_, roots, _) = BodyConfig::<AS_RSX>::new_call_body().parse_component_body(input)?;
Ok(Self {
custom_context,
roots,
@@ -26,11 +27,12 @@ impl Parse for RsxBody<AS_RSX> {
}
/// The HTML variant of parsing rsx!
impl Parse for RsxBody<AS_HTML> {
impl Parse for CallBody<AS_HTML> {
fn parse(input: ParseStream) -> Result<Self> {
let custom_context = try_parse_custom_context(input)?;
let (_, roots, _) =
BodyParseConfig::<AS_HTML>::new_as_body().parse_component_body(&input)?;
// parsing the contents is almost like parsing the inner of any element, but with no props
let (_, roots, _) = BodyConfig::<AS_HTML>::new_call_body().parse_component_body(input)?;
Ok(Self {
custom_context,
roots,
@@ -50,7 +52,7 @@ fn try_parse_custom_context(input: ParseStream) -> Result<Option<Ident>> {
}
/// Serialize the same way, regardless of flavor
impl<const A: HtmlOrRsx> ToTokens for RsxBody<A> {
impl<const A: HtmlOrRsx> ToTokens for CallBody<A> {
fn to_tokens(&self, out_tokens: &mut TokenStream2) {
let inner = if self.roots.len() == 1 {
let inner = &self.roots[0];
+70 -15
View File
@@ -41,7 +41,7 @@ impl Parse for Component<AS_RSX> {
let content: ParseBuffer;
syn::braced!(content in stream);
let cfg: BodyParseConfig<AS_RSX> = BodyParseConfig {
let cfg: BodyConfig<AS_RSX> = BodyConfig {
allow_children: true,
allow_fields: true,
allow_manual_props: true,
@@ -59,19 +59,73 @@ impl Parse for Component<AS_RSX> {
}
impl Parse for Component<AS_HTML> {
fn parse(stream: ParseStream) -> Result<Self> {
let _l_tok = stream.parse::<Token![<]>()?;
let name = syn::Path::parse_mod_style(stream)?;
// parse the guts
let content: ParseBuffer;
syn::braced!(content in stream);
let mut manual_props = None;
let cfg: BodyParseConfig<AS_HTML> = BodyParseConfig {
allow_children: true,
allow_fields: true,
allow_manual_props: true,
};
let mut body: Vec<ComponentField<AS_HTML>> = vec![];
let mut children: Vec<BodyNode<AS_HTML>> = vec![];
let (body, children, manual_props) = cfg.parse_component_body(&content)?;
if stream.peek(Token![..]) {
stream.parse::<Token![..]>()?;
manual_props = Some(stream.parse::<Expr>()?);
}
while !stream.peek(Token![>]) {
// self-closing
if stream.peek(Token![/]) {
stream.parse::<Token![/]>()?;
stream.parse::<Token![>]>()?;
return Ok(Self {
name,
manual_props,
body,
children,
});
}
body.push(stream.parse::<ComponentField<AS_HTML>>()?);
}
stream.parse::<Token![>]>()?;
'parsing: loop {
if stream.peek(Token![<]) {
break 'parsing;
}
// [1] Break if empty
if stream.is_empty() {
break 'parsing;
}
children.push(stream.parse::<BodyNode<AS_HTML>>()?);
}
// closing element
stream.parse::<Token![<]>()?;
stream.parse::<Token![/]>()?;
let close = syn::Path::parse_mod_style(stream)?;
if close != name {
return Err(Error::new_spanned(
close,
"closing element does not match opening",
));
}
stream.parse::<Token![>]>()?;
// // parse the guts
// let content: ParseBuffer;
// syn::braced!(content in stream);
// let cfg: BodyConfig<AS_HTML> = BodyConfig {
// allow_children: true,
// allow_fields: true,
// allow_manual_props: true,
// };
// let (body, children, manual_props) = cfg.parse_component_body(&content)?;
Ok(Self {
name,
@@ -82,15 +136,15 @@ impl Parse for Component<AS_HTML> {
}
}
pub struct BodyParseConfig<const AS: HtmlOrRsx> {
pub struct BodyConfig<const AS: HtmlOrRsx> {
pub allow_fields: bool,
pub allow_children: bool,
pub allow_manual_props: bool,
}
impl<const AS: HtmlOrRsx> BodyParseConfig<AS> {
impl<const AS: HtmlOrRsx> BodyConfig<AS> {
/// The configuration to parse the root
pub fn new_as_body() -> Self {
pub fn new_call_body() -> Self {
Self {
allow_children: true,
allow_fields: false,
@@ -99,7 +153,7 @@ impl<const AS: HtmlOrRsx> BodyParseConfig<AS> {
}
}
impl BodyParseConfig<AS_RSX> {
impl BodyConfig<AS_RSX> {
// todo: unify this body parsing for both elements and components
// both are style rather ad-hoc, though components are currently more configured
pub fn parse_component_body(
@@ -156,7 +210,7 @@ impl BodyParseConfig<AS_RSX> {
Ok((body, children, manual_props))
}
}
impl BodyParseConfig<AS_HTML> {
impl BodyConfig<AS_HTML> {
// todo: unify this body parsing for both elements and components
// both are style rather ad-hoc, though components are currently more configured
pub fn parse_component_body(
@@ -201,6 +255,7 @@ impl BodyParseConfig<AS_HTML> {
"This item is not allowed to accept children.",
));
}
children.push(content.parse::<BodyNode<AS_HTML>>()?);
}
+17 -23
View File
@@ -78,7 +78,7 @@ impl Parse for Element<AS_HTML> {
let mut attributes: Vec<ElementAttr<AS_HTML>> = vec![];
let mut listeners: Vec<ElementAttr<AS_HTML>> = vec![];
let children: Vec<BodyNode<AS_HTML>> = vec![];
let mut children: Vec<BodyNode<AS_HTML>> = vec![];
let key = None;
while !stream.peek(Token![>]) {
@@ -115,7 +115,8 @@ impl Parse for Element<AS_HTML> {
match name_str.as_str() {
"style" => {}
"key" => {}
"classes" | "namespace" | "ref" | _ => {
_ => {
// "classes" | "namespace" | "ref" | _ => {
let ty = if stream.peek(LitStr) {
let rawtext = stream.parse::<LitStr>().unwrap();
AttrType::BumpText(rawtext)
@@ -138,37 +139,30 @@ impl Parse for Element<AS_HTML> {
}
stream.parse::<Token![>]>()?;
'parsing: loop {
if stream.peek(Token![<]) {
break 'parsing;
}
// [1] Break if empty
if stream.is_empty() {
break 'parsing;
}
children.push(stream.parse::<BodyNode<AS_HTML>>()?);
}
// closing element
stream.parse::<Token![<]>()?;
stream.parse::<Token![/]>()?;
let close = Ident::parse_any(stream)?;
if close.to_string() != el_name.to_string() {
if close != el_name {
return Err(Error::new_spanned(
close,
"closing element does not match opening",
));
}
stream.parse::<Token![>]>()?;
// 'parsing: loop {
// // if stream.peek(Token![>]) {}
// // // [1] Break if empty
// // if content.is_empty() {
// // break 'parsing;
// // }
// if content.peek(Ident) && content.peek2(Token![:]) && !content.peek3(Token![:]) {
// parse_element_body(
// &content,
// &mut attributes,
// &mut listeners,
// &mut key,
// name.clone(),
// )?;
// } else {
// children.push(stream.parse::<BodyNode<AS_HTML>>()?);
// }
// }
Ok(Self {
key,
+1
View File
@@ -17,6 +17,7 @@ mod component;
mod element;
mod fragment;
mod node;
mod parse_rsx;
// Re-export the namespaces into each other
pub use ambiguous::*;
+2
View File
@@ -0,0 +1,2 @@
struct CallBody {}
pub fn parse_callbody() {}