Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement CSS3 Calc #7496

Merged
merged 23 commits into from Sep 2, 2015
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9556141
Implement Calc for LengthOrPercentage
dzbarsky Aug 11, 2015
cb4d878
Implement Calc for LengthOrPercentageOrAuto
dzbarsky Aug 12, 2015
2bb6b45
Implement proper calc parsing
dzbarsky Aug 12, 2015
5df4b82
Implement font relative and viewport relative units for calc
dzbarsky Aug 12, 2015
af4d2e9
Clean up serialization and other hacks
dzbarsky Aug 13, 2015
64dc954
Clean up serialization code a little
dzbarsky Aug 13, 2015
cfa1e46
Clean up AST simplification code
dzbarsky Aug 13, 2015
6573e80
Properly serialize % values in calc expressions
dzbarsky Aug 13, 2015
663e0f6
Simplify like terms in all sum expressions, not just toplevel calc
dzbarsky Aug 13, 2015
63d0429
Simplify the calc AST simplification code
dzbarsky Aug 13, 2015
67db4fb
Remove stray changes
dzbarsky Aug 13, 2015
f8bd7c4
Fix some calc parsing panics
dzbarsky Aug 14, 2015
164af05
Expand out nested products
dzbarsky Aug 15, 2015
dcac654
Use the type system to enforce that product nodes are simplified away
dzbarsky Aug 15, 2015
53e8f7d
Get rid of some cloning
dzbarsky Aug 15, 2015
5ac205b
Clean up some stray lines
dzbarsky Aug 15, 2015
cdae523
Address review comments
dzbarsky Aug 23, 2015
25829bb
Add calc reftest
dzbarsky Aug 26, 2015
2591a89
Add calc parsing tests for the other properties
dzbarsky Aug 26, 2015
80d471d
Merge branch 'master' into calc
SimonSapin Sep 1, 2015
b30c4f7
Add support for the ch unit in calc()
SimonSapin Sep 1, 2015
9f48dcd
Fix font-size keywords parsing.
SimonSapin Sep 2, 2015
b51b7dd
Fix ch/em confusion.
SimonSapin Sep 2, 2015
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Clean up serialization and other hacks

  • Loading branch information
