// Copyright 2015 The html5ever Project Developers. See the // COPYRIGHT file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #![feature(plugin)] #![plugin(string_cache_plugin)] extern crate string_cache; extern crate tendril; extern crate html5ever; extern crate html5ever_dom_sink; use std::default::Default; use tendril::{StrTendril, SliceExt}; use html5ever::driver::ParseOpts; use html5ever::{parse_fragment, parse, one_input, serialize}; use html5ever_dom_sink::rcdom::RcDom; fn parse_and_serialize(input: StrTendril) -> StrTendril { let dom: RcDom = parse_fragment(one_input(input), atom!(body), ParseOpts::default()); let inner = &dom.document.borrow().children[0]; let mut result = vec![]; serialize(&mut result, inner, Default::default()).unwrap(); StrTendril::try_from_byte_slice(&result).unwrap() } macro_rules! test { ($name:ident, $input:expr, $output:expr) => { #[test] fn $name() { assert_eq!($output, &*parse_and_serialize($input.to_tendril())); } }; // Shorthand for $output = $input ($name:ident, $input:expr) => { test!($name, $input, $input); }; } test!(empty, r#""#); test!(smoke_test, r#"

Hello, World!

"#); test!(misnest, r#"

Hello!

, World!"#, r#"

Hello!

, World!"#); test!(attr_literal, r#""#); test!(attr_escape_amp, r#""#); test!(attr_escape_amp_2, r#""#, r#""#); test!(attr_escape_nbsp, "", r#""#); test!(attr_escape_quot, r#""#, r#""#); test!(attr_escape_several, r#""#, r#""#); test!(text_literal, r#"

"'"

"#); test!(text_escape_amp, r#"

&

"#); test!(text_escape_amp_2, r#"

&

"#, r#"

&

"#); test!(text_escape_nbsp, "

x\u{a0}y

", r#"

x y

"#); test!(text_escape_lt, r#"

<

"#); test!(text_escape_gt, r#"

>

"#); test!(text_escape_gt2, r#"

>

"#, r#"

>

"#); test!(script_literal, r#""#); test!(style_literal, r#""#); test!(xmp_literal, r#"(x & 1) < 2; y > "foo" + 'bar'"#); test!(iframe_literal, r#""#); test!(noembed_literal, r#"(x & 1) < 2; y > "foo" + 'bar'"#); test!(noframes_literal, r#"(x & 1) < 2; y > "foo" + 'bar'"#); test!(pre_lf_0, "
foo bar
"); test!(pre_lf_1, "
\nfoo bar
", "
foo bar
"); test!(pre_lf_2, "
\n\nfoo bar
"); test!(textarea_lf_0, ""); test!(textarea_lf_1, "", ""); test!(textarea_lf_2, ""); test!(listing_lf_0, "foo bar"); test!(listing_lf_1, "\nfoo bar", "foo bar"); test!(listing_lf_2, "\n\nfoo bar"); test!(comment_1, r#"

hi

"#); test!(comment_2, r#"

hi

"#); test!(comment_3, r#"

hi

"#); test!(comment_4, r#"

hi

"#); // FIXME: test serialization of qualified tag/attribute names that can't be // parsed from HTML test!(attr_ns_1, r#""#); test!(attr_ns_2, r#""#); test!(attr_ns_3, r#""#); test!(attr_ns_4, r#""#); #[test] fn doctype() { let dom: RcDom = parse(one_input("".to_tendril()), ParseOpts::default()); dom.document.borrow_mut().children.truncate(1); // Remove let mut result = vec![]; serialize(&mut result, &dom.document, Default::default()).unwrap(); assert_eq!(String::from_utf8(result).unwrap(), "\n"); }