Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/servo/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn content(to_layout: chan<layout::msg>) -> chan<msg> {

// Note: we can parse the next document in parallel
// with any previous documents.
let stream = html::spawn_parser_task(filename);
let stream = lexer::spawn_html_parser_task(filename);
let root = parser::html_builder::build_dom(scope, stream);

// Now, join the layout so that they will see the latest
Expand Down
7 changes: 4 additions & 3 deletions src/servo/dom/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ enum element_subclass {
es_head
}

#[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_scope = rcu::scope<node_data, layout_data>;
Expand Down
125 changes: 125 additions & 0 deletions src/servo/dom/style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import io::println;


enum display_type{
block,
inline
}

enum style_decl{
font_size(uint),
display(display_type),
text_color(uint),
background_color(uint)
}

enum attr{
exists(str),
exact(str, str),
includes(str, str),
starts_with(str, str)
}

enum selector{
element(str, [attr]),
child(~selector, ~selector),
descendant(~selector, ~selector),
sibling(~selector, ~selector)
}

type rule = (selector, [style_decl]);

type stylesheet = [rule];


fn print_list<T>(list : [T], print : fn(T) -> str) -> str {
let l = vec::len(list);
if l == 0u { ret "" }

let mut res = print(list[0]);
let mut i = 1u;

while i < l {
res += ", ";
res += print(list[i]);
i += 1u;
}

ret res;
}

fn print_display(dis_ty : display_type) -> str {
alt dis_ty {
block { "block" }
inline { "inline" }
}
}

fn print_style(decl : style_decl) -> str{
alt decl {
font_size(s) { #fmt("Font size = %u px", s) }
display(dis_ty) { #fmt("Display style = %s", print_display(dis_ty)) }
text_color(c) { #fmt("Text color = 0x%06x", c) }
background_color(c) { #fmt("Background color = 0x%06x", c) }
}
}

fn print_attr(attribute : attr) -> str {
alt attribute {
exists(att) { #fmt("[%s]", att) }
exact(att, val) { #fmt("[%s = %s]", att, val) }
includes(att, val) { #fmt("[%s ~= %s]", att, val) }
starts_with(att, val) { #fmt("[%s |= %s]", att, val) }
}
}

fn print_selector(select : ~selector) -> str {
alt *select {
element(s, attrs) { #fmt("Element %s with attributes: %s", s,
print_list(attrs, print_attr)) }
child(sel1, sel2) { #fmt("(%s) > (%s)", print_selector(sel1),
print_selector(sel2)) }
descendant(sel1, sel2) { #fmt("(%s) (%s)", print_selector(sel1),
print_selector(sel2)) }
sibling(sel1, sel2) { #fmt("(%s) + (%s)", print_selector(sel1),
print_selector(sel2)) }
}
}

fn print_rule(rule : rule) -> str {
alt rule {
(sel, styles) {
let sel_str = print_selector(~(copy sel));
let sty_str = print_list(styles, print_style);

#fmt("Selector: %s, Style: {%s}", sel_str, sty_str)
}
}
}

fn print_sheet(sheet : stylesheet) -> str {
#fmt("CSS Rules: %s", print_list(sheet, print_rule))
}

#[test]
fn test_pretty_print() {
let test1 = [(element("p", []), [font_size(32u)])];
let actual1 = print_sheet(test1);
let expected1 = "CSS Rules: Selector: Element p with attributes: ," +
" Style: {Font size = 32 px}";

assert(actual1 == expected1);

let elmt1 = ~element("*", []);
let elmt2 = ~element("body", [exact("class", "2")]);

let test2 = [(descendant(elmt1, elmt2),
[display(block), text_color(0u)])];

let actual2 = print_sheet(test2);
let expected2 = "CSS Rules: Selector: (Element * with attributes: ) " +
"(Element body with attributes: [class = 2]), " +
"Style: {Display style = block, Text color = 0x000000}";

assert(actual2 == expected2);
}
Loading