-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCSSOMParser.java
More file actions
444 lines (402 loc) · 15.5 KB
/
CSSOMParser.java
File metadata and controls
444 lines (402 loc) · 15.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
* Copyright (c) 2019-2024 Ronald Brill.
*
* 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
* https://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 org.htmlunit.cssparser.parser;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import org.htmlunit.cssparser.dom.AbstractCSSRuleImpl;
import org.htmlunit.cssparser.dom.CSSCharsetRuleImpl;
import org.htmlunit.cssparser.dom.CSSFontFaceRuleImpl;
import org.htmlunit.cssparser.dom.CSSImportRuleImpl;
import org.htmlunit.cssparser.dom.CSSMediaRuleImpl;
import org.htmlunit.cssparser.dom.CSSPageRuleImpl;
import org.htmlunit.cssparser.dom.CSSRuleListImpl;
import org.htmlunit.cssparser.dom.CSSStyleDeclarationImpl;
import org.htmlunit.cssparser.dom.CSSStyleRuleImpl;
import org.htmlunit.cssparser.dom.CSSStyleSheetImpl;
import org.htmlunit.cssparser.dom.CSSUnknownRuleImpl;
import org.htmlunit.cssparser.dom.CSSValueImpl;
import org.htmlunit.cssparser.dom.MediaListImpl;
import org.htmlunit.cssparser.dom.Property;
import org.htmlunit.cssparser.parser.javacc.CSS3Parser;
import org.htmlunit.cssparser.parser.media.MediaQueryList;
import org.htmlunit.cssparser.parser.selector.SelectorList;
import org.w3c.dom.DOMException;
/**
* @author Ronald Brill
*/
public class CSSOMParser {
private final AbstractCSSParser parser_;
private CSSStyleSheetImpl parentStyleSheet_;
/**
* Creates new CSSOMParser.
* @param parser the parser
*/
public CSSOMParser(final AbstractCSSParser parser) {
parser_ = parser;
}
/**
* Creates new CSSOMParser.
*/
public CSSOMParser() {
parser_ = new CSS3Parser();
}
/**
* <p>setErrorHandler.</p>
*
* @param eh the error handler to be used
*/
public void setErrorHandler(final CSSErrorHandler eh) {
parser_.setErrorHandler(eh);
}
/**
* Enable or disable semantic validation.
*
* @param enabled true to enable semantic validation
*/
public void setSemanticValidationEnabled(final boolean enabled) {
parser_.setSemanticValidationEnabled(enabled);
}
/**
* Parses a SAC input source into a CSSOM style sheet.
*
* @param source the SAC input source
* @param href the href
* @return the CSSOM style sheet
* @throws IOException if the underlying SAC parser throws an IOException
*/
public CSSStyleSheetImpl parseStyleSheet(final InputSource source, final String href) throws IOException {
final CSSOMHandler handler = new CSSOMHandler();
handler.setHref(href);
parser_.setDocumentHandler(handler);
parser_.parseStyleSheet(source);
final Object o = handler.getRoot();
if (o instanceof CSSStyleSheetImpl) {
return (CSSStyleSheetImpl) o;
}
return null;
}
/**
* Parses a input string into a CSSOM style declaration.
*
* @param styleDecl the input string
* @return the CSSOM style declaration
* @throws IOException if the underlying SAC parser throws an IOException
*/
public CSSStyleDeclarationImpl parseStyleDeclaration(final String styleDecl) throws IOException {
final CSSStyleDeclarationImpl sd = new CSSStyleDeclarationImpl(null);
parseStyleDeclaration(sd, styleDecl);
return sd;
}
/**
* Parses a input string into a CSSOM style declaration.
*
* @param styleDecl the input string
* @param sd the CSSOM style declaration
* @throws IOException if the underlying SAC parser throws an IOException
*/
public void parseStyleDeclaration(final CSSStyleDeclarationImpl sd, final String styleDecl) throws IOException {
try (InputSource source = new InputSource(new StringReader(styleDecl))) {
final Deque<Object> nodeStack = new ArrayDeque<>();
nodeStack.push(sd);
final CSSOMHandler handler = new CSSOMHandler(nodeStack);
parser_.setDocumentHandler(handler);
parser_.parseStyleDeclaration(source);
}
}
/**
* Parses a input string into a CSSValue.
*
* @param propertyValue the input string
* @return the css value
* @throws IOException if the underlying SAC parser throws an IOException
*/
public CSSValueImpl parsePropertyValue(final String propertyValue) throws IOException {
try (InputSource source = new InputSource(new StringReader(propertyValue))) {
final CSSOMHandler handler = new CSSOMHandler();
parser_.setDocumentHandler(handler);
final LexicalUnit lu = parser_.parsePropertyValue(source);
if (null == lu) {
return null;
}
return new CSSValueImpl(lu);
}
}
/**
* Parses a string into a CSSRule.
*
* @param rule the input string
* @return the css rule
* @throws IOException if the underlying SAC parser throws an IOException
*/
public AbstractCSSRuleImpl parseRule(final String rule) throws IOException {
try (InputSource source = new InputSource(new StringReader(rule))) {
final CSSOMHandler handler = new CSSOMHandler();
parser_.setDocumentHandler(handler);
parser_.parseRule(source);
return (AbstractCSSRuleImpl) handler.getRoot();
}
}
/**
* Parses a string into a CSSSelectorList.
*
* @param selectors the input string
* @return the css selector list
* @throws IOException if the underlying SAC parser throws an IOException
*/
public SelectorList parseSelectors(final String selectors) throws IOException {
try (InputSource source = new InputSource(new StringReader(selectors))) {
final HandlerBase handler = new HandlerBase();
parser_.setDocumentHandler(handler);
return parser_.parseSelectors(source);
}
}
/**
* Parses a string into a MediaQueryList.
*
* @param media the input string
* @return the css media query list
* @throws IOException if the underlying SAC parser throws an IOException
*/
public MediaQueryList parseMedia(final String media) throws IOException {
try (InputSource source = new InputSource(new StringReader(media))) {
final HandlerBase handler = new HandlerBase();
parser_.setDocumentHandler(handler);
return parser_.parseMedia(source);
}
}
/**
* <p>setParentStyleSheet.</p>
*
* @param parentStyleSheet the new parent stylesheet
*/
public void setParentStyleSheet(final CSSStyleSheetImpl parentStyleSheet) {
parentStyleSheet_ = parentStyleSheet;
}
/**
* <p>getParentStyleSheet.</p>
*
* @return the parent style sheet
*/
protected CSSStyleSheetImpl getParentStyleSheet() {
return parentStyleSheet_;
}
class CSSOMHandler implements DocumentHandler {
private final Deque<Object> nodeStack_;
private Object root_;
private String href_;
private String getHref() {
return href_;
}
private void setHref(final String href) {
href_ = href;
}
CSSOMHandler(final Deque<Object> nodeStack) {
nodeStack_ = nodeStack;
}
CSSOMHandler() {
nodeStack_ = new ArrayDeque<>();
}
Object getRoot() {
return root_;
}
@Override
public void startDocument(final InputSource source) throws CSSException {
if (nodeStack_.isEmpty()) {
final CSSStyleSheetImpl ss = new CSSStyleSheetImpl();
CSSOMParser.this.setParentStyleSheet(ss);
ss.setHref(getHref());
ss.setMediaText(source.getMedia());
ss.setTitle(source.getTitle());
// Create the rule list
final CSSRuleListImpl rules = new CSSRuleListImpl();
ss.setCssRules(rules);
nodeStack_.push(ss);
nodeStack_.push(rules);
}
}
@Override
public void endDocument(final InputSource source) throws CSSException {
// Pop the rule list and style sheet nodes
nodeStack_.pop();
root_ = nodeStack_.pop();
}
@Override
public void ignorableAtRule(final String atRule, final Locator locator) throws CSSException {
// Create the unknown rule and add it to the rule list
final CSSUnknownRuleImpl ir = new CSSUnknownRuleImpl(
CSSOMParser.this.getParentStyleSheet(),
getParentRule(),
atRule);
ir.setLocator(locator);
if (!nodeStack_.isEmpty()) {
((CSSRuleListImpl) nodeStack_.peek()).add(ir);
}
else {
root_ = ir;
}
}
@Override
public void charset(final String characterEncoding, final Locator locator)
throws CSSException {
final CSSCharsetRuleImpl cr = new CSSCharsetRuleImpl(
CSSOMParser.this.getParentStyleSheet(),
getParentRule(),
characterEncoding);
cr.setLocator(locator);
if (!nodeStack_.isEmpty()) {
((CSSRuleListImpl) nodeStack_.peek()).add(cr);
}
else {
root_ = cr;
}
}
@Override
public void importStyle(final String uri, final MediaQueryList media,
final String defaultNamespaceURI, final Locator locator) throws CSSException {
// Create the import rule and add it to the rule list
final CSSImportRuleImpl ir = new CSSImportRuleImpl(
CSSOMParser.this.getParentStyleSheet(),
getParentRule(),
uri,
new MediaListImpl(media));
ir.setLocator(locator);
if (!nodeStack_.isEmpty()) {
((CSSRuleListImpl) nodeStack_.peek()).add(ir);
}
else {
root_ = ir;
}
}
@Override
public void startMedia(final MediaQueryList media, final Locator locator) throws CSSException {
final MediaListImpl ml = new MediaListImpl(media);
// Create the media rule and add it to the rule list
final CSSMediaRuleImpl mr = new CSSMediaRuleImpl(
CSSOMParser.this.getParentStyleSheet(),
getParentRule(),
ml);
mr.setLocator(locator);
if (!nodeStack_.isEmpty()) {
((CSSRuleListImpl) nodeStack_.peek()).add(mr);
}
// Create the rule list
final CSSRuleListImpl rules = new CSSRuleListImpl();
mr.setRuleList(rules);
nodeStack_.push(mr);
nodeStack_.push(rules);
}
@Override
public void endMedia(final MediaQueryList media) throws CSSException {
// Pop the rule list and media rule nodes
nodeStack_.pop();
root_ = nodeStack_.pop();
}
@Override
public void startPage(final String name, final String pseudoPage, final Locator locator)
throws CSSException {
// Create the page rule and add it to the rule list
final CSSPageRuleImpl pr = new CSSPageRuleImpl(
CSSOMParser.this.getParentStyleSheet(),
getParentRule(), pseudoPage);
pr.setLocator(locator);
if (!nodeStack_.isEmpty()) {
((CSSRuleListImpl) nodeStack_.peek()).add(pr);
}
// Create the style declaration
final CSSStyleDeclarationImpl decl = new CSSStyleDeclarationImpl(pr);
pr.setStyle(decl);
nodeStack_.push(pr);
nodeStack_.push(decl);
}
@Override
public void endPage(final String name, final String pseudoPage) throws CSSException {
// Pop both the style declaration and the page rule nodes
nodeStack_.pop();
root_ = nodeStack_.pop();
}
@Override
public void startFontFace(final Locator locator) throws CSSException {
// Create the font face rule and add it to the rule list
final CSSFontFaceRuleImpl ffr = new CSSFontFaceRuleImpl(
CSSOMParser.this.getParentStyleSheet(),
getParentRule());
ffr.setLocator(locator);
if (!nodeStack_.isEmpty()) {
((CSSRuleListImpl) nodeStack_.peek()).add(ffr);
}
// Create the style declaration
final CSSStyleDeclarationImpl decl = new CSSStyleDeclarationImpl(ffr);
ffr.setStyle(decl);
nodeStack_.push(ffr);
nodeStack_.push(decl);
}
@Override
public void endFontFace() throws CSSException {
// Pop both the style declaration and the font face rule nodes
nodeStack_.pop();
root_ = nodeStack_.pop();
}
@Override
public void startSelector(final SelectorList selectors, final Locator locator) throws CSSException {
// Create the style rule and add it to the rule list
final CSSStyleRuleImpl sr = new CSSStyleRuleImpl(
CSSOMParser.this.getParentStyleSheet(),
getParentRule(), selectors);
sr.setLocator(locator);
if (!nodeStack_.isEmpty()) {
final Object o = nodeStack_.peek();
((CSSRuleListImpl) o).add(sr);
}
// Create the style declaration
final CSSStyleDeclarationImpl decl = new CSSStyleDeclarationImpl(sr);
sr.setStyle(decl);
nodeStack_.push(sr);
nodeStack_.push(decl);
}
@Override
public void endSelector(final SelectorList selectors) throws CSSException {
// Pop both the style declaration and the style rule nodes
nodeStack_.pop();
root_ = nodeStack_.pop();
}
@Override
public void property(final String name, final LexicalUnit value, final boolean important,
final Locator locator) {
final CSSStyleDeclarationImpl decl = (CSSStyleDeclarationImpl) nodeStack_.peek();
try {
final Property property = new Property(name, new CSSValueImpl(value), important);
property.setLocator(locator);
decl.addProperty(property);
}
catch (final DOMException e) {
parser_.getErrorHandler().error(parser_.toCSSParseException(e));
}
}
private AbstractCSSRuleImpl getParentRule() {
if (!nodeStack_.isEmpty() && nodeStack_.size() > 1) {
final Iterator<Object> iter = nodeStack_.iterator();
iter.next(); // skip first
final Object node = iter.next();
if (node instanceof AbstractCSSRuleImpl) {
return (AbstractCSSRuleImpl) node;
}
}
return null;
}
}
}