diff options
author | Patrick Walton <pcwalton@mimiga.net> | 2012-05-17 18:21:38 -0700 |
---|---|---|
committer | Patrick Walton <pcwalton@mimiga.net> | 2012-05-17 18:22:06 -0700 |
commit | d4ea1477a764f992ae8faae684287cc2917f9c63 (patch) | |
tree | dc1c44afd0e42aadb42c8ccc9fe7aa8129b497d5 /src/servo/parser/html_builder.rs | |
parent | 9c28245e2fde3c3a95f517590a4166cb88b4d1ed (diff) | |
download | servo-d4ea1477a764f992ae8faae684287cc2917f9c63.tar.gz servo-d4ea1477a764f992ae8faae684287cc2917f9c63.zip |
Parse HTML attributes and support images of different widths and heights
Diffstat (limited to 'src/servo/parser/html_builder.rs')
-rw-r--r-- | src/servo/parser/html_builder.rs | 47 |
1 files changed, 42 insertions, 5 deletions
diff --git a/src/servo/parser/html_builder.rs b/src/servo/parser/html_builder.rs index 6f764ef1913..67bb0fc1eba 100644 --- a/src/servo/parser/html_builder.rs +++ b/src/servo/parser/html_builder.rs @@ -1,4 +1,4 @@ -// Constructs a DOM tree from an incoming token stream. +#[doc="Constructs a DOM tree from an incoming token stream."] import dom::rcu::writer_methods; import dom::base::{methods, rd_tree_ops, wr_tree_ops}; @@ -7,6 +7,35 @@ import parser = parser::html; import html::token; import gfx::geom; +fn link_up_attribute(scope: dom::node_scope, node: dom::node, key: str, + value: str) { + // TODO: Implement atoms so that we don't always perform string + // comparisons. + // FIXME: This is wrong... we should not have DIV and IMG be separate types + // of nodes and instead have them inherit from Element, obviously. + scope.rd(node) { + |node_contents| + alt node_contents.kind { + dom::nk_img(dims) if key == "width" { + alt int::from_str(value) { + none { /* drop on the floor */ } + some(s) { dims.width = geom::px_to_au(s); } + } + } + dom::nk_img(dims) if key == "height" { + alt int::from_str(value) { + none { /* drop on the floor */ } + some(s) { dims.height = geom::px_to_au(s); } + } + } + dom::nk_div | dom::nk_img(*) { /* drop on the floor */ } + dom::nk_text(*) { + fail "attempt to link up an attribute to a text node" + } + } + } +} + fn build_dom(scope: dom::node_scope, stream: port<token>) -> dom::node { // The current reference node. @@ -16,14 +45,14 @@ fn build_dom(scope: dom::node_scope, #debug["token=%?", token]; alt token { parser::to_eof { break; } - parser::to_start_tag("div") { + parser::to_start_opening_tag("div") { #debug["DIV"]; let new_node = scope.new_node( dom::nk_div); scope.add_child(cur, new_node); cur = new_node; } - parser::to_start_tag("img") { + parser::to_start_opening_tag("img") { #debug["IMG"]; let new_node = scope.new_node( dom::nk_img({mut width: geom::px_to_au(100), @@ -31,8 +60,15 @@ fn build_dom(scope: dom::node_scope, scope.add_child(cur, new_node); cur = new_node; } - parser::to_start_tag(t) { - fail ("Unrecognized tag: " + t); + parser::to_start_opening_tag(t) { + fail ("Unrecognized tag: " + t); + } + parser::to_attr(key, value) { + #debug["attr: %? = %?", key, value]; + link_up_attribute(scope, cur, key, value); + } + parser::to_end_opening_tag { + #debug("end opening tag"); } parser::to_end_tag(_) { // TODO: Assert that the closing tag has the right name. @@ -54,3 +90,4 @@ fn build_dom(scope: dom::node_scope, } ret cur; } + |