]> git.proxmox.com Git - rustc.git/blob - src/vendor/html5ever/tests/serializer.rs
New upstream version 1.22.1+dfsg1
[rustc.git] / src / vendor / html5ever / tests / serializer.rs
1 // Copyright 2014-2017 The html5ever Project Developers. See the
2 // COPYRIGHT file at the top-level directory of this distribution.
3 //
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.
9
10 #[macro_use] extern crate html5ever;
11
12 use std::default::Default;
13
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};
18
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![],
23 ).one(input);
24 let inner = &dom.document.children.borrow()[0];
25
26 let mut result = vec![];
27 serialize(&mut result, inner, Default::default()).unwrap();
28 StrTendril::try_from_byte_slice(&result).unwrap()
29 }
30
31 macro_rules! test {
32 ($name:ident, $input:expr, $output:expr) => {
33 #[test]
34 fn $name() {
35 assert_eq!($output, &*parse_and_serialize($input.to_tendril()));
36 }
37 };
38
39 // Shorthand for $output = $input
40 ($name:ident, $input:expr) => {
41 test!($name, $input, $input);
42 };
43 }
44
45 test!(empty, r#""#);
46 test!(smoke_test, r#"<p><i>Hello</i>, World!</p>"#);
47
48 test!(misnest, r#"<p><i>Hello!</p>, World!</i>"#,
49 r#"<p><i>Hello!</i></p><i>, World!</i>"#);
50
51 test!(attr_literal, r#"<base foo="<'>">"#);
52 test!(attr_escape_amp, r#"<base foo="&amp;">"#);
53 test!(attr_escape_amp_2, r#"<base foo=&amp>"#, r#"<base foo="&amp;">"#);
54 test!(attr_escape_nbsp, "<base foo=x\u{a0}y>", r#"<base foo="x&nbsp;y">"#);
55 test!(attr_escape_quot, r#"<base foo='"'>"#, r#"<base foo="&quot;">"#);
56 test!(attr_escape_several, r#"<span foo=3 title='test "with" &amp;quot;'>"#,
57 r#"<span foo="3" title="test &quot;with&quot; &amp;quot;"></span>"#);
58
59 test!(text_literal, r#"<p>"'"</p>"#);
60 test!(text_escape_amp, r#"<p>&amp;</p>"#);
61 test!(text_escape_amp_2, r#"<p>&amp</p>"#, r#"<p>&amp;</p>"#);
62 test!(text_escape_nbsp, "<p>x\u{a0}y</p>", r#"<p>x&nbsp;y</p>"#);
63 test!(text_escape_lt, r#"<p>&lt;</p>"#);
64 test!(text_escape_gt, r#"<p>&gt;</p>"#);
65 test!(text_escape_gt2, r#"<p>></p>"#, r#"<p>&gt;</p>"#);
66
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>"#);
73
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>");
77
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>");
81
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>");
85
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>"#);
90
91 // FIXME: test serialization of qualified tag/attribute names that can't be
92 // parsed from HTML
93
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>"#);
98
99 #[test]
100 fn doctype() {
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>");
107 }