Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to the way Render handles attributes #45

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
7 changes: 5 additions & 2 deletions render/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ use std::fmt::{Result, Write};
/// Render a component
///
/// This is the underlying mechanism of the `#[component]` macro
pub trait Render: Sized {
pub trait Render {
/// Render the component to a writer.
/// Make sure you escape html correctly using the `render::html_escaping` module
fn render_into<W: Write>(self, writer: &mut W) -> Result;

/// Render the component to string
fn render(self) -> String {
fn render(self) -> String
where
Self: Sized,
{
let mut buf = String::new();
self.render_into(&mut buf).unwrap();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This stuff makes the Render trait more portable-- we still need sized as its a dependency of the writer structure we're doing, but we can at least scope this requirement to the render method so that it'll only complain about being passed around when used directly in the html! macro... 🤞

buf
Expand Down
2 changes: 1 addition & 1 deletion render/src/simple_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn write_attributes<'a, W: Write>(maybe_attributes: Attributes<'a>, writer: &mut
Some(mut attributes) => {
for (key, value) in attributes.drain() {
write!(writer, " {}=\"", key)?;
escape_html(&value, writer)?;
write!(writer, "{}", value)?;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we stop escaping the html of data that goes into attributes. This does mean library users will need to do this themselves. In future I'd suggest some kind of marker that would escape this unless it was attached.

write!(writer, "\"")?;
}
Ok(())
Expand Down
1 change: 0 additions & 1 deletion render_macros/src/children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ impl Children {
1 => quote! { Some(#(#children_quotes),*) },
_ => {
let mut iter = children_quotes.iter();

let first = iter.next().unwrap();
let second = iter.next().unwrap();

Expand Down
33 changes: 30 additions & 3 deletions render_macros/src/element_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use syn::ext::IdentExt;
use syn::parse::{Parse, ParseStream, Result};
use syn::spanned::Spanned;

pub type AttributeKey = syn::punctuated::Punctuated<syn::Ident, syn::Token![-]>;
pub type AttributeKey = syn::punctuated::Punctuated<proc_macro2::Ident, proc_macro2::Punct>;

pub enum ElementAttribute {
Punned(AttributeKey),
Expand Down Expand Up @@ -94,14 +94,41 @@ impl Hash for ElementAttribute {

impl Parse for ElementAttribute {
fn parse(input: ParseStream) -> Result<Self> {
let name = AttributeKey::parse_separated_nonempty_with(input, syn::Ident::parse_any)?;
let not_punned = input.peek(syn::Token![=]);
let mut name: syn::punctuated::Punctuated<proc_macro2::Ident, proc_macro2::Punct> =
syn::punctuated::Punctuated::new();

// Parse the input up to the space
loop {
let value = syn::Ident::parse_any(&input).unwrap();
name.push_value(value);

if input.peek(syn::Token![=]) {
break;
}

let punct = input.parse().unwrap();
name.push_punct(punct);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop is similar to the implementation in parse_separated_nonempty_with. Key thing I was looking to do here was to give us the ability to modify how and when this accrues / breaks so we can better handle the edge cases. I'm not sure I ended up going very far with this but think it probably makes sense to hold onto this here.


// Peak for incoming equals to check if its punned
let mut not_punned = input.peek(syn::Token![=]);

if !not_punned {
not_punned = input.peek2(syn::Token![=]);
}

if !not_punned {
not_punned = input.peek3(syn::Token![=]);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because there are multiple sets of punctuation that can occur in a row we look forward to see if any of the next 3 Punctuated's have an equals in it. This may ultimately become a limitation of the library that it can only handle 2 distinct piece of punctuation before the equals though that would be a vast improvement.


if !not_punned {
return Ok(Self::Punned(name));
}

// Parse equals
input.parse::<syn::Token![=]>()?;

// Parse body
let value = input.parse::<syn::Block>()?;

Ok(Self::WithValue(name, value))
Expand Down
15 changes: 11 additions & 4 deletions render_macros/src/element_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,17 @@ impl<'a> ToTokens for SimpleElementAttributes<'a> {
.attributes
.iter()
.map(|attribute| {
let mut iter = attribute.ident().iter();
let first_word = iter.next().unwrap().unraw();
let ident = iter.fold(first_word.to_string(), |acc, curr| {
format!("{}-{}", acc, curr.unraw())
let mut iter = attribute.ident().pairs();
let ident = iter.fold("".to_string(), |acc, curr| {
format!(
"{}{}{}",
acc,
curr.value(),
match curr.punct() {
Some(p) => p.as_char().to_string(),
None => "".to_string(),
}
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This properly collapses our punctuation and ident fields back into a proper string when there are multiple.

});
let value = attribute.value_tokens();

Expand Down
Loading