-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJStyleSheetWrapper.java
More file actions
315 lines (288 loc) · 12.5 KB
/
Copy pathJStyleSheetWrapper.java
File metadata and controls
315 lines (288 loc) · 12.5 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
Copyright 2014 Uproot Labs India Pvt Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package co.uproot.css.domimpl;
import org.w3c.dom.DOMException;
import org.w3c.dom.Node;
import org.w3c.dom.css.CSSRule;
import org.w3c.dom.css.CSSRuleList;
import org.w3c.dom.css.CSSStyleSheet;
import org.w3c.dom.stylesheets.MediaList;
import org.w3c.dom.stylesheets.StyleSheet;
import org.w3c.dom.stylesheets.StyleSheetList;
/**
* This is a wrapper for CSS DOM API implementation.
*
* Acts as a base Class for <code>CSSNodeStyleSheet</code> and
* <code>CSSRuleStyleSheet</code>.
*
* This class implements the common methods between the two class mentioned.
*
*/
public final class JStyleSheetWrapper implements CSSStyleSheet {
private volatile cz.vutbr.web.css.StyleSheet jStyleSheet;
private final CSSStyleSheet parentStyleSheet;
final private String mediaStr;
final private StyleSheetBridge bridge;
private volatile boolean disabled;
final private String href;
private Node ownerNode;
final private String type;
final private String title;
JStyleSheetWrapper(final cz.vutbr.web.css.StyleSheet jStyleSheet, final String mediaStr, final String href, final Node ownerNode,
final CSSStyleSheet parentStyleSheet, final String type, final String title, final StyleSheetBridge bridge) {
this.jStyleSheet = jStyleSheet;
this.mediaStr = mediaStr;
this.href = href;
this.bridge = bridge;
this.ownerNode = ownerNode;
this.type = type;
this.title = title;
this.parentStyleSheet = parentStyleSheet;
}
/**
* @param jStyleSheet
* parsed style sheet from jStyleParser
* @param mediaStr
* The intended destination media for style information. The media is
* often specified in the <code>ownerNode</code>. If no media has
* been specified, the <code>MediaList</code> will be empty. See the
* media attribute definition for the <code>LINK</code> element in
* HTML 4.0, and the media pseudo-attribute for the XML style sheet
* processing instruction . Modifying the media list may cause a
* change to the attribute <code>disabled</code>.
* @param href
* If the style sheet is a linked style sheet, the value of its
* attribute is its location. For inline style sheets, the value of
* this attribute is <code>null</code>. See the href attribute
* definition for the <code>LINK</code> element in HTML 4.0, and the
* href pseudo-attribute for the XML style sheet processing
* instruction.
* @param parentStyleSheet
* For style sheet languages that support the concept of style sheet
* inclusion, this attribute represents the including style sheet, if
* one exists. If the style sheet is a top-level style sheet, or the
* style sheet language does not support inclusion, the value of this
* attribute is <code>null</code>.
* @param bridge
* callback to notify any changes in the style sheet or to
* dynamically get data from the caller.
*/
public JStyleSheetWrapper(final cz.vutbr.web.css.StyleSheet jStyleSheet, final String mediaStr, final String href,
final CSSStyleSheet parentStyleSheet, final StyleSheetBridge bridge) {
this(jStyleSheet, mediaStr, href, null, parentStyleSheet, null, null, bridge);
}
/**
* @param jStyleSheet
* parsed style sheet from jStyleParser
* @param mediaStr
* The intended destination media for style information. The media is
* often specified in the <code>ownerNode</code>. If no media has
* been specified, the <code>MediaList</code> will be empty. See the
* media attribute definition for the <code>LINK</code> element in
* HTML 4.0, and the media pseudo-attribute for the XML style sheet
* processing instruction . Modifying the media list may cause a
* change to the attribute <code>disabled</code>.
* @param href
* If the style sheet is a linked style sheet, the value of its
* attribute is its location. For inline style sheets, the value of
* this attribute is <code>null</code>. See the href attribute
* definition for the <code>LINK</code> element in HTML 4.0, and the
* href pseudo-attribute for the XML style sheet processing
* instruction.
* @param type
* the type of the style sheet. e.g. "text/css"
* @param title
* The advisory title. The title is often specified in the
* <code>ownerNode</code>. See the title attribute definition for the
* <code>LINK</code> element in HTML 4.0, and the title
* pseudo-attribute for the XML style sheet processing instruction.
* @param ownerNode
* The node that associates this style sheet with the document. For
* HTML, this may be the corresponding <code>LINK</code> or
* <code>STYLE</code> element. For XML, it may be the linking
* processing instruction. For style sheets that are included by
* other style sheets, the value of this attribute is
* <code>null</code>.
* @param bridge
* callback to notify any changes in the style sheet or to
* dynamically get data from the caller.
*/
public JStyleSheetWrapper(final cz.vutbr.web.css.StyleSheet jStyleSheet, final String mediaStr, final String href, final String type,
final String title, final Node ownerNode, final StyleSheetBridge bridge) {
this(jStyleSheet, mediaStr, href, ownerNode, null, type, title, bridge);
}
/**
* @return The state of the style sheet <code>true</code> if the style sheet
* is disabled, <code>false</code> otherwise
*/
public boolean getDisabled() {
return this.disabled;
}
/**
* This will enable/disable the style sheet and also send a notification to
* all the listeners about this change in the style sheet.
*
* @param disabled
* state of the style sheet <code>true</code> if the style sheet is
* disabled, <code>false</code> otherwise
*/
public void setDisabled(final boolean disabled) {
this.disabled = disabled;
this.informChanged();
}
/**
* @return If the style sheet is a linked style sheet, the value returned is
* its location. For inline style sheets, the value returned will be
* <code>null</code>. For <code>@import</code> rules the returned
* value will be the url specified in the rule.
*/
public String getHref() {
return this.href;
}
/**
* @return A list of media to which this style sheet is applicable
*/
public MediaList getMedia() {
return new MediaListImpl(mediaStr, this);
}
/**
* @return style sheet language for this style sheet. <code>null</code> for
* <code>@import</code> rule as it does not have a type associated
* with it.
*/
public String getType() {
return this.type;
}
/**
* @return The node that associates this style sheet with the document. For
* HTML, this may be the corresponding <code>LINK</code> or
* <code>STYLE</code> element. For XML, it may be the linking
* processing instruction. <code>null</code> for <code>@import</code>
* as the <code>@import</code> rule is not associated any
* <code>Node</code>.
*/
public Node getOwnerNode() {
return this.ownerNode;
}
// TODO hide it from JS
public void setOwnerNode(final Node ownerNode) {
this.ownerNode = ownerNode;
}
/**
* @return The containing <code>Style Sheet</code>, applicable only for
* <code>@import</code> rules. <code>null</code> for nodes as the
* style sheet is a top-level style sheet, either from
* <code>LINK</code> or <code>STYLE</code> element.
*/
public StyleSheet getParentStyleSheet() {
return this.parentStyleSheet;
}
/**
* @return The advisory title. The title specified in the
* <code>ownerNode</code>. e.g. the title attribute of the
* <code>LINK</code> element in HTML 4.0, and the title
* pseudo-attribute for the XML style sheet processing instruction.
* <code>null</code> for Import rules as the <code>@import</code> rule
* does not have a title associated with it.
*/
public String getTitle() {
return this.title;
}
public CSSRule getOwnerRule() {
// TODO Import Rules are not yet supported, once supported this method should return
// the @import rule which gets this style sheet
// null for other cases
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "this operation is not supported");
}
/**
* @return A list of rules contained in this style sheet.
*/
public CSSRuleList getCssRules() {
if (this.jStyleSheet != null) {
return new CSSRuleListImpl(jStyleSheet, this);
}
throw new DOMException(DOMException.INVALID_ACCESS_ERR, "A parameter or an operation is not supported by the underlying object");
}
/**
* Used to insert a new rule into the style sheet. The new rule now becomes
* part of the cascade.
*
* @param rule
* The parsable text representing the rule. For rule sets this
* contains both the selector and the style declaration. For
* at-rules, this specifies both the at-identifier and the rule
* content.
* @param index
* The index within the style sheet's rule list of the rule before
* which to insert the specified rule. If the specified index is
* equal to the length of the style sheet's rule collection, the rule
* will be added to the end of the style sheet.
* @return The index within the style sheet's rule collection of the newly
* inserted rule.
* @exception DOMException
* HIERARCHY_REQUEST_ERR: Raised if the rule cannot be inserted
* at the specified index e.g. if an <code>@import</code> rule is
* inserted after a standard rule set or other at-rule. <br>
* INDEX_SIZE_ERR: Raised if the specified index is not a valid
* insertion point. <br>
* NO_MODIFICATION_ALLOWED_ERR: Raised if this style sheet is
* readonly. <br>
* SYNTAX_ERR: Raised if the specified rule has a syntax error
* and is unparsable.
*/
//TODO handle all the different types of exceptions as mentioned above
public int insertRule(final String rule, final int index) throws DOMException {
final cz.vutbr.web.css.StyleSheet jSheet = CSSUtils.parse(rule);
if (jSheet.size() > 0) {
this.jStyleSheet.add(index, jSheet.get(0));
bridge.notifyStyleSheetChanged(this);
return index;
}
return -1;
}
/**
* Used to delete a rule from the style sheet.
*
* @param index
* The index within the style sheet's rule list of the rule to
* remove.
* @exception DOMException
* INDEX_SIZE_ERR: Raised if the specified index does not
* correspond to a rule in the style sheet's rule list. <br>
* NO_MODIFICATION_ALLOWED_ERR: Raised if this style sheet is
* read-only.
*/
public void deleteRule(final int index) throws DOMException {
this.jStyleSheet.remove(index);
bridge.notifyStyleSheetChanged(this);
}
// TODO hide it from JS
public cz.vutbr.web.css.StyleSheet getJStyleSheet() {
return this.jStyleSheet;
}
// TODO hide it from JS
public void setJStyleSheet(final cz.vutbr.web.css.StyleSheet jStyleSheet) {
this.jStyleSheet = jStyleSheet;
}
protected void informChanged() {
bridge.notifyStyleSheetChanged(this);
}
/**
* @param styleSheets
* Creates an StyleSheetList instance from these style sheets
* @return StyleSheetList object constructed from the list of style sheets
*/
public static StyleSheetList getStyleSheets(final StyleSheetBridge bridge) {
return new StyleSheetListImpl(bridge);
}
}