Skip to content
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

Closed
wants to merge 32 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
eb2c7e0
Merge pull request #11 from servo/master
niravjain Nov 13, 2017
015ef37
Merge pull request #12 from servo/master
CJ8664 Nov 22, 2017
43aeb7c
Added microdata module
niravjain Nov 24, 2017
f306835
Code to send msg from servo to servoshell (EmbedderMsg)
CJ8664 Nov 25, 2017
0530512
Merge branch 'master' of https://github.com/CJ8664/servo
CJ8664 Nov 25, 2017
f939632
naming convention
CJ8664 Nov 25, 2017
3d68d2a
trying serde_json crate
CJ8664 Nov 25, 2017
a4ed961
Uploading erroneous code
CJ8664 Nov 25, 2017
4628943
Uploading erroneous code
CJ8664 Nov 25, 2017
cbfc666
Merge remote-tracking branch 'refs/remotes/origin/serde_try' into ser…
CJ8664 Nov 25, 2017
146dd58
Merge pull request #13 from CJ8664/serde_try
CJ8664 Nov 25, 2017
9284187
serde json working for Hashmap
CJ8664 Nov 25, 2017
dd4dc70
Updated the servo-shell communication
CJ8664 Nov 29, 2017
43650c2
vCard working
CJ8664 Nov 29, 2017
19408a0
Adding adr to vCard
niravjain Nov 30, 2017
79d140d
Adding adr to vCard
niravjain Nov 30, 2017
aab6900
Merged vcard and json logic
CJ8664 Nov 30, 2017
35468f8
Fixed tidy errors
niravjain Nov 30, 2017
8cafd64
Updated code to pass the type of microdata as a parameter
CJ8664 Dec 1, 2017
87d1efa
Merge branch 'vcard' of https://github.com/CJ8664/servo into vcard
CJ8664 Dec 1, 2017
c580c3f
Added code to notify user via change in title
CJ8664 Dec 1, 2017
3cd9731
Created dummy test cases
CJ8664 Dec 1, 2017
d0cb12e
Removed JSON code and replaced with a stub
CJ8664 Dec 1, 2017
0ae2d08
Merge pull request #14 from CJ8664/vcard
CJ8664 Dec 1, 2017
7131823
Merge pull request #15 from servo/master
CJ8664 Dec 1, 2017
a3e5b9f
Updated manifest
CJ8664 Dec 1, 2017
0e5023d
Partially updated the code based on reviews
CJ8664 Dec 2, 2017
37f70c6
Fixed lint issues
CJ8664 Dec 2, 2017
543de1c
Fixed lint issues
CJ8664 Dec 2, 2017
a781bba
Merge fix
CJ8664 Dec 12, 2017
04a043d
Merge branch 'servo-master'
CJ8664 Dec 12, 2017
c740aab
Rebuild cargo
CJ8664 Dec 12, 2017
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

vCard working

  • Loading branch information
CJ8664 committed Nov 29, 2017
commit 43650c2770a484b9594777135a6f49a3f6b1c74d
@@ -2,28 +2,108 @@
* 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 serde_json;
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::document::Document;
use dom::element::Element;
use dom::htmlelement::HTMLElement;
use dom::node::Node;
use std::borrow::Cow;
use std::collections::HashMap;

pub struct Microdata {}

This comment has been minimized.

Copy link
@jdm

jdm Dec 1, 2017

Member

Rather than an empty structure with static methods, we should use plain old functions in this file instead. Code in other modules can use microdata::parse instead.


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) -> String {

rating.insert("a", "1");
rating.insert("b", "2");
rating.insert("c", "3");
rating.insert("d", "4");
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();

book_reviews.insert("Adventures of Huckleberry Finn", rating);
result += "BEGIN:VCARD\nPROFILE:VCARD\nVERSION:4.0\nSOURCE:";
result += doc.url().as_str();
result += "\nNAME:";
result += doc.Title().trim();
result += "\n";

let j = serde_json::to_string(&book_reviews);
for element in ele.traverse_preorder().filter_map(DomRoot::downcast::<Element>){
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 {

This comment has been minimized.

Copy link
@jdm

jdm Dec 1, 2017

Member

The implementation here doesn't appear to follow the algorithms defined in the specification. I would expect to see code that follows a pattern like this:

// https://html.spec.whatwg.org/multipage/#extracting-a-vcard
// Steps 1 & 2
let elements = ele.traverse_preorder().filter_map(DomRoot::downcast::<HTMLElement>);
let node = elements.find(|element| {
    element.Itemtypes().iter().find(|val| val == "http://microformats.org/profile/hcard").is_some())
});
let node = match node {
    Some(node) => node,
    None => /* return no vcard */,
};
/* prepare the vcard output */
// Step 11
for element in collect_properties_of_item(node) {
    for property in element.PropertyNames() {
        // Step 11.2
        let value = property_value(element, property);
        /* remaining steps */
    }
}
/* finish up vcard output */

Does that make sense?

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));
}
}
}
}
}
//println!("{:?}", master_map);
for (info_type, detail_map) in &master_map {
//println!("{} -> {:?}", info_type, detail_map);
match info_type.as_str() {
"n" => {
let mut given_name = "";
let mut family_name = "";
if detail_map.contains_key("family-name") {
family_name = detail_map.get("family-name").unwrap();
}
if detail_map.contains_key("given-name") {
given_name = detail_map.get("given-name").unwrap();
}
result += format!("N:{};{}\n", family_name, given_name).as_str();
result += format!("FN:{} {}\n", given_name, family_name).as_str();
},
"org" => {
let mut organization_unit = "";
let mut organization_name = "";
if detail_map.contains_key("organization-unit"){
organization_unit = detail_map.get("organization-unit").unwrap();
}
if detail_map.contains_key("organization-name"){
organization_name = detail_map.get("organization-name").unwrap();
}
result += format!("ORG:{};{}\n", organization_name, organization_unit).as_str();
},
"tel" => {

},
"adr" => {

// Print, write to a file, or send to an HTTP server.
println!("printing json from Microdata module");
println!("{:?}", j);
},
_ => {},
}
}
result += "END:VCARD";
println!("{}", result);
return result;
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.