gecko-dev/servo/tests/unit/style/custom_properties.rs
Emilio Cobos Álvarez 49ddbca093 servo: Merge #18783 - style: Introduce CustomPropertiesBuilder (from emilio:custom-props-builder); r=SimonSapin
I'm about to introduce more state here to implement optimizations for custom
property cascading, so this abstraction is useful to encapsulate that state.

Source-Repo: https://github.com/servo/servo
Source-Revision: 55a37930b218713fff4ba84b4fa1e43a0455e498

--HG--
extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear
extra : subtree_revision : 836b42e1e79da3ddb1f695622c4b23920fd90c22
2017-10-08 09:50:04 -05:00

44 lines
1.3 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use cssparser::{Parser, ParserInput};
use servo_arc::Arc;
use style::custom_properties::{Name, SpecifiedValue, CustomPropertiesMap, CustomPropertiesBuilder};
use style::properties::DeclaredValue;
use test::{self, Bencher};
fn cascade(
name_and_value: &[(&str, &str)],
inherited: Option<&Arc<CustomPropertiesMap>>,
) -> Option<Arc<CustomPropertiesMap>> {
let values = name_and_value.iter().map(|&(name, value)| {
let mut input = ParserInput::new(value);
let mut parser = Parser::new(&mut input);
(Name::from(name), SpecifiedValue::parse(&mut parser).unwrap())
}).collect::<Vec<_>>();
let mut builder = CustomPropertiesBuilder::new(inherited);
for &(ref name, ref val) in &values {
builder.cascade(name, DeclaredValue::Value(val));
}
builder.build()
}
#[bench]
fn cascade_custom_simple(b: &mut Bencher) {
b.iter(|| {
let parent = cascade(&[
("foo", "10px"),
("bar", "100px"),
], None);
test::black_box(cascade(&[
("baz", "calc(40em + 4px)"),
("bazz", "calc(30em + 4px)"),
], parent.as_ref()))
})
}