diff options
Diffstat (limited to 'components')
-rw-r--r-- | components/script/Cargo.toml | 5 | ||||
-rw-r--r-- | components/script/dom/bindings/trace.rs | 15 | ||||
-rw-r--r-- | components/script/dom/document.rs | 21 | ||||
-rw-r--r-- | components/script/dom/element.rs | 22 | ||||
-rw-r--r-- | components/script/dom/node.rs | 22 | ||||
-rw-r--r-- | components/script/dom/servohtmlparser.rs | 105 | ||||
-rw-r--r-- | components/script/dom/webidls/ServoHTMLParser.webidl | 9 | ||||
-rw-r--r-- | components/script/layout_interface.rs | 22 | ||||
-rw-r--r-- | components/script/lib.rs | 8 | ||||
-rw-r--r-- | components/script/parse/html.rs | 521 | ||||
-rw-r--r-- | components/script/script_task.rs | 16 |
11 files changed, 702 insertions, 64 deletions
diff --git a/components/script/Cargo.toml b/components/script/Cargo.toml index 152cb48fab2..04667508c99 100644 --- a/components/script/Cargo.toml +++ b/components/script/Cargo.toml @@ -42,8 +42,9 @@ git = "https://github.com/servo/rust-cssparser" [dependencies.geom] git = "https://github.com/servo/rust-geom" -[dependencies.hubbub] -git = "https://github.com/servo/rust-hubbub" +[dependencies.html5ever] +git = "https://github.com/servo/html5ever" +branch = "servo" [dependencies.encoding] git = "https://github.com/lifthrasiir/rust-encoding" diff --git a/components/script/dom/bindings/trace.rs b/components/script/dom/bindings/trace.rs index 18fb7c6e8d8..a7fffe4a11e 100644 --- a/components/script/dom/bindings/trace.rs +++ b/components/script/dom/bindings/trace.rs @@ -42,7 +42,6 @@ use std::collections::hashmap::HashMap; use collections::hash::Hash; use style::PropertyDeclarationBlock; use std::comm::{Receiver, Sender}; -use hubbub::hubbub::QuirksMode; use string_cache::{Atom, Namespace}; use js::rust::Cx; use http::headers::response::HeaderCollection as ResponseHeaderCollection; @@ -55,7 +54,9 @@ use servo_msg::constellation_msg::ConstellationChan; use servo_util::smallvec::{SmallVec1, SmallVec}; use servo_util::str::LengthOrPercentageOrAuto; use layout_interface::{LayoutRPC, LayoutChan}; +use dom::node::{Node, TrustedNodeAddress}; use dom::bindings::utils::WindowProxyHandler; +use html5ever::tree_builder::QuirksMode; impl<T: Reflectable> JSTraceable for JS<T> { fn trace(&self, trc: *mut JSTracer) { @@ -207,6 +208,7 @@ untraceable!(ConstellationChan) untraceable!(LayoutChan) untraceable!(WindowProxyHandler) untraceable!(UntrustedNodeAddress) +untraceable!(LengthOrPercentageOrAuto) impl<'a> JSTraceable for &'a str { #[inline] @@ -236,5 +238,12 @@ impl JSTraceable for Box<LayoutRPC+'static> { } } -untraceable!(LengthOrPercentageOrAuto) - +impl JSTraceable for TrustedNodeAddress { + fn trace(&self, s: *mut JSTracer) { + let TrustedNodeAddress(addr) = *self; + let node = addr as *const Node; + unsafe { + JS::from_raw(node).trace(s) + } + } +} diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 4843fe0207c..8d767be9016 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -52,12 +52,12 @@ use dom::range::Range; use dom::treewalker::TreeWalker; use dom::uievent::UIEvent; use dom::window::{Window, WindowHelpers}; -use html::hubbub_html_parser::build_element_from_tag; -use hubbub::hubbub::{QuirksMode, NoQuirks, LimitedQuirks, FullQuirks}; +use parse::html::build_element_from_tag; use servo_util::namespace; use servo_util::str::{DOMString, split_html_space_chars}; -use string_cache::Atom; +use html5ever::tree_builder::{QuirksMode, NoQuirks, LimitedQuirks, Quirks}; +use string_cache::{Atom, QualName}; use url::Url; use std::collections::hashmap::HashMap; @@ -426,7 +426,7 @@ impl<'a> DocumentMethods for JSRef<'a, Document> { fn CompatMode(self) -> DOMString { match self.quirks_mode.get() { LimitedQuirks | NoQuirks => "CSS1Compat".to_string(), - FullQuirks => "BackCompat".to_string() + Quirks => "BackCompat".to_string() } } @@ -492,7 +492,8 @@ impl<'a> DocumentMethods for JSRef<'a, Document> { return Err(InvalidCharacter); } let local_name = local_name.as_slice().to_ascii_lower(); - Ok(build_element_from_tag(local_name, ns!(HTML), None, self)) + let name = QualName::new(ns!(HTML), Atom::from_slice(local_name.as_slice())); + Ok(build_element_from_tag(name, None, self)) } // http://dom.spec.whatwg.org/#dom-document-createelementns @@ -512,9 +513,9 @@ impl<'a> DocumentMethods for JSRef<'a, Document> { QName => {} } - let (prefix_from_qname, - local_name_from_qname) = get_attribute_parts(qualified_name.as_slice()); - match (&ns, prefix_from_qname.clone(), local_name_from_qname.as_slice()) { + let (prefix_from_qname, local_name_from_qname) + = get_attribute_parts(qualified_name.as_slice()); + match (&ns, prefix_from_qname, local_name_from_qname) { // throw if prefix is not null and namespace is null (&ns!(""), Some(_), _) => { debug!("Namespace can't be null with a non-null prefix"); @@ -536,8 +537,8 @@ impl<'a> DocumentMethods for JSRef<'a, Document> { } if ns == ns!(HTML) { - Ok(build_element_from_tag(local_name_from_qname.to_string(), ns, - prefix_from_qname.map(|s| s.to_string()), self)) + let name = QualName::new(ns!(HTML), Atom::from_slice(local_name_from_qname)); + Ok(build_element_from_tag(name, prefix_from_qname.map(|s| s.to_string()), self)) } else { Ok(Element::new(local_name_from_qname.to_string(), ns, prefix_from_qname.map(|s| s.to_string()), self)) diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index 793ca673dac..0ad67d7ee67 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -42,7 +42,7 @@ use servo_util::str::{DOMString, LengthOrPercentageOrAuto}; use std::ascii::StrAsciiExt; use std::default::Default; use std::mem; -use string_cache::{Atom, Namespace}; +use string_cache::{Atom, Namespace, QualName}; use url::UrlParser; #[dom_struct] @@ -397,9 +397,8 @@ pub trait AttributeHandlers { fn get_attributes(self, local_name: &Atom) -> Vec<Temporary<Attr>>; fn set_attribute_from_parser(self, - local_name: Atom, + name: QualName, value: DOMString, - namespace: Namespace, prefix: Option<DOMString>); fn set_attribute(self, name: &Atom, value: AttrValue); fn do_set_attribute(self, local_name: Atom, value: AttrValue, @@ -445,19 +444,24 @@ impl<'a> AttributeHandlers for JSRef<'a, Element> { } fn set_attribute_from_parser(self, - local_name: Atom, + qname: QualName, value: DOMString, - namespace: Namespace, prefix: Option<DOMString>) { + // Don't set if the attribute already exists, so we can handle add_attrs_if_missing + if self.attrs.borrow().iter().map(|attr| attr.root()) + .any(|a| *a.local_name() == qname.local && *a.namespace() == qname.ns) { + return; + } + let name = match prefix { - None => local_name.clone(), + None => qname.local.clone(), Some(ref prefix) => { - let name = format!("{:s}:{:s}", *prefix, local_name.as_slice()); + let name = format!("{:s}:{:s}", *prefix, qname.local.as_slice()); Atom::from_slice(name.as_slice()) }, }; - let value = self.parse_attribute(&namespace, &local_name, value); - self.do_set_attribute(local_name, value, name, namespace, prefix, |_| false) + let value = self.parse_attribute(&qname.ns, &qname.local, value); + self.do_set_attribute(qname.local, value, name, qname.ns, prefix, |_| false) } fn set_attribute(self, name: &Atom, value: AttrValue) { diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index 94753b96d48..724f9423bb3 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -44,9 +44,9 @@ use dom::text::Text; use dom::virtualmethods::{VirtualMethods, vtable_for}; use dom::window::Window; use geom::rect::Rect; -use html::hubbub_html_parser::build_element_from_tag; +use parse::html::build_element_from_tag; use layout_interface::{ContentBoxResponse, ContentBoxesResponse, LayoutRPC, - LayoutChan, ReapLayoutDataMsg, TrustedNodeAddress}; + LayoutChan, ReapLayoutDataMsg}; use devtools_traits::NodeInfo; use script_traits::UntrustedNodeAddress; use servo_util::geometry::Au; @@ -56,7 +56,7 @@ use style::{parse_selector_list_from_str, matches}; use js::jsapi::{JSContext, JSObject, JSTracer, JSRuntime}; use js::jsfriendapi; use libc; -use libc::uintptr_t; +use libc::{uintptr_t, c_void}; use std::cell::{Cell, RefCell, Ref, RefMut}; use std::default::Default; use std::iter::{Map, Filter}; @@ -65,6 +65,7 @@ use style; use style::ComputedValues; use sync::Arc; use uuid; +use string_cache::QualName; // // The basic Node structure @@ -1530,8 +1531,12 @@ impl Node { }, ElementNodeTypeId(..) => { let element: JSRef<Element> = ElementCast::to_ref(node).unwrap(); - let element = build_element_from_tag(element.local_name().as_slice().to_string(), - element.namespace().clone(), Some(element.prefix().as_slice().to_string()), *document); + let name = QualName { + ns: element.namespace().clone(), + local: element.local_name().clone() + }; + let element = build_element_from_tag(name, + Some(element.prefix().as_slice().to_string()), *document); NodeCast::from_temporary(element) }, TextNodeTypeId => { @@ -2159,6 +2164,13 @@ impl Reflectable for Node { } } +/// The address of a node known to be valid. These are sent from script to layout, +/// and are also used in the HTML parser interface. + +#[allow(raw_pointer_deriving)] +#[deriving(Clone, PartialEq, Eq)] +pub struct TrustedNodeAddress(pub *const c_void); + pub fn document_from_node<T: NodeBase+Reflectable>(derived: JSRef<T>) -> Temporary<Document> { let node: JSRef<Node> = NodeCast::from_ref(derived); node.owner_doc() diff --git a/components/script/dom/servohtmlparser.rs b/components/script/dom/servohtmlparser.rs new file mode 100644 index 00000000000..c4d594186c8 --- /dev/null +++ b/components/script/dom/servohtmlparser.rs @@ -0,0 +1,105 @@ +/* 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/. */ + +//! The bulk of the HTML parser integration is in `script::parse::html`. +//! This module is mostly about its interaction with DOM memory management. + +use dom::bindings::codegen::Bindings::ServoHTMLParserBinding; +use dom::bindings::global; +use dom::bindings::trace::JSTraceable; +use dom::bindings::js::{JS, JSRef, Temporary}; +use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object}; +use dom::node::TrustedNodeAddress; +use dom::document::Document; +use parse::html::JSMessage; + +use std::default::Default; +use std::cell::RefCell; +use url::Url; +use js::jsapi::JSTracer; +use html5ever::tokenizer; +use html5ever::tree_builder; +use html5ever::tree_builder::{TreeBuilder, TreeBuilderOpts}; + +#[must_root] +#[jstraceable] +pub struct Sink { + pub js_chan: Sender<JSMessage>, + pub base_url: Option<Url>, + pub document: JS<Document>, +} + +pub type Tokenizer = tokenizer::Tokenizer<TreeBuilder<TrustedNodeAddress, Sink>>; + +// NB: JSTraceable is *not* auto-derived. +// You must edit the impl below if you add fields! +#[must_root] +#[privatize] +pub struct ServoHTMLParser { + reflector_: Reflector, + tokenizer: RefCell<Tokenizer>, +} + +impl ServoHTMLParser { + #[allow(unrooted_must_root)] + pub fn new(js_chan: Sender<JSMessage>, base_url: Option<Url>, document: JSRef<Document>) + -> Temporary<ServoHTMLParser> { + let window = document.window().root(); + let sink = Sink { + js_chan: js_chan, + base_url: base_url, + document: JS::from_rooted(document), + }; + + let tb = TreeBuilder::new(sink, TreeBuilderOpts { + ignore_missing_rules: true, + .. Default::default() + }); + + let tok = tokenizer::Tokenizer::new(tb, Default::default()); + + let parser = ServoHTMLParser { + reflector_: Reflector::new(), + tokenizer: RefCell::new(tok), + }; + + reflect_dom_object(box parser, &global::Window(*window), ServoHTMLParserBinding::Wrap) + } + + #[inline] + pub fn tokenizer<'a>(&'a self) -> &'a RefCell<Tokenizer> { + &self.tokenizer + } +} + +impl Reflectable for ServoHTMLParser { + fn reflector<'a>(&'a self) -> &'a Reflector { + &self.reflector_ + } +} + +struct Tracer { + trc: *mut JSTracer, +} + +impl tree_builder::Tracer<TrustedNodeAddress> for Tracer { + fn trace_handle(&self, node: TrustedNodeAddress) { + node.trace(self.trc); + } +} + +impl JSTraceable for ServoHTMLParser { + fn trace(&self, trc: *mut JSTracer) { + let tracer = Tracer { + trc: trc, + }; + let tracer = &tracer as &tree_builder::Tracer<TrustedNodeAddress>; + + self.reflector_.trace(trc); + let tokenizer = self.tokenizer.borrow(); + let tree_builder = tokenizer.sink(); + tree_builder.trace_handles(tracer); + tree_builder.sink().trace(trc); + } +} diff --git a/components/script/dom/webidls/ServoHTMLParser.webidl b/components/script/dom/webidls/ServoHTMLParser.webidl new file mode 100644 index 00000000000..02ad2667a96 --- /dev/null +++ b/components/script/dom/webidls/ServoHTMLParser.webidl @@ -0,0 +1,9 @@ +/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +// FIXME: find a better way to hide this from content (#3688) +[NoInterfaceObject] +interface ServoHTMLParser { +}; diff --git a/components/script/layout_interface.rs b/components/script/layout_interface.rs index a3c8de5a66a..735893f497a 100644 --- a/components/script/layout_interface.rs +++ b/components/script/layout_interface.rs @@ -6,14 +6,10 @@ /// coupling between these two components, and enables the DOM to be placed in a separate crate /// from layout. -use dom::bindings::js::JS; -use dom::bindings::trace::JSTraceable; -use dom::node::{Node, LayoutDataRef}; +use dom::node::LayoutDataRef; use geom::point::Point2D; use geom::rect::Rect; -use js::jsapi::JSTracer; -use libc::c_void; use script_traits::{ScriptControlChan, OpaqueScriptLayoutChannel, UntrustedNodeAddress}; use servo_msg::constellation_msg::WindowSizeData; use servo_util::geometry::Au; @@ -23,6 +19,8 @@ use std::owned::BoxAny; use style::Stylesheet; use url::Url; +pub use dom::node::TrustedNodeAddress; + /// Asynchronous messages that script can send to layout. pub enum Msg { /// Adds the given stylesheet to the document. @@ -70,20 +68,6 @@ pub trait LayoutRPC { fn mouse_over(&self, node: TrustedNodeAddress, point: Point2D<f32>) -> Result<MouseOverResponse, ()>; } -/// The address of a node known to be valid. These must only be sent from content -> layout, -/// because we do not trust layout. -pub struct TrustedNodeAddress(pub *const c_void); - -impl JSTraceable for TrustedNodeAddress { - fn trace(&self, s: *mut JSTracer) { - let TrustedNodeAddress(addr) = *self; - let node = addr as *const Node; - unsafe { - JS::from_raw(node).trace(s) - } - } -} - pub struct ContentBoxResponse(pub Rect<Au>); pub struct ContentBoxesResponse(pub Vec<Rect<Au>>); pub struct HitTestResponse(pub UntrustedNodeAddress); diff --git a/components/script/lib.rs b/components/script/lib.rs index 8053937f275..e03801f5217 100644 --- a/components/script/lib.rs +++ b/components/script/lib.rs @@ -20,7 +20,7 @@ extern crate devtools_traits; extern crate cssparser; extern crate collections; extern crate geom; -extern crate hubbub; +extern crate html5ever; extern crate encoding; extern crate http; extern crate js; @@ -191,6 +191,7 @@ pub mod dom { pub mod progressevent; pub mod range; pub mod screen; + pub mod servohtmlparser; pub mod text; pub mod treewalker; pub mod uievent; @@ -210,9 +211,8 @@ pub mod dom { pub mod testbinding; } -/// Parsers for HTML and CSS. -pub mod html { - pub mod hubbub_html_parser; +pub mod parse { + pub mod html; } pub mod layout_interface; diff --git a/components/script/parse/html.rs b/components/script/parse/html.rs new file mode 100644 index 00000000000..829d5d0c40b --- /dev/null +++ b/components/script/parse/html.rs @@ -0,0 +1,521 @@ +/* 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/. */ + +use dom::attr::AttrHelpers; +use dom::bindings::codegen::Bindings::AttrBinding::AttrMethods; +use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods; +use dom::bindings::codegen::InheritTypes::{NodeCast, ElementCast, HTMLScriptElementCast}; +use dom::bindings::js::{JS, JSRef, Temporary, OptionalRootable, Root}; +use dom::document::{Document, DocumentHelpers}; +use dom::element::{AttributeHandlers, ElementHelpers}; +use dom::htmlelement::HTMLElement; +use dom::htmlheadingelement::{Heading1, Heading2, Heading3, Heading4, Heading5, Heading6}; +use dom::htmlformelement::HTMLFormElement; +use dom::htmlscriptelement::HTMLScriptElementHelpers; +use dom::node::{Node, NodeHelpers, TrustedNodeAddress}; +use dom::servohtmlparser; +use dom::servohtmlparser::ServoHTMLParser; +use dom::types::*; +use page::Page; + +use encoding::all::UTF_8; +use encoding::types::{Encoding, DecodeReplace}; + +use servo_net::resource_task::{Load, LoadData, Payload, Done, ResourceTask, load_whole_resource}; +use servo_msg::constellation_msg::LoadData as MsgLoadData; +use servo_util::task::spawn_named; +use servo_util::str::DOMString; +use std::ascii::StrAsciiExt; +use std::comm::{channel, Sender, Receiver}; +use std::str::MaybeOwned; +use url::{Url, UrlParser}; +use http::headers::HeaderEnum; +use time; +use html5ever::Attribute; +use html5ever::tree_builder::{TreeSink, QuirksMode, NodeOrText, AppendNode, AppendText}; +use string_cache::QualName; + +pub struct JSFile { + pub data: String, + pub url: Option<Url>, +} + +pub type JSResult = Vec<JSFile>; + +pub enum HTMLInput { + InputString(String), + InputUrl(Url), +} + +pub enum JSMessage { + JSTaskNewFile(Url), + JSTaskNewInlineScript(String, Option<Url>), + JSTaskExit +} + +/// Messages generated by the HTML parser upon discovery of additional resources +pub enum HtmlDiscoveryMessage { + HtmlDiscoveredScript(JSResult) +} + +pub struct HtmlParserResult { + pub discovery_port: Receiver<HtmlDiscoveryMessage>, +} + +fn js_script_listener(to_parent: Sender<HtmlDiscoveryMessage>, + from_parent: Receiver<JSMessage>, + resource_task: ResourceTask) { + let mut result_vec = vec!(); + + loop { + match from_parent.recv_opt() { + Ok(JSTaskNewFile(url)) => { + match load_whole_resource(&resource_task, url.clone()) { + Err(_) => { + error!("error loading script {:s}", url.serialize()); + } + Ok((metadata, bytes)) => { + let decoded = UTF_8.decode(bytes.as_slice(), DecodeReplace).unwrap(); + result_vec.push(JSFile { + data: decoded.to_string(), + url: Some(metadata.final_url), + }); + } + } + } + Ok(JSTaskNewInlineScript(data, url)) => { + result_vec.push(JSFile { data: data, url: url }); + } + Ok(JSTaskExit) | Err(()) => { + break; + } + } + } + + assert!(to_parent.send_opt(HtmlDiscoveredScript(result_vec)).is_ok()); +} + +// Parses an RFC 2616 compliant date/time string, and returns a localized +// date/time string in a format suitable for document.lastModified. +fn parse_last_modified(timestamp: &str) -> String { + let format = "%m/%d/%Y %H:%M:%S"; + + // RFC 822, updated by RFC 1123 + match time::strptime(timestamp, "%a, %d %b %Y %T %Z") { + Ok(t) => return t.to_local().strftime(format), + Err(_) => () + } + + // RFC 850, obsoleted by RFC 1036 + match time::strptime(timestamp, "%A, %d-%b-%y %T %Z") { + Ok(t) => return t.to_local().strftime(format), + Err(_) => () + } + + // ANSI C's asctime() format + match time::strptime(timestamp, "%c") { + Ok(t) => t.to_local().strftime(format), + Err(_) => String::from_str("") + } +} + +pub fn build_element_from_tag(name: QualName, + prefix: Option<DOMString>, + document: JSRef<Document>) -> Temporary<Element> { + if name.ns != ns!(HTML) { + return Element::new(name.local.as_slice().to_string(), name.ns, None, document); + } + + macro_rules! make( + ($ctor:ident $(, $arg:expr)*) => ({ + let obj = $ctor::new(name.local.as_slice().to_string(), prefix, document $(, $arg)*); + ElementCast::from_temporary(obj) + }) + ) + + // This is a big match, and the IDs for inline-interned atoms are not very structured. + // Perhaps we should build a perfect hash from those IDs instead. + match name.local { + atom!("a") => make!(HTMLAnchorElement), + atom!("abbr") => make!(HTMLElement), + atom!("acronym") => make!(HTMLElement), + atom!("address") => make!(HTMLElement), + atom!("applet") => make!(HTMLAppletElement), + atom!("area") => make!(HTMLAreaElement), + atom!("article") => make!(HTMLElement), + atom!("aside") => make!(HTMLElement), + atom!("audio") => make!(HTMLAudioElement), + atom!("b") => make!(HTMLElement), + atom!("base") => make!(HTMLBaseElement), + atom!("bdi") => make!(HTMLElement), + atom!("bdo") => make!(HTMLElement), + atom!("bgsound") => make!(HTMLElement), + atom!("big") => make!(HTMLElement), + atom!("blockquote") => make!(HTMLElement), + atom!("body") => make!(HTMLBodyElement), + atom!("br") => make!(HTMLBRElement), + atom!("button") => make!(HTMLButtonElement), + atom!("canvas") => make!(HTMLCanvasElement), + atom!("caption") => make!(HTMLTableCaptionElement), + atom!("center") => make!(HTMLElement), + atom!("cite") => make!(HTMLElement), + atom!("code") => make!(HTMLElement), + atom!("col") => make!(HTMLTableColElement), + atom!("colgroup") => make!(HTMLTableColElement), + atom!("data") => make!(HTMLDataElement), + atom!("datalist") => make!(HTMLDataListElement), + atom!("dd") => make!(HTMLElement), + atom!("del") => make!(HTMLModElement), + atom!("details") => make!(HTMLElement), + atom!("dfn") => make!(HTMLElement), + atom!("dir") => make!(HTMLDirectoryElement), + atom!("div") => make!(HTMLDivElement), + atom!("dl") => make!(HTMLDListElement), + atom!("dt") => make!(HTMLElement), + atom!("em") => make!(HTMLElement), + atom!("embed") => make!(HTMLEmbedElement), + atom!("fieldset") => make!(HTMLFieldSetElement), + atom!("figcaption") => make!(HTMLElement), + atom!("figure") => make!(HTMLElement), + atom!("font") => make!(HTMLFontElement), + atom!("footer") => make!(HTMLElement), + atom!("form") => make!(HTMLFormElement), + atom!("frame") => make!(HTMLFrameElement), + atom!("frameset") => make!(HTMLFrameSetElement), + atom!("h1") => make!(HTMLHeadingElement, Heading1), + atom!("h2") => make!(HTMLHeadingElement, Heading2), + atom!("h3") => make!(HTMLHeadingElement, Heading3), + atom!("h4") => make!(HTMLHeadingElement, Heading4), + atom!("h5") => make!(HTMLHeadingElement, Heading5), + atom!("h6") => make!(HTMLHeadingElement, Heading6), + atom!("head") => make!(HTMLHeadElement), + atom!("header") => make!(HTMLElement), + atom!("hgroup") => make!(HTMLElement), + atom!("hr") => make!(HTMLHRElement), + atom!("html") => make!(HTMLHtmlElement), + atom!("i") => make!(HTMLElement), + atom!("iframe") => make!(HTMLIFrameElement), + atom!("img") => make!(HTMLImageElement), + atom!("input") => make!(HTMLInputElement), + atom!("ins") => make!(HTMLModElement), + atom!("isindex") => make!(HTMLElement), + atom!("kbd") => make!(HTMLElement), + atom!("label") => make!(HTMLLabelElement), + atom!("legend") => make!(HTMLLegendElement), + atom!("li") => make!(HTMLLIElement), + atom!("link") => make!(HTMLLinkElement), + atom!("main") => make!(HTMLElement), + atom!("map") => make!(HTMLMapElement), + atom!("mark") => make!(HTMLElement), + atom!("marquee") => make!(HTMLElement), + atom!("meta") => make!(HTMLMetaElement), + atom!("meter") => make!(HTMLMeterElement), + atom!("nav") => make!(HTMLElement), + atom!("nobr") => make!(HTMLElement), + atom!("noframes") => make!(HTMLElement), + atom!("noscript") => make!(HTMLElement), + atom!("object") => make!(HTMLObjectElement), + atom!("ol") => make!(HTMLOListElement), + atom!("optgroup") => make!(HTMLOptGroupElement), + atom!("option") => make!(HTMLOptionElement), + atom!("output") => make!(HTMLOutputElement), + atom!("p") => make!(HTMLParagraphElement), + atom!("param") => make!(HTMLParamElement), + atom!("pre") => make!(HTMLPreElement), + atom!("progress") => make!(HTMLProgressElement), + atom!("q") => make!(HTMLQuoteElement), + atom!("rp") => make!(HTMLElement), + atom!("rt") => make!(HTMLElement), + atom!("ruby") => make!(HTMLElement), + atom!("s") => make!(HTMLElement), + atom!("samp") => make!(HTMLElement), + atom!("script") => make!(HTMLScriptElement), + atom!("section") => make!(HTMLElement), + atom!("select") => make!(HTMLSelectElement), + atom!("small") => make!(HTMLElement), + atom!("source") => make!(HTMLSourceElement), + atom!("spacer") => make!(HTMLElement), + atom!("span") => make!(HTMLSpanElement), + atom!("strike") => make!(HTMLElement), + atom!("strong") => make!(HTMLElement), + atom!("style") => make!(HTMLStyleElement), + atom!("sub") => make!(HTMLElement), + atom!("summary") => make!(HTMLElement), + atom!("sup") => make!(HTMLElement), + atom!("table") => make!(HTMLTableElement), + atom!("tbody") => make!(HTMLTableSectionElement), + atom!("td") => make!(HTMLTableDataCellElement), + atom!("template") => make!(HTMLTemplateElement), + atom!("textarea") => make!(HTMLTextAreaElement), + atom!("th") => make!(HTMLTableHeaderCellElement), + atom!("time") => make!(HTMLTimeElement), + atom!("title") => make!(HTMLTitleElement), + atom!("tr") => make!(HTMLTableRowElement), + atom!("tt") => make!(HTMLElement), + atom!("track") => make!(HTMLTrackElement), + atom!("u") => make!(HTMLElement), + atom!("ul") => make!(HTMLUListElement), + atom!("var") => make!(HTMLElement), + atom!("video") => make!(HTMLVideoElement), + atom!("wbr") => make!(HTMLElement), + _ => make!(HTMLUnknownElement), + } +} + +trait SinkHelpers { + fn get_or_create(&self, child: NodeOrText<TrustedNodeAddress>) -> Temporary<Node>; +} + +impl SinkHelpers for servohtmlparser::Sink { + fn get_or_create(&self, child: NodeOrText<TrustedNodeAddress>) -> Temporary<Node> { + match child { + AppendNode(n) => Temporary::new(unsafe { JS::from_trusted_node_address(n) }), + AppendText(t) => { + let doc = self.document.root(); + let text = Text::new(t, *doc); + NodeCast::from_temporary(text) + } + } + } +} + +impl<'a> TreeSink<TrustedNodeAddress> for servohtmlparser::Sink { + fn get_document(&mut self) -> TrustedNodeAddress { + let doc = self.document.root(); + let node: JSRef<Node> = NodeCast::from_ref(*doc); + node.to_trusted_node_address() + } + + fn same_node(&self, x: TrustedNodeAddress, y: TrustedNodeAddress) -> bool { + x == y + } + + fn elem_name(&self, target: TrustedNodeAddress) -> QualName { + let node: Root<Node> = unsafe { JS::from_trusted_node_address(target).root() }; + let elem: JSRef<Element> = ElementCast::to_ref(*node) + .expect("tried to get name of non-Element in HTML parsing"); + QualName { + ns: elem.get_namespace().clone(), + local: elem.get_local_name().clone(), + } + } + + fn create_element(&mut self, name: QualName, attrs: Vec<Attribute>) + -> TrustedNodeAddress { + let doc = self.document.root(); + let elem = build_element_from_tag(name, None, *doc).root(); + + for attr in attrs.into_iter() { + elem.set_attribute_from_parser(attr.name, attr.value, None); + } + + let node: JSRef<Node> = NodeCast::from_ref(*elem); + node.to_trusted_node_address() + } + + fn create_comment(&mut self, text: String) -> TrustedNodeAddress { + let doc = self.document.root(); + let comment = Comment::new(text, *doc); + let node: Root<Node> = NodeCast::from_temporary(comment).root(); + node.to_trusted_node_address() + } + + fn append_before_sibling(&mut self, + sibling: TrustedNodeAddress, + new_node: NodeOrText<TrustedNodeAddress>) -> Result<(), NodeOrText<TrustedNodeAddress>> { + // If there is no parent, return the node to the parser. + let sibling: Root<Node> = unsafe { JS::from_trusted_node_address(sibling).root() }; + let parent = match sibling.parent_node() { + Some(p) => p.root(), + None => return Err(new_node), + }; + + let child = self.get_or_create(new_node).root(); + assert!(parent.InsertBefore(*child, Some(*sibling)).is_ok()); + Ok(()) + } + + fn parse_error(&mut self, msg: MaybeOwned<'static>) { + error!("Parse error: {:s}", msg); + } + + fn set_quirks_mode(&mut self, mode: QuirksMode) { + let doc = self.document.root(); + doc.set_quirks_mode(mode); + } + + fn append(&mut self, parent: TrustedNodeAddress, child: NodeOrText<TrustedNodeAddress>) { + let parent: Root<Node> = unsafe { JS::from_trusted_node_address(parent).root() }; + let child = self.get_or_create(child).root(); + + // FIXME(#3701): Use a simpler algorithm and merge adjacent text nodes + assert!(parent.AppendChild(*child).is_ok()); + } + + fn append_doctype_to_document(&mut self, name: String, public_id: String, system_id: String) { + let doc = self.document.root(); + let doc_node: JSRef<Node> = NodeCast::from_ref(*doc); + let doctype = DocumentType::new(name, Some(public_id), Some(system_id), *doc); + let node: Root<Node> = NodeCast::from_temporary(doctype).root(); + + assert!(doc_node.AppendChild(*node).is_ok()); + } + + fn add_attrs_if_missing(&mut self, target: TrustedNodeAddress, attrs: Vec<Attribute>) { + let node: Root<Node> = unsafe { JS::from_trusted_node_address(target).root() }; + let elem: JSRef<Element> = ElementCast::to_ref(*node) + .expect("tried to set attrs on non-Element in HTML parsing"); + for attr in attrs.into_iter() { + elem.set_attribute_from_parser(attr.name, attr.value, None); + } + } + + fn remove_from_parent(&mut self, _target: TrustedNodeAddress) { + error!("remove_from_parent not implemented!"); + } + + fn mark_script_already_started(&mut self, _node: TrustedNodeAddress) { + error!("mark_script_already_started not implemented!"); + } + + fn complete_script(&mut self, node: TrustedNodeAddress) { + let node: Root<Node> = unsafe { JS::from_trusted_node_address(node).root() }; + let script: Option<JSRef<HTMLScriptElement>> = + HTMLScriptElementCast::to_ref(*node); + let script = match script { + Some(script) if script.is_javascript() => script, + _ => return, + }; + + let script_element: JSRef<Element> = ElementCast::from_ref(script); + match script_element.get_attribute(ns!(""), &atom!("src")).root() { + Some(src) => { + debug!("found script: {:s}", src.deref().Value()); + let mut url_parser = UrlParser::new(); + match self.base_url { + None => (), + Some(ref base_url) => { + url_parser.base_url(base_url); + } + }; + match url_parser.parse(src.deref().value().as_slice()) { + Ok(new_url) => self.js_chan.send(JSTaskNewFile(new_url)), + Err(e) => debug!("Parsing url {:s} failed: {:?}", src.deref().Value(), e) + }; + } + None => { + let scriptnode: JSRef<Node> = NodeCast::from_ref(script); + let data = Node::collect_text_contents(scriptnode.children()); + debug!("script data = {:?}", data); + self.js_chan.send(JSTaskNewInlineScript(data, self.base_url.clone())); + } + } + } +} + +// The url from msg_load_data is ignored here +pub fn parse_html(page: &Page, + document: JSRef<Document>, + input: HTMLInput, + resource_task: ResourceTask, + msg_load_data: Option<MsgLoadData>) + -> HtmlParserResult { + // Spawn a JS parser to receive JavaScript. + let (discovery_chan, discovery_port) = channel(); + let resource_task2 = resource_task.clone(); + let js_result_chan = discovery_chan.clone(); + let (js_chan, js_msg_port) = channel(); + spawn_named("parse_html:js", proc() { + js_script_listener(js_result_chan, js_msg_port, resource_task2.clone()); + }); + + let (base_url, load_response) = match input { + InputUrl(ref url) => { + // Wait for the LoadResponse so that the parser knows the final URL. + let (input_chan, input_port) = channel(); + let mut load_data = LoadData::new(url.clone()); + msg_load_data.map(|m| { + load_data.headers = m.headers; + load_data.method = m.method; + load_data.data = m.data; + }); + resource_task.send(Load(load_data, input_chan)); + + let load_response = input_port.recv(); + + debug!("Fetched page; metadata is {:?}", load_response.metadata); + + load_response.metadata.headers.as_ref().map(|headers| { + let header = headers.iter().find(|h| + h.header_name().as_slice().to_ascii_lower() == "last-modified".to_string() + ); + + match header { + Some(h) => document.set_last_modified( + parse_last_modified(h.header_value().as_slice())), + None => {}, + }; + }); + + let base_url = load_response.metadata.final_url.clone(); + + { + // Store the final URL before we start parsing, so that DOM routines + // (e.g. HTMLImageElement::update_image) can resolve relative URLs + // correctly. + *page.mut_url() = Some((base_url.clone(), true)); + } + + (Some(base_url), Some(load_response)) + }, + InputString(_) => { + match *page.url() { + Some((ref page_url, _)) => (Some(page_url.clone()), None), + None => (None, None), + } + }, + }; + + let parser = ServoHTMLParser::new(js_chan.clone(), base_url.clone(), document).root(); + let parser: JSRef<ServoHTMLParser> = *parser; + + match input { + InputString(s) => { + parser.tokenizer().borrow_mut().feed(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); + }, + _ => { + for msg in load_response.progress_port.iter() { + match msg { + Payload(data) => { + // FIXME: use Vec<u8> (html5ever #34) + let data = String::from_utf8(data).unwrap(); + parser.tokenizer().borrow_mut().feed(data); + } + Done(Err(err)) => { + fail!("Failed to load page URL {:s}, error: {:s}", url.serialize(), err); + } + Done(Ok(())) => break, + } + } + } + } + } + } + + parser.tokenizer().borrow_mut().end(); + + debug!("finished parsing"); + js_chan.send(JSTaskExit); + + HtmlParserResult { + discovery_port: discovery_port, + } +} diff --git a/components/script/script_task.rs b/components/script/script_task.rs index 17259132eb6..214d666d636 100644 --- a/components/script/script_task.rs +++ b/components/script/script_task.rs @@ -27,8 +27,7 @@ use dom::node::{ElementNodeTypeId, Node, NodeHelpers}; use dom::window::{Window, WindowHelpers}; use dom::worker::{Worker, TrustedWorkerAddress}; use dom::xmlhttprequest::{TrustedXHRAddress, XMLHttpRequest, XHRProgress}; -use html::hubbub_html_parser::{InputString, InputUrl, HtmlParserResult, HtmlDiscoveredScript}; -use html::hubbub_html_parser; +use parse::html::{InputString, InputUrl, HtmlParserResult, HtmlDiscoveredScript, parse_html}; use layout_interface::{ScriptLayoutChan, LayoutChan, ReflowForDisplay}; use layout_interface; use page::{Page, IterablePage, Frame}; @@ -781,16 +780,9 @@ impl ScriptTask { // Parse HTML. // // Note: We can parse the next document in parallel with any previous documents. - let html_parsing_result = - hubbub_html_parser::parse_html(&*page, - *document, - parser_input, - self.resource_task.clone(), - Some(load_data)); - - let HtmlParserResult { - discovery_port - } = html_parsing_result; + let HtmlParserResult { discovery_port } + = parse_html(&*page, *document, parser_input, self.resource_task.clone(), + Some(load_data)); { // Create the root frame. |