1 // Copyright 2014-2017 The html5ever Project Developers. See the
2 // COPYRIGHT file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 #[macro_use] extern crate html5ever;
12 use std
::default::Default
;
14 use html5ever
::{parse_fragment, parse_document, serialize, QualName}
;
15 use html5ever
::driver
::ParseOpts
;
16 use html5ever
::rcdom
::RcDom
;
17 use html5ever
::tendril
::{StrTendril, SliceExt, TendrilSink}
;
19 fn parse_and_serialize(input
: StrTendril
) -> StrTendril
{
20 let dom
= parse_fragment(
21 RcDom
::default(), ParseOpts
::default(),
22 QualName
::new(None
, ns
!(html
), local_name
!("body")), vec
![],
24 let inner
= &dom
.document
.children
.borrow()[0];
26 let mut result
= vec
![];
27 serialize(&mut result
, inner
, Default
::default()).unwrap();
28 StrTendril
::try_from_byte_slice(&result
).unwrap()
32 ($name
:ident
, $input
:expr
, $output
:expr
) => {
35 assert_eq
!($output
, &*parse_and_serialize($input
.to_tendril()));
39 // Shorthand for $output = $input
40 ($name
:ident
, $input
:expr
) => {
41 test
!($name
, $input
, $input
);
46 test
!(smoke_test
, r
#"<p><i>Hello</i>, World!</p>"#);
48 test
!(misnest
, r
#"<p><i>Hello!</p>, World!</i>"#,
49 r
#"<p><i>Hello!</i></p><i>, World!</i>"#);
51 test
!(attr_literal
, r
#"<base foo="<'>">"#);
52 test
!(attr_escape_amp
, r
#"<base foo="&">"#);
53 test
!(attr_escape_amp_2
, r
#"<base foo=&>"#, r#"<base foo="&">"#);
54 test
!(attr_escape_nbsp
, "<base foo=x\u{a0}y>", r
#"<base foo="x y">"#);
55 test
!(attr_escape_quot
, r
#"<base foo='"'>"#, r#"<base foo=""">"#);
56 test!(attr_escape_several, r#"<span foo=3 title='test "with" &quot;'>"#,
57 r#"<span foo="3" title="test "with" &quot;"></span>"#);
59 test!(text_literal, r#"<p>"'"</p>"#);
60 test!(text_escape_amp, r#"<p>&</p>"#);
61 test!(text_escape_amp_2, r#"<p>&</p>"#, r#"<p>&</p>"#);
62 test!(text_escape_nbsp, "<p>x\u{a0}y</p>", r#"<p>x y</p>"#);
63 test!(text_escape_lt, r#"<p><</p>"#);
64 test!(text_escape_gt, r#"<p>></p>"#);
65 test!(text_escape_gt2, r#"<p>></p>"#, r#"<p>></p>"#);
67 test!(script_literal, r#"<script>(x & 1) < 2; y > "foo" + 'bar'</script>"#);
68 test!(style_literal, r#"<style>(x & 1) < 2; y > "foo" + 'bar'</style>"#);
69 test!(xmp_literal, r#"<xmp>(x & 1) < 2; y > "foo" + 'bar'</xmp>"#);
70 test!(iframe_literal, r#"<iframe>(x & 1) < 2; y > "foo" + 'bar'</iframe>"#);
71 test!(noembed_literal, r#"<noembed>(x & 1) < 2; y > "foo" + 'bar'</noembed>"#);
72 test!(noframes_literal, r#"<noframes>(x & 1) < 2; y > "foo" + 'bar'</noframes>"#);
74 test!(pre_lf_0, "<pre>foo bar</pre>");
75 test!(pre_lf_1, "<pre>\nfoo bar</pre>", "<pre>foo bar</pre>");
76 test!(pre_lf_2, "<pre>\n\nfoo bar</pre>");
78 test!(textarea_lf_0, "<textarea>foo bar</textarea>");
79 test!(textarea_lf_1, "<textarea>\nfoo bar</textarea>", "<textarea>foo bar</textarea>");
80 test!(textarea_lf_2, "<textarea>\n\nfoo bar</textarea>");
82 test!(listing_lf_0, "<listing>foo bar</listing>");
83 test!(listing_lf_1, "<listing>\nfoo bar</listing>", "<listing>foo bar</listing>");
84 test!(listing_lf_2, "<listing>\n\nfoo bar</listing>");
86 test!(comment_1, r#"<p>hi <!--world--></p>"#);
87 test!(comment_2, r#"<p>hi <!-- world--></p>"#);
88 test!(comment_3, r#"<p>hi <!--world --></p>"#);
89 test!(comment_4, r#"<p>hi <!-- world --></p>"#);
91 // FIXME: test serialization of qualified tag/attribute names that can't be
94 test!(attr_ns_1, r#"<svg xmlns="bleh"></svg>"#);
95 test!(attr_ns_2, r#"<svg xmlns:foo="bleh"></svg>"#);
96 test!(attr_ns_3, r#"<svg xmlns:xlink="bleh"></svg>"#);
97 test!(attr_ns_4, r#"<svg xlink:href="bleh"></svg>"#);
101 let dom = parse_document(
102 RcDom::default(), ParseOpts::default()).one("<!doctype html>");
103 dom.document.children.borrow_mut().truncate(1); // Remove <html>
104 let mut result = vec![];
105 serialize(&mut result, &dom.document, Default::default()).unwrap();
106 assert_eq!(String::from_utf8(result).unwrap(), "<!DOCTYPE html>");