Skip to content

Commit

Permalink
fix: Use correct case for XML (SVG) elements
Browse files Browse the repository at this point in the history
Previously XML elements which use some upper case letters would be
converted to all-lowercase during string parsing. As xml is
case-sensitive, this breaks the element. An example is the XML
linearGradient.

This was caused by the code incorrectly converting all element names to
lowercase to ensure HTML tags are correctly assigned, as they use lower
case internally, but the browser function tagName returns all caps for
HTML tags.

Fix this by only converting HTML element names to lower case.

Fix: seed-rs#377
  • Loading branch information
klemens committed May 9, 2020
1 parent af11d5e commit 77cf6f1
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/browser/dom/virtual_dom_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,11 @@ impl<Ms> From<&web_sys::Element> for El<Ms> {
/// and markdown strings. Includes children, recursively added.
#[allow(clippy::too_many_lines)]
fn from(ws_el: &web_sys::Element) -> Self {
// Result of tag_name is all caps, but tag From<String> expects lower.
// Probably is more pure to match by xlmns attribute instead.
let mut el = match ws_el.tag_name().to_lowercase().as_ref() {
"svg" => El::empty_svg(ws_el.tag_name().to_lowercase().into()),
_ => El::empty(ws_el.tag_name().to_lowercase().into()),
let namespace = ws_el.namespace_uri().map(Namespace::from);
let mut el = match namespace {
// tag_name returns all caps for HTML, but Tag::from uses lowercase names for HTML
Some(Namespace::Html) => El::empty(ws_el.tag_name().to_lowercase().into()),
_ => El::empty(ws_el.tag_name().into()),
};

// Populate attributes
Expand Down Expand Up @@ -420,14 +420,14 @@ impl<Ms> From<&web_sys::Element> for El<Ms> {
"solidcolor",
];

if svg_tags.contains(&ws_el.tag_name().to_lowercase().as_str()) {
if svg_tags.contains(&ws_el.tag_name().as_str()) {
el.namespace = Some(Namespace::Svg);
}

if let Some(ns) = ws_el.namespace_uri() {
if let Some(ref ns) = namespace {
// Prevent attaching a `xlmns` attribute to normal HTML elements.
if ns != "http://www.w3.org/1999/xhtml" {
el.namespace = Some(ns.into());
if ns != &Namespace::Html {
el.namespace = namespace;
}
}

Expand Down

0 comments on commit 77cf6f1

Please # to comment.