Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 04f6b6dfd5055b5de8b202be15ffe5d0fae8da91 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.html.core.internal.htmlcss;



import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSSelector;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorList;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule;
import org.eclipse.wst.css.core.internal.util.AbstractCssTraverser;
import org.eclipse.wst.css.core.internal.util.CSSStyleDeclarationFactory;
import org.w3c.dom.Element;
import org.w3c.dom.css.ElementCSSInlineStyle;

/**
 */
public class CSSQueryTraverser extends AbstractCssTraverser {

	private Element element;
	private String pseudoName;
	private CSSQueryContext context = null;
	ICSSStyleDeclaration decl = null;

	/**
	 */
	public ICSSStyleDeclaration getDeclaration() {
		try {
			ICSSStyleDeclaration inlineStyle = (ICSSStyleDeclaration) ((ElementCSSInlineStyle) element).getStyle();
			if (inlineStyle != null) {
				if (context == null) {
					context = new CSSQueryContext();
				}
				context.overrideWithExpand(inlineStyle, 10000);
				// style attribute's specificity is 100 (in CSS2 spec.) and
				// our implement use 100 as base number (see CSSSelector.java)
			}
		}
		catch (ClassCastException ex) {
			// element is not ElementCSSInlineStyle ???
		}
		if (context == null)
			return null;

		if (decl == null)
			decl = CSSStyleDeclarationFactory.getInstance().createStyleDeclaration();
		context.applyFull(decl);
		return decl;
	}

	/**
	 */
	private void overwriteDeclaration(ICSSStyleDeclaration d, int specificity) {
		if (d == null)
			return;
		if (context == null)
			context = new CSSQueryContext();
		context.overrideWithExpand(d, specificity);
	}

	/**
	 */
	protected short postNode(ICSSNode node) {
		return TRAV_CONT;
	}

	/**
	 */
	protected short preNode(ICSSNode node) {
		if (node instanceof ICSSStyleRule) {
			// style rule
			ICSSStyleRule style = (ICSSStyleRule) node;
			ICSSSelectorList list = style.getSelectors();
			int nSelectors = list.getLength();
			int maxSpecificity = -1;
			for (int iSelector = 0; iSelector < nSelectors; iSelector++) {
				// Check each Selector Lists
				ICSSSelector selector = list.getSelector(iSelector);
				int specificity = selector.getSpecificity();
				if (maxSpecificity < specificity && selector.match(element, pseudoName)) {
					maxSpecificity = specificity;
				}
			}
			if (maxSpecificity >= 0) {
				// apply this style to the element
				overwriteDeclaration((ICSSStyleDeclaration) style.getStyle(), maxSpecificity);
			}
			return TRAV_PRUNE;
		}
		return TRAV_CONT;
	}

	/**
	 */
	private void resetContext() {
		context = null;
	}

	/**
	 */
	public void setElement(Element element, String pseudoName) {
		this.element = element;
		this.pseudoName = pseudoName;
		resetContext();
	}
}

Back to the top