var() substitution: insert /**/ between tokens that would be parse as one token.

This commit is contained in:
Simon Sapin 2015-09-09 22:37:03 +02:00
parent 81dd1ab363
commit d56ea10770
6 changed files with 156 additions and 104 deletions

View file

@ -26,8 +26,8 @@ pub fn parse_name(s: &str) -> Result<Name, ()> {
pub struct SpecifiedValue { pub struct SpecifiedValue {
css: String, css: String,
first_token: TokenSerializationType, first_token_type: TokenSerializationType,
last_token: TokenSerializationType, last_token_type: TokenSerializationType,
/// Custom property names in var() functions. /// Custom property names in var() functions.
references: HashSet<Name>, references: HashSet<Name>,
@ -35,28 +35,59 @@ pub struct SpecifiedValue {
pub struct BorrowedSpecifiedValue<'a> { pub struct BorrowedSpecifiedValue<'a> {
css: &'a str, css: &'a str,
first_token: TokenSerializationType, first_token_type: TokenSerializationType,
last_token: TokenSerializationType, last_token_type: TokenSerializationType,
references: Option<&'a HashSet<Name>>, references: Option<&'a HashSet<Name>>,
} }
#[derive(Clone, HeapSizeOf)] #[derive(Clone, HeapSizeOf)]
pub struct ComputedValue { pub struct ComputedValue {
css: String, css: String,
first_token: TokenSerializationType, first_token_type: TokenSerializationType,
last_token: TokenSerializationType, last_token_type: TokenSerializationType,
} }
pub type ComputedValuesMap = HashMap<Name, ComputedValue>; pub type ComputedValuesMap = HashMap<Name, ComputedValue>;
impl ComputedValue {
fn empty() -> ComputedValue {
ComputedValue {
css: String::new(),
last_token_type: TokenSerializationType::nothing(),
first_token_type: TokenSerializationType::nothing(),
}
}
fn push(&mut self, css: &str, css_first_token_type: TokenSerializationType,
css_last_token_type: TokenSerializationType) {
self.first_token_type.set_if_nothing(css_first_token_type);
// If self.first_token_type was nothing,
// self.last_token_type is also nothing and this will be false:
if self.last_token_type.needs_separator_when_before(css_first_token_type) {
self.css.push_str("/**/")
}
self.css.push_str(css);
self.last_token_type = css_last_token_type
}
fn push_from(&mut self, position: (SourcePosition, TokenSerializationType),
input: &Parser, last_token_type: TokenSerializationType) {
self.push(input.slice_from(position.0), position.1, last_token_type)
}
fn push_variable(&mut self, variable: &ComputedValue) {
self.push(&variable.css, variable.first_token_type, variable.last_token_type)
}
}
pub fn parse(input: &mut Parser) -> Result<SpecifiedValue, ()> { pub fn parse(input: &mut Parser) -> Result<SpecifiedValue, ()> {
let start = input.position(); let start = input.position();
let mut references = Some(HashSet::new()); let mut references = Some(HashSet::new());
let (first_token, last_token) = try!(parse_declaration_value(input, &mut references)); let (first, last) = try!(parse_declaration_value(input, &mut references));
Ok(SpecifiedValue { Ok(SpecifiedValue {
css: input.slice_from(start).to_owned(), css: input.slice_from(start).to_owned(),
first_token: first_token, first_token_type: first,
last_token: last_token, last_token_type: last,
references: references.unwrap(), references: references.unwrap(),
}) })
} }
@ -147,8 +178,8 @@ pub fn cascade<'a>(custom_properties: &mut Option<HashMap<&'a Name, BorrowedSpec
Some(ref inherited) => inherited.iter().map(|(key, inherited_value)| { Some(ref inherited) => inherited.iter().map(|(key, inherited_value)| {
(key, BorrowedSpecifiedValue { (key, BorrowedSpecifiedValue {
css: &inherited_value.css, css: &inherited_value.css,
first_token: inherited_value.first_token, first_token_type: inherited_value.first_token_type,
last_token: inherited_value.last_token, last_token_type: inherited_value.last_token_type,
references: None references: None
}) })
}).collect(), }).collect(),
@ -161,8 +192,8 @@ pub fn cascade<'a>(custom_properties: &mut Option<HashMap<&'a Name, BorrowedSpec
DeclaredValue::Value(ref specified_value) => { DeclaredValue::Value(ref specified_value) => {
map.insert(name, BorrowedSpecifiedValue { map.insert(name, BorrowedSpecifiedValue {
css: &specified_value.css, css: &specified_value.css,
first_token: specified_value.first_token, first_token_type: specified_value.first_token_type,
last_token: specified_value.last_token, last_token_type: specified_value.last_token_type,
references: Some(&specified_value.references), references: Some(&specified_value.references),
}); });
}, },
@ -175,10 +206,10 @@ pub fn cascade<'a>(custom_properties: &mut Option<HashMap<&'a Name, BorrowedSpec
} }
} }
pub fn finish_cascade(custom_properties: Option<HashMap<&Name, BorrowedSpecifiedValue>>, pub fn finish_cascade(specified_values_map: Option<HashMap<&Name, BorrowedSpecifiedValue>>,
inherited: &Option<Arc<HashMap<Name, ComputedValue>>>) inherited: &Option<Arc<HashMap<Name, ComputedValue>>>)
-> Option<Arc<HashMap<Name, ComputedValue>>> { -> Option<Arc<HashMap<Name, ComputedValue>>> {
if let Some(mut map) = custom_properties { if let Some(mut map) = specified_values_map {
remove_cycles(&mut map); remove_cycles(&mut map);
Some(Arc::new(substitute_all(map, inherited))) Some(Arc::new(substitute_all(map, inherited)))
} else { } else {
@ -230,60 +261,61 @@ fn remove_cycles(map: &mut HashMap<&Name, BorrowedSpecifiedValue>) {
} }
/// Replace `var()` functions for all custom properties. /// Replace `var()` functions for all custom properties.
fn substitute_all(custom_properties: HashMap<&Name, BorrowedSpecifiedValue>, fn substitute_all(specified_values_map: HashMap<&Name, BorrowedSpecifiedValue>,
inherited: &Option<Arc<HashMap<Name, ComputedValue>>>) inherited: &Option<Arc<HashMap<Name, ComputedValue>>>)
-> HashMap<Name, ComputedValue> { -> HashMap<Name, ComputedValue> {
let mut substituted_map = HashMap::new(); let mut computed_values_map = HashMap::new();
let mut invalid = HashSet::new(); let mut invalid = HashSet::new();
for (&name, value) in &custom_properties { for (&name, value) in &specified_values_map {
// If this value is invalid at computed-time it wont be inserted in substituted_map. // If this value is invalid at computed-time it wont be inserted in computed_values_map.
// Nothing else to do. // Nothing else to do.
let _ = substitute_one( let _ = substitute_one(
name, value, &custom_properties, inherited, None, &mut substituted_map, &mut invalid); name, value, &specified_values_map, inherited, None,
&mut computed_values_map, &mut invalid);
} }
substituted_map computed_values_map
} }
/// Replace `var()` functions for one custom property. /// Replace `var()` functions for one custom property.
/// Also recursively record results for other custom properties referenced by `var()` functions. /// Also recursively record results for other custom properties referenced by `var()` functions.
/// Return `Err(())` for invalid at computed time. /// Return `Err(())` for invalid at computed time.
/// or `Ok(last_token_type that was pushed to partial_computed_value)` otherwise.
fn substitute_one(name: &Name, fn substitute_one(name: &Name,
specified_value: &BorrowedSpecifiedValue, specified_value: &BorrowedSpecifiedValue,
custom_properties: &HashMap<&Name, BorrowedSpecifiedValue>, specified_values_map: &HashMap<&Name, BorrowedSpecifiedValue>,
inherited: &Option<Arc<HashMap<Name, ComputedValue>>>, inherited: &Option<Arc<HashMap<Name, ComputedValue>>>,
substituted: Option<&mut String>, partial_computed_value: Option<&mut ComputedValue>,
substituted_map: &mut HashMap<Name, ComputedValue>, computed_values_map: &mut HashMap<Name, ComputedValue>,
invalid: &mut HashSet<Name>) invalid: &mut HashSet<Name>)
-> Result<(), ()> { -> Result<TokenSerializationType, ()> {
if let Some(computed_value) = substituted_map.get(name) { if let Some(computed_value) = computed_values_map.get(name) {
if let Some(substituted) = substituted { if let Some(partial_computed_value) = partial_computed_value {
substituted.push_str(&computed_value.css) partial_computed_value.push_variable(computed_value)
} }
return Ok(()) return Ok(computed_value.last_token_type)
} }
if invalid.contains(name) { if invalid.contains(name) {
return Err(()); return Err(());
} }
let computed_value = if specified_value.references.map(|set| set.is_empty()) == Some(false) { let computed_value = if specified_value.references.map(|set| set.is_empty()) == Some(false) {
let mut substituted = String::new(); let mut partial_computed_value = ComputedValue::empty();
let mut input = Parser::new(&specified_value.css); let mut input = Parser::new(&specified_value.css);
let mut start = input.position(); let mut position = (input.position(), specified_value.first_token_type);
if substitute_block(&mut input, &mut start, &mut substituted, &mut |name, substituted| { let result = substitute_block(
if let Some(other_specified_value) = custom_properties.get(name) { &mut input, &mut position, &mut partial_computed_value,
substitute_one(name, other_specified_value, custom_properties, inherited, &mut |name, partial_computed_value| {
Some(substituted), substituted_map, invalid) if let Some(other_specified_value) = specified_values_map.get(name) {
substitute_one(name, other_specified_value, specified_values_map, inherited,
Some(partial_computed_value), computed_values_map, invalid)
} else { } else {
Err(()) Err(())
} }
}).is_ok() {
substituted.push_str(input.slice_from(start));
ComputedValue {
css: substituted,
// FIXME: what if these are `var(` or the corresponding `)`?
first_token: specified_value.first_token,
last_token: specified_value.last_token,
} }
);
if let Ok(last_token_type) = result {
partial_computed_value.push_from(position, &input, last_token_type);
partial_computed_value
} else { } else {
// Invalid at computed-value time. Use the inherited value. // Invalid at computed-value time. Use the inherited value.
if let Some(inherited_value) = inherited.as_ref().and_then(|i| i.get(name)) { if let Some(inherited_value) = inherited.as_ref().and_then(|i| i.get(name)) {
@ -297,57 +329,82 @@ fn substitute_one(name: &Name,
// The specified value contains no var() reference // The specified value contains no var() reference
ComputedValue { ComputedValue {
css: specified_value.css.to_owned(), css: specified_value.css.to_owned(),
first_token: specified_value.first_token, first_token_type: specified_value.first_token_type,
last_token: specified_value.last_token, last_token_type: specified_value.last_token_type,
} }
}; };
if let Some(substituted) = substituted { if let Some(partial_computed_value) = partial_computed_value {
substituted.push_str(&computed_value.css) partial_computed_value.push_variable(&computed_value)
} }
substituted_map.insert(name.clone(), computed_value); let last_token_type = computed_value.last_token_type;
Ok(()) computed_values_map.insert(name.clone(), computed_value);
Ok(last_token_type)
} }
/// Replace `var()` functions in an arbitrary bit of input. /// Replace `var()` functions in an arbitrary bit of input.
/// ///
/// The `substitute_one` callback is called for each `var()` function in `input`. /// The `substitute_one` callback is called for each `var()` function in `input`.
/// If the variable has its initial value, /// If the variable has its initial value,
/// the callback should return `Err(())` and leave `substituted` unchanged. /// the callback should return `Err(())` and leave `partial_computed_value` unchanged.
/// Otherwise, it should push the value of the variable (with its own `var()` functions replaced) /// Otherwise, it should push the value of the variable (with its own `var()` functions replaced)
/// to `substituted` and return `Ok(())`. /// to `partial_computed_value` and return `Ok(last_token_type of what was pushed)`
/// ///
/// Return `Err(())` if `input` is invalid at computed-value time. /// Return `Err(())` if `input` is invalid at computed-value time.
/// or `Ok(last_token_type that was pushed to partial_computed_value)` otherwise.
fn substitute_block<F>(input: &mut Parser, fn substitute_block<F>(input: &mut Parser,
start: &mut SourcePosition, position: &mut (SourcePosition, TokenSerializationType),
substituted: &mut String, partial_computed_value: &mut ComputedValue,
substitute_one: &mut F) substitute_one: &mut F)
-> Result<(), ()> -> Result<TokenSerializationType, ()>
where F: FnMut(&Name, &mut String) -> Result<(), ()> { where F: FnMut(&Name, &mut ComputedValue) -> Result<TokenSerializationType, ()> {
let mut last_token_type = TokenSerializationType::nothing();
let mut set_position_at_next_iteration = false;
loop { loop {
let before_this_token = input.position(); let before_this_token = input.position();
let token = if let Ok(token) = input.next() { token } else { break }; let next = input.next_including_whitespace_and_comments();
if set_position_at_next_iteration {
*position = (before_this_token, match next {
Ok(ref token) => token.serialization_type(),
Err(()) => TokenSerializationType::nothing(),
});
set_position_at_next_iteration = false;
}
let token = if let Ok(token) = next {
token
} else {
break
};
match token { match token {
Token::Function(ref name) if name.eq_ignore_ascii_case("var") => { Token::Function(ref name) if name.eq_ignore_ascii_case("var") => {
substituted.push_str(input.slice(*start..before_this_token)); partial_computed_value.push(
input.slice(position.0..before_this_token), position.1, last_token_type);
try!(input.parse_nested_block(|input| { try!(input.parse_nested_block(|input| {
// parse_var_function() ensures neither .unwrap() will fail. // parse_var_function() ensures neither .unwrap() will fail.
let name = input.expect_ident().unwrap(); let name = input.expect_ident().unwrap();
let name = parse_name(&name).unwrap(); let name = parse_name(&name).unwrap();
if substitute_one(&name, substituted).is_ok() { if let Ok(last) = substitute_one(&name, partial_computed_value) {
last_token_type = last;
// Skip over the fallback, as `parse_nested_block` would return `Err` // Skip over the fallback, as `parse_nested_block` would return `Err`
// if we dont consume all of `input`. // if we dont consume all of `input`.
// FIXME: Add a specialized method to cssparser to do this with less work. // FIXME: Add a specialized method to cssparser to do this with less work.
while let Ok(_) = input.next() {} while let Ok(_) = input.next() {}
} else { } else {
try!(input.expect_comma()); try!(input.expect_comma());
let mut start = input.position(); let position = input.position();
try!(substitute_block(input, &mut start, substituted, substitute_one)); let first_token_type = input.next_including_whitespace_and_comments()
substituted.push_str(input.slice_from(start)); // parse_var_function() ensures that .unwrap() will not fail.
.unwrap()
.serialization_type();
input.reset(position);
let mut position = (position, first_token_type);
last_token_type = try!(substitute_block(
input, &mut position, partial_computed_value, substitute_one));
partial_computed_value.push_from(position, input, last_token_type);
} }
Ok(()) Ok(())
})); }));
*start = input.position(); set_position_at_next_iteration = true
} }
Token::Function(_) | Token::Function(_) |
@ -355,11 +412,13 @@ fn substitute_block<F>(input: &mut Parser,
Token::CurlyBracketBlock | Token::CurlyBracketBlock |
Token::SquareBracketBlock => { Token::SquareBracketBlock => {
try!(input.parse_nested_block(|input| { try!(input.parse_nested_block(|input| {
substitute_block(input, start, substituted, substitute_one) substitute_block(input, position, partial_computed_value, substitute_one)
})); }));
// Its the same type for CloseCurlyBracket and CloseSquareBracket.
last_token_type = Token::CloseParenthesis.serialization_type();
} }
_ => {} _ => last_token_type = token.serialization_type()
} }
} }
// FIXME: deal with things being implicitly closed at the end of the input. E.g. // FIXME: deal with things being implicitly closed at the end of the input. E.g.
@ -368,31 +427,27 @@ fn substitute_block<F>(input: &mut Parser,
// <p style="background: var(--color) var(--image) top left; --image: url('a.png"></p> // <p style="background: var(--color) var(--image) top left; --image: url('a.png"></p>
// </div> // </div>
// ``` // ```
Ok(()) Ok(last_token_type)
} }
/// Replace `var()` functions for a non-custom property. /// Replace `var()` functions for a non-custom property.
/// Return `Err(())` for invalid at computed time. /// Return `Err(())` for invalid at computed time.
pub fn substitute(input: &str, custom_properties: &Option<Arc<HashMap<Name, ComputedValue>>>) pub fn substitute(input: &str, first_token_type: TokenSerializationType,
computed_values_map: &Option<Arc<HashMap<Name, ComputedValue>>>)
-> Result<String, ()> { -> Result<String, ()> {
let empty_map; let mut substituted = ComputedValue::empty();
let custom_properties = if let &Some(ref arc) = custom_properties {
&**arc
} else {
empty_map = HashMap::new();
&empty_map
};
let mut substituted = String::new();
let mut input = Parser::new(input); let mut input = Parser::new(input);
let mut start = input.position(); let mut position = (input.position(), first_token_type);
try!(substitute_block(&mut input, &mut start, &mut substituted, &mut |name, substituted| { let last_token_type = try!(substitute_block(
if let Some(value) = custom_properties.get(name) { &mut input, &mut position, &mut substituted, &mut |name, substituted| {
substituted.push_str(&value.css); if let Some(value) = computed_values_map.as_ref().and_then(|map| map.get(name)) {
Ok(()) substituted.push_variable(value);
Ok(value.last_token_type)
} else { } else {
Err(()) Err(())
} }
})); }
substituted.push_str(input.slice_from(start)); ));
Ok(substituted) substituted.push_from(position, &input, last_token_type);
Ok(substituted.css)
} }

View file

@ -16,7 +16,7 @@ use std::mem;
use std::sync::Arc; use std::sync::Arc;
use cssparser::{Parser, Color, RGBA, AtRuleParser, DeclarationParser, use cssparser::{Parser, Color, RGBA, AtRuleParser, DeclarationParser,
DeclarationListParser, parse_important, ToCss}; DeclarationListParser, parse_important, ToCss, TokenSerializationType};
use url::Url; use url::Url;
use util::geometry::Au; use util::geometry::Au;
use util::logical_geometry::{LogicalMargin, PhysicalSide, WritingMode}; use util::logical_geometry::{LogicalMargin, PhysicalSide, WritingMode};
@ -210,9 +210,11 @@ pub mod longhands {
let var = input.seen_var_functions(); let var = input.seen_var_functions();
if specified.is_err() && var { if specified.is_err() && var {
input.reset(start); input.reset(start);
try!(::custom_properties::parse_declaration_value(input, &mut None)); let (first_token_type, _) = try!(
::custom_properties::parse_declaration_value(input, &mut None));
return Ok(DeclaredValue::WithVariables { return Ok(DeclaredValue::WithVariables {
css: input.slice_from(start).to_owned(), css: input.slice_from(start).to_owned(),
first_token_type: first_token_type,
base_url: context.base_url.clone(), base_url: context.base_url.clone(),
from_shorthand: Shorthand::None, from_shorthand: Shorthand::None,
}) })
@ -4890,12 +4892,14 @@ pub mod shorthands {
Ok(()) Ok(())
} else if var { } else if var {
input.reset(start); input.reset(start);
try!(::custom_properties::parse_declaration_value(input, &mut None)); let (first_token_type, _) = try!(
::custom_properties::parse_declaration_value(input, &mut None));
let css = input.slice_from(start); let css = input.slice_from(start);
% for sub_property in shorthand.sub_properties: % for sub_property in shorthand.sub_properties:
declarations.push(PropertyDeclaration::${sub_property.camel_case}( declarations.push(PropertyDeclaration::${sub_property.camel_case}(
DeclaredValue::WithVariables { DeclaredValue::WithVariables {
css: css.to_owned(), css: css.to_owned(),
first_token_type: first_token_type,
base_url: context.base_url.clone(), base_url: context.base_url.clone(),
from_shorthand: Shorthand::${shorthand.camel_case}, from_shorthand: Shorthand::${shorthand.camel_case},
} }
@ -5609,10 +5613,10 @@ mod property_bit_field {
where F: FnOnce(&DeclaredValue<longhands::${property.ident}::SpecifiedValue>) -> R where F: FnOnce(&DeclaredValue<longhands::${property.ident}::SpecifiedValue>) -> R
{ {
if let DeclaredValue::WithVariables { if let DeclaredValue::WithVariables {
ref css, ref base_url, from_shorthand ref css, first_token_type, ref base_url, from_shorthand
} = *value { } = *value {
f(& f(&
::custom_properties::substitute(css, custom_properties) ::custom_properties::substitute(css, first_token_type, custom_properties)
.and_then(|css| { .and_then(|css| {
// As of this writing, only the base URL is used for property values: // As of this writing, only the base URL is used for property values:
let context = ParserContext::new( let context = ParserContext::new(
@ -5798,7 +5802,12 @@ pub enum Shorthand {
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, PartialEq, Eq, Debug)]
pub enum DeclaredValue<T> { pub enum DeclaredValue<T> {
Value(T), Value(T),
WithVariables { css: String, base_url: Url, from_shorthand: Shorthand }, WithVariables {
css: String,
first_token_type: TokenSerializationType,
base_url: Url,
from_shorthand: Shorthand
},
Initial, Initial,
Inherit, Inherit,
// There is no Unset variant here. // There is no Unset variant here.

View file

@ -1,3 +0,0 @@
[variable-declaration-14.htm]
type: reftest
expected: FAIL

View file

@ -1,3 +0,0 @@
[variable-declaration-53.htm]
type: reftest
expected: FAIL

View file

@ -1,3 +0,0 @@
[variable-declaration-54.htm]
type: reftest
expected: FAIL

View file

@ -1,3 +0,0 @@
[variable-declaration-55.htm]
type: reftest
expected: FAIL