2019-08-25 17:01:51 +00:00
|
|
|
// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
use crate::message::MessagePayload;
|
|
|
|
use crate::ns;
|
2019-10-22 23:32:41 +00:00
|
|
|
use crate::util::error::Error;
|
2019-08-25 17:01:51 +00:00
|
|
|
use minidom::{Element, Node};
|
|
|
|
use std::collections::HashMap;
|
2019-10-22 23:32:41 +00:00
|
|
|
use std::convert::TryFrom;
|
2019-08-25 17:01:51 +00:00
|
|
|
|
|
|
|
// TODO: Use a proper lang type.
|
|
|
|
type Lang = String;
|
|
|
|
|
|
|
|
/// Container for formatted text.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct XhtmlIm {
|
|
|
|
/// Map of language to body element.
|
2019-08-25 18:02:33 +00:00
|
|
|
bodies: HashMap<Lang, Body>,
|
2019-08-25 17:01:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl XhtmlIm {
|
|
|
|
/// Serialise formatted text to HTML.
|
|
|
|
pub fn to_html(self) -> String {
|
|
|
|
let mut html = Vec::new();
|
|
|
|
// TODO: use the best language instead.
|
|
|
|
for (lang, body) in self.bodies {
|
2019-08-25 18:02:33 +00:00
|
|
|
if lang.is_empty() {
|
|
|
|
assert!(body.xml_lang.is_none());
|
2019-08-25 17:01:51 +00:00
|
|
|
} else {
|
2019-08-25 18:02:33 +00:00
|
|
|
assert_eq!(Some(lang), body.xml_lang);
|
|
|
|
}
|
|
|
|
for tag in body.children {
|
|
|
|
html.push(tag.to_html());
|
2019-08-25 17:01:51 +00:00
|
|
|
}
|
2019-08-25 18:02:33 +00:00
|
|
|
break;
|
2019-08-25 17:01:51 +00:00
|
|
|
}
|
|
|
|
html.concat()
|
|
|
|
}
|
2019-08-25 18:02:33 +00:00
|
|
|
|
|
|
|
/// Removes all unknown elements.
|
2019-09-04 16:14:39 +00:00
|
|
|
fn flatten(self) -> XhtmlIm {
|
2019-08-25 18:02:33 +00:00
|
|
|
let mut bodies = HashMap::new();
|
|
|
|
for (lang, body) in self.bodies {
|
|
|
|
let children = body.children.into_iter().fold(vec![], |mut acc, child| {
|
|
|
|
match child {
|
|
|
|
Child::Tag(Tag::Unknown(children)) => acc.extend(children),
|
|
|
|
any => acc.push(any),
|
|
|
|
}
|
|
|
|
acc
|
|
|
|
});
|
2019-10-22 23:32:41 +00:00
|
|
|
let body = Body { children, ..body };
|
2019-08-25 18:02:33 +00:00
|
|
|
bodies.insert(lang, body);
|
|
|
|
}
|
2019-10-22 23:32:41 +00:00
|
|
|
XhtmlIm { bodies }
|
2019-08-25 18:02:33 +00:00
|
|
|
}
|
2019-08-25 17:01:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MessagePayload for XhtmlIm {}
|
|
|
|
|
|
|
|
impl TryFrom<Element> for XhtmlIm {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<XhtmlIm, Error> {
|
|
|
|
check_self!(elem, "html", XHTML_IM);
|
|
|
|
check_no_attributes!(elem, "html");
|
|
|
|
|
|
|
|
let mut bodies = HashMap::new();
|
|
|
|
for child in elem.children() {
|
|
|
|
if child.is("body", ns::XHTML) {
|
|
|
|
let child = child.clone();
|
2021-10-11 13:22:19 +00:00
|
|
|
let lang = child.attr("xml:lang").unwrap_or("").to_string();
|
2019-08-25 18:02:33 +00:00
|
|
|
let body = Body::try_from(child)?;
|
2019-08-25 17:01:51 +00:00
|
|
|
match bodies.insert(lang, body) {
|
|
|
|
None => (),
|
2019-10-22 23:32:41 +00:00
|
|
|
Some(_) => {
|
|
|
|
return Err(Error::ParseError(
|
|
|
|
"Two identical language bodies found in XHTML-IM.",
|
|
|
|
))
|
|
|
|
}
|
2019-08-25 17:01:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(Error::ParseError("Unknown element in XHTML-IM."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 16:14:39 +00:00
|
|
|
Ok(XhtmlIm { bodies }.flatten())
|
2019-08-25 17:01:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<XhtmlIm> for Element {
|
|
|
|
fn from(wrapper: XhtmlIm) -> Element {
|
2020-03-28 12:07:26 +00:00
|
|
|
Element::builder("html", ns::XHTML_IM)
|
2019-09-06 14:03:58 +00:00
|
|
|
.append_all(wrapper.bodies.into_iter().map(|(lang, body)| {
|
2019-09-05 09:51:05 +00:00
|
|
|
if lang.is_empty() {
|
|
|
|
assert!(body.xml_lang.is_none());
|
|
|
|
} else {
|
2019-09-06 14:03:58 +00:00
|
|
|
assert_eq!(Some(lang), body.xml_lang);
|
2019-09-05 09:51:05 +00:00
|
|
|
}
|
2019-09-06 14:03:58 +00:00
|
|
|
Element::from(body)
|
2019-07-24 22:20:38 +00:00
|
|
|
}))
|
2019-08-25 17:01:51 +00:00
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
enum Child {
|
|
|
|
Tag(Tag),
|
|
|
|
Text(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Child {
|
|
|
|
fn to_html(self) -> String {
|
|
|
|
match self {
|
|
|
|
Child::Tag(tag) => tag.to_html(),
|
|
|
|
Child::Text(text) => text,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct Property {
|
|
|
|
key: String,
|
|
|
|
value: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
type Css = Vec<Property>;
|
|
|
|
|
|
|
|
fn get_style_string(style: Css) -> Option<String> {
|
|
|
|
let mut result = vec![];
|
|
|
|
for Property { key, value } in style {
|
|
|
|
result.push(format!("{}: {}", key, value));
|
|
|
|
}
|
|
|
|
if result.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(result.join("; "))
|
|
|
|
}
|
|
|
|
|
2019-08-25 18:02:33 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct Body {
|
|
|
|
style: Css,
|
|
|
|
xml_lang: Option<String>,
|
|
|
|
children: Vec<Child>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Element> for Body {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<Body, Error> {
|
|
|
|
let mut children = vec![];
|
|
|
|
for child in elem.nodes() {
|
|
|
|
match child {
|
|
|
|
Node::Element(child) => children.push(Child::Tag(Tag::try_from(child.clone())?)),
|
|
|
|
Node::Text(text) => children.push(Child::Text(text.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 23:32:41 +00:00
|
|
|
Ok(Body {
|
|
|
|
style: parse_css(elem.attr("style")),
|
|
|
|
xml_lang: elem.attr("xml:lang").map(|xml_lang| xml_lang.to_string()),
|
|
|
|
children,
|
|
|
|
})
|
2019-08-25 18:02:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Body> for Element {
|
|
|
|
fn from(body: Body) -> Element {
|
2020-03-28 12:07:26 +00:00
|
|
|
Element::builder("body", ns::XHTML)
|
2019-08-25 18:02:33 +00:00
|
|
|
.attr("style", get_style_string(body.style))
|
|
|
|
.attr("xml:lang", body.xml_lang)
|
2019-07-24 22:20:38 +00:00
|
|
|
.append_all(children_to_nodes(body.children))
|
2019-08-25 18:02:33 +00:00
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 17:01:51 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
enum Tag {
|
2019-10-22 23:32:41 +00:00
|
|
|
A {
|
|
|
|
href: Option<String>,
|
|
|
|
style: Css,
|
|
|
|
type_: Option<String>,
|
|
|
|
children: Vec<Child>,
|
|
|
|
},
|
|
|
|
Blockquote {
|
|
|
|
style: Css,
|
|
|
|
children: Vec<Child>,
|
|
|
|
},
|
2019-08-25 17:01:51 +00:00
|
|
|
Br,
|
2019-10-22 23:32:41 +00:00
|
|
|
Cite {
|
|
|
|
style: Css,
|
|
|
|
children: Vec<Child>,
|
|
|
|
},
|
|
|
|
Em {
|
|
|
|
children: Vec<Child>,
|
|
|
|
},
|
|
|
|
Img {
|
|
|
|
src: Option<String>,
|
|
|
|
alt: Option<String>,
|
|
|
|
}, // TODO: height, width, style
|
|
|
|
Li {
|
|
|
|
style: Css,
|
|
|
|
children: Vec<Child>,
|
|
|
|
},
|
|
|
|
Ol {
|
|
|
|
style: Css,
|
|
|
|
children: Vec<Child>,
|
|
|
|
},
|
|
|
|
P {
|
|
|
|
style: Css,
|
|
|
|
children: Vec<Child>,
|
|
|
|
},
|
|
|
|
Span {
|
|
|
|
style: Css,
|
|
|
|
children: Vec<Child>,
|
|
|
|
},
|
|
|
|
Strong {
|
|
|
|
children: Vec<Child>,
|
|
|
|
},
|
|
|
|
Ul {
|
|
|
|
style: Css,
|
|
|
|
children: Vec<Child>,
|
|
|
|
},
|
2019-08-25 17:01:51 +00:00
|
|
|
Unknown(Vec<Child>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Tag {
|
|
|
|
fn to_html(self) -> String {
|
|
|
|
match self {
|
2019-10-22 23:32:41 +00:00
|
|
|
Tag::A {
|
|
|
|
href,
|
|
|
|
style,
|
|
|
|
type_,
|
|
|
|
children,
|
|
|
|
} => {
|
2019-08-25 17:01:51 +00:00
|
|
|
let href = write_attr(href, "href");
|
|
|
|
let style = write_attr(get_style_string(style), "style");
|
|
|
|
let type_ = write_attr(type_, "type");
|
2019-10-22 23:32:41 +00:00
|
|
|
format!(
|
|
|
|
"<a{}{}{}>{}</a>",
|
|
|
|
href,
|
|
|
|
style,
|
|
|
|
type_,
|
|
|
|
children_to_html(children)
|
|
|
|
)
|
|
|
|
}
|
2019-08-25 17:01:51 +00:00
|
|
|
Tag::Blockquote { style, children } => {
|
|
|
|
let style = write_attr(get_style_string(style), "style");
|
2019-10-22 23:32:41 +00:00
|
|
|
format!(
|
|
|
|
"<blockquote{}>{}</blockquote>",
|
|
|
|
style,
|
|
|
|
children_to_html(children)
|
|
|
|
)
|
|
|
|
}
|
2019-08-25 17:01:51 +00:00
|
|
|
Tag::Br => String::from("<br>"),
|
|
|
|
Tag::Cite { style, children } => {
|
|
|
|
let style = write_attr(get_style_string(style), "style");
|
|
|
|
format!("<cite{}>{}</cite>", style, children_to_html(children))
|
2019-10-22 23:32:41 +00:00
|
|
|
}
|
2019-08-25 17:01:51 +00:00
|
|
|
Tag::Em { children } => format!("<em>{}</em>", children_to_html(children)),
|
|
|
|
Tag::Img { src, alt } => {
|
|
|
|
let src = write_attr(src, "src");
|
|
|
|
let alt = write_attr(alt, "alt");
|
|
|
|
format!("<img{}{}>", src, alt)
|
|
|
|
}
|
|
|
|
Tag::Li { style, children } => {
|
|
|
|
let style = write_attr(get_style_string(style), "style");
|
|
|
|
format!("<li{}>{}</li>", style, children_to_html(children))
|
|
|
|
}
|
|
|
|
Tag::Ol { style, children } => {
|
|
|
|
let style = write_attr(get_style_string(style), "style");
|
|
|
|
format!("<ol{}>{}</ol>", style, children_to_html(children))
|
|
|
|
}
|
|
|
|
Tag::P { style, children } => {
|
|
|
|
let style = write_attr(get_style_string(style), "style");
|
|
|
|
format!("<p{}>{}</p>", style, children_to_html(children))
|
|
|
|
}
|
|
|
|
Tag::Span { style, children } => {
|
|
|
|
let style = write_attr(get_style_string(style), "style");
|
|
|
|
format!("<span{}>{}</span>", style, children_to_html(children))
|
|
|
|
}
|
2019-09-04 16:14:39 +00:00
|
|
|
Tag::Strong { children } => format!("<strong>{}</strong>", children_to_html(children)),
|
2019-08-25 17:01:51 +00:00
|
|
|
Tag::Ul { style, children } => {
|
|
|
|
let style = write_attr(get_style_string(style), "style");
|
|
|
|
format!("<ul{}>{}</ul>", style, children_to_html(children))
|
|
|
|
}
|
2019-10-22 23:32:41 +00:00
|
|
|
Tag::Unknown(_) => {
|
|
|
|
panic!("No unknown element should be present in XHTML-IM after parsing.")
|
|
|
|
}
|
2019-08-25 17:01:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Element> for Tag {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<Tag, Error> {
|
|
|
|
let mut children = vec![];
|
|
|
|
for child in elem.nodes() {
|
|
|
|
match child {
|
|
|
|
Node::Element(child) => children.push(Child::Tag(Tag::try_from(child.clone())?)),
|
|
|
|
Node::Text(text) => children.push(Child::Text(text.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(match elem.name() {
|
2019-10-22 23:32:41 +00:00
|
|
|
"a" => Tag::A {
|
|
|
|
href: elem.attr("href").map(|href| href.to_string()),
|
|
|
|
style: parse_css(elem.attr("style")),
|
|
|
|
type_: elem.attr("type").map(|type_| type_.to_string()),
|
|
|
|
children,
|
|
|
|
},
|
|
|
|
"blockquote" => Tag::Blockquote {
|
|
|
|
style: parse_css(elem.attr("style")),
|
|
|
|
children,
|
|
|
|
},
|
2019-08-25 17:01:51 +00:00
|
|
|
"br" => Tag::Br,
|
2019-10-22 23:32:41 +00:00
|
|
|
"cite" => Tag::Cite {
|
|
|
|
style: parse_css(elem.attr("style")),
|
|
|
|
children,
|
|
|
|
},
|
2019-08-25 17:01:51 +00:00
|
|
|
"em" => Tag::Em { children },
|
2019-10-22 23:32:41 +00:00
|
|
|
"img" => Tag::Img {
|
|
|
|
src: elem.attr("src").map(|src| src.to_string()),
|
|
|
|
alt: elem.attr("alt").map(|alt| alt.to_string()),
|
|
|
|
},
|
|
|
|
"li" => Tag::Li {
|
|
|
|
style: parse_css(elem.attr("style")),
|
|
|
|
children,
|
|
|
|
},
|
|
|
|
"ol" => Tag::Ol {
|
|
|
|
style: parse_css(elem.attr("style")),
|
|
|
|
children,
|
|
|
|
},
|
|
|
|
"p" => Tag::P {
|
|
|
|
style: parse_css(elem.attr("style")),
|
|
|
|
children,
|
|
|
|
},
|
|
|
|
"span" => Tag::Span {
|
|
|
|
style: parse_css(elem.attr("style")),
|
|
|
|
children,
|
|
|
|
},
|
2019-08-25 17:01:51 +00:00
|
|
|
"strong" => Tag::Strong { children },
|
2019-10-22 23:32:41 +00:00
|
|
|
"ul" => Tag::Ul {
|
|
|
|
style: parse_css(elem.attr("style")),
|
|
|
|
children,
|
|
|
|
},
|
2019-08-25 17:01:51 +00:00
|
|
|
_ => Tag::Unknown(children),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Tag> for Element {
|
|
|
|
fn from(tag: Tag) -> Element {
|
|
|
|
let (name, attrs, children) = match tag {
|
2019-10-22 23:32:41 +00:00
|
|
|
Tag::A {
|
|
|
|
href,
|
|
|
|
style,
|
|
|
|
type_,
|
|
|
|
children,
|
|
|
|
} => (
|
|
|
|
"a",
|
|
|
|
{
|
|
|
|
let mut attrs = vec![];
|
|
|
|
if let Some(href) = href {
|
|
|
|
attrs.push(("href", href));
|
|
|
|
}
|
|
|
|
if let Some(style) = get_style_string(style) {
|
|
|
|
attrs.push(("style", style));
|
|
|
|
}
|
|
|
|
if let Some(type_) = type_ {
|
|
|
|
attrs.push(("type", type_));
|
|
|
|
}
|
|
|
|
attrs
|
|
|
|
},
|
|
|
|
children,
|
|
|
|
),
|
|
|
|
Tag::Blockquote { style, children } => (
|
|
|
|
"blockquote",
|
|
|
|
match get_style_string(style) {
|
|
|
|
Some(style) => vec![("style", style)],
|
|
|
|
None => vec![],
|
|
|
|
},
|
|
|
|
children,
|
|
|
|
),
|
2019-08-25 17:01:51 +00:00
|
|
|
Tag::Br => ("br", vec![], vec![]),
|
2019-10-22 23:32:41 +00:00
|
|
|
Tag::Cite { style, children } => (
|
|
|
|
"cite",
|
|
|
|
match get_style_string(style) {
|
|
|
|
Some(style) => vec![("style", style)],
|
|
|
|
None => vec![],
|
|
|
|
},
|
|
|
|
children,
|
|
|
|
),
|
2019-08-25 17:01:51 +00:00
|
|
|
Tag::Em { children } => ("em", vec![], children),
|
|
|
|
Tag::Img { src, alt } => {
|
|
|
|
let mut attrs = vec![];
|
|
|
|
if let Some(src) = src {
|
|
|
|
attrs.push(("src", src));
|
|
|
|
}
|
|
|
|
if let Some(alt) = alt {
|
|
|
|
attrs.push(("alt", alt));
|
|
|
|
}
|
|
|
|
("img", attrs, vec![])
|
2019-10-22 23:32:41 +00:00
|
|
|
}
|
|
|
|
Tag::Li { style, children } => (
|
|
|
|
"li",
|
|
|
|
match get_style_string(style) {
|
|
|
|
Some(style) => vec![("style", style)],
|
|
|
|
None => vec![],
|
|
|
|
},
|
|
|
|
children,
|
|
|
|
),
|
|
|
|
Tag::Ol { style, children } => (
|
|
|
|
"ol",
|
|
|
|
match get_style_string(style) {
|
|
|
|
Some(style) => vec![("style", style)],
|
|
|
|
None => vec![],
|
|
|
|
},
|
|
|
|
children,
|
|
|
|
),
|
|
|
|
Tag::P { style, children } => (
|
|
|
|
"p",
|
|
|
|
match get_style_string(style) {
|
|
|
|
Some(style) => vec![("style", style)],
|
|
|
|
None => vec![],
|
|
|
|
},
|
|
|
|
children,
|
|
|
|
),
|
|
|
|
Tag::Span { style, children } => (
|
|
|
|
"span",
|
|
|
|
match get_style_string(style) {
|
|
|
|
Some(style) => vec![("style", style)],
|
|
|
|
None => vec![],
|
|
|
|
},
|
|
|
|
children,
|
|
|
|
),
|
2019-08-25 17:01:51 +00:00
|
|
|
Tag::Strong { children } => ("strong", vec![], children),
|
2019-10-22 23:32:41 +00:00
|
|
|
Tag::Ul { style, children } => (
|
|
|
|
"ul",
|
|
|
|
match get_style_string(style) {
|
|
|
|
Some(style) => vec![("style", style)],
|
|
|
|
None => vec![],
|
|
|
|
},
|
|
|
|
children,
|
|
|
|
),
|
|
|
|
Tag::Unknown(_) => {
|
|
|
|
panic!("No unknown element should be present in XHTML-IM after parsing.")
|
|
|
|
}
|
2019-08-25 17:01:51 +00:00
|
|
|
};
|
2020-04-02 20:45:20 +00:00
|
|
|
let mut builder = Element::builder(name, ns::XHTML).append_all(children_to_nodes(children));
|
2019-08-25 17:01:51 +00:00
|
|
|
for (key, value) in attrs {
|
|
|
|
builder = builder.attr(key, value);
|
|
|
|
}
|
|
|
|
builder.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 14:03:58 +00:00
|
|
|
fn children_to_nodes(children: Vec<Child>) -> impl IntoIterator<Item = Node> {
|
2019-08-25 17:01:51 +00:00
|
|
|
children.into_iter().map(|child| match child {
|
|
|
|
Child::Tag(tag) => Node::Element(Element::from(tag)),
|
|
|
|
Child::Text(text) => Node::Text(text),
|
2019-09-06 14:03:58 +00:00
|
|
|
})
|
2019-08-25 17:01:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn children_to_html(children: Vec<Child>) -> String {
|
2019-10-22 23:32:41 +00:00
|
|
|
children
|
|
|
|
.into_iter()
|
|
|
|
.map(|child| child.to_html())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.concat()
|
2019-08-25 17:01:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_attr(attr: Option<String>, name: &str) -> String {
|
|
|
|
match attr {
|
|
|
|
Some(attr) => format!(" {}='{}'", name, attr),
|
|
|
|
None => String::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_css(style: Option<&str>) -> Css {
|
|
|
|
let mut properties = vec![];
|
|
|
|
if let Some(style) = style {
|
|
|
|
// TODO: make that parser a bit more resilient to things.
|
2021-10-11 13:22:19 +00:00
|
|
|
for part in style.split(';') {
|
2019-10-22 23:32:41 +00:00
|
|
|
let mut part = part
|
2021-10-11 13:22:19 +00:00
|
|
|
.splitn(2, ':')
|
2019-10-22 23:32:41 +00:00
|
|
|
.map(|a| a.to_string())
|
|
|
|
.collect::<Vec<_>>();
|
2019-08-25 17:01:51 +00:00
|
|
|
let key = part.pop().unwrap();
|
|
|
|
let value = part.pop().unwrap();
|
|
|
|
properties.push(Property { key, value });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
properties
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
2020-10-29 17:39:22 +00:00
|
|
|
assert_size!(XhtmlIm, 32);
|
|
|
|
assert_size!(Child, 56);
|
|
|
|
assert_size!(Tag, 52);
|
2019-08-25 17:01:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
2020-10-29 17:39:48 +00:00
|
|
|
assert_size!(XhtmlIm, 48);
|
2022-09-30 14:31:03 +00:00
|
|
|
assert_size!(Child, 104);
|
2019-08-25 17:01:51 +00:00
|
|
|
assert_size!(Tag, 104);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty() {
|
|
|
|
let elem: Element = "<html xmlns='http://jabber.org/protocol/xhtml-im'/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
|
|
|
let xhtml = XhtmlIm::try_from(elem).unwrap();
|
|
|
|
assert_eq!(xhtml.bodies.len(), 0);
|
|
|
|
|
|
|
|
let elem: Element = "<html xmlns='http://jabber.org/protocol/xhtml-im'><body xmlns='http://www.w3.org/1999/xhtml'/></html>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
|
|
|
let xhtml = XhtmlIm::try_from(elem).unwrap();
|
|
|
|
assert_eq!(xhtml.bodies.len(), 1);
|
|
|
|
|
|
|
|
let elem: Element = "<html xmlns='http://jabber.org/protocol/xhtml-im' xmlns:html='http://www.w3.org/1999/xhtml'><html:body xml:lang='fr'/><html:body xml:lang='en'/></html>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
|
|
|
let xhtml = XhtmlIm::try_from(elem).unwrap();
|
|
|
|
assert_eq!(xhtml.bodies.len(), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_two_same_langs() {
|
|
|
|
let elem: Element = "<html xmlns='http://jabber.org/protocol/xhtml-im' xmlns:html='http://www.w3.org/1999/xhtml'><html:body/><html:body/></html>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
|
|
|
let error = XhtmlIm::try_from(elem).unwrap_err();
|
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "Two identical language bodies found in XHTML-IM.");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tag() {
|
|
|
|
let elem: Element = "<body xmlns='http://www.w3.org/1999/xhtml'/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2019-08-25 18:02:33 +00:00
|
|
|
let body = Body::try_from(elem).unwrap();
|
|
|
|
assert_eq!(body.children.len(), 0);
|
2019-08-25 17:01:51 +00:00
|
|
|
|
|
|
|
let elem: Element = "<body xmlns='http://www.w3.org/1999/xhtml'><p>Hello world!</p></body>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2019-08-25 18:02:33 +00:00
|
|
|
let mut body = Body::try_from(elem).unwrap();
|
|
|
|
assert_eq!(body.style.len(), 0);
|
|
|
|
assert_eq!(body.xml_lang, None);
|
|
|
|
assert_eq!(body.children.len(), 1);
|
|
|
|
let p = match body.children.pop() {
|
2019-08-25 17:01:51 +00:00
|
|
|
Some(Child::Tag(tag)) => tag,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
let mut children = match p {
|
|
|
|
Tag::P { style, children } => {
|
|
|
|
assert_eq!(style.len(), 0);
|
|
|
|
assert_eq!(children.len(), 1);
|
|
|
|
children
|
2019-10-22 23:32:41 +00:00
|
|
|
}
|
2019-08-25 17:01:51 +00:00
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
let text = match children.pop() {
|
|
|
|
Some(Child::Text(text)) => text,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(text, "Hello world!");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unknown_element() {
|
|
|
|
let elem: Element = "<html xmlns='http://jabber.org/protocol/xhtml-im'><body xmlns='http://www.w3.org/1999/xhtml'><coucou>Hello world!</coucou></body></html>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2019-09-05 09:51:05 +00:00
|
|
|
let parsed = XhtmlIm::try_from(elem).unwrap();
|
|
|
|
let parsed2 = parsed.clone();
|
|
|
|
let html = parsed.to_html();
|
2019-08-25 17:01:51 +00:00
|
|
|
assert_eq!(html, "Hello world!");
|
2019-09-05 09:51:05 +00:00
|
|
|
|
|
|
|
let elem = Element::from(parsed2);
|
2022-04-23 13:29:03 +00:00
|
|
|
assert_eq!(String::from(&elem), "<html xmlns='http://jabber.org/protocol/xhtml-im'><body xmlns='http://www.w3.org/1999/xhtml'>Hello world!</body></html>");
|
2019-08-25 17:01:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_generate_html() {
|
|
|
|
let elem: Element = "<html xmlns='http://jabber.org/protocol/xhtml-im'><body xmlns='http://www.w3.org/1999/xhtml'><p>Hello world!</p></body></html>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
|
|
|
let xhtml_im = XhtmlIm::try_from(elem).unwrap();
|
|
|
|
let html = xhtml_im.to_html();
|
|
|
|
assert_eq!(html, "<p>Hello world!</p>");
|
|
|
|
|
|
|
|
let elem: Element = "<html xmlns='http://jabber.org/protocol/xhtml-im'><body xmlns='http://www.w3.org/1999/xhtml'><p>Hello <strong>world</strong>!</p></body></html>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
|
|
|
let xhtml_im = XhtmlIm::try_from(elem).unwrap();
|
|
|
|
let html = xhtml_im.to_html();
|
|
|
|
assert_eq!(html, "<p>Hello <strong>world</strong>!</p>");
|
|
|
|
}
|
2019-08-25 18:02:06 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generate_tree() {
|
|
|
|
let world = "world".to_string();
|
|
|
|
|
2019-10-22 23:32:41 +00:00
|
|
|
Body {
|
|
|
|
style: vec![],
|
|
|
|
xml_lang: Some("en".to_string()),
|
|
|
|
children: vec![Child::Tag(Tag::P {
|
|
|
|
style: vec![],
|
|
|
|
children: vec![
|
|
|
|
Child::Text("Hello ".to_string()),
|
|
|
|
Child::Tag(Tag::Strong {
|
|
|
|
children: vec![Child::Text(world)],
|
|
|
|
}),
|
|
|
|
Child::Text("!".to_string()),
|
|
|
|
],
|
|
|
|
})],
|
|
|
|
};
|
2019-08-25 18:02:06 +00:00
|
|
|
}
|
2019-08-25 17:01:51 +00:00
|
|
|
}
|