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.

aboutsummaryrefslogtreecommitdiffstats
blob: 6572ae64fb18427a63bd0a856372f69093a8a624 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*******************************************************************************
 * 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 java.util.Enumeration;

import org.eclipse.wst.css.core.internal.contentmodel.PropCMProperty;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSValue;
import org.eclipse.wst.css.core.internal.util.CSSLinkConverter;
import org.eclipse.wst.css.core.internal.util.declaration.CSSPropertyContext;

/**
 */
class CSSQueryContext extends CSSPropertyContext {

	/**
	 */
	public CSSQueryContext() {
		super();
	}

	/**
	 */
	public CSSQueryContext(ICSSStyleDeclaration decl) {
		super(decl);
	}

	/**
	 *
	 */
	public void applyFull(ICSSStyleDeclaration decl) {
		if (decl == null)
			return;
		Enumeration keys = fProperties.keys();
		while (keys.hasMoreElements()) {
			Object key = keys.nextElement();
			Object val = fProperties.get(key);

			if (val instanceof CSSQueryDeclarationData) {
				ICSSStyleDeclItem declItem = ((CSSQueryDeclarationData) val).getDeclItem();
				if (declItem.getLength() <= 0) {
					ICSSStyleDeclItem itemToRemove = decl.getDeclItemNode(key.toString());
					if (itemToRemove != null) {
						decl.removeDeclItemNode(itemToRemove);
					}
				}
				else {
					decl.setDeclItemNode(declItem);
				}
			}
			else {
				String value = (val instanceof ICSSValue) ? ((ICSSValue) val).getCSSValueText() : val.toString();

				if (value == null || value.length() <= 0) {
					ICSSStyleDeclItem itemToRemove = decl.getDeclItemNode(key.toString());
					if (itemToRemove != null) {
						decl.removeDeclItemNode(itemToRemove);
					}
				}
				else {
					decl.setProperty(key.toString(), value, null);
				}
			}
		}
	}

	/**
	 */
	private boolean check(String propName, boolean important, int specificity) {
		Object current = fProperties.get(propName);
		if (current != null && current instanceof CSSQueryValueData) {
			CSSQueryValueData currentValue = (CSSQueryValueData) current;
			if ((!important && currentValue.important) || (currentValue.getSpecificity() > specificity)) {
				return false;
			}
		}
		return true;
	}

	/**
	 */
	public void overrideWithExpand(ICSSStyleDeclaration decl, int specificity) {
		if (decl == null)
			return;

		CSSLinkConverter conv = new CSSLinkConverter(decl.getOwnerDocument().getModel());

		int nProperties = decl.getLength();
		for (int i = 0; i < nProperties; i++) {
			String propName = decl.item(i);
			if (propName != null) {
				String propN = propName.trim().toLowerCase();
				if (propN.length() != 0) {
					PropCMProperty prop = PropCMProperty.getInstanceOf(propN);
					String priority = decl.getPropertyPriority(propName);
					boolean important = priority != null && priority.length() > 0;
					if (prop != null && prop.isShorthand()) {
						// expand shorthand property
						CSSQueryContext context = new CSSQueryContext();
						expandToLeaf(prop, decl.getPropertyValue(propName), context);

						Enumeration properties = context.properties();
						while (properties.hasMoreElements()) {
							propN = properties.nextElement().toString();
							if (check(propN, important, specificity)) {
								fProperties.put(propN, new CSSQueryValueData(conv.toAbsolute(context.get(propN)), important, specificity));
							}
						}
					}
					else {
						if (check(propN, important, specificity)) {
							ICSSStyleDeclItem declItem = (ICSSStyleDeclItem) decl.getDeclItemNode(propName).cloneNode(true);
							int nValues = declItem.getLength();
							for (int j = 0; j < nValues; j++) {
								conv.toAbsolute(declItem.item(j));
							}
							declItem.setPriority(null);
							fProperties.put(propN, new CSSQueryDeclarationData(declItem, important, specificity));
						}
					}
				}
			}
		}
	}
}

Back to the top