node -> Node

This commit is contained in:
Patrick Walton 2012-06-14 17:11:06 -07:00
parent 3f1b23b524
commit 876e1307e3
8 changed files with 54 additions and 51 deletions

View file

@ -6,7 +6,7 @@ import util::tree;
import dvec::{dvec, extensions};
enum node_data = {
tree: tree::fields<node>,
tree: tree::fields<Node>,
kind: ~node_kind,
};
@ -56,43 +56,44 @@ enum ElementKind {
HTMLImageElement({mut size: Size2D<au>})
}
#[doc="The rd_aux data is a (weak) pointer to the layout data, which
contains the CSS info as well as the primary box. Note that
there may be multiple boxes per DOM node."]
#[doc="
The rd_aux data is a (weak) pointer to the layout data, which contains the CSS info as well as
the primary box. Note that there may be multiple boxes per DOM node.
"]
type node = rcu::handle<node_data, layout_data>;
type Node = rcu::handle<node_data, layout_data>;
type node_scope = rcu::scope<node_data, layout_data>;
fn node_scope() -> node_scope { rcu::scope() }
impl methods for node_scope {
fn new_node(-k: node_kind) -> node {
fn new_node(-k: node_kind) -> Node {
self.handle(node_data({tree: tree::empty(),
kind: ~k}))
}
}
impl of tree::rd_tree_ops<node> for node_scope {
fn each_child(node: node, f: fn(node) -> bool) {
impl of tree::rd_tree_ops<Node> for node_scope {
fn each_child(node: Node, f: fn(Node) -> bool) {
tree::each_child(self, node, f)
}
fn get_parent(node: node) -> option<node> {
fn get_parent(node: Node) -> option<Node> {
tree::get_parent(self, node)
}
fn with_tree_fields<R>(node: node, f: fn(tree::fields<node>) -> R) -> R {
fn with_tree_fields<R>(node: Node, f: fn(tree::fields<Node>) -> R) -> R {
self.rd(node) { |n| f(n.tree) }
}
}
impl of tree::wr_tree_ops<node> for node_scope {
fn add_child(node: node, child: node) {
impl of tree::wr_tree_ops<Node> for node_scope {
fn add_child(node: Node, child: Node) {
tree::add_child(self, node, child)
}
fn with_tree_fields<R>(node: node, f: fn(tree::fields<node>) -> R) -> R {
fn with_tree_fields<R>(node: Node, f: fn(tree::fields<Node>) -> R) -> R {
self.wr(node) { |n| f(n.tree) }
}
}

View file

@ -1,7 +1,7 @@
#[doc="Fundamental layout structures and algorithms."]
import dom::base::{Element, ElementKind, HTMLDivElement, HTMLImageElement, node_data, node_kind};
import dom::base::{node};
import dom::base::{Node};
import dom::rcu;
import dom::rcu::reader_methods;
import gfx::geometry;
@ -36,7 +36,7 @@ class appearance {
enum box = {
tree: tree::fields<@box>,
node: node,
node: Node,
mut bounds: Rect<au>,
kind: box_kind,
appearance: appearance
@ -48,12 +48,12 @@ enum layout_data = {
};
enum ntree { ntree }
impl of tree::rd_tree_ops<node> for ntree {
fn each_child(node: node, f: fn(node) -> bool) {
impl of tree::rd_tree_ops<Node> for ntree {
fn each_child(node: Node, f: fn(Node) -> bool) {
tree::each_child(self, node, f)
}
fn with_tree_fields<R>(&&n: node, f: fn(tree::fields<node>) -> R) -> R {
fn with_tree_fields<R>(&&n: Node, f: fn(tree::fields<Node>) -> R) -> R {
n.rd { |n| f(n.tree) }
}
}
@ -121,7 +121,7 @@ impl layout_methods for @box {
// Debugging
impl node_methods_priv for node {
impl node_methods_priv for Node {
#[doc="Dumps the node tree, for debugging, with indentation."]
fn dump_indent(indent: uint) {
let mut s = "";
@ -137,7 +137,7 @@ impl node_methods_priv for node {
}
}
impl node_methods for node {
impl node_methods for Node {
#[doc="Dumps the subtree rooted at this node, for debugging."]
fn dump() {
self.dump_indent(0u);
@ -147,7 +147,7 @@ impl node_methods for node {
#[cfg(test)]
mod test {
import dom::base::{ElementData, HTMLDivElement, HTMLImageElement, methods, Element, node_data};
import dom::base::{node, node_kind, wr_tree_ops};
import dom::base::{Node, node_kind, wr_tree_ops};
import dom::rcu::scope;
import box_builder::{box_builder_methods};

View file

@ -1,6 +1,6 @@
#[doc="Creates CSS boxes from a DOM."]
import dom::base::{ElementData, HTMLDivElement, HTMLImageElement, Element, Text, node};
import dom::base::{ElementData, HTMLDivElement, HTMLImageElement, Element, Text, Node};
import dom::style::{display_type, di_block, di_inline, di_none};
import dom::rcu::reader_methods;
import gfx::geometry;
@ -16,17 +16,20 @@ export box_builder_methods;
enum ctxt = {
// The parent node that we're scanning.
parent_node: node,
parent_node: Node,
// The parent box that these boxes will be added to.
parent_box: @box,
//
// The current anonymous box that we're currently appending inline nodes to.
//
// See CSS2 9.2.1.1.
//
mut anon_box: option<@box>
};
fn new_box(n: node, kind: box_kind) -> @box {
fn new_box(n: Node, kind: box_kind) -> @box {
@box({tree: tree::empty(),
node: n,
mut bounds: geometry::zero_rect_au(),
@ -34,7 +37,7 @@ fn new_box(n: node, kind: box_kind) -> @box {
appearance: appearance() })
}
fn create_context(parent_node: node, parent_box: @box) -> ctxt {
fn create_context(parent_node: Node, parent_box: @box) -> ctxt {
ret ctxt({
parent_node: parent_node,
parent_box: parent_box,
@ -153,10 +156,10 @@ impl methods for ctxt {
}
}
impl box_builder_priv for node {
impl box_builder_priv for Node {
#[doc="
Determines the kind of box that this node needs. Also, for images,
computes the intrinsic size.
Determines the kind of box that this node needs. Also, for images, computes the intrinsic
size.
"]
fn determine_box_kind() -> box_kind {
alt self.rd({ |n| copy n.kind }) {
@ -172,7 +175,7 @@ impl box_builder_priv for node {
}
}
impl box_builder_methods for node {
impl box_builder_methods for Node {
#[doc="Creates boxes for this node. This is the entry point."]
fn construct_boxes() -> @box {
let box_kind = self.determine_box_kind();

View file

@ -11,7 +11,7 @@ import gfx::geometry::{au, au_to_px, box, px_to_au};
import geom::point::Point2D;
import geom::rect::Rect;
import gfx::renderer;
import dom::base::node;
import dom::base::Node;
import dom::rcu::scope;
import dom::style::stylesheet;
import layout::base::*;
@ -22,7 +22,7 @@ import dl = display_list;
import util::color::methods;
enum Msg {
BuildMsg(node, stylesheet),
BuildMsg(Node, stylesheet),
PingMsg(chan<content::PingMsg>),
ExitMsg
}

View file

@ -1,5 +1,5 @@
#[doc="Applies style to boxes."]
import dom::base::{HTMLImageElement, Element, node};
import dom::base::{HTMLImageElement, Element, Node};
import dom::rcu::reader_methods;
import image::base::load;
import layout::base::*;

View file

@ -1,6 +1,6 @@
#[doc="Perform css selector matching"]
import dom::base::{node, Element, ElementData, Text};
import dom::base::{Element, ElementData, Node, Text};
import dom::style::{selector, style_decl, font_size, display, text_color, background_color,
stylesheet, element, child, descendant, sibling, attr, exact, exists, includes,
starts_with};
@ -63,7 +63,7 @@ fn attrs_match(attr: attr, elmt: ElementData) -> bool {
}
}
impl priv_matching_methods for node {
impl priv_matching_methods for Node {
#[doc="
Checks if the given CSS selector, which must describe a single element with no relational
information, describes the given HTML element.
@ -132,8 +132,7 @@ impl priv_matching_methods for node {
sibling(sel1, sel2) {
if !self.matches_element(sel2) { ret false; }
// loop over this node's previous siblings to see if they
// match
// Loop over this node's previous siblings to see if they match.
alt self.rd { |n| n.tree.prev_sibling } {
some(sib) {
let mut cur_sib = sib;
@ -171,7 +170,7 @@ impl priv_matching_methods for node {
}
}
impl matching_methods for node {
impl matching_methods for Node {
#[doc="Compare an html element to a list of css rules and update its
style according to the rules matching it."]
fn match_css_style(styles : stylesheet) -> computed_style {
@ -179,17 +178,16 @@ impl matching_methods for node {
let style =
@default_style_for_node_kind(node_kind);
// Loop over each rule, see if our node matches what is
// described in the rule. If it matches, update its style.
// As we don't currently have priorities of style information,
// the latest rule takes precedence so we can just overwrite
// style information.
// Loop over each rule, see if our node matches what is described in the rule. If it
// matches, update its style. As we don't currently have priorities of style information,
// the latest rule takes precedence over the others. So we just overwrite style
// information as we go.
for styles.each { |sty|
let (selectors, decls) <- *(copy sty);
for selectors.each { |sel|
if self.matches_selector(sel) {
#debug("Matched selector {%?} with node {%?}",
*sel, node_kind);
#debug("Matched selector {%?} with node {%?}", *sel, node_kind);
for decls.each { |decl|
update_style(style, decl);
}
@ -209,7 +207,7 @@ mod test {
import dvec::{dvec, extensions};
import io::println;
fn new_node_from_attr(scope: node_scope, -name: str, -val: str) -> node {
fn new_node_from_attr(scope: node_scope, -name: str, -val: str) -> Node {
let elmt = ElementData("div", ~HTMLDivElement);
let attr = ~attr(name, val);
elmt.attrs.push(attr);

View file

@ -1,7 +1,7 @@
#[doc="High-level interface to CSS selector matching."]
import dom::style::{display_type, di_block, di_inline, di_none, stylesheet};
import dom::base::{HTMLDivElement, HTMLHeadElement, HTMLImageElement, Element, Text, node};
import dom::base::{Element, HTMLDivElement, HTMLHeadElement, HTMLImageElement, Node, Text};
import dom::base::node_kind;
import dom::rcu::reader_methods;
import layout::base::*; // FIXME: resolve bug requires *
@ -33,7 +33,7 @@ fn default_style_for_node_kind(kind: node_kind) -> computed_style {
}
}
impl style_priv for node {
impl style_priv for Node {
#[doc="
Performs CSS selector matching on a node.
@ -56,7 +56,7 @@ impl style_priv for node {
}
}
impl style_methods for node {
impl style_methods for Node {
#[doc="
Returns the computed style for the given node. If CSS selector matching has not yet been
performed, fails.

View file

@ -2,7 +2,8 @@
import dom::rcu::writer_methods;
import dom::base::{attr, ElementKind, HTMLDivElement, HTMLHeadElement, HTMLImageElement};
import dom::base::{UnknownElement, methods, Element, ElementData, Text, rd_tree_ops, wr_tree_ops};
import dom::base::{UnknownElement, methods, Element, ElementData, Node, Text, rd_tree_ops};
import dom::base::{wr_tree_ops};
import dom = dom::base;
import dvec::extensions;
import geom::size::Size2D;
@ -11,7 +12,7 @@ import gfx::geometry::au;
import parser = parser::lexer::html;
import parser::token;
fn link_up_attribute(scope: dom::node_scope, node: dom::node, -key: str, -value: str) {
fn link_up_attribute(scope: dom::node_scope, node: Node, -key: str, -value: str) {
// TODO: Implement atoms so that we don't always perform string comparisons.
scope.rd(node) {
|node_contents|
@ -64,7 +65,7 @@ fn build_element_kind(tag_name: str) -> ~ElementKind {
}
}
fn build_dom(scope: dom::node_scope, stream: port<token>) -> dom::node {
fn build_dom(scope: dom::node_scope, stream: port<token>) -> Node {
// The current reference node.
let mut cur = scope.new_node(Element(ElementData("html", ~HTMLDivElement)));
loop {