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: d87b011eb8ca21cb58eee5f556eb7d317ff55bc1 (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
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
/*******************************************************************************
 * Copyright (c) 2004, 2005 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 java.util.Iterator;
import java.util.Vector;

import org.eclipse.wst.css.core.internal.parserz.CSSRegionContexts;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSAttr;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSNodeList;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSRuleContainer;
import org.eclipse.wst.css.core.internal.util.CSSUtil;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;

/**
 * 
 */
class CSSModelUtil {

	/**
	 * 
	 */
	static boolean canContainBrace(ICSSNode node) {
		return (node != null && (node instanceof ICSSRuleContainer || node instanceof CSSRuleDeclContainer)) ? true : false;
	}

	/**
	 * @param parent
	 *            org.eclipse.wst.css.core.model.interfaces.ICSSNode
	 */
	static void cleanupContainer(ICSSNode parent) {
		if (parent == null) {
			return;
		}
		for (ICSSNode child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
			cleanupContainer(child);
		}
		if (parent instanceof CSSStructuredDocumentRegionContainer) {
			((CSSStructuredDocumentRegionContainer) parent).setRangeStructuredDocumentRegion(null, null);
		}
		else if (parent instanceof CSSRegionContainer) {
			((CSSRegionContainer) parent).setRangeRegion(null, null, null);
		}
	}

	static boolean diagnoseNode(ICSSNode parent, IStructuredDocument structuredDocument) {
		// check this
		Vector errors = new Vector();
		if (parent instanceof CSSStructuredDocumentRegionContainer) {
			CSSStructuredDocumentRegionContainer node = (CSSStructuredDocumentRegionContainer) parent;
			String nodeText = CSSUtil.getClassString(node) + ": ";//$NON-NLS-1$
			IStructuredDocumentRegion flatNode = node.getFirstStructuredDocumentRegion();
			if (flatNode == null && (!(node instanceof CSSStyleDeclarationImpl || node instanceof CSSStyleSheetImpl) || node.getFirstChild() != null)) {
				errors.add(nodeText + "first flat node is null."); //$NON-NLS-1$
			}
			else if (flatNode != null) {
				IStructuredDocumentRegion modelNode = structuredDocument.getRegionAtCharacterOffset(flatNode.getStart());
				if (flatNode != modelNode) {
					errors.add(nodeText + "first flat node is not in model."); //$NON-NLS-1$
				}
			}
			flatNode = node.getLastStructuredDocumentRegion();
			if (flatNode == null && (!(node instanceof CSSStyleDeclarationImpl || node instanceof CSSStyleSheetImpl) || node.getFirstChild() != null)) {
				errors.add(nodeText + "last flat node is null."); //$NON-NLS-1$
			}
			else if (flatNode != null) {
				IStructuredDocumentRegion modelNode = structuredDocument.getRegionAtCharacterOffset(flatNode.getStart());
				if (flatNode != modelNode) {
					errors.add(nodeText + "last flat node is not in model."); //$NON-NLS-1$
				}
			}
		}
		else if (parent instanceof CSSRegionContainer) {
			CSSRegionContainer node = (CSSRegionContainer) parent;
			String nodeText = CSSUtil.getClassString(node) + ": ";//$NON-NLS-1$
			ITextRegion region = node.getFirstRegion();
			IStructuredDocumentRegion parentRegion = node.getDocumentRegion();
			if (region == null && (!(node instanceof MediaListImpl) || node.getFirstChild() != null)) {
				errors.add(nodeText + "first region is null."); //$NON-NLS-1$
			}
			else if (region != null) {
				int offset = parentRegion.getStartOffset(region);
				IStructuredDocumentRegion modelNode = structuredDocument.getRegionAtCharacterOffset(offset);
				ITextRegion modelRegion = modelNode.getRegionAtCharacterOffset(offset);
				if (region != modelRegion) {
					errors.add(nodeText + "first region is not in model."); //$NON-NLS-1$
				}
			}
			region = node.getLastRegion();
			if (region == null && (!(node instanceof MediaListImpl) || node.getFirstChild() != null)) {
				errors.add(nodeText + "last region is null."); //$NON-NLS-1$
			}
			else if (region != null) {
				int offset = parentRegion.getStartOffset(region);
				IStructuredDocumentRegion modelNode = structuredDocument.getRegionAtCharacterOffset(offset);
				ITextRegion modelRegion = modelNode.getRegionAtCharacterOffset(offset);
				if (region != modelRegion) {
					errors.add(nodeText + "last region is not in model."); //$NON-NLS-1$
				}
			}
		}

		ICSSNodeList attrs = parent.getAttributes();
		int nAttrs = attrs.getLength();
		for (int i = 0; i < nAttrs; i++) {
			ICSSAttr attr = (ICSSAttr) attrs.item(i);
			CSSRegionContainer node = (CSSRegionContainer) attr;
			String nodeText = CSSUtil.getClassString(node) + "(" + attr.getName() + "): ";//$NON-NLS-2$//$NON-NLS-1$
			ITextRegion region = node.getFirstRegion();
			IStructuredDocumentRegion parentRegion = node.getDocumentRegion();
			if (region == null && 0 < attr.getValue().length()) {
				errors.add(nodeText + "first region is null."); //$NON-NLS-1$
			}
			else if (region != null) {
				int offset = parentRegion.getStartOffset(region);
				IStructuredDocumentRegion modelNode = structuredDocument.getRegionAtCharacterOffset(offset);
				ITextRegion modelRegion = modelNode.getRegionAtCharacterOffset(offset);
				if (region != modelRegion) {
					errors.add(nodeText + "first region is not in model."); //$NON-NLS-1$
				}
			}
			region = node.getLastRegion();
			if (region == null && 0 < attr.getValue().length()) {
				errors.add(nodeText + "last region is null."); //$NON-NLS-1$
			}
			else if (region != null) {
				int offset = parentRegion.getStartOffset(region);
				IStructuredDocumentRegion modelNode = structuredDocument.getRegionAtCharacterOffset(offset);
				ITextRegion modelRegion = modelNode.getRegionAtCharacterOffset(offset);
				if (region != modelRegion) {
					errors.add(nodeText + "last region is not in model."); //$NON-NLS-1$
				}
			}
		}

		if (!errors.isEmpty()) {
			Iterator i = errors.iterator();
			while (i.hasNext()) {
				CSSUtil.debugOut((String) i.next());
			}
			return false;
		}
		else {
			return true;
		}
	}

	/**
	 * @return boolean
	 * @param node
	 *            org.eclipse.wst.css.core.model.interfaces.ICSSNode
	 */
	static boolean diagnoseTree(ICSSNode parent, IStructuredDocument structuredDocument) {
		if (parent == null) {
			return false;
		}
		// check children
		for (ICSSNode child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
			diagnoseTree(child, structuredDocument);
		}

		diagnoseNode(parent, structuredDocument);

		return true;
	}

	/**
	 * If needed, modify last flat node
	 */
	static void expandStructuredDocumentRegionContainer(CSSStructuredDocumentRegionContainer target, IStructuredDocumentRegion flatNode) {
		if (target == null || flatNode == null) {
			return;
		}

		IStructuredDocumentRegion lastNode = target.getLastStructuredDocumentRegion();
		if (lastNode == flatNode) {
			return;
		}
		if (lastNode == null || lastNode.getStart() < flatNode.getStart()) {
			target.setLastStructuredDocumentRegion(flatNode);
		}
	}

	/*
	 * 
	 */
	static ICSSNode findBraceContainer(ICSSNode node) {
		for (ICSSNode i = node; i != null; i = i.getParentNode()) {
			if (CSSModelUtil.canContainBrace(i)) {
				return i;
			}
		}
		return null;
	}

	/**
	 * 
	 */
	static int getDepth(ICSSNode node) {
		int depth = -1;
		while (node != null) {
			depth++;
			node = node.getParentNode();
		}
		return depth;
	}

	/**
	 * 
	 */
	static boolean isBraceClosed(ICSSNode node) {
		boolean bClosed = true;
		if (!(node instanceof CSSStructuredDocumentRegionContainer)) {
			return bClosed;
		}

		IStructuredDocumentRegion first = ((CSSStructuredDocumentRegionContainer) node).getFirstStructuredDocumentRegion();
		IStructuredDocumentRegion last = ((CSSStructuredDocumentRegionContainer) node).getLastStructuredDocumentRegion();
		if (first == null || last == null) {
			return bClosed;
		}
		if (last.getStart() < first.getStart()) {
			return bClosed;
		}

		IStructuredDocumentRegion flatNode = first;
		int nOpen = 0;
		int nClose = 0;
		do {
			String type = CSSUtil.getStructuredDocumentRegionType(flatNode);
			if (type == CSSRegionContexts.CSS_LBRACE) {
				nOpen++;
			}
			else if (type == CSSRegionContexts.CSS_RBRACE) {
				nClose++;
			}
			flatNode = flatNode.getNext();
		}
		while (flatNode != null && flatNode != last);

		if ((nOpen == 0 && nClose == 0) || nClose < nOpen) {
			bClosed = false;
		}

		return bClosed;
	}

	/**
	 * only for insertion..
	 */
	static boolean isInterruption(CSSStructuredDocumentRegionContainer target, IStructuredDocumentRegion flatNode) {
		if (target == null || flatNode == null) {
			return false;
		}
		int start = flatNode.getStart();
		IStructuredDocumentRegion firstNode = target.getFirstStructuredDocumentRegion();
		IStructuredDocumentRegion lastNode = target.getLastStructuredDocumentRegion();
		if (firstNode != null && firstNode.getStart() < start && lastNode != null && start < lastNode.getStart()) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 */
	static boolean isParentOf(ICSSNode parent, ICSSNode child) {
		if (parent == null || child == null) {
			return false;
		}

		for (ICSSNode node = child; node != null; node = node.getParentNode()) {
			if (parent == node) {
				return true;
			}
		}

		return false;
	}
}

Back to the top