Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ curl-sys = { git = "https://github.com/lexmount/curl-rust", rev = "0392e12475d8d
cookie = { git = "https://github.com/ldm0/cookie-rs", branch = "priority" }
v8 = { path = "vendor/v8-152.2.0" }
deno_v8 = { path = "vendor/deno_v8-0.3.0" }
selectors = { git = "https://github.com/lexmount/stylo", rev = "db43a7a011d98407392d4a23f7c30d1a3a95aef8" }
servo_arc = { git = "https://github.com/lexmount/stylo", rev = "db43a7a011d98407392d4a23f7c30d1a3a95aef8" }
stylo = { git = "https://github.com/lexmount/stylo", rev = "db43a7a011d98407392d4a23f7c30d1a3a95aef8" }
stylo_atoms = { git = "https://github.com/lexmount/stylo", rev = "db43a7a011d98407392d4a23f7c30d1a3a95aef8" }
stylo_dom = { git = "https://github.com/lexmount/stylo", rev = "db43a7a011d98407392d4a23f7c30d1a3a95aef8" }
stylo_static_prefs = { git = "https://github.com/lexmount/stylo", rev = "db43a7a011d98407392d4a23f7c30d1a3a95aef8" }
stylo_traits = { git = "https://github.com/lexmount/stylo", rev = "db43a7a011d98407392d4a23f7c30d1a3a95aef8" }
selectors = { git = "https://github.com/lexmount/stylo", rev = "e6b36a449b61d17568a6e7f56c6574115fe3f73b" }
servo_arc = { git = "https://github.com/lexmount/stylo", rev = "e6b36a449b61d17568a6e7f56c6574115fe3f73b" }
stylo = { git = "https://github.com/lexmount/stylo", rev = "e6b36a449b61d17568a6e7f56c6574115fe3f73b" }
stylo_atoms = { git = "https://github.com/lexmount/stylo", rev = "e6b36a449b61d17568a6e7f56c6574115fe3f73b" }
stylo_dom = { git = "https://github.com/lexmount/stylo", rev = "e6b36a449b61d17568a6e7f56c6574115fe3f73b" }
stylo_static_prefs = { git = "https://github.com/lexmount/stylo", rev = "e6b36a449b61d17568a6e7f56c6574115fe3f73b" }
stylo_traits = { git = "https://github.com/lexmount/stylo", rev = "e6b36a449b61d17568a6e7f56c6574115fe3f73b" }
svgtypes = { git = "https://github.com/lexmount/svgtypes", rev = "520374d714d69f8e2dc93c81a9d6be559ecde3ff" }
# Preserve original SVG selector inputs and collect unclipped source geometry.
usvg = { git = "https://github.com/lexmount/resvg", rev = "a137793a5749c27862b724eef60c4734526b14a0" }
Expand Down
143 changes: 50 additions & 93 deletions moli-css-parse/src/cssom_block.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use crate::{ParsedCssomProperty, cssom_property::with_detached_style_context};
use cssparser::Parser;
use style::{
moli_declaration_block::{
CssDeclarationBlock as NativeBlock, CssDeclarationEntry, CssMutationProjection,
CssRemoveResult, CssSetResult,
},
properties::{
Importance, PropertyDeclaration, PropertyDeclarationBlock, PropertyDeclarationId,
PropertyId, parse_property_declaration_list,
CssRemoveResult, CssSetResult, ParsedCssomProperty, declaration_block_css_text,
declaration_entry, parse_declarations, property_declaration_name, property_value_by_id,
with_detached_style_context,
},
properties::{PropertyDeclarationBlock, PropertyId, SourcePropertyDeclarationUpdate},
servo_arc::Arc,
shared_lock::{Locked, SharedRwLock},
};
use style_traits::CssString;

/// A writable CSSOM block or the immutable block already shared with Stylo.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -89,13 +85,13 @@ impl CssDeclarationBlock {
}

pub fn css_text(&self) -> String {
self.with_declarations(serialize_block)
self.with_declarations(declaration_block_css_text)
}

pub fn item(&self, index: usize) -> Option<String> {
self.with_declarations(|block| {
let (declaration, _) = block.declaration_importance_iter().nth(index)?;
Some(declaration_name(declaration.id()))
Some(property_declaration_name(declaration.id()).into_owned())
})
}

Expand Down Expand Up @@ -142,52 +138,43 @@ impl CssDeclarationBlock {
value: &str,
priority: bool,
) -> CssMutationProjection {
let projection = with_detached_style_context(|context| {
let property = PropertyId::parse(name, context).ok()?;
let affected_names = Self::affected_names_for_property(name)?;
if value.is_empty() {
let result = self.remove_property_by_id(&property);
return Some(CssMutationProjection {
set_result: if result.changed {
CssSetResult::ChangedPropertySet
} else {
CssSetResult::Unchanged
},
entries: Vec::new(),
affected_names,
stored_names: Vec::new(),
has_unresolved_value: false,
});
}
let parsed = ParsedCssomProperty::parse_value(property, value, priority, context)?;
let entries = parsed.entries()?;
let has_unresolved_value = parsed.has_unresolved_value();
let before = self.css_text();
let modified_existing = self
.with_declarations(|b| b.first_declaration_to_remove(parsed.property()).is_some());
self.mutate(|block| parsed.replace_in_declaration_list(block));
let set_result = if self.css_text() == before {
CssSetResult::Unchanged
} else if modified_existing {
CssSetResult::ModifiedExisting
} else {
CssSetResult::ChangedPropertySet
};
Some(CssMutationProjection {
set_result,
affected_names,
stored_names: entries.iter().map(|entry| entry.name.clone()).collect(),
entries,
has_unresolved_value,
let Some(parsed) = with_detached_style_context(|context| {
ParsedCssomProperty::parse(name, value, priority, context)
}) else {
return CssMutationProjection::parse_error();
};
if parsed.is_removal()
&& !self.with_declarations(|block| {
block
.first_declaration_to_remove(parsed.property())
.is_some()
})
});
projection.unwrap_or_else(|| CssMutationProjection {
set_result: CssSetResult::ParseError,
affected_names: Vec::new(),
entries: Vec::new(),
stored_names: Vec::new(),
has_unresolved_value: false,
})
{
return CssMutationProjection::removal(parsed.affected_names(), false);
}
self.mutate(|block| parsed.replace_with_projection(block))
}

pub fn apply_parsed_property(
&mut self,
parsed: ParsedCssomProperty,
compare_single_declaration: bool,
) -> bool {
if compare_single_declaration
&& parsed.is_single_declaration()
&& parsed.is_ordinary_declaration_list()
{
let mut updates = SourcePropertyDeclarationUpdate::default();
if self.with_declarations(|block| parsed.prepare_for_update(block, &mut updates)) {
self.mutate(|block| parsed.apply_update(block, &mut updates));
return true;
}
if self.with_declarations(|block| parsed.has_same_specified_value(block)) {
return false;
}
}
self.mutate(|block| parsed.replace_expanded_declarations(block));
true
}

pub fn remove_property(&mut self, name: &str) -> CssRemoveResult {
Expand All @@ -212,43 +199,8 @@ impl CssDeclarationBlock {
}
}

fn property_value_by_id(block: &PropertyDeclarationBlock, property: &PropertyId) -> String {
let mut value = CssString::new();
let _ = block.property_value_to_css(property, &mut value);
value
}

fn serialize_block(block: &PropertyDeclarationBlock) -> String {
let mut output = CssString::new();
let _ = block.to_css(&mut output);
output
}

fn declaration_name(property: PropertyDeclarationId<'_>) -> String {
match property.name().as_ref() {
"font-width" => "font-stretch".to_owned(),
name => name.to_owned(),
}
}

pub(crate) fn declaration_entry(
declaration: &PropertyDeclaration,
importance: Importance,
) -> Option<CssDeclarationEntry> {
let mut value = CssString::new();
declaration.to_css(&mut value).ok()?;
Some(CssDeclarationEntry {
name: declaration_name(declaration.id()),
value,
priority: importance.important(),
})
}

pub fn parse_declaration_block(text: &str) -> CssDeclarationBlock {
with_detached_style_context(|context| {
let block = parse_property_declaration_list(context, &mut Parser::new(text), &[]);
CssDeclarationBlock::new(block)
})
CssDeclarationBlock::new(parse_declarations(text))
}

#[cfg(test)]
Expand Down Expand Up @@ -312,17 +264,22 @@ mod tests {
let published = block.publish();
assert!(Arc::ptr_eq(&published, &block.publish()));
assert_eq!(block.property_value("width").as_deref(), Some("10px"));
assert_eq!(
block.set_property("margin", "", false),
CssSetResult::Unchanged
);
assert!(Arc::ptr_eq(&published, &block.publish()));
assert!(block.set_property("width", "30px", false) != CssSetResult::ParseError);
let next = block.publish();
assert!(!Arc::ptr_eq(&published, &next));
let lock = SharedRwLock::new();
let guard = lock.read();
assert_eq!(
serialize_block(published.read_with(&guard)),
declaration_block_css_text(published.read_with(&guard)),
"width: 10px; height: 20px;"
);
assert_eq!(
serialize_block(next.read_with(&guard)),
declaration_block_css_text(next.read_with(&guard)),
"height: 20px; width: 30px;"
);
}
Expand Down
Loading
Loading