mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
Auto merge of #14304 - KiChjang:css-style-rule-style, r=Manishearth
Implement CSSStyleRule.style <!-- Please describe your changes on the following line: --> --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] These changes fix #14209 (github issue number if applicable). <!-- Either: --> - [ ] There are tests for these changes OR - [ ] These changes do not require tests because _____ <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/14304) <!-- Reviewable:end -->
This commit is contained in:
commit
d05cae5072
9 changed files with 205 additions and 131 deletions
|
@ -3,6 +3,7 @@
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use dom::bindings::codegen::Bindings::CSSStyleDeclarationBinding::{self, CSSStyleDeclarationMethods};
|
use dom::bindings::codegen::Bindings::CSSStyleDeclarationBinding::{self, CSSStyleDeclarationMethods};
|
||||||
|
use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
|
||||||
use dom::bindings::error::{Error, ErrorResult, Fallible};
|
use dom::bindings::error::{Error, ErrorResult, Fallible};
|
||||||
use dom::bindings::inheritance::Castable;
|
use dom::bindings::inheritance::Castable;
|
||||||
use dom::bindings::js::{JS, Root};
|
use dom::bindings::js::{JS, Root};
|
||||||
|
@ -24,11 +25,59 @@ use style_traits::ToCss;
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
pub struct CSSStyleDeclaration {
|
pub struct CSSStyleDeclaration {
|
||||||
reflector_: Reflector,
|
reflector_: Reflector,
|
||||||
owner: JS<Element>,
|
owner: CSSStyleOwner,
|
||||||
readonly: bool,
|
readonly: bool,
|
||||||
pseudo: Option<PseudoElement>,
|
pseudo: Option<PseudoElement>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(HeapSizeOf, JSTraceable)]
|
||||||
|
#[must_root]
|
||||||
|
pub enum CSSStyleOwner {
|
||||||
|
Element(JS<Element>),
|
||||||
|
CSSStyleRule(JS<Window>,
|
||||||
|
#[ignore_heap_size_of = "Arc"]
|
||||||
|
Arc<RwLock<PropertyDeclarationBlock>>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CSSStyleOwner {
|
||||||
|
fn style_attribute(&self) -> Option<Arc<RwLock<PropertyDeclarationBlock>>> {
|
||||||
|
match *self {
|
||||||
|
CSSStyleOwner::Element(ref el) => {
|
||||||
|
if let Some(ref pdb) = *el.style_attribute().borrow() {
|
||||||
|
Some(pdb.clone())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CSSStyleOwner::CSSStyleRule(_, ref pdb) => {
|
||||||
|
Some(pdb.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn window(&self) -> Root<Window> {
|
||||||
|
match *self {
|
||||||
|
CSSStyleOwner::Element(ref el) => window_from_node(&**el),
|
||||||
|
CSSStyleOwner::CSSStyleRule(ref window, _) => Root::from_ref(&**window),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush_style(&self, pdb: &PropertyDeclarationBlock) {
|
||||||
|
if let CSSStyleOwner::Element(ref el) = *self {
|
||||||
|
el.set_style_attr(pdb.to_css_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn dirty(&self) {
|
||||||
|
match *self {
|
||||||
|
CSSStyleOwner::Element(ref el) =>
|
||||||
|
el.upcast::<Node>().dirty(NodeDamage::NodeStyleDamaged),
|
||||||
|
CSSStyleOwner::CSSStyleRule(ref window, _) =>
|
||||||
|
window.Document().invalidate_stylesheets(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, HeapSizeOf)]
|
#[derive(PartialEq, HeapSizeOf)]
|
||||||
pub enum CSSModificationAccess {
|
pub enum CSSModificationAccess {
|
||||||
ReadWrite,
|
ReadWrite,
|
||||||
|
@ -49,20 +98,22 @@ macro_rules! css_properties(
|
||||||
);
|
);
|
||||||
|
|
||||||
impl CSSStyleDeclaration {
|
impl CSSStyleDeclaration {
|
||||||
pub fn new_inherited(owner: &Element,
|
#[allow(unrooted_must_root)]
|
||||||
|
pub fn new_inherited(owner: CSSStyleOwner,
|
||||||
pseudo: Option<PseudoElement>,
|
pseudo: Option<PseudoElement>,
|
||||||
modification_access: CSSModificationAccess)
|
modification_access: CSSModificationAccess)
|
||||||
-> CSSStyleDeclaration {
|
-> CSSStyleDeclaration {
|
||||||
CSSStyleDeclaration {
|
CSSStyleDeclaration {
|
||||||
reflector_: Reflector::new(),
|
reflector_: Reflector::new(),
|
||||||
owner: JS::from_ref(owner),
|
owner: owner,
|
||||||
readonly: modification_access == CSSModificationAccess::Readonly,
|
readonly: modification_access == CSSModificationAccess::Readonly,
|
||||||
pseudo: pseudo,
|
pseudo: pseudo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(unrooted_must_root)]
|
||||||
pub fn new(global: &Window,
|
pub fn new(global: &Window,
|
||||||
owner: &Element,
|
owner: CSSStyleOwner,
|
||||||
pseudo: Option<PseudoElement>,
|
pseudo: Option<PseudoElement>,
|
||||||
modification_access: CSSModificationAccess)
|
modification_access: CSSModificationAccess)
|
||||||
-> Root<CSSStyleDeclaration> {
|
-> Root<CSSStyleDeclaration> {
|
||||||
|
@ -74,14 +125,20 @@ impl CSSStyleDeclaration {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_computed_style(&self, property: PropertyId) -> DOMString {
|
fn get_computed_style(&self, property: PropertyId) -> DOMString {
|
||||||
let node = self.owner.upcast::<Node>();
|
match self.owner {
|
||||||
if !node.is_in_doc() {
|
CSSStyleOwner::CSSStyleRule(..) =>
|
||||||
// TODO: Node should be matched against the style rules of this window.
|
panic!("get_computed_style called on CSSStyleDeclaration with a CSSStyleRule owner"),
|
||||||
// Firefox is currently the only browser to implement this.
|
CSSStyleOwner::Element(ref el) => {
|
||||||
return DOMString::new();
|
let node = el.upcast::<Node>();
|
||||||
|
if !node.is_in_doc() {
|
||||||
|
// TODO: Node should be matched against the style rules of this window.
|
||||||
|
// Firefox is currently the only browser to implement this.
|
||||||
|
return DOMString::new();
|
||||||
|
}
|
||||||
|
let addr = node.to_trusted_node_address();
|
||||||
|
window_from_node(node).resolved_style_query(addr, self.pseudo.clone(), property)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let addr = node.to_trusted_node_address();
|
|
||||||
window_from_node(&*self.owner).resolved_style_query(addr, self.pseudo.clone(), property)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_property_value(&self, id: PropertyId) -> DOMString {
|
fn get_property_value(&self, id: PropertyId) -> DOMString {
|
||||||
|
@ -90,17 +147,14 @@ impl CSSStyleDeclaration {
|
||||||
return self.get_computed_style(id);
|
return self.get_computed_style(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
let style_attribute = self.owner.style_attribute().borrow();
|
if let Some(ref lock) = self.owner.style_attribute() {
|
||||||
let style_attribute = if let Some(ref lock) = *style_attribute {
|
let mut string = String::new();
|
||||||
lock.read()
|
lock.read().property_value_to_css(&id, &mut string).unwrap();
|
||||||
|
DOMString::from(string)
|
||||||
} else {
|
} else {
|
||||||
// No style attribute is like an empty style attribute: no matching declaration.
|
// No style attribute is like an empty style attribute: no matching declaration.
|
||||||
return DOMString::new()
|
DOMString::new()
|
||||||
};
|
}
|
||||||
|
|
||||||
let mut string = String::new();
|
|
||||||
style_attribute.property_value_to_css(&id, &mut string).unwrap();
|
|
||||||
DOMString::from(string)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_property(&self, id: PropertyId, value: DOMString, priority: DOMString) -> ErrorResult {
|
fn set_property(&self, id: PropertyId, value: DOMString, priority: DOMString) -> ErrorResult {
|
||||||
|
@ -109,24 +163,20 @@ impl CSSStyleDeclaration {
|
||||||
return Err(Error::NoModificationAllowed);
|
return Err(Error::NoModificationAllowed);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut style_attribute = self.owner.style_attribute().borrow_mut();
|
|
||||||
|
|
||||||
if value.is_empty() {
|
if value.is_empty() {
|
||||||
// Step 4
|
// Step 4
|
||||||
let empty;
|
let empty = {
|
||||||
{
|
if let Some(ref lock) = self.owner.style_attribute() {
|
||||||
let mut style_attribute = if let Some(ref lock) = *style_attribute {
|
let mut style_attribute = lock.write();
|
||||||
lock.write()
|
style_attribute.remove_property(&id);
|
||||||
|
style_attribute.declarations.is_empty()
|
||||||
} else {
|
} else {
|
||||||
// No style attribute is like an empty style attribute: nothing to remove.
|
// No style attribute is like an empty style attribute: nothing to remove.
|
||||||
return Ok(())
|
return Ok(())
|
||||||
};
|
}
|
||||||
|
};
|
||||||
style_attribute.remove_property(&id);
|
if let (&CSSStyleOwner::Element(ref el), true) = (&self.owner, empty) {
|
||||||
empty = style_attribute.declarations.is_empty()
|
*el.style_attribute().borrow_mut() = None;
|
||||||
}
|
|
||||||
if empty {
|
|
||||||
*style_attribute = None;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Step 5
|
// Step 5
|
||||||
|
@ -137,29 +187,28 @@ impl CSSStyleDeclaration {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Step 6
|
// Step 6
|
||||||
let window = window_from_node(&*self.owner);
|
let window = self.owner.window();
|
||||||
let declarations =
|
let declarations =
|
||||||
parse_one_declaration(id, &value, &window.get_url(), window.css_error_reporter(),
|
parse_one_declaration(id, &value, &window.get_url(), window.css_error_reporter(),
|
||||||
ParserContextExtraData::default());
|
ParserContextExtraData::default());
|
||||||
|
|
||||||
// Step 7
|
// Step 7
|
||||||
let declarations = if let Ok(declarations) = declarations {
|
let declarations = match declarations {
|
||||||
declarations
|
Ok(declarations) => declarations,
|
||||||
} else {
|
Err(_) => return Ok(())
|
||||||
return Ok(());
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Step 8
|
// Step 8
|
||||||
// Step 9
|
// Step 9
|
||||||
match *style_attribute {
|
match self.owner.style_attribute() {
|
||||||
Some(ref lock) => {
|
Some(ref lock) => {
|
||||||
let mut style_attribute = lock.write();
|
let mut style_attribute = lock.write();
|
||||||
for declaration in declarations {
|
for declaration in declarations {
|
||||||
style_attribute.set_parsed_declaration(declaration, importance);
|
style_attribute.set_parsed_declaration(declaration, importance);
|
||||||
}
|
}
|
||||||
self.owner.set_style_attr(style_attribute.to_css_string());
|
self.owner.flush_style(&style_attribute);
|
||||||
}
|
}
|
||||||
ref mut option @ None => {
|
None => {
|
||||||
let important_count = if importance.important() {
|
let important_count = if importance.important() {
|
||||||
declarations.len() as u32
|
declarations.len() as u32
|
||||||
} else {
|
} else {
|
||||||
|
@ -169,14 +218,17 @@ impl CSSStyleDeclaration {
|
||||||
declarations: declarations.into_iter().map(|d| (d, importance)).collect(),
|
declarations: declarations.into_iter().map(|d| (d, importance)).collect(),
|
||||||
important_count: important_count,
|
important_count: important_count,
|
||||||
};
|
};
|
||||||
self.owner.set_style_attr(block.to_css_string());
|
if let CSSStyleOwner::Element(ref el) = self.owner {
|
||||||
*option = Some(Arc::new(RwLock::new(block)));
|
el.set_style_attr(block.to_css_string());
|
||||||
|
*el.style_attribute().borrow_mut() = Some(Arc::new(RwLock::new(block)));
|
||||||
|
} else {
|
||||||
|
panic!("set_property called on a CSSStyleDeclaration with a non-Element owner");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let node = self.owner.upcast::<Node>();
|
self.owner.dirty();
|
||||||
node.dirty(NodeDamage::NodeStyleDamaged);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -184,12 +236,7 @@ impl CSSStyleDeclaration {
|
||||||
impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
|
impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
|
||||||
// https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-length
|
// https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-length
|
||||||
fn Length(&self) -> u32 {
|
fn Length(&self) -> u32 {
|
||||||
let elem = self.owner.upcast::<Element>();
|
self.owner.style_attribute().as_ref().map_or(0, |lock| lock.read().declarations.len() as u32)
|
||||||
let len = match *elem.style_attribute().borrow() {
|
|
||||||
Some(ref lock) => lock.read().declarations.len(),
|
|
||||||
None => 0,
|
|
||||||
};
|
|
||||||
len as u32
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-item
|
// https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-item
|
||||||
|
@ -217,18 +264,15 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
|
||||||
return DOMString::new()
|
return DOMString::new()
|
||||||
};
|
};
|
||||||
|
|
||||||
let style_attribute = self.owner.style_attribute().borrow();
|
if let Some(ref lock) = self.owner.style_attribute() {
|
||||||
let style_attribute = if let Some(ref lock) = *style_attribute {
|
if lock.read().property_priority(&id).important() {
|
||||||
lock.read()
|
DOMString::from("important")
|
||||||
|
} else {
|
||||||
|
// Step 4
|
||||||
|
DOMString::new()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// No style attribute is like an empty style attribute: no matching declaration.
|
// No style attribute is like an empty style attribute: no matching declaration.
|
||||||
return DOMString::new()
|
|
||||||
};
|
|
||||||
|
|
||||||
if style_attribute.property_priority(&id).important() {
|
|
||||||
DOMString::from("important")
|
|
||||||
} else {
|
|
||||||
// Step 4
|
|
||||||
DOMString::new()
|
DOMString::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -243,7 +287,7 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
|
||||||
let id = if let Ok(id) = PropertyId::parse(property.into()) {
|
let id = if let Ok(id) = PropertyId::parse(property.into()) {
|
||||||
id
|
id
|
||||||
} else {
|
} else {
|
||||||
// Unkwown property
|
// Unknown property
|
||||||
return Ok(())
|
return Ok(())
|
||||||
};
|
};
|
||||||
self.set_property(id, value, priority)
|
self.set_property(id, value, priority)
|
||||||
|
@ -271,16 +315,14 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
|
||||||
_ => return Ok(()),
|
_ => return Ok(()),
|
||||||
};
|
};
|
||||||
|
|
||||||
let style_attribute = self.owner.style_attribute().borrow();
|
if let Some(ref lock) = self.owner.style_attribute() {
|
||||||
if let Some(ref lock) = *style_attribute {
|
|
||||||
let mut style_attribute = lock.write();
|
let mut style_attribute = lock.write();
|
||||||
|
|
||||||
// Step 5 & 6
|
// Step 5 & 6
|
||||||
style_attribute.set_importance(&id, importance);
|
style_attribute.set_importance(&id, importance);
|
||||||
|
|
||||||
self.owner.set_style_attr(style_attribute.to_css_string());
|
self.owner.flush_style(&style_attribute);
|
||||||
let node = self.owner.upcast::<Node>();
|
self.owner.dirty();
|
||||||
node.dirty(NodeDamage::NodeStyleDamaged);
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -304,31 +346,26 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
|
||||||
return Ok(DOMString::new())
|
return Ok(DOMString::new())
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut style_attribute = self.owner.style_attribute().borrow_mut();
|
|
||||||
let mut string = String::new();
|
let mut string = String::new();
|
||||||
let empty;
|
let empty = {
|
||||||
{
|
if let Some(ref lock) = self.owner.style_attribute() {
|
||||||
let mut style_attribute = if let Some(ref lock) = *style_attribute {
|
let mut style_attribute = lock.write();
|
||||||
lock.write()
|
// Step 3
|
||||||
|
style_attribute.property_value_to_css(&id, &mut string).unwrap();
|
||||||
|
|
||||||
|
// Step 4 & 5
|
||||||
|
style_attribute.remove_property(&id);
|
||||||
|
self.owner.flush_style(&style_attribute);
|
||||||
|
style_attribute.declarations.is_empty()
|
||||||
} else {
|
} else {
|
||||||
// No style attribute is like an empty style attribute: nothing to remove.
|
// No style attribute is like an empty style attribute: nothing to remove.
|
||||||
return Ok(DOMString::new())
|
return Ok(DOMString::new())
|
||||||
};
|
}
|
||||||
|
};
|
||||||
// Step 3
|
if let (&CSSStyleOwner::Element(ref el), true) = (&self.owner, empty) {
|
||||||
style_attribute.property_value_to_css(&id, &mut string).unwrap();
|
*el.style_attribute().borrow_mut() = None;
|
||||||
|
|
||||||
// Step 4 & 5
|
|
||||||
style_attribute.remove_property(&id);
|
|
||||||
self.owner.set_style_attr(style_attribute.to_css_string());
|
|
||||||
empty = style_attribute.declarations.is_empty()
|
|
||||||
}
|
}
|
||||||
if empty {
|
self.owner.dirty();
|
||||||
*style_attribute = None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let node = self.owner.upcast::<Node>();
|
|
||||||
node.dirty(NodeDamage::NodeStyleDamaged);
|
|
||||||
|
|
||||||
// Step 6
|
// Step 6
|
||||||
Ok(DOMString::from(string))
|
Ok(DOMString::from(string))
|
||||||
|
@ -346,11 +383,8 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
|
||||||
|
|
||||||
// https://dev.w3.org/csswg/cssom/#the-cssstyledeclaration-interface
|
// https://dev.w3.org/csswg/cssom/#the-cssstyledeclaration-interface
|
||||||
fn IndexedGetter(&self, index: u32) -> Option<DOMString> {
|
fn IndexedGetter(&self, index: u32) -> Option<DOMString> {
|
||||||
let index = index as usize;
|
self.owner.style_attribute().as_ref().and_then(|lock| {
|
||||||
let elem = self.owner.upcast::<Element>();
|
lock.read().declarations.get(index as usize).map(|entry| {
|
||||||
let style_attribute = elem.style_attribute().borrow();
|
|
||||||
style_attribute.as_ref().and_then(|lock| {
|
|
||||||
lock.read().declarations.get(index).map(|entry| {
|
|
||||||
let (ref declaration, importance) = *entry;
|
let (ref declaration, importance) = *entry;
|
||||||
let mut css = declaration.to_css_string();
|
let mut css = declaration.to_css_string();
|
||||||
if importance.important() {
|
if importance.important() {
|
||||||
|
@ -363,19 +397,13 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
|
||||||
|
|
||||||
// https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext
|
// https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext
|
||||||
fn CssText(&self) -> DOMString {
|
fn CssText(&self) -> DOMString {
|
||||||
let elem = self.owner.upcast::<Element>();
|
self.owner.style_attribute().as_ref().map_or(DOMString::new(), |lock|
|
||||||
let style_attribute = elem.style_attribute().borrow();
|
DOMString::from(lock.read().to_css_string()))
|
||||||
|
|
||||||
if let Some(lock) = style_attribute.as_ref() {
|
|
||||||
DOMString::from(lock.read().to_css_string())
|
|
||||||
} else {
|
|
||||||
DOMString::new()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext
|
// https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext
|
||||||
fn SetCssText(&self, value: DOMString) -> ErrorResult {
|
fn SetCssText(&self, value: DOMString) -> ErrorResult {
|
||||||
let window = window_from_node(self.owner.upcast::<Node>());
|
let window = self.owner.window();
|
||||||
|
|
||||||
// Step 1
|
// Step 1
|
||||||
if self.readonly {
|
if self.readonly {
|
||||||
|
@ -385,15 +413,16 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
|
||||||
// Step 3
|
// Step 3
|
||||||
let decl_block = parse_style_attribute(&value, &window.get_url(), window.css_error_reporter(),
|
let decl_block = parse_style_attribute(&value, &window.get_url(), window.css_error_reporter(),
|
||||||
ParserContextExtraData::default());
|
ParserContextExtraData::default());
|
||||||
*self.owner.style_attribute().borrow_mut() = if decl_block.declarations.is_empty() {
|
if let CSSStyleOwner::Element(ref el) = self.owner {
|
||||||
self.owner.set_style_attr(String::new());
|
*el.style_attribute().borrow_mut() = if decl_block.declarations.is_empty() {
|
||||||
None // Step 2
|
el.set_style_attr(String::new());
|
||||||
} else {
|
None // Step 2
|
||||||
self.owner.set_style_attr(decl_block.to_css_string());
|
} else {
|
||||||
Some(Arc::new(RwLock::new(decl_block)))
|
el.set_style_attr(decl_block.to_css_string());
|
||||||
};
|
Some(Arc::new(RwLock::new(decl_block)))
|
||||||
let node = self.owner.upcast::<Node>();
|
};
|
||||||
node.dirty(NodeDamage::NodeStyleDamaged);
|
}
|
||||||
|
self.owner.dirty();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,11 +2,12 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use dom::bindings::codegen::Bindings::CSSStyleRuleBinding;
|
use dom::bindings::codegen::Bindings::CSSStyleRuleBinding::{self, CSSStyleRuleMethods};
|
||||||
use dom::bindings::js::Root;
|
use dom::bindings::js::{JS, MutNullableJS, Root};
|
||||||
use dom::bindings::reflector::reflect_dom_object;
|
use dom::bindings::reflector::{DomObject, reflect_dom_object};
|
||||||
use dom::bindings::str::DOMString;
|
use dom::bindings::str::DOMString;
|
||||||
use dom::cssrule::{CSSRule, SpecificCSSRule};
|
use dom::cssrule::{CSSRule, SpecificCSSRule};
|
||||||
|
use dom::cssstyledeclaration::{CSSModificationAccess, CSSStyleDeclaration, CSSStyleOwner};
|
||||||
use dom::cssstylesheet::CSSStyleSheet;
|
use dom::cssstylesheet::CSSStyleSheet;
|
||||||
use dom::window::Window;
|
use dom::window::Window;
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
|
@ -19,6 +20,7 @@ pub struct CSSStyleRule {
|
||||||
cssrule: CSSRule,
|
cssrule: CSSRule,
|
||||||
#[ignore_heap_size_of = "Arc"]
|
#[ignore_heap_size_of = "Arc"]
|
||||||
stylerule: Arc<RwLock<StyleRule>>,
|
stylerule: Arc<RwLock<StyleRule>>,
|
||||||
|
style_decl: MutNullableJS<CSSStyleDeclaration>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CSSStyleRule {
|
impl CSSStyleRule {
|
||||||
|
@ -27,6 +29,7 @@ impl CSSStyleRule {
|
||||||
CSSStyleRule {
|
CSSStyleRule {
|
||||||
cssrule: CSSRule::new_inherited(parent_stylesheet),
|
cssrule: CSSRule::new_inherited(parent_stylesheet),
|
||||||
stylerule: stylerule,
|
stylerule: stylerule,
|
||||||
|
style_decl: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,6 +40,10 @@ impl CSSStyleRule {
|
||||||
window,
|
window,
|
||||||
CSSStyleRuleBinding::Wrap)
|
CSSStyleRuleBinding::Wrap)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn style_rule(&self) -> Arc<RwLock<StyleRule>> {
|
||||||
|
self.stylerule.clone()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SpecificCSSRule for CSSStyleRule {
|
impl SpecificCSSRule for CSSStyleRule {
|
||||||
|
@ -49,3 +56,16 @@ impl SpecificCSSRule for CSSStyleRule {
|
||||||
self.stylerule.read().to_css_string().into()
|
self.stylerule.read().to_css_string().into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl CSSStyleRuleMethods for CSSStyleRule {
|
||||||
|
// https://drafts.csswg.org/cssom/#dom-cssstylerule-style
|
||||||
|
fn Style(&self) -> Root<CSSStyleDeclaration> {
|
||||||
|
self.style_decl.or_init(|| {
|
||||||
|
CSSStyleDeclaration::new(self.global().as_window(),
|
||||||
|
CSSStyleOwner::CSSStyleRule(JS::from_ref(self.global().as_window()),
|
||||||
|
self.stylerule.read().block.clone()),
|
||||||
|
None,
|
||||||
|
CSSModificationAccess::ReadWrite)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -13,9 +13,9 @@ use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
|
||||||
use dom::bindings::error::{Error, ErrorResult};
|
use dom::bindings::error::{Error, ErrorResult};
|
||||||
use dom::bindings::inheritance::{ElementTypeId, HTMLElementTypeId, NodeTypeId};
|
use dom::bindings::inheritance::{ElementTypeId, HTMLElementTypeId, NodeTypeId};
|
||||||
use dom::bindings::inheritance::Castable;
|
use dom::bindings::inheritance::Castable;
|
||||||
use dom::bindings::js::{MutNullableJS, Root, RootedReference};
|
use dom::bindings::js::{JS, MutNullableJS, Root, RootedReference};
|
||||||
use dom::bindings::str::DOMString;
|
use dom::bindings::str::DOMString;
|
||||||
use dom::cssstyledeclaration::{CSSModificationAccess, CSSStyleDeclaration};
|
use dom::cssstyledeclaration::{CSSModificationAccess, CSSStyleDeclaration, CSSStyleOwner};
|
||||||
use dom::document::{Document, FocusType};
|
use dom::document::{Document, FocusType};
|
||||||
use dom::domstringmap::DOMStringMap;
|
use dom::domstringmap::DOMStringMap;
|
||||||
use dom::element::{AttributeMutation, Element};
|
use dom::element::{AttributeMutation, Element};
|
||||||
|
@ -115,7 +115,10 @@ impl HTMLElementMethods for HTMLElement {
|
||||||
fn Style(&self) -> Root<CSSStyleDeclaration> {
|
fn Style(&self) -> Root<CSSStyleDeclaration> {
|
||||||
self.style_decl.or_init(|| {
|
self.style_decl.or_init(|| {
|
||||||
let global = window_from_node(self);
|
let global = window_from_node(self);
|
||||||
CSSStyleDeclaration::new(&global, self.upcast::<Element>(), None, CSSModificationAccess::ReadWrite)
|
CSSStyleDeclaration::new(&global,
|
||||||
|
CSSStyleOwner::Element(JS::from_ref(self.upcast())),
|
||||||
|
None,
|
||||||
|
CSSModificationAccess::ReadWrite)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,5 +6,5 @@
|
||||||
[Exposed=Window]
|
[Exposed=Window]
|
||||||
interface CSSStyleRule : CSSRule {
|
interface CSSStyleRule : CSSRule {
|
||||||
// attribute DOMString selectorText;
|
// attribute DOMString selectorText;
|
||||||
// [SameObject, PutForwards=cssText] readonly attribute CSSStyleDeclaration style;
|
[SameObject, PutForwards=cssText] readonly attribute CSSStyleDeclaration style;
|
||||||
};
|
};
|
||||||
|
|
|
@ -19,7 +19,7 @@ use dom::bindings::codegen::Bindings::WindowBinding::{ScrollBehavior, ScrollToOp
|
||||||
use dom::bindings::codegen::UnionTypes::RequestOrUSVString;
|
use dom::bindings::codegen::UnionTypes::RequestOrUSVString;
|
||||||
use dom::bindings::error::{Error, ErrorResult, Fallible};
|
use dom::bindings::error::{Error, ErrorResult, Fallible};
|
||||||
use dom::bindings::inheritance::Castable;
|
use dom::bindings::inheritance::Castable;
|
||||||
use dom::bindings::js::{MutNullableJS, Root};
|
use dom::bindings::js::{JS, MutNullableJS, Root};
|
||||||
use dom::bindings::num::Finite;
|
use dom::bindings::num::Finite;
|
||||||
use dom::bindings::refcounted::Trusted;
|
use dom::bindings::refcounted::Trusted;
|
||||||
use dom::bindings::reflector::DomObject;
|
use dom::bindings::reflector::DomObject;
|
||||||
|
@ -28,7 +28,7 @@ use dom::bindings::structuredclone::StructuredCloneData;
|
||||||
use dom::bindings::utils::{GlobalStaticData, WindowProxyHandler};
|
use dom::bindings::utils::{GlobalStaticData, WindowProxyHandler};
|
||||||
use dom::browsingcontext::BrowsingContext;
|
use dom::browsingcontext::BrowsingContext;
|
||||||
use dom::crypto::Crypto;
|
use dom::crypto::Crypto;
|
||||||
use dom::cssstyledeclaration::{CSSModificationAccess, CSSStyleDeclaration};
|
use dom::cssstyledeclaration::{CSSModificationAccess, CSSStyleDeclaration, CSSStyleOwner};
|
||||||
use dom::document::{AnimationFrameCallback, Document};
|
use dom::document::{AnimationFrameCallback, Document};
|
||||||
use dom::element::Element;
|
use dom::element::Element;
|
||||||
use dom::event::Event;
|
use dom::event::Event;
|
||||||
|
@ -701,7 +701,10 @@ impl WindowMethods for Window {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Step 5.
|
// Step 5.
|
||||||
CSSStyleDeclaration::new(self, element, pseudo, CSSModificationAccess::Readonly)
|
CSSStyleDeclaration::new(self,
|
||||||
|
CSSStyleOwner::Element(JS::from_ref(element)),
|
||||||
|
pseudo,
|
||||||
|
CSSModificationAccess::Readonly)
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://drafts.csswg.org/cssom-view/#dom-window-innerheight
|
// https://drafts.csswg.org/cssom-view/#dom-window-innerheight
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
[cssstyledeclaration-mutability.htm]
|
|
||||||
type: testharness
|
|
||||||
[StyleSheet's CSSStyleDeclaration is mutable]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
|
@ -120,15 +120,9 @@
|
||||||
[CSSStyleRule interface: attribute selectorText]
|
[CSSStyleRule interface: attribute selectorText]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[CSSStyleRule interface: attribute style]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[CSSStyleRule interface: style_element.sheet.cssRules[0\] must inherit property "selectorText" with the proper type (0)]
|
[CSSStyleRule interface: style_element.sheet.cssRules[0\] must inherit property "selectorText" with the proper type (0)]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[CSSStyleRule interface: style_element.sheet.cssRules[0\] must inherit property "style" with the proper type (1)]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[CSSRule interface: style_element.sheet.cssRules[0\] must inherit property "parentRule" with the proper type (10)]
|
[CSSRule interface: style_element.sheet.cssRules[0\] must inherit property "parentRule" with the proper type (10)]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
|
|
|
@ -39695,6 +39695,12 @@
|
||||||
"url": "/cssom/CSSRuleList.html"
|
"url": "/cssom/CSSRuleList.html"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"cssom/CSSStyleRule.html": [
|
||||||
|
{
|
||||||
|
"path": "cssom/CSSStyleRule.html",
|
||||||
|
"url": "/cssom/CSSStyleRule.html"
|
||||||
|
}
|
||||||
|
],
|
||||||
"cssom/CSSStyleSheet.html": [
|
"cssom/CSSStyleSheet.html": [
|
||||||
{
|
{
|
||||||
"path": "cssom/CSSStyleSheet.html",
|
"path": "cssom/CSSStyleSheet.html",
|
||||||
|
|
24
tests/wpt/web-platform-tests/cssom/CSSStyleRule.html
Normal file
24
tests/wpt/web-platform-tests/cssom/CSSStyleRule.html
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
<!doctype html>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title></title>
|
||||||
|
<script src="/resources/testharness.js"></script>
|
||||||
|
<script src="/resources/testharnessreport.js"></script>
|
||||||
|
<style type="text/css" id="styleElement">
|
||||||
|
div { margin: 10px; padding: 0px; }
|
||||||
|
</style>
|
||||||
|
<script>
|
||||||
|
var styleSheet = document.getElementById("styleElement").sheet;
|
||||||
|
var rule = styleSheet.cssRules[0];
|
||||||
|
|
||||||
|
test(function() {
|
||||||
|
assert_equals(typeof rule.style, "object");
|
||||||
|
assert_equals(rule.style.margin, "10px");
|
||||||
|
assert_equals(rule.style.padding, "0px");
|
||||||
|
|
||||||
|
rule.style.padding = "5px";
|
||||||
|
rule.style.border = "1px solid";
|
||||||
|
|
||||||
|
assert_equals(rule.style.padding, "5px");
|
||||||
|
assert_equals(rule.style.border, "1px solid");
|
||||||
|
}, "CSSStyleRule: style property");
|
||||||
|
</script>
|
Loading…
Add table
Add a link
Reference in a new issue