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 11 commits
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…
Some generated files are not rendered by default. Learn more.
| @@ -193,6 +193,6 @@ pub trait WindowMethods { | ||
| /// run the event loop at the vsync interval. | ||
| fn set_animation_state(&self, _state: AnimationState) {} | ||
|
|
||
| /// Print Microdata on the Console | ||
| fn print_microdata(&self, _data: String) {} | ||
| /// Print Microdata on the Console or write to file | ||
| fn print_microdata(&self, _data: String, _datatype: String) {} | ||
jdm
Member
|
||
| } | ||
| @@ -1710,9 +1710,17 @@ impl Document { | ||
| // TODO: completely loaded. | ||
|
|
||
| // Step 13. | ||
jdm
Member
|
||
| 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>()); | ||
| if !result.get("vcard").unwrap().is_empty() { | ||
| let event = ScriptMsg::SendMicrodata(result.get("vcard").unwrap().to_string(), "vcard".to_string()); | ||
| self.send_to_constellation(event); | ||
| self.SetTitle(DOMString::from("Extracted vCard".to_string())); | ||
jdm
Member
|
||
| } else if !result.get("json").unwrap().is_empty() { | ||
| let event = ScriptMsg::SendMicrodata(result.get("json").unwrap().to_string(), "json".to_string()); | ||
| self.send_to_constellation(event); | ||
| self.SetTitle(DOMString::from("Extracted JSON".to_string())); | ||
| } | ||
| } | ||
|
|
||
| // 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(); | ||
| @@ -2,28 +2,160 @@ | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| use dom::bindings::codegen::Bindings::DocumentBinding::DocumentBinding::DocumentMethods; | ||
| 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 serde_json; | ||
| use std::borrow::Cow; | ||
| use std::collections::HashMap; | ||
|
|
||
| pub struct Microdata {} | ||
jdm
Member
|
||
|
|
||
| impl Microdata { | ||
| //[Pref="dom.microdata.testing.enabled"] | ||
| pub fn parse() { | ||
| println!("Hello"); | ||
| let mut book_reviews = HashMap::new(); | ||
| let mut rating = HashMap::new(); | ||
| pub fn parse(doc: &Document, node: &Node) -> HashMap<String, String> { | ||
jdm
Member
|
||
| 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; | ||
| let mut result: String = String::new(); | ||
| let mut master_map: HashMap<String, HashMap<String, String>> = HashMap::new(); | ||
| let mut master_key: String = String::new(); | ||
|
|
||
| result += "BEGIN:VCARD\nPROFILE:VCARD\nVERSION:4.0\nSOURCE:"; | ||
| result += doc.url().as_str(); | ||
|
|
||
| let title = doc.Title(); | ||
| if !title.is_empty() && !title.trim().is_empty() { | ||
| result += "\nNAME:"; | ||
| result += title.trim(); | ||
| } | ||
|
|
||
| result += "\n"; | ||
|
|
||
| for element in ele.traverse_preorder().filter_map(DomRoot::downcast::<Element>) { | ||
jdm
Member
|
||
| if element.is::<HTMLElement>() { | ||
| if element.has_attribute(&local_name!("itemtype")) { | ||
| let mut atoms = element.get_tokenlist_attribute(&local_name!("itemtype"), ); | ||
| if !atoms.is_empty() { | ||
| let val = atoms.remove(0); | ||
| if val.trim() == "http://microformats.org/profile/hcard" { | ||
| if !start_vcard { | ||
| start_vcard = true; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if start_vcard { | ||
jdm
Member
|
||
| let mut atoms = element.get_tokenlist_attribute(&local_name!("itemprop"), ); | ||
| if !atoms.is_empty() { | ||
| let temp_key = atoms.remove(0); | ||
| if element.has_attribute(&local_name!("itemscope")) { | ||
| master_key = String::from(temp_key.trim()).to_owned(); | ||
| let dup_master_key = Cow::Borrowed(&master_key); | ||
| master_map.entry(dup_master_key.to_string()).or_insert(HashMap::new()); | ||
| } else { | ||
| let temp = String::from(temp_key.trim()).to_owned(); | ||
| let dup_key = Cow::Borrowed(&temp); | ||
| let data = String::from(element.GetInnerHTML().unwrap()); | ||
| let dup_master_key = Cow::Borrowed(&master_key); | ||
| let temp_map = master_map.entry(dup_master_key.to_string()).or_insert(HashMap::new()); | ||
| temp_map.insert(dup_key.to_string(), String::from(data)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| let vcard_parts = ["n", "org", "tel", "adr"]; | ||
| for info_type in vcard_parts.iter() { | ||
| let detail_map_val = master_map.get(*info_type); | ||
| if detail_map_val.is_none() { | ||
| continue; | ||
| } | ||
| let detail_map = detail_map_val.unwrap(); | ||
| match *info_type { | ||
| "n" => { | ||
| let mut n_value: String = String::new(); | ||
|
|
||
| rating.insert("a", "1"); | ||
| rating.insert("b", "2"); | ||
| rating.insert("c", "3"); | ||
| rating.insert("d", "4"); | ||
| let name_parts = ["family-name", "given-name", | ||
| "additional-name", "honorific-prefix", "honorific-suffix"]; | ||
| for part in name_parts.iter() { | ||
| if detail_map.contains_key(*part) { | ||
| n_value += format!("{};", detail_map.get(*part).unwrap()).as_str(); | ||
| } | ||
| } | ||
| n_value.pop(); | ||
|
|
||
| book_reviews.insert("Adventures of Huckleberry Finn", rating); | ||
| result += format!("{}:{}\n", info_type.to_ascii_uppercase(), n_value).as_str(); | ||
| }, | ||
| "org" => { | ||
| let mut org_value: String = String::new(); | ||
|
|
||
| let j = serde_json::to_string(&book_reviews); | ||
| let org_parts = ["organization-name", "organization-unit"]; | ||
| for part in org_parts.iter() { | ||
| if detail_map.contains_key(*part) { | ||
| org_value += format!("{};", detail_map.get(*part).unwrap()).as_str(); | ||
| } | ||
| } | ||
| org_value.pop(); | ||
|
|
||
| result += format!("{}:{}\n", info_type.to_ascii_uppercase(), org_value).as_str(); | ||
| }, | ||
| "tel" => { | ||
| let mut tel_value: String = String::new(); | ||
|
|
||
| let tel_parts = ["value"]; | ||
| for part in tel_parts.iter() { | ||
| if detail_map.contains_key(*part) { | ||
| tel_value += format!("{};", detail_map.get(*part).unwrap()).as_str(); | ||
| } | ||
| } | ||
| tel_value.pop(); | ||
|
|
||
| result += format!("{}:{}\n", info_type.to_ascii_uppercase(), tel_value).as_str(); | ||
| }, | ||
| "adr" => { | ||
| let mut adr_value: String = String::new(); | ||
|
|
||
| let adr_parts = ["street-address", "locality", "region", "postal-code", | ||
| "country-name", "post-office-box", "extended-address"]; | ||
| for part in adr_parts.iter() { | ||
| if detail_map.contains_key(*part) { | ||
| adr_value += format!("{};", detail_map.get(*part).unwrap()).as_str(); | ||
| } | ||
| } | ||
| adr_value.pop(); | ||
|
|
||
| result += format!("{}:{}\n", info_type.to_ascii_uppercase(), adr_value).as_str(); | ||
| }, | ||
| _ => {}, | ||
| } | ||
| } | ||
| result += "END:VCARD"; | ||
| if start_vcard { | ||
| return result; | ||
| } else { | ||
| return "".to_string(); | ||
| } | ||
| } | ||
|
|
||
| // Print, write to a file, or send to an HTTP server. | ||
| println!("printing json from Microdata module"); | ||
| println!("{:?}", j); | ||
| pub fn parse_json(node: &Node) -> String { | ||
| // TODO Write the logic for JSON Parsing | ||
| return "".to_string(); | ||
| } | ||
| } | ||
| @@ -0,0 +1,12 @@ | ||
| <!doctype html> | ||
| <meta charset="utf-8"> | ||
| <title>JSON</title> | ||
| <script src="/resources/testharness.js"></script> | ||
| <script src="/resources/testharnessreport.js"></script> | ||
|
|
||
| <script> | ||
| test(function () { | ||
| /* do something long and slow here */ | ||
jdm
Member
|
||
| assert_true(true); | ||
| }, "No operations"); | ||
| </script> | ||
| @@ -0,0 +1,12 @@ | ||
| <!doctype html> | ||
| <meta charset="utf-8"> | ||
| <title>vCard</title> | ||
| <script src="/resources/testharness.js"></script> | ||
| <script src="/resources/testharnessreport.js"></script> | ||
|
|
||
| <script> | ||
| test(function () { | ||
| /* do something long and slow here */ | ||
| assert_true(true); | ||
| }, "No operations"); | ||
| </script> |
We should provide slightly more informative documentation here ;)