1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
/* 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 dom::bindings::codegen::Bindings::CSSStyleDeclarationBinding::CSSStyleDeclarationMethods;
use dom::bindings::codegen::InheritTypes::{NodeCast, ElementCast};
use dom::bindings::error::ErrorResult;
use dom::bindings::js::{JS, JSRef, OptionalRootedRootable};
use dom::bindings::utils::{Reflectable, Reflector};
use dom::document::DocumentHelpers;
use dom::element::{Element, ElementHelpers};
use dom::node::window_from_node;
use dom::htmlelement::HTMLElement;
use dom::node::{document_from_node, NodeDamage, Node};
use servo_util::str::DOMString;
use string_cache::Atom;
use style::{is_supported_property, longhands_from_shorthand, parse_style_attribute};
use style::PropertyDeclaration;
use std::ascii::AsciiExt;
#[dom_struct]
pub struct CSSStyleDeclaration {
reflector_: Reflector,
owner: JS<HTMLElement>,
}
fn serialize_list(list: &Vec<PropertyDeclaration>) -> DOMString {
let mut result = String::new();
for declaration in list.iter() {
result.push_str(serialize_value(declaration).as_slice());
result.push_str(" ");
}
result
}
fn serialize_value(declaration: &PropertyDeclaration) -> DOMString {
declaration.value()
}
impl CSSStyleDeclaration {
pub fn new_inherited(owner: JSRef<HTMLElement>) -> CSSStyleDeclaration {
CSSStyleDeclaration {
reflector_: Reflector::new(),
owner: JS::from_rooted(owner),
}
}
}
trait PrivateCSSStyleDeclarationHelpers {
fn get_declaration(self, property: &Atom) -> Option<PropertyDeclaration>;
}
impl<'a> PrivateCSSStyleDeclarationHelpers for JSRef<'a, CSSStyleDeclaration> {
fn get_declaration(self, property: &Atom) -> Option<PropertyDeclaration> {
let owner = self.owner.root();
let element: JSRef<Element> = ElementCast::from_ref(*owner);
element.get_inline_style_declaration(property).map(|decl| decl.clone())
}
}
impl<'a> CSSStyleDeclarationMethods for JSRef<'a, CSSStyleDeclaration> {
fn Length(self) -> u32 {
let owner = self.owner.root();
let elem: JSRef<Element> = ElementCast::from_ref(*owner);
let len = match *elem.style_attribute().borrow() {
Some(ref declarations) => declarations.normal.len() + declarations.important.len(),
None => 0
};
len as u32
}
fn Item(self, index: u32) -> DOMString {
let owner = self.owner.root();
let elem: JSRef<Element> = ElementCast::from_ref(*owner);
let style_attribute = elem.style_attribute().borrow();
let result = style_attribute.as_ref().and_then(|declarations| {
if index as uint > declarations.normal.len() {
declarations.important
.get(index as uint - declarations.normal.len())
.map(|decl| format!("{} !important", decl))
} else {
declarations.normal
.get(index as uint)
.map(|decl| format!("{}", decl))
}
});
result.unwrap_or("".to_string())
}
// http://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-getpropertyvalue
fn GetPropertyValue(self, property: DOMString) -> DOMString {
// Step 1
let property = Atom::from_slice(property.as_slice().to_ascii_lower().as_slice());
// Step 2
let longhand_properties = longhands_from_shorthand(property.as_slice());
if let Some(longhand_properties) = longhand_properties {
// Step 2.1
let mut list = vec!();
// Step 2.2
for longhand in longhand_properties.iter() {
// Step 2.2.1
let declaration = self.get_declaration(&Atom::from_slice(longhand.as_slice()));
// Step 2.2.2 & 2.2.3
match declaration {
Some(declaration) => list.push(declaration),
None => return "".to_string(),
}
}
// Step 2.3
return serialize_list(&list);
}
// Step 3 & 4
if let Some(ref declaration) = self.get_declaration(&property) {
serialize_value(declaration)
} else {
"".to_string()
}
}
// http://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-setproperty
fn SetProperty(self, property: DOMString, value: DOMString,
priority: DOMString) -> ErrorResult {
//TODO: disallow modifications if readonly flag is set
// Step 2
let property = property.as_slice().to_ascii_lower();
// Step 3
if !is_supported_property(property.as_slice()) {
return Ok(());
}
// Step 4
if value.is_empty() {
self.RemoveProperty(property);
return Ok(());
}
// Step 5
let priority = priority.as_slice().to_ascii_lower();
if priority.as_slice() != "!important" && !priority.is_empty() {
return Ok(());
}
// Step 6
let mut synthesized_declaration = String::from_str(property.as_slice());
synthesized_declaration.push_str(": ");
synthesized_declaration.push_str(value.as_slice());
let owner = self.owner.root();
let window = window_from_node(*owner).root();
let page = window.page();
let decl_block = parse_style_attribute(synthesized_declaration.as_slice(),
&page.get_url());
// Step 7
if decl_block.normal.len() == 0 {
return Ok(());
}
let owner = self.owner.root();
let element: JSRef<Element> = ElementCast::from_ref(*owner);
// Step 8
for decl in decl_block.normal.iter() {
// Step 9
element.update_inline_style(decl.clone(), !priority.is_empty());
}
let document = document_from_node(element).root();
let node: JSRef<Node> = NodeCast::from_ref(element);
document.content_changed(node, NodeDamage::NodeStyleDamaged);
Ok(())
}
// http://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-setpropertyvalue
fn SetPropertyValue(self, property: DOMString, value: DOMString) -> ErrorResult {
self.SetProperty(property, value, "".to_string())
}
// http://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-removeproperty
fn RemoveProperty(self, property: DOMString) -> DOMString {
//TODO: disallow modifications if readonly flag is set
// Step 2
let property = property.as_slice().to_ascii_lower();
// Step 3
let value = self.GetPropertyValue(property.clone());
let longhand_properties = longhands_from_shorthand(property.as_slice());
match longhand_properties {
Some(longhands) => {
// Step 4
for longhand in longhands.iter() {
self.RemoveProperty(longhand.clone());
}
}
None => {
// Step 5
let owner = self.owner.root();
let elem: JSRef<Element> = ElementCast::from_ref(*owner);
elem.remove_inline_style_property(property)
}
}
// Step 6
value
}
// http://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-cssfloat
fn CssFloat(self) -> DOMString {
self.GetPropertyValue("float".to_string())
}
// http://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-cssfloat
fn SetCssFloat(self, value: DOMString) -> ErrorResult {
self.SetPropertyValue("float".to_string(), value)
}
fn IndexedGetter(self, index: u32, found: &mut bool) -> DOMString {
let rval = self.Item(index);
*found = index < self.Length();
rval
}
}
impl Reflectable for CSSStyleDeclaration {
fn reflector<'a>(&'a self) -> &'a Reflector {
&self.reflector_
}
}
|