use assert_matches2::assert_matches; use ruma_html::{ matrix::{AnchorUri, MatrixElement}, Html, }; #[test] fn elements() { let raw_html = "\

Title

\
\

This is some text

\
\ This is scrolling "; let html = Html::parse(raw_html); let mut html_children = html.children(); // `

` element. let h1_node = html_children.next().unwrap(); let h1_element = h1_node.as_element().unwrap().to_matrix(); assert_matches!(h1_element.element, MatrixElement::H(heading)); assert_eq!(heading.level, 1); assert!(h1_element.attrs.is_empty()); // `
` element. let div_node = html_children.next().unwrap(); let div_element = div_node.as_element().unwrap().to_matrix(); assert_matches!(div_element.element, MatrixElement::Div(div)); assert_eq!(div.maths, None); // The `class` attribute is not supported. assert_eq!(div_element.attrs.len(), 1); // `

` element. let p_node = div_node.first_child().unwrap(); let p_element = p_node.as_element().unwrap().to_matrix(); assert_matches!(p_element.element, MatrixElement::P); assert!(p_element.attrs.is_empty()); // Text of `

` element. let p_text_node = p_node.first_child().unwrap(); // `` element. let em_node = p_text_node.next_sibling().unwrap(); let em_element = em_node.as_element().unwrap().to_matrix(); assert_matches!(em_element.element, MatrixElement::Em); assert!(em_element.attrs.is_empty()); // `` element. let marquee_node = html_children.next().unwrap(); let marquee_element = marquee_node.as_element().unwrap().to_matrix(); assert_matches!(marquee_element.element, MatrixElement::Other(_)); assert_eq!(marquee_element.attrs.len(), 1); } #[test] fn span_attributes() { let raw_html = "\ \ Hidden and colored\ \ "; let html = Html::parse(raw_html); let mut html_children = html.children(); let span_node = html_children.next().unwrap(); let span_element = span_node.as_element().unwrap().to_matrix(); assert_matches!(span_element.element, MatrixElement::Span(span)); assert_eq!(span.color.unwrap().as_ref(), "#00ff00"); assert_eq!(span.bg_color.unwrap().as_ref(), "#ff0000"); // Uses the first spoiler attribute, the second is dropped. assert!(span.spoiler.unwrap().is_empty()); assert!(span_element.attrs.is_empty()); } #[test] fn a_attributes() { let raw_html = "\ \ Link with all supported attributes\ \ Link with valid matrix scheme URI\ Link with invalid matrix scheme URI\ Link with valid matrix.to URI\ Link with invalid matrix.to URI\ Link with unsupported scheme\ "; let html = Html::parse(raw_html); let mut html_children = html.children(); // First `` element, with all supported attributes. let node = html_children.next().unwrap(); let element = node.as_element().unwrap().to_matrix(); assert_matches!(element.element, MatrixElement::A(anchor)); assert_eq!(anchor.target.unwrap().as_ref(), "_blank"); assert_matches!(anchor.href.unwrap(), AnchorUri::Other(uri)); assert_eq!(uri.as_ref(), "https://localhost/"); assert!(element.attrs.is_empty()); // Second `` element, with valid matrix scheme URI. let node = html_children.next().unwrap(); let element = node.as_element().unwrap().to_matrix(); assert_matches!(element.element, MatrixElement::A(anchor)); assert!(anchor.target.is_none()); assert_matches!(anchor.href.unwrap(), AnchorUri::Matrix(uri)); assert_eq!(uri.to_string(), "matrix:r/somewhere:localhost"); assert!(element.attrs.is_empty()); // Third `` element, with invalid matrix scheme URI. let node = html_children.next().unwrap(); let element = node.as_element().unwrap().to_matrix(); assert_matches!(element.element, MatrixElement::A(anchor)); assert!(anchor.target.is_none()); assert!(anchor.href.is_none()); // The `href` attribute is in the unsupported attributes. assert_eq!(element.attrs.len(), 1); // Fourth `` element, with valid matrix.to URI. let node = html_children.next().unwrap(); let element = node.as_element().unwrap().to_matrix(); assert_matches!(element.element, MatrixElement::A(anchor)); assert!(anchor.target.is_none()); assert_matches!(anchor.href.unwrap(), AnchorUri::MatrixTo(uri)); assert_eq!(uri.to_string(), "https://matrix.to/#/%23somewhere:example.org"); assert!(element.attrs.is_empty()); // Fifth `` element, with invalid matrix.to URI. let node = html_children.next().unwrap(); let element = node.as_element().unwrap().to_matrix(); assert_matches!(element.element, MatrixElement::A(anchor)); assert!(anchor.target.is_none()); assert!(anchor.href.is_none()); // The `href` attribute is in the unsupported attributes. assert_eq!(element.attrs.len(), 1); // Sixth `` element, with unsupported scheme. let node = html_children.next().unwrap(); let element = node.as_element().unwrap().to_matrix(); assert_matches!(element.element, MatrixElement::A(anchor)); assert!(anchor.target.is_none()); assert!(anchor.href.is_none()); // The `href` attribute is in the unsupported attributes. assert_eq!(element.attrs.len(), 1); } #[test] fn img_attributes() { let raw_html = "\ \"Image\ \"Image\ "; let html = Html::parse(raw_html); let mut html_children = html.children(); // First `` element, with valid attributes. let node = html_children.next().unwrap(); let element = node.as_element().unwrap().to_matrix(); assert_matches!(element.element, MatrixElement::Img(image)); assert_eq!(image.width.unwrap(), 200); assert_eq!(image.height.unwrap(), 200); assert_eq!(image.alt.unwrap().as_ref(), "Image with valid attributes"); assert_eq!(image.title.unwrap().as_ref(), "Image with valid attributes"); assert_eq!(image.src.unwrap(), "mxc://localhost/abc123"); assert!(element.attrs.is_empty()); // Second `` element, with invalid attributes. let node = html_children.next().unwrap(); let element = node.as_element().unwrap().to_matrix(); assert_matches!(element.element, MatrixElement::Img(image)); assert!(image.width.is_none()); assert!(image.height.is_none()); assert_eq!(image.alt.unwrap().as_ref(), "Image with invalid attributes"); assert_eq!(image.title.unwrap().as_ref(), "Image with invalid attributes"); assert!(image.src.is_none()); // Invalid attributes are in the unsupported attributes. assert_eq!(element.attrs.len(), 3); } #[test] fn ol_attributes() { let raw_html = "\
    \
  1. Item in list with valid start attribute
  2. \
\
    \
  1. Item in list with invalid start attribute
  2. \
\ "; let html = Html::parse(raw_html); let mut html_children = html.children(); // First `
    ` element, with valid `start` attribute. let node = html_children.next().unwrap(); let element = node.as_element().unwrap().to_matrix(); assert_matches!(element.element, MatrixElement::Ol(ol)); assert_eq!(ol.start.unwrap(), 2); assert!(element.attrs.is_empty()); // First `
      ` element, with invalid `start` attribute. let node = html_children.next().unwrap(); let element = node.as_element().unwrap().to_matrix(); assert_matches!(element.element, MatrixElement::Ol(ol)); assert!(ol.start.is_none()); assert_eq!(element.attrs.len(), 1); } #[test] fn code_attributes() { let raw_html = "\ \ let s = \"Code with only `language-` class\";\ \ \ let s = \"Code with other class\";\ \ \ let s = \"Code with several classes beginning with `language-` class\";\ \ \ let s = \"Code with several classes not beginning with `language-` class\";\ \ \ let s = \"Code with invalid `language-` class\";\ \ \ let s = \"Code with other class containing `language-`\";\ \ "; let html = Html::parse(raw_html); let mut html_children = html.children(); // First `` element, with only `language-` class. let node = html_children.next().unwrap(); let element = node.as_element().unwrap().to_matrix(); assert_matches!(element.element, MatrixElement::Code(code)); assert_eq!(code.language.unwrap().as_ref(), "rust"); assert!(element.attrs.is_empty()); // Second `` element, with other class. let node = html_children.next().unwrap(); let element = node.as_element().unwrap().to_matrix(); assert_matches!(element.element, MatrixElement::Code(code)); assert!(code.language.is_none()); // `class` is in unsupported attributes. assert_eq!(element.attrs.len(), 1); // Third `` element, with several classes beginning with `language-` class. let node = html_children.next().unwrap(); let element = node.as_element().unwrap().to_matrix(); assert_matches!(element.element, MatrixElement::Code(code)); assert_eq!(code.language.unwrap().as_ref(), "rust"); // Because it contains other classes, `class` is also in unsupported attributes. assert_eq!(element.attrs.len(), 1); // Fourth `` element, with several classes not beginning with `language-` class. let node = html_children.next().unwrap(); let element = node.as_element().unwrap().to_matrix(); assert_matches!(element.element, MatrixElement::Code(code)); assert_eq!(code.language.unwrap().as_ref(), "rust"); // Because it contains other classes, `class` is also in unsupported attributes. assert_eq!(element.attrs.len(), 1); // Fifth `` element, with invalid `language-` class. let node = html_children.next().unwrap(); let element = node.as_element().unwrap().to_matrix(); assert_matches!(element.element, MatrixElement::Code(code)); assert!(code.language.is_none()); // `class` is in unsupported attributes. assert_eq!(element.attrs.len(), 1); // Sixth `` element, with other class containing `language-`. let node = html_children.next().unwrap(); let element = node.as_element().unwrap().to_matrix(); assert_matches!(element.element, MatrixElement::Code(code)); assert!(code.language.is_none()); // `class` is in unsupported attributes. assert_eq!(element.attrs.len(), 1); }