dzbarsky committed Aug 26, 2015
commit af4d2e910e2134f4ac597053488f572eb70ba3df
@@ -775,8 +775,7 @@ pub mod longhands {
% endfor
&T::Length(length) => write!(f, "{:?}", length),
&T::Percentage(number) => write!(f, "{}%", number),
// XXX HACK WRONG
&T::Calc(calc) => write!(f, "{}%", 10.),
&T::Calc(calc) => write!(f, "{:?}", calc)
}
}
}
@@ -1914,9 +1913,9 @@ pub mod longhands {
specified::LengthOrPercentage::Length(value) => value,
specified::LengthOrPercentage::Percentage(value) =>
specified::Length::FontRelative(specified::FontRelativeLength::Em(value)),
// XXX WRONG HACK
specified::LengthOrPercentage::Calc(calc) =>
specified::Length::FontRelative(specified::FontRelativeLength::Em(20.)),
// FIXME(dzbarsky) handle calc for font-size
specified::LengthOrPercentage::Calc(_) =>
specified::Length::FontRelative(specified::FontRelativeLength::Em(1.)),
})
.or_else(|()| {
match_ignore_ascii_case! { try!(input.expect_ident()),
@@ -607,35 +607,61 @@ pub mod specified {

impl ToCss for Calc {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
// XXX WRONG HACK
try!(write!(dest, "calc("));
if let Some(FontRelativeLength::Em(em)) = self.em {
try!(write!(dest, "{}em", em));
}
if let Some(FontRelativeLength::Ex(ex)) = self.ex {
try!(write!(dest, "{}ex", ex));
}
if let Some(absolute) = self.absolute {
try!(write!(dest, "{}px", Au::to_px(absolute)));
}
if let Some(FontRelativeLength::Rem(rem)) = self.rem {
try!(write!(dest, "{}rem", rem));
}
if let Some(ViewportPercentageLength::Vh(vh)) = self.vh {
try!(write!(dest, "{}vh", vh));
}
if let Some(ViewportPercentageLength::Vmax(vmax)) = self.vmax {
try!(write!(dest, "{}vmax", vmax));

macro_rules! count {
( $( $val:ident ),* ) => {
{
let mut count = 0;
$(
if let Some(_) = self.$val {
count += 1;
}
)*
count
}
};
}
if let Some(ViewportPercentageLength::Vmin(vmin)) = self.vmin {
try!(write!(dest, "{}vmin", vmin));

macro_rules! serialize {
( $( [$val:ident; $name:expr] ),* ) => {
{
let mut first_value = true;
$(
if let Some(val) = self.$val {
if !first_value {
try!(write!(dest, " + "));
} else {
first_value = false;
}
try!(write!(dest, "{:?}{}", val, $name));
}
)*
}
};
}
if let Some(ViewportPercentageLength::Vw(vw)) = self.vw {
try!(write!(dest, "{}vw", vw));

let count = count!(em, ex, absolute, rem, vh, vmax, vmin, vw, percentage);
assert!(count > 0);

if count > 1 {
try!(write!(dest, "calc("));
}

serialize!(
[em; "em"],
[ex; "ex"],
[absolute; "px"],
[rem; "rem"],
[vh; "vh"],
[vmax; "vmax"],
[vmin; "vmin"],
[vw; "vw"],
[percentage; "%"]);

write!(dest, ")")
if count > 1 {
try!(write!(dest, ")"));
}
Ok(())
}
}

@@ -1308,8 +1334,7 @@ pub mod computed {
match self {
&LengthOrPercentage::Length(length) => write!(f, "{:?}", length),
&LengthOrPercentage::Percentage(percentage) => write!(f, "{}%", percentage * 100.),
// XXX HACK WRONG
&LengthOrPercentage::Calc(calc) => write!(f, "{}%", 100.),
&LengthOrPercentage::Calc(calc) => write!(f, "{:?}", calc),
}
}
}
@@ -1356,8 +1381,7 @@ pub mod computed {
&LengthOrPercentageOrAuto::Length(length) => write!(f, "{:?}", length),
&LengthOrPercentageOrAuto::Percentage(percentage) => write!(f, "{}%", percentage * 100.),
&LengthOrPercentageOrAuto::Auto => write!(f, "auto"),
// XXX HACK WRONG
&LengthOrPercentageOrAuto::Calc(calc) => write!(f, "{}%", 100.),
&LengthOrPercentageOrAuto::Calc(calc) => write!(f, "{:?}", calc),
}
}
}
@@ -9,7 +9,8 @@ use parser::{ParserContext, log_css_error};
use properties::longhands;
use stylesheets::Origin;
use util::geometry::{Au, PagePx, ViewportPx};
use values::specified::{AllowedNumericType, Length, LengthOrPercentageOrAuto};
use values::computed::{Context, ToComputedValue};
use values::specified::{AllowedNumericType, LengthOrPercentageOrAuto};

use std::ascii::AsciiExt;
use std::collections::hash_map::{Entry, HashMap};
@@ -420,25 +421,42 @@ impl ViewportConstraints {
let initial_viewport = Size2D::new(Au::from_f32_px(initial_viewport.width.get()),
Au::from_f32_px(initial_viewport.height.get()));


let context = Context {
is_root_element: false,
viewport_size: initial_viewport,
inherited_font_weight: longhands::font_weight::get_initial_value(),
inherited_font_size: longhands::font_size::get_initial_value(),
inherited_text_decorations_in_effect: longhands::_servo_text_decorations_in_effect::get_initial_value(),
font_size: longhands::font_size::get_initial_value(),
root_font_size: longhands::font_size::get_initial_value(),
display: longhands::display::get_initial_value(),
color: longhands::color::get_initial_value(),
text_decoration: longhands::text_decoration::get_initial_value(),
overflow_x: longhands::overflow_x::get_initial_value(),
overflow_y: longhands::overflow_y::get_initial_value(),
positioned: false,
floated: false,
border_top_present: false,
border_right_present: false,
border_bottom_present: false,
border_left_present: false,
outline_style_present: false,
};

macro_rules! to_pixel_length {
($value:ident, $dimension:ident) => {
if let Some($value) = $value {
match $value {
LengthOrPercentageOrAuto::Length(ref value) => Some(match value {
&Length::Absolute(length) => length,
&Length::FontRelative(length) => {
let initial_font_size = longhands::font_size::get_initial_value();
length.to_computed_value(initial_font_size, initial_font_size)
}
&Length::ViewportPercentage(length) =>
length.to_computed_value(initial_viewport),
_ => unreachable!()
}),
LengthOrPercentageOrAuto::Length(value) =>
Some(value.to_computed_value(&context)),
LengthOrPercentageOrAuto::Percentage(value) =>
Some(initial_viewport.$dimension.scale_by(value)),
LengthOrPercentageOrAuto::Auto => None,
// XXX WRONG HACK
LengthOrPercentageOrAuto::Calc(calc) => None,
LengthOrPercentageOrAuto::Calc(calc) => {
let calc = calc.to_computed_value(&context);
Some(initial_viewport.$dimension.scale_by(calc.percentage()) + calc.length())
}
}
} else {
None
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.