From ceb496230a112c463e4cc11559648093124b69c8 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Tue, 16 Oct 2012 19:21:49 -0700 Subject: [PATCH] Add something like absolute positioning --- src/servo/css/parser.rs | 5 +++++ src/servo/css/parser_util.rs | 11 +++++++++++ src/servo/css/resolve/matching.rs | 5 +++++ src/servo/css/styles.rs | 14 ++++++++++++-- src/servo/css/values.rs | 14 +++++++++++++- src/servo/layout/box.rs | 23 ++++++++++++++++++++--- src/test/test-absolute.css | 7 +++++++ src/test/test-absolute.html | 11 +++++++++++ 8 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 src/test/test-absolute.css create mode 100644 src/test/test-absolute.html diff --git a/src/servo/css/parser.rs b/src/servo/css/parser.rs index 05bb6d5ab80..5c113f68c26 100644 --- a/src/servo/css/parser.rs +++ b/src/servo/css/parser.rs @@ -167,6 +167,11 @@ impl TokenReader : ParserMethods { ~"width" => parse_box_sizing(val).extract(|res| Width(res)), ~"border-width" => parse_length(val).map(|res| BorderWidth(Specified(*res))), ~"border-color" => parse_color(val).map(|res| BorderColor(Specified(BdrColor(*res)))), + ~"position" => parse_position(val).extract(|res| Position(res)), + ~"top" => parse_length(val).map(|res| Top(Specified(*res))), + ~"right" => parse_length(val).map(|res| Right(Specified(*res))), + ~"bottom" => parse_length(val).map(|res| Bottom(Specified(*res))), + ~"left" => parse_length(val).map(|res| Left(Specified(*res))), _ => { #debug["Recieved unknown style property '%s'", val]; None } }; match desc { diff --git a/src/servo/css/parser_util.rs b/src/servo/css/parser_util.rs index d36edd28100..19352128c94 100644 --- a/src/servo/css/parser_util.rs +++ b/src/servo/css/parser_util.rs @@ -44,6 +44,17 @@ fn parse_absolute_size(str : &str) -> ParseResult { } } +fn parse_position(str: &str) -> ParseResult { + // FIXME: Bad copy + match str.to_str() { + ~"static" => Value(PosStatic), + ~"relative" => Value(PosRelative), + ~"absolute" => Value(PosAbsolute), + ~"fixed" => Value(PosFixed), + _ => Fail + } +} + fn parse_relative_size(str: &str) -> ParseResult { // FIXME: Bad copy. Can't match &str match str.to_str() { diff --git a/src/servo/css/resolve/matching.rs b/src/servo/css/resolve/matching.rs index f894529fc13..608f0aea478 100644 --- a/src/servo/css/resolve/matching.rs +++ b/src/servo/css/resolve/matching.rs @@ -184,6 +184,11 @@ impl Node : PrivStyleMethods { Width(size) => layout.style.width = size, BorderColor(col) => layout.style.border_color = col, BorderWidth(size) => layout.style.border_width = size, + Position(pos) => layout.style.position = pos, + Top(pos) => layout.style.top = pos, + Right(pos) => layout.style.right = pos, + Bottom(pos) => layout.style.bottom = pos, + Left(pos) => layout.style.left = pos, }; }) } diff --git a/src/servo/css/styles.rs b/src/servo/css/styles.rs index d2844aba77c..22660398255 100644 --- a/src/servo/css/styles.rs +++ b/src/servo/css/styles.rs @@ -23,7 +23,12 @@ type SpecifiedStyle = {mut background_color : CSSValue, mut width : CSSValue, mut border_color : CSSValue, mut border_style : CSSValue, - mut border_width : CSSValue + mut border_width : CSSValue, + mut position : CSSValue, + mut top : CSSValue, + mut right : CSSValue, + mut bottom : CSSValue, + mut left : CSSValue }; trait DefaultStyleMethods { @@ -89,7 +94,12 @@ fn empty_style_for_node_kind(kind: &NodeKind) -> SpecifiedStyle { mut width : Initial, mut border_color : Initial, mut border_style : Initial, - mut border_width : Initial} + mut border_width : Initial, + mut position : Initial, + mut top : Initial, + mut right : Initial, + mut bottom : Initial, + mut left : Initial} } trait StyleMethods { diff --git a/src/servo/css/values.rs b/src/servo/css/values.rs index 2b7b3d6c153..1835acf4ae7 100644 --- a/src/servo/css/values.rs +++ b/src/servo/css/values.rs @@ -175,6 +175,13 @@ enum CSSFontSize { PercentSize(float) } +enum CSSPosition { + PosStatic, + PosRelative, + PosAbsolute, + PosFixed +} + // Stylesheet parts enum StyleDeclaration { @@ -185,7 +192,12 @@ enum StyleDeclaration { Color(CSSValue), Width(CSSValue), BorderColor(CSSValue), - BorderWidth(CSSValue) + BorderWidth(CSSValue), + Position(CSSValue), + Top(CSSValue), + Right(CSSValue), + Bottom(CSSValue), + Left(CSSValue), } pub enum Attr { diff --git a/src/servo/layout/box.rs b/src/servo/layout/box.rs index 4b9868bcf54..e06bc1c979b 100644 --- a/src/servo/layout/box.rs +++ b/src/servo/layout/box.rs @@ -9,7 +9,7 @@ use core::dvec::DVec; use core::to_str::ToStr; use core::rand; use css::styles::SpecifiedStyle; -use css::values::{BoxSizing, Length, Px, CSSDisplay, Specified, BgColor, BgColorTransparent, BdrColor}; +use css::values::{BoxSizing, Length, Px, CSSDisplay, Specified, BgColor, BgColorTransparent, BdrColor, PosAbsolute}; use dl = gfx::display_list; use dom::element::{ElementKind, HTMLDivElement, HTMLImageElement}; use dom::node::{Element, Node, NodeData, NodeKind, NodeTree}; @@ -390,8 +390,25 @@ impl RenderBox : RenderBoxMethods { return; } - let bounds : Rect = Rect(self.d().position.origin.add(offset), - copy self.d().position.size); + let style = self.d().node.style(); + + let bounds : Rect = match style.position { + Specified(PosAbsolute) => { + let x_offset = match style.left { + Specified(Px(px)) => au::from_frac_px(px), + _ => self.d().position.origin.x + }; + let y_offset = match style.top { + Specified(Px(px)) => au::from_frac_px(px), + _ => self.d().position.origin.y + }; + Rect(Point2D(x_offset, y_offset), copy self.d().position.size) + } + _ => { + Rect(self.d().position.origin.add(offset), + copy self.d().position.size) + } + }; self.add_bgcolor_to_list(list, bounds); diff --git a/src/test/test-absolute.css b/src/test/test-absolute.css new file mode 100644 index 00000000000..c4391fcf9bd --- /dev/null +++ b/src/test/test-absolute.css @@ -0,0 +1,7 @@ +img { + position: absolute; + top: 200px; + left: 100px; + border-width: 10px; + border-color: blue +} \ No newline at end of file diff --git a/src/test/test-absolute.html b/src/test/test-absolute.html new file mode 100644 index 00000000000..e98b7fbefc7 --- /dev/null +++ b/src/test/test-absolute.html @@ -0,0 +1,11 @@ + + + + + + + + + + +