Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5fed31cf4a93df137588568336b8dde9c88b0225 (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
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
/*******************************************************************************
 * Copyright (c) 2004, 2012 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.css.core.internal.document;



import org.eclipse.wst.css.core.internal.contentmodel.PropCMProperty;
import org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatterFactory;
import org.eclipse.wst.css.core.internal.formatter.CSSSourceGenerator;
import org.eclipse.wst.css.core.internal.formatter.StyleDeclItemFormatter;
import org.eclipse.wst.css.core.internal.parser.CSSSourceParser;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSPrimitiveValue;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem;
import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList;
import org.eclipse.wst.sse.core.internal.text.TextRegionListImpl;
import org.w3c.dom.DOMException;
import org.w3c.dom.css.CSSValue;


/**
 * 
 */
class CSSStyleDeclItemImpl extends CSSStructuredDocumentRegionContainer implements ICSSStyleDeclItem {

	private java.lang.String fPropertyName;

	/**
	 * CSSStyleDeclItemImpl constructor comment.
	 * 
	 */
	CSSStyleDeclItemImpl(CSSStyleDeclItemImpl that) {
		super(that);
		this.fPropertyName = that.fPropertyName;
	}

	/**
	 * CSSStyleDeclItemImpl constructor comment.
	 */
	CSSStyleDeclItemImpl(String propertyName) {
		super();
		this.fPropertyName = propertyName;
	}

	/**
	 * @return org.w3c.dom.css.CSSPrimitiveValue
	 * @param value
	 *            org.w3c.dom.css.CSSPrimitiveValue
	 */
	public ICSSPrimitiveValue appendValue(ICSSPrimitiveValue value) throws DOMException {
		return insertValueBefore(value, null);
	}

	/**
	 * @param deep
	 *            boolean
	 */
	public ICSSNode cloneNode(boolean deep) {
		CSSStyleDeclItemImpl cloned = new CSSStyleDeclItemImpl(this);

		if (deep)
			cloneChildNodes(cloned, deep);

		return cloned;
	}

	/**
	 * @return java.lang.String
	 */
	String generateValueSource() {
		CSSSourceGenerator formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter(this);
		if (formatter != null && formatter instanceof StyleDeclItemFormatter)
			return ((StyleDeclItemFormatter) formatter).formatValue(this).toString();
		else
			return "";//$NON-NLS-1$
	}

	/**
	 * @return org.w3c.dom.css.CSSValue
	 */
	public CSSValue getCSSValue() {
		int nValue = getLength();
		if (nValue <= 0)
			return null;
		else if (nValue == 1)
			return item(0);
		else
			return this;
	}

	/**
	 * @return java.lang.String
	 */
	public java.lang.String getCSSValueText() {
		if (getFirstChild() == null)
			return "";//$NON-NLS-1$
		// check whether children has flatnodes
		ICSSNode child = getFirstChild();
		while (child != null) {
			if (((IndexedRegion) child).getEndOffset() <= 0)
				return generateValueSource();
			child = child.getNextSibling();
		}

		IStructuredDocumentRegion node = getFirstStructuredDocumentRegion();
		int start = ((IndexedRegion) getFirstChild()).getStartOffset() - node.getStartOffset();
		int end = ((IndexedRegion) getLastChild()).getEndOffset() - node.getStartOffset();
		return node.getText().substring(start, end);
	}

	/**
	 * @return short
	 */
	public short getCssValueType() {
		return CSS_VALUE_LIST;
	}

	/**
	 * @return int
	 */
	public int getLength() {
		int i = 0;
		for (ICSSNode node = getFirstChild(); node != null; node = node.getNextSibling()) {
			if (node instanceof CSSPrimitiveValueImpl)
				i++;
		}
		return i;
	}

	/**
	 * @return short
	 */
	public short getNodeType() {
		return STYLEDECLITEM_NODE;
	}

	/**
	 * @return java.lang.String
	 */
	public java.lang.String getPriority() {
		return getAttribute(IMPORTANT);
	}

	/**
	 * @return java.lang.String
	 */
	public String getPropertyName() {
		String name = fPropertyName.trim();
		return PropCMProperty.getInstanceOf(name) != null ? fPropertyName.trim().toLowerCase() : name;
	}

	/**
	 * @return org.w3c.dom.css.CSSPrimitiveValue
	 * @param newValue
	 *            org.w3c.dom.css.CSSPrimitiveValue
	 * @param refValue
	 *            org.w3c.dom.css.CSSPrimitiveValue
	 */
	ICSSPrimitiveValue insertValueBefore(ICSSPrimitiveValue newValue, ICSSPrimitiveValue refValue) {
		ICSSNode node = insertBefore((CSSNodeImpl) newValue, (CSSNodeImpl) refValue);
		return (ICSSPrimitiveValue) node;
	}

	/**
	 * @return org.w3c.dom.css.CSSValue
	 * @param index
	 *            int
	 */
	public CSSValue item(int index) {
		int i = 0;
		for (ICSSNode node = getFirstChild(); node != null; node = node.getNextSibling()) {
			if (node instanceof CSSPrimitiveValueImpl) {
				if (i++ == index)
					return (CSSValue) node;
			}
		}
		return null;
	}

	/**
	 * @return org.w3c.dom.css.CSSPrimitiveValue
	 * @param value
	 *            org.w3c.dom.css.CSSPrimitiveValue
	 */
	public ICSSPrimitiveValue removeValue(ICSSPrimitiveValue value) throws DOMException {
		ICSSNode node = removeChild((CSSNodeImpl) value);
		return (ICSSPrimitiveValue) node;
	}

	/**
	 * @return org.w3c.dom.css.CSSPrimitiveValue
	 * @param newValue
	 *            org.w3c.dom.css.CSSPrimitiveValue
	 * @param oldValue
	 *            org.w3c.dom.css.CSSPrimitiveValue
	 */
	public ICSSPrimitiveValue replaceValue(ICSSPrimitiveValue newValue, ICSSPrimitiveValue oldValue) throws DOMException {
		if (oldValue == null)
			return newValue;
		if (newValue != null)
			insertValueBefore(newValue, oldValue);
		return removeValue(oldValue);
	}

	/**
	 * @param value
	 *            java.lang.String
	 * @exception org.w3c.dom.DOMException
	 *                The exception description.
	 */
	public void setCssValueText(String value) throws DOMException {
		ICSSNode child = getFirstChild();
		while (child != null) {
			ICSSNode next = child.getNextSibling();
			if (child instanceof ICSSPrimitiveValue) {
				removeChild((CSSNodeImpl) child);
			}
			child = next;
		}
		setCssValueTextCore(value);
	}

	private void setCssValueTextCore(String value) throws DOMException {
		CSSSourceParser parser = new CSSSourceParser();
		parser.setParserMode(CSSSourceParser.MODE_DECLARATION_VALUE);
		parser.reset(value);
		
		IStructuredDocumentRegion node = parser.getDocumentRegions();

		if (node == null) {
			return;
		}
		if (node.getNext() != null) {
			throw new DOMException(DOMException.INVALID_MODIFICATION_ERR, "");//$NON-NLS-1$
		}

		CSSDeclarationItemParser itemParser = new CSSDeclarationItemParser(getOwnerDocument());
		itemParser.setStructuredDocumentTemporary(true);
		// make a copy of nodelist because setupValues will destroy list
		ITextRegionList nodeList = new TextRegionListImpl(node.getRegions());
		itemParser.setupValues(this, node, nodeList, value);
	}

	/**
	 * @param newPriority
	 *            java.lang.String
	 */
	public void setPriority(java.lang.String newPriority) throws DOMException {
		setAttribute(IMPORTANT, newPriority);
	}
}

Back to the top