Skip to content

Commit

Permalink
Capitalized css style types, removed redundant pretty-printing.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmeyerho committed Jun 30, 2012
1 parent e4edb86 commit 55d8fc3
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 235 deletions.
4 changes: 1 addition & 3 deletions src/servo/content.rs
Expand Up @@ -13,8 +13,6 @@ import result::{ok, err};
import dom::base::NodeScope;
import dom::rcu::WriterMethods;
import dom::style;
import style::print_sheet;
import parser::css_lexer::spawn_css_lexer_task;
import parser::html_lexer::spawn_html_lexer_task;
import parser::css_builder::build_stylesheet;
import parser::html_builder::build_dom;
Expand Down Expand Up @@ -72,7 +70,7 @@ fn Content(layout: Layout) -> Content {
let css_rules = style_port.recv();

// Apply the css rules to the dom tree:
#debug["%s", print_sheet(css_rules)];
#debug["%?", css_rules];

// Now, join the layout so that they will see the latest
// changes we have made.
Expand Down
155 changes: 22 additions & 133 deletions src/servo/dom/style.rs
@@ -1,143 +1,32 @@
import util::color::{Color, methods};
import util::color::css_colors::black;
import util::color::Color;

enum display_type{
di_block,
di_inline,
di_none
enum DisplayType{
DisBlock,
DisInline,
DisNone
}

enum style_decl{
font_size(uint), // Currently assumes format '# pt'
display(display_type),
text_color(Color),
background_color(Color)
enum StyleDeclaration{
FontSize(uint), // Currently assumes format '# pt'
Display(DisplayType),
TextColor(Color),
BackgroundColor(Color)
}

enum attr{
exists(str),
exact(str, str),
includes(str, str),
starts_with(str, str)
enum Attr{
Exists(str),
Exact(str, str),
Includes(str, str),
StartsWith(str, str)
}

enum selector{
element(str, [attr]),
child(~selector, ~selector),
descendant(~selector, ~selector),
sibling(~selector, ~selector)
enum Selector{
Element(str, [Attr]),
Child(~Selector, ~Selector),
Descendant(~Selector, ~Selector),
Sibling(~Selector, ~Selector)
}

type rule = ([~selector], [style_decl]);
type Rule = ([~Selector], [StyleDeclaration]);

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_list_vert<T>(list : [T], print : fn(T) -> str) -> str {
let l = vec::len(list);
if l == 0u { ret "" }

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

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

ret res;
}

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

fn print_style(decl : style_decl) -> str{
alt decl {
font_size(s) { #fmt["Font size = %u pt", s] }
display(dis_ty) { #fmt["Display style = %s", print_display(dis_ty)] }
text_color(c) { #fmt["Text color = %s", c.print()] }
background_color(c) { #fmt["Background color = %s", c.print()] }
}
}

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 {
(sels, styles) {
let sel_str = print_list(sels, print_selector);
let sty_str = print_list(styles, print_style);

#fmt["Selectors: %s; Style: {%s}", sel_str, sty_str]
}
}
}

fn print_sheet(sheet : stylesheet) -> str {
#fmt["CSS Rules:\n%s", print_list_vert(sheet, print_rule)]
}

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

assert(actual1 == expected1);

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

let test2 = [~([~descendant(elmt1, elmt2)],
[display(di_block), text_color(black())])];

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

assert(actual2 == expected2);
}
type Stylesheet = [~Rule];
40 changes: 20 additions & 20 deletions src/servo/layout/box_builder.rs
@@ -1,7 +1,7 @@
#[doc="Creates CSS boxes from a DOM."]

import dom::base::{ElementData, HTMLDivElement, HTMLImageElement, Element, Text, Node};
import dom::style::{display_type, di_block, di_inline, di_none};
import dom::style::{DisplayType, DisBlock, DisInline, DisNone};
import dom::rcu::ReaderMethods;
import gfx::geometry;
import layout::base::{BlockBox, Box, BoxKind, BoxTreeReadMethods, BoxTreeWriteMethods, InlineBox};
Expand Down Expand Up @@ -51,16 +51,16 @@ impl methods for ctxt {

// Determine the child's display.
let disp = kid.get_computed_style().display;
if disp != di_inline {
if disp != DisInline {
self.finish_anonymous_box_if_necessary();
}

// Add the child's box to the current enclosing box or the current anonymous box.
alt kid.get_computed_style().display {
di_block {
DisBlock {
BTree.add_child(self.parent_box, kid_box);
}
di_inline {
DisInline {
let anon_box = alt self.anon_box {
none {
//
Expand All @@ -79,7 +79,7 @@ impl methods for ctxt {
};
BTree.add_child(anon_box, kid_box);
}
di_none {
DisNone {
// Nothing to do.
}
}
Expand All @@ -99,23 +99,23 @@ impl methods for ctxt {

// Determine the child's display.
let disp = kid.get_computed_style().display;
if disp != di_inline {
if disp != DisInline {
// TODO
}

// Add the child's box to the current enclosing box.
alt kid.get_computed_style().display {
di_block {
// TODO
#warn("TODO: non-inline display found inside inline box");
BTree.add_child(self.parent_box, kid_box);
}
di_inline {
BTree.add_child(self.parent_box, kid_box);
}
di_none {
// Nothing to do.
}
DisBlock {
// TODO
#warn("TODO: non-inline display found inside inline box");
BTree.add_child(self.parent_box, kid_box);
}
DisInline {
BTree.add_child(self.parent_box, kid_box);
}
DisNone {
// Nothing to do.
}
}
}
}
Expand All @@ -126,9 +126,9 @@ impl methods for ctxt {
self.parent_node.dump();

alt self.parent_node.get_computed_style().display {
di_block { self.construct_boxes_for_block_children(); }
di_inline { self.construct_boxes_for_inline_children(); }
di_none { /* Nothing to do. */ }
DisBlock { self.construct_boxes_for_block_children(); }
DisInline { self.construct_boxes_for_inline_children(); }
DisNone { /* Nothing to do. */ }
}

self.finish_anonymous_box_if_necessary();
Expand Down
4 changes: 2 additions & 2 deletions src/servo/layout/layout_task.rs
Expand Up @@ -7,7 +7,7 @@ import arc::arc;

import display_list_builder::build_display_list;
import dom::base::{Node};
import dom::style::stylesheet;
import dom::style::Stylesheet;
import gfx::geometry::px_to_au;
import gfx::renderer::Renderer;
import base::{NodeMethods, layout_methods};
Expand All @@ -21,7 +21,7 @@ import comm::*;
type Layout = chan<Msg>;

enum Msg {
BuildMsg(Node, stylesheet),
BuildMsg(Node, Stylesheet),
PingMsg(chan<content::PingMsg>),
ExitMsg
}
Expand Down

0 comments on commit 55d8fc3

Please sign in to comment.