Add and hook up parsing for Doctype and Comment node types

This commit is contained in:
Brian J. Burg 2012-09-05 18:35:41 -07:00
parent 3f2d253a71
commit d260e06688
7 changed files with 58 additions and 20 deletions

View file

@ -62,10 +62,27 @@ enum NodeData = {
};
enum NodeKind {
Doctype(DoctypeData),
Comment(~str),
Element(ElementData),
Text(~str)
}
struct DoctypeData {
let name: ~str;
let public_id: Option<~str>;
let system_id: Option<~str>;
let force_quirks: bool;
new (name: ~str, public_id: Option<~str>,
system_id: Option<~str>, force_quirks: bool) {
self.name = name;
self.public_id = public_id;
self.system_id = system_id;
self.force_quirks = force_quirks;
}
}
struct ElementData {
let tag_name: ~str;
let kind: ~ElementKind;

View file

@ -9,7 +9,7 @@ import js::jsapi::bindgen::*;
import js::glue::bindgen::*;
import js::crust::{JS_PropertyStub, JS_StrictPropertyStub, JS_EnumerateStub, JS_ConvertStub};
import dom::base::{Node, NodeScope, Element, Text};
import dom::base::{Node, NodeScope, Element, Text, Doctype, Comment};
import utils::{rust_box, squirrel_away_unique, get_compartment, domstring_to_jsval, str};
import libc::c_uint;
import ptr::null;
@ -47,10 +47,15 @@ fn create(cx: *JSContext, node: Node, scope: NodeScope) -> jsobj unsafe {
~Element(*) => {
element::create(cx, node, scope)
}
~Text(*) => {
fail ~"no text node bindings yet";
}
~Comment(*) => {
fail ~"no comment node bindings yet";
}
~Doctype(*) => {
fail ~"no doctype node bindings yet";
}
}
}
}
@ -127,7 +132,9 @@ extern fn getNodeType(cx: *JSContext, _argc: c_uint, vp: *mut jsval) -> JSBool {
let nodeType = do (*bundle).payload.node.read |nd| {
match nd.kind {
~Element(*) => 1,
~Text(*) => 3
~Text(*) => 3,
~Comment(*) => 8,
~Doctype(*) => 10
}
};
*vp = RUST_INT_TO_JSVAL(nodeType);

View file

@ -171,7 +171,8 @@ impl Node : PrivBoxBuilder {
fail ~"The specified display style should be a default instead of none"
}
}
}
},
_ => fail ~"unstyleable node type encountered"
}
}
}

View file

@ -80,7 +80,7 @@ impl Node : PrivMatchingMethods {
return true;
}
Text(*) => { /*fall through, currently unsupported*/ }
_ => { /*fall through, currently unsupported*/ }
}
}
}

View file

@ -3,8 +3,8 @@
import std::arc::{ARC, get, clone};
import dom::style::{DisplayType, DisBlock, DisInline, DisNone, Stylesheet, Unit, Auto};
import dom::base::{Element, HTMLDivElement, HTMLHeadElement, HTMLImageElement, Node, NodeKind, UnknownElement, HTMLScriptElement};
import dom::base::{Text};
import dom::base::{HTMLDivElement, HTMLHeadElement, HTMLImageElement, UnknownElement, HTMLScriptElement};
import dom::base::{Comment, Doctype, Element, Node, NodeKind, Text};
import util::color::{Color, rgb};
import util::color::css_colors::{white, black};
import base::{LayoutData, NTree};
@ -29,7 +29,8 @@ impl NodeKind : DefaultStyleMethods {
fn default_color() -> Color {
match self {
Text(*) => white(),
Element(*) => white()
Element(*) => white(),
_ => fail ~"unstyleable node type encountered"
}
}
@ -44,7 +45,8 @@ impl NodeKind : DefaultStyleMethods {
HTMLScriptElement => DisNone,
UnknownElement => DisInline,
}
}
},
_ => fail ~"unstyleable node type encountered"
}
}

View file

@ -57,8 +57,8 @@ fn link_up_attribute(scope: NodeScope, node: Node, -key: ~str, -value: ~str) {
}
}
Text(*) => {
fail ~"attempt to link up an attribute to a text node"
_ => {
fail ~"attempt to link up an attribute to an unstyleable node"
}
}
})

View file

@ -1,5 +1,6 @@
use dom::base::{Attr, Element, ElementData, ElementKind, HTMLDivElement, HTMLHeadElement};
use dom::base::{HTMLImageElement, HTMLScriptElement, Node, NodeScope, Text, UnknownElement};
use dom::base::{Attr, Comment, Doctype, DoctypeData, Element, ElementData, ElementKind};
use dom::base::{HTMLDivElement, HTMLHeadElement, HTMLImageElement, HTMLScriptElement};
use dom::base::{Node, NodeScope, Text, UnknownElement};
use dom::style::Stylesheet;
use geom::size::Size2D;
use gfx::geometry::px_to_au;
@ -152,16 +153,26 @@ fn parse_html(scope: NodeScope, url: Url, resource_task: ResourceTask) -> HtmlPa
parser.set_document_node(reinterpret_cast(&root));
parser.enable_scripting(true);
parser.set_tree_handler(@hubbub::TreeHandler {
create_comment: |_data| {
create_comment: |data: &str| {
debug!("create comment");
0u // FIXME: causes segfaults
},
create_doctype: |_doctype| {
debug!("create doctype");
let new_node = scope.new_node(Element(ElementData(~"doctype", ~UnknownElement)));
let new_node = scope.new_node(Comment(from_slice(data)));
unsafe { reinterpret_cast(&new_node) }
},
create_element: |tag| {
create_doctype: |doctype: &hubbub::Doctype| {
debug!("create doctype");
let name = from_slice(doctype.name);
let public_id = match doctype.public_id {
None => None,
Some(id) => Some(from_slice(id))
};
let system_id = match doctype.system_id {
None => None,
Some(id) => Some(from_slice(id))
};
let new_node = scope.new_node(Doctype(DoctypeData(name, public_id, system_id, doctype.force_quirks)));
unsafe { reinterpret_cast(&new_node) }
},
create_element: |tag: &hubbub::Tag| {
debug!("create element");
let element_kind = build_element_kind(tag.name);
let node = scope.new_node(Element(ElementData(from_slice(tag.name), element_kind)));