fix svg attributes

This commit is contained in:
Evan Almloff committed 2022-06-30 07:17:59 -05:00
1 parent ca13bf7358
commit e444d3d4bc
3 files changed
+47 -22

No files matched your search

+28 -14
View File
@@ -1,7 +1,17 @@
use crate::elements::*;
// map the rsx name of the attribute to the html name of the attribute and the namespace that contains it
pub fn attrbute_to_static_str(attr: &str) -> Option<(&'static str, Option<&'static str>)> {
pub fn attrbute_to_static_str(
attr: &str,
element: &'static str,
namespace: Option<&'static str>,
) -> Option<(&'static str, Option<&'static str>)> {
if namespace == Some("http://www.w3.org/2000/svg") {
svg::MAPPED_ATTRIBUTES
.iter()
.find(|(a, _)| *a == attr)
.map(|(_, b)| (*b, None))
} else {
NO_NAMESPACE_ATTRIBUTES
.iter()
.find(|&a| *a == attr)
@@ -18,31 +28,34 @@ pub fn attrbute_to_static_str(attr: &str) -> Option<(&'static str, Option<&'stat
.find(|(a, _)| *a == attr)
.map(|(_, b)| (*b, None))
})
.or_else(|| {
svg::MAPPED_ATTRIBUTES
.iter()
.find(|(a, _)| *a == attr)
.map(|(_, b)| (*b, None))
})
}
.or_else(|| {
ELEMENTS_WITH_MAPPED_ATTRIBUTES
.iter()
.find_map(|(_, attrs)| {
.find_map(|(el, attrs)| {
(element == *el)
.then(|| {
attrs
.iter()
.find(|(a, _)| *a == attr)
.map(|(_, b)| (*b, None))
})
.flatten()
})
})
.or_else(|| {
ELEMENTS_WITH_NAMESPACE
.iter()
.find_map(|(_, _, attrs)| attrs.iter().find(|a| **a == attr).map(|a| (*a, None)))
ELEMENTS_WITH_NAMESPACE.iter().find_map(|(el, ns, attrs)| {
(element == *el && namespace == Some(*ns))
.then(|| attrs.iter().find(|a| **a == attr).map(|a| (*a, None)))
.flatten()
})
})
.or_else(|| {
ELEMENTS_WITHOUT_NAMESPACE
.iter()
.find_map(|(_, attrs)| attrs.iter().find(|a| **a == attr).map(|a| (*a, None)))
ELEMENTS_WITHOUT_NAMESPACE.iter().find_map(|(el, attrs)| {
(element == *el)
.then(|| attrs.iter().find(|a| **a == attr).map(|a| (*a, None)))
.flatten()
})
})
}
@@ -615,6 +628,7 @@ mapped_trait_methods! {
}
pub mod svg {
mapped_trait_methods! {
accent_height: "accent-height",
accumulate: "accumulate",
+1 -1
View File
@@ -54,7 +54,7 @@ macro_rules! builder_constructors {
$(
(
stringify!($name),
stringify!($namespace),
$namespace,
&[
$(
stringify!($fil),
+18 -7
View File
@@ -76,6 +76,8 @@ fn build_node<'a>(
Ok(factory.text(format_args!("{}", text)))
}
BodyNode::Element(el) => {
let tag = bump.alloc(el.name.to_string());
if let Some((tag, ns)) = element_to_static_str(tag) {
let attributes: &mut Vec<Attribute> = bump.alloc(Vec::new());
for attr in &el.attributes {
match &attr.attr {
@@ -84,21 +86,28 @@ fn build_node<'a>(
ElementAttr::AttrText { name, value } => (
name.to_string(),
IfmtInput::from_str(&value.value()).map_err(|err| {
Error::ParseError(ParseError::new(err, ctx.location.clone()))
Error::ParseError(ParseError::new(
err,
ctx.location.clone(),
))
})?,
name.span(),
),
ElementAttr::CustomAttrText { name, value } => (
name.value(),
IfmtInput::from_str(&value.value()).map_err(|err| {
Error::ParseError(ParseError::new(err, ctx.location.clone()))
Error::ParseError(ParseError::new(
err,
ctx.location.clone(),
))
})?,
name.span(),
),
_ => unreachable!(),
};
if let Some((name, namespace)) = attrbute_to_static_str(&name) {
if let Some((name, namespace)) = attrbute_to_static_str(&name, tag, ns)
{
let value = bump.alloc(resolve_ifmt(&value, &ctx.captured)?);
attributes.push(Attribute {
name,
@@ -131,7 +140,9 @@ fn build_node<'a>(
.iter()
.find(|(n, _)| parse_str::<Expr>(*n).unwrap() == *value)
{
if let Some((name, namespace)) = attrbute_to_static_str(&name) {
if let Some((name, namespace)) =
attrbute_to_static_str(&name, tag, ns)
{
let value = bump.alloc(resulting_value.clone());
attributes.push(Attribute {
name,
@@ -174,13 +185,13 @@ fn build_node<'a>(
listeners.push(listener)
} else {
return Err(Error::RecompileRequiredError(
RecompileReason::CapturedListener(expr.to_token_stream().to_string()),
RecompileReason::CapturedListener(
expr.to_token_stream().to_string(),
),
));
}
}
}
let tag = bump.alloc(el.name.to_string());
if let Some((tag, ns)) = element_to_static_str(tag) {
match el.key {
None => Ok(factory.raw_element(
tag,