Skip to content

Commit

Permalink
Adding initial version of parser trait
Browse files Browse the repository at this point in the history
Added parse_chunk method declaration to parser

Removed unnecessary visibilty for parse_chunk function

Implemented parse_chunk function

Implemented parse_chunk fn for ServoHTMLParser

Moved parser trait to mod.rs and added finish fn

added licence header to mod.rs and other review comments

Fixed trailing space issue

Fixed failed tabular space test
  • Loading branch information
nchinth committed Oct 29, 2014
1 parent 7ba02bb commit 6a736c7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
10 changes: 10 additions & 0 deletions components/script/dom/servohtmlparser.rs
Expand Up @@ -14,6 +14,7 @@ use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
use dom::node::TrustedNodeAddress;
use dom::document::{Document, DocumentHelpers};
use parse::html::JSMessage;
use parse::Parser;

use servo_util::task_state;

Expand Down Expand Up @@ -43,6 +44,15 @@ pub struct ServoHTMLParser {
tokenizer: DOMRefCell<Tokenizer>,
}

impl Parser for ServoHTMLParser{
fn parse_chunk(&self, input: String) {
self.tokenizer().borrow_mut().feed(input);
}
fn finish(&self){
self.tokenizer().borrow_mut().end();
}
}

impl ServoHTMLParser {
#[allow(unrooted_must_root)]
pub fn new(js_chan: Sender<JSMessage>, base_url: Option<Url>, document: JSRef<Document>)
Expand Down
4 changes: 1 addition & 3 deletions components/script/lib.rs
Expand Up @@ -211,9 +211,7 @@ pub mod dom {
pub mod testbinding;
}

pub mod parse {
pub mod html;
}
pub mod parse;

pub mod layout_interface;
pub mod page;
Expand Down
9 changes: 5 additions & 4 deletions components/script/parse/html.rs
Expand Up @@ -18,6 +18,7 @@ use dom::servohtmlparser;
use dom::servohtmlparser::ServoHTMLParser;
use dom::types::*;
use page::Page;
use parse::Parser;

use encoding::all::UTF_8;
use encoding::types::{Encoding, DecodeReplace};
Expand Down Expand Up @@ -486,22 +487,22 @@ pub fn parse_html(page: &Page,

match input {
InputString(s) => {
parser.tokenizer().borrow_mut().feed(s);
parser.parse_chunk(s);
}
InputUrl(url) => {
let load_response = load_response.unwrap();
match load_response.metadata.content_type {
Some((ref t, _)) if t.as_slice().eq_ignore_ascii_case("image") => {
let page = format!("<html><body><img src='{:s}' /></body></html>", base_url.as_ref().unwrap().serialize());
parser.tokenizer().borrow_mut().feed(page);
parser.parse_chunk(page);
},
_ => {
for msg in load_response.progress_port.iter() {
match msg {
Payload(data) => {
// FIXME: use Vec<u8> (html5ever #34)
let data = UTF_8.decode(data.as_slice(), DecodeReplace).unwrap();
parser.tokenizer().borrow_mut().feed(data);
parser.parse_chunk(data);
}
Done(Err(err)) => {
fail!("Failed to load page URL {:s}, error: {:s}", url.serialize(), err);
Expand All @@ -514,7 +515,7 @@ pub fn parse_html(page: &Page,
}
}

parser.tokenizer().borrow_mut().end();
parser.finish();

task_state::exit(InHTMLParser);

Expand Down
10 changes: 10 additions & 0 deletions components/script/parse/mod.rs
@@ -0,0 +1,10 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */

pub mod html;

pub trait Parser {
fn parse_chunk(&self,input: String);
fn finish(&self);
}

5 comments on commit 6a736c7

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from jdm
at juzer10@6a736c7

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging juzer10/servo/master = 6a736c7 into auto

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

juzer10/servo/master = 6a736c7 merged ok, testing candidate = 5858fcc

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 5858fcc

Please sign in to comment.