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]
11 extern crate html5ever;
12 
13 use std::borrow::Cow;
14 use std::collections::HashMap;
15 use std::default::Default;
16 use std::io;
17 
18 use html5ever::parse_document;
19 use html5ever::tendril::*;
20 use html5ever::tree_builder::{ElementFlags, NodeOrText, QuirksMode, TreeSink};
21 use html5ever::{Attribute, ExpandedName, QualName};
22 
23 struct Sink {
24     next_id: usize,
25     names: HashMap<usize, QualName>,
26 }
27 
28 impl Sink {
get_id(&mut self) -> usize29     fn get_id(&mut self) -> usize {
30         let id = self.next_id;
31         self.next_id += 2;
32         id
33     }
34 }
35 
36 impl TreeSink for Sink {
37     type Handle = usize;
38     type Output = Self;
finish(self) -> Self39     fn finish(self) -> Self {
40         self
41     }
42 
get_document(&mut self) -> usize43     fn get_document(&mut self) -> usize {
44         0
45     }
46 
get_template_contents(&mut self, target: &usize) -> usize47     fn get_template_contents(&mut self, target: &usize) -> usize {
48         if let Some(expanded_name!(html "template")) = self.names.get(&target).map(|n| n.expanded())
49         {
50             target + 1
51         } else {
52             panic!("not a template element")
53         }
54     }
55 
same_node(&self, x: &usize, y: &usize) -> bool56     fn same_node(&self, x: &usize, y: &usize) -> bool {
57         x == y
58     }
59 
elem_name(&self, target: &usize) -> ExpandedName60     fn elem_name(&self, target: &usize) -> ExpandedName {
61         self.names.get(target).expect("not an element").expanded()
62     }
63 
create_element(&mut self, name: QualName, _: Vec<Attribute>, _: ElementFlags) -> usize64     fn create_element(&mut self, name: QualName, _: Vec<Attribute>, _: ElementFlags) -> usize {
65         let id = self.get_id();
66         self.names.insert(id, name);
67         id
68     }
69 
create_comment(&mut self, _text: StrTendril) -> usize70     fn create_comment(&mut self, _text: StrTendril) -> usize {
71         self.get_id()
72     }
73 
74     #[allow(unused_variables)]
create_pi(&mut self, target: StrTendril, value: StrTendril) -> usize75     fn create_pi(&mut self, target: StrTendril, value: StrTendril) -> usize {
76         unimplemented!()
77     }
78 
append_before_sibling(&mut self, _sibling: &usize, _new_node: NodeOrText<usize>)79     fn append_before_sibling(&mut self, _sibling: &usize, _new_node: NodeOrText<usize>) {}
80 
append_based_on_parent_node( &mut self, _element: &usize, _prev_element: &usize, _new_node: NodeOrText<usize>, )81     fn append_based_on_parent_node(
82         &mut self,
83         _element: &usize,
84         _prev_element: &usize,
85         _new_node: NodeOrText<usize>,
86     ) {
87     }
88 
parse_error(&mut self, _msg: Cow<'static, str>)89     fn parse_error(&mut self, _msg: Cow<'static, str>) {}
set_quirks_mode(&mut self, _mode: QuirksMode)90     fn set_quirks_mode(&mut self, _mode: QuirksMode) {}
append(&mut self, _parent: &usize, _child: NodeOrText<usize>)91     fn append(&mut self, _parent: &usize, _child: NodeOrText<usize>) {}
92 
append_doctype_to_document(&mut self, _: StrTendril, _: StrTendril, _: StrTendril)93     fn append_doctype_to_document(&mut self, _: StrTendril, _: StrTendril, _: StrTendril) {}
add_attrs_if_missing(&mut self, target: &usize, _attrs: Vec<Attribute>)94     fn add_attrs_if_missing(&mut self, target: &usize, _attrs: Vec<Attribute>) {
95         assert!(self.names.contains_key(&target), "not an element");
96     }
remove_from_parent(&mut self, _target: &usize)97     fn remove_from_parent(&mut self, _target: &usize) {}
reparent_children(&mut self, _node: &usize, _new_parent: &usize)98     fn reparent_children(&mut self, _node: &usize, _new_parent: &usize) {}
mark_script_already_started(&mut self, _node: &usize)99     fn mark_script_already_started(&mut self, _node: &usize) {}
100 }
101 
main()102 fn main() {
103     let sink = Sink {
104         next_id: 1,
105         names: HashMap::new(),
106     };
107     let stdin = io::stdin();
108     parse_document(sink, Default::default())
109         .from_utf8()
110         .read_from(&mut stdin.lock())
111         .unwrap();
112 }
113