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

Adding sync method to update atrr from inline style updates #9410

Closed
wants to merge 15 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Shorthand serialization now takes iterators instead of array slice as…

… argument
  • Loading branch information
craftytrickster committed May 19, 2016
commit d33958872ae1e22539b3f676a3d2fe5072cad64b
@@ -145,8 +145,8 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
}

// Step 2.3
let list = list.iter().map(|x| &*x).collect::<Vec<_>>();
let serialized_value = shorthand.serialize_shorthand_to_string(&list);
let mut list = list.iter().map(|x| &*x);
let serialized_value = shorthand.serialize_shorthand_to_string(&mut list);
return DOMString::from(serialized_value);
}

@@ -338,7 +338,13 @@ impl ToCss for PropertyDeclarationBlock {
// TODO: serialize shorthand does not take is_important into account currently
// Substep 5
let was_serialized =
try!(shorthand.serialize_shorthand_to_buffer(dest, &current_longhands[..], &mut is_first_serialization));
try!(
shorthand.serialize_shorthand_to_buffer(
dest,
&mut current_longhands.iter().cloned(),
&mut is_first_serialization
)
);
// If serialization occured, Substep 7 & 8 will have been completed

// Substep 6
@@ -365,10 +371,10 @@ impl ToCss for PropertyDeclarationBlock {

// Steps 3.3.5, 3.3.6 & 3.3.7
try!(append_serialization(dest,
&property.to_string(),
AppendableValue::Declaration(declaration),
important,
&mut is_first_serialization));
&property.to_string(),
AppendableValue::Declaration(declaration),
important,
&mut is_first_serialization));

// Step 3.3.8
already_serialized.push(property);
@@ -381,6 +387,7 @@ impl ToCss for PropertyDeclarationBlock {

enum AppendableValue<'a> {
Declaration(&'a PropertyDeclaration),
DeclarationsForShorthand(&'a mut Iterator<Item=&'a PropertyDeclaration>),
Css(&'a str)
}

@@ -401,6 +408,7 @@ fn append_serialization<W>(dest: &mut W,
try!(write!(dest, "{}:", property_name));

match appendable_value {
AppendableValue::Css(css) => try!(write!(dest, " {}", css)),
AppendableValue::Declaration(decl) => {
if !decl.value_is_unparsed() {
// for normal parsed values, add a space between key: and value
@@ -409,8 +417,13 @@ fn append_serialization<W>(dest: &mut W,

try!(decl.to_css(dest));
},
AppendableValue::Css(css) => try!(write!(dest, "{}", css))
};
AppendableValue::DeclarationsForShorthand(decls) => {
for decl in decls {
try!(write!(dest, " "));
try!(decl.to_css(dest));
}
}
}

if is_important {
try!(write!(dest, " !important"));
@@ -589,49 +602,62 @@ impl Shorthand {
}

/// Serializes possible shorthand value to String.
pub fn serialize_shorthand_to_string(self, declarations: &[&PropertyDeclaration]) -> String {
pub fn serialize_shorthand_to_string(self, declarations: &mut Iterator<Item=&PropertyDeclaration>) -> String {
let mut result = String::new();
self.serialize_shorthand_to_buffer(&mut result, declarations, &mut true).unwrap();
result
}


/// Serializes possible shorthand value to input buffer given a list of longhand declarations.
/// On success, returns true if shorthand value is written and false if no shorthand value is present.
pub fn serialize_shorthand_to_buffer<W>(self,
dest: &mut W,
declarations: &[&PropertyDeclaration],
declarations: &mut Iterator<Item=&PropertyDeclaration>,
is_first_serialization: &mut bool)
-> Result<bool, fmt::Error> where W: Write {

let property_name = self.name();
// FIXME: I know that creating this list here is wrong, but I couldn't get it to compile otherwise
// using plain iterators, need advice!
let declaration_list: Vec<_> = declarations.cloned().collect();
let mut declarations = declaration_list.iter();

let first_declaration = match declarations.next() {
Some(declaration) => declaration,
None => return Ok(false)
};

let property_name = &self.name();

// https://drafts.csswg.org/css-variables/#variables-in-shorthands
if let Some(css) = declarations[0].with_variables_from_shorthand(self) {
if declarations[1..]
.iter()
.all(|d| d.with_variables_from_shorthand(self) == Some(css)) {

append_serialization(
dest, property_name, AppendableValue::Css(css), false, is_first_serialization
).and_then(|_| Ok(true))
} else {
Ok(false)
}
} else {
if declarations.iter().any(|d| d.with_variables()) {
Ok(false)
} else {
for declaration in declarations.iter() {
try!(append_serialization(
dest, property_name, AppendableValue::Declaration(declaration), false, is_first_serialization
));
}
// FIXME: this needs property-specific code, which probably should be in style/
// "as appropriate according to the grammar of shorthand "
// https://drafts.csswg.org/cssom/#serialize-a-css-value
Ok(true)
}
if let Some(css) = first_declaration.with_variables_from_shorthand(self) {
if declarations.all(|d| d.with_variables_from_shorthand(self) == Some(css)) {
return append_serialization(
dest, property_name, AppendableValue::Css(css), false, is_first_serialization
).and_then(|_| Ok(true));
}
else {
return Ok(false);
}
}

if !declaration_list.iter().any(|d| d.with_variables()) {
try!(
append_serialization(
dest,
property_name,
AppendableValue::DeclarationsForShorthand(&mut declaration_list.iter()),
false,
is_first_serialization
)
);
// FIXME: this needs property-specific code, which probably should be in style/
// "as appropriate according to the grammar of shorthand "
// https://drafts.csswg.org/cssom/#serialize-a-css-value
return Ok(true);
}

Ok(false)
}
}

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.