New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vCard/JSON extraction #19443
vCard/JSON extraction #19443
Changes from 1 commit
eb2c7e0
015ef37
43aeb7c
f306835
0530512
f939632
3d68d2a
a4ed961
4628943
cbfc666
146dd58
9284187
dd4dc70
43650c2
19408a0
79d140d
aab6900
35468f8
8cafd64
87d1efa
c580c3f
3cd9731
d0cb12e
0ae2d08
7131823
a3e5b9f
0e5023d
37f70c6
543de1c
a781bba
04a043d
c740aab
File filter...
Jump to…
Merged vcard and json logic
- Loading branch information
| @@ -1710,9 +1710,15 @@ impl Document { | ||
| // TODO: completely loaded. | ||
|
|
||
| // Step 13. | ||
CJ8664
Author
|
||
| let result = Microdata::parse(self); | ||
| let event = ScriptMsg::SendMicrodata(result); | ||
| self.send_to_constellation(event); | ||
| let htmlelement = self.get_html_element(); | ||
| let result = Microdata::parse(self, htmlelement.unwrap().upcast::<Node>()); //Chirag | ||
| if !result.get("vcard").unwrap().is_empty() { | ||
| let event = ScriptMsg::SendMicrodata(result.get("vcard").unwrap().to_string()); | ||
| self.send_to_constellation(event); | ||
| } else { | ||
| let event = ScriptMsg::SendMicrodata(result.get("json").unwrap().to_string()); | ||
| self.send_to_constellation(event); | ||
| } | ||
| } | ||
|
|
||
| // https://html.spec.whatwg.org/multipage/#pending-parsing-blocking-script | ||
| @@ -923,6 +923,10 @@ impl Element { | ||
| &self.local_name | ||
| } | ||
|
|
||
| pub fn tag_name(&self) -> DOMString{ | ||
| self.TagName() | ||
jdm
Member
|
||
| } | ||
|
|
||
| pub fn parsed_name(&self, mut name: DOMString) -> LocalName { | ||
| if self.html_element_in_html_document() { | ||
| name.make_ascii_lowercase(); | ||
| @@ -6,18 +6,39 @@ use dom::bindings::codegen::Bindings::DocumentBinding::DocumentBinding::Document | ||
| use dom::bindings::codegen::Bindings::ElementBinding::ElementBinding::ElementMethods; | ||
| use dom::bindings::inheritance::Castable; | ||
| use dom::bindings::root::DomRoot; | ||
| use dom::characterdata::CharacterData; | ||
| use dom::document::Document; | ||
| use dom::element::Element; | ||
| use dom::htmlelement::HTMLElement; | ||
| use dom::node::Node; | ||
| use dom::text::Text; | ||
| use std::borrow::Cow; | ||
| use std::collections::HashMap; | ||
| use serde_json; | ||
|
|
||
| pub struct Microdata {} | ||
jdm
Member
|
||
| #[derive(Serialize,Clone)] | ||
| #[serde(untagged)] | ||
| enum Data { | ||
| StrValue(String), | ||
| VecValue(Vec<Data>), | ||
| DataValue(Box<Data>), | ||
| HashValue(HashMap<String,Data>) | ||
| } | ||
|
|
||
| impl Microdata { | ||
| //[Pref="dom.microdata.testing.enabled"] | ||
| pub fn parse(doc: &Document) -> String { | ||
| pub fn parse(doc: &Document, node : &Node) -> HashMap<String, String> { | ||
| //let dup_doc = Cow::Borrowed(doc); | ||
| let serialized_vcard = Self::parse_vcard(doc); | ||
| let serialized_json = Self::parse_json(node); | ||
| let mut serialized_data : HashMap<String, String> = HashMap::new(); | ||
| serialized_data.insert("vcard".to_string(), serialized_vcard); | ||
| serialized_data.insert("json".to_string(), serialized_json); | ||
| return serialized_data; | ||
| } | ||
|
|
||
| pub fn parse_vcard(doc: &Document) -> String { | ||
|
|
||
| let ele = doc.upcast::<Node>(); | ||
| let mut start_vcard = false; | ||
| @@ -131,4 +152,69 @@ impl Microdata { | ||
| println!("{}", result); | ||
| return result; | ||
| } | ||
|
|
||
| pub fn parse_json(node: &Node) -> String { | ||
| let json_data : Data = Self::traverse(node).unwrap(); | ||
| let json = serde_json::to_string(&json_data); | ||
| //println!("printing json from microdata {:?}", json); | ||
| return json.ok().unwrap(); | ||
| } | ||
|
|
||
| fn get_attr_value(element: &Element, property: &str)-> Option<String> { | ||
| println!("{:?}",property); | ||
| // let mut atoms = match property { | ||
| // "itemprop" => element.get_tokenlist_attribute(&local_name!("itemprop"), ), | ||
| // "itemtype" => element.get_tokenlist_attribute(&local_name!("itemtype"), ), | ||
| // _ => {}, | ||
| // }; | ||
| // if !atoms.is_empty() { | ||
| // let temp_key = atoms.remove(0); | ||
| // return Some(String::from(temp_key.trim()).to_owned()); | ||
| // } | ||
| // else { | ||
| // return None; | ||
| // } | ||
| Some(String::from("itemprop")) | ||
| } | ||
|
|
||
| fn traverse( node: &Node)-> Option<Data> { | ||
| if !node.is::<Element>(){ | ||
| if let Some(ref text) = node.downcast::<Text>(){ | ||
| let mut content = String::new(); | ||
| content.push_str(&text.upcast::<CharacterData>().data()); | ||
| return Some(Data::StrValue(String::from(content))); | ||
| } | ||
| None | ||
| } | ||
| else { | ||
| let element = node.downcast::<Element>().unwrap(); | ||
| let mut head_str = String::from(""); | ||
| let mut parent_vec:Vec<Data> = Vec::new(); | ||
| let item_type = Self::get_attr_value(element,"itemtype").unwrap(); | ||
| // if element.has_attribute(&local_name!("itemscope")) && element.has_attribute(&local_name!("itemtype")) && !element.has_attribute(&local_name!("itemprop")) { | ||
| // head_str = String::from("items"); | ||
| // let mut propMap:HashMap<String,Data> = HashMap::new(); | ||
| // //Data::HashValue(propMap) | ||
| // let item_type = Self::get_attr_value(element,"item_type").unwrap(); | ||
|
|
||
| // } | ||
| // else if element.has_attribute(&local_name!("itemprop")) && element.has_attribute(&local_name!("item_type")) { | ||
|
|
||
| // head_str = Self::get_attr_value(element,"itemprop").unwrap(); | ||
| // let item_type = Self::get_attr_value(element,"item_type").unwrap(); | ||
|
|
||
| // } | ||
| // else { | ||
| // return None; | ||
| // } | ||
| let mut inner_map:HashMap<String,Data> = HashMap::new(); | ||
| for child in node.children(){ | ||
| if let Some(childData) = Self::traverse(child.upcast::<Node>()){ | ||
| parent_vec.push(childData); | ||
| } | ||
| } | ||
| inner_map.insert(head_str,Data::VecValue(parent_vec)); | ||
| Some(Data::HashValue(inner_map)) | ||
| } | ||
| } | ||
| } | ||
The comments here refer to actual step numbers defined in the HTML specification. We should not add ones for steps that do not exist :)