Skip to content

Commit

Permalink
Add MathML support + test. (yewstack#3121)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonas Scholz <[email protected]>
  • Loading branch information
jonas-scholz123 and jonas-scholz123 authored Mar 18, 2023
1 parent 8387af4 commit b85313b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/yew/src/dom_bundle/btag/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use web_sys::{Element, HtmlTextAreaElement as TextAreaElement};

use super::{BList, BNode, BSubtree, DomSlot, Reconcilable, ReconcileTarget};
use crate::html::AnyScope;
use crate::virtual_dom::vtag::{InputFields, VTagInner, Value, SVG_NAMESPACE};
use crate::virtual_dom::vtag::{InputFields, VTagInner, Value, MATHML_NAMESPACE, SVG_NAMESPACE};
use crate::virtual_dom::{Attributes, Key, VTag};
use crate::NodeRef;

Expand Down Expand Up @@ -232,6 +232,7 @@ impl Reconcilable for VTag {
impl VTag {
fn create_element(&self, parent: &Element) -> Element {
let tag = self.tag();

if tag == "svg"
|| parent
.namespace_uri()
Expand All @@ -241,6 +242,15 @@ impl VTag {
document()
.create_element_ns(namespace, tag)
.expect("can't create namespaced element for vtag")
} else if tag == "math"
|| parent
.namespace_uri()
.map_or(false, |ns| ns == MATHML_NAMESPACE)
{
let namespace = Some(MATHML_NAMESPACE);
document()
.create_element_ns(namespace, tag)
.expect("can't create namespaced element for vtag")
} else {
document()
.create_element(tag)
Expand Down Expand Up @@ -588,6 +598,19 @@ mod tests {
assert_namespace(&g_tag, SVG_NAMESPACE);
}

#[test]
fn supports_mathml() {
let (root, scope, parent) = setup_parent();
let mfrac_node = html! { <mfrac> </mfrac> };
let math_node = html! { <math>{mfrac_node}</math> };

let math_tag = assert_vtag(math_node);
let (_, math_tag) = math_tag.attach(&root, &scope, &parent, DomSlot::at_end());
assert_namespace(&math_tag, MATHML_NAMESPACE);
let mfrac_tag = assert_btag_ref(math_tag.children().get(0).unwrap());
assert_namespace(mfrac_tag, MATHML_NAMESPACE);
}

#[test]
fn it_compares_values() {
let a = html! {
Expand Down
3 changes: 3 additions & 0 deletions packages/yew/src/virtual_dom/vtag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use crate::html::{IntoPropValue, NodeRef};
/// SVG namespace string used for creating svg elements
pub const SVG_NAMESPACE: &str = "http://www.w3.org/2000/svg";

/// MathML namespace string used for creating MathML elements
pub const MATHML_NAMESPACE: &str = "http://www.w3.org/1998/Math/MathML";

/// Default namespace for html elements
pub const HTML_NAMESPACE: &str = "http://www.w3.org/1999/xhtml";

Expand Down

0 comments on commit b85313b

Please sign in to comment.