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: 8ec0839321b141526a465b50c2687f945e028b4a (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
/*******************************************************************************
 * 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.validate;

import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
import org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMText;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class HTMLElementContentValidator extends PrimeValidator {

	/**
	 * HTMLElementContentValidator constructor comment.
	 */
	public HTMLElementContentValidator() {
		super();
	}

	/**
	 * Allowing the INodeAdapter to compare itself against the type allows it
	 * to return true in more than one case.
	 */
	public boolean isAdapterForType(Object type) {
		return ((type == HTMLElementContentValidator.class) || super.isAdapterForType(type));
	}

	/**
	 */
	public void validate(IndexedRegion node) {
		Element target = (Element) node;
		if (CMUtil.isForeign(target))
			return;

		validateContent(target, target.getChildNodes());
	}

	private void validateContent(Element parent, NodeList children) {
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			if (child == null)
				continue;

			// perform actual validation
			validateNode(parent, child);
		}
	}

	// private int countExplicitSiblings(Element parent, String tagName) {
	// NodeList children = parent.getChildNodes();
	// int count = 0;
	// for (int i = 0; i < children.getLength(); i++) {
	// Node child = children.item(i);
	// if (child.getNodeType() != Node.ELEMENT_NODE)
	// continue;
	// if (tagName.equalsIgnoreCase(((Element) child).getTagName())) {
	// count++;
	// }
	// }
	// return count;
	// }

	/*
	 * The implementation of the following method is practical but accurate.
	 * The accurate maximum occurence should be retreive from the content
	 * model. However, it is useful enough, since almost implicit elements are
	 * HTML, HEAD, or BODY.
	 */
	// private int getMaxOccur(Element parent, String childTag) {
	// return 1;
	// }
	private void validateNode(Element target, Node child) {
		// NOTE: If the target element is 'UNKNOWN', that is, it has no
		// element declaration, the content type of the element should be
		// regarded as 'ANY'. -- 9/10/2001
		int contentType = CMElementDeclaration.ANY;
		CMElementDeclaration edec = CMUtil.getDeclaration(target);
		if (edec != null)
			contentType = edec.getContentType();

		int error = ErrorState.NONE_ERROR;
		int segType = FMUtil.SEG_WHOLE_TAG;

		switch (child.getNodeType()) {
			case Node.ELEMENT_NODE :
				Element childElem = (Element) child;
				// Defect 200321:
				// This validator cares only HTML/XHTML elements. If a child
				// is
				// an element of foreign markup languages, just ignore it.
				if (CMUtil.isForeign(childElem))
					return;

				CMElementDeclaration ced = CMUtil.getDeclaration((Element) child);
				// Defect 186774: If a child is not one of HTML elements,
				// it should be regarded as a valid child regardless the
				// type of the parent content model. -- 10/12/2001
				if (ced == null || CMUtil.isSSI(ced) || (!CMUtil.isHTML(ced)))
					return;

				switch (contentType) {
					case CMElementDeclaration.ANY :
						// Keep going.
						return;
					case CMElementDeclaration.ELEMENT :
					case CMElementDeclaration.MIXED :
						if (ced == null)
							return;
						if (CMUtil.isValidChild(edec, ced))
							return;
						// Now, it is the time to check inclusion, unless the
						// target
						// document is not a XHTML.
						if (!CMUtil.isXHTML(edec)) {
							// pure HTML
							if (CMUtil.isValidInclusion(ced, target))
								return;
						}
						error = ErrorState.INVALID_CONTENT_ERROR;
						break;
					default :
						error = ErrorState.INVALID_CONTENT_ERROR;
						break;
				}
				// Mark the whole START tag as an error segment.
				segType = FMUtil.SEG_START_TAG;
				break;
			case Node.TEXT_NODE :
				switch (contentType) {
					case CMElementDeclaration.ANY :
					case CMElementDeclaration.MIXED :
					case CMElementDeclaration.PCDATA :
					case CMElementDeclaration.CDATA :
						// D184339
						// Keep going.
						return;
					case CMElementDeclaration.ELEMENT :
					case CMElementDeclaration.EMPTY :
						if (((IDOMText) child).isElementContentWhitespace())
							return;
						error = ErrorState.INVALID_CONTENT_ERROR;
						break;
					default :
						error = ErrorState.INVALID_CONTENT_ERROR;
						break;
				}
				// Mark the whole node as an error segment.
				segType = FMUtil.SEG_WHOLE_TAG;
				break;
			case Node.COMMENT_NODE :
			case Node.PROCESSING_INSTRUCTION_NODE :
				if (contentType != CMElementDeclaration.EMPTY)
					return;
				error = ErrorState.INVALID_CONTENT_ERROR;
				// Mark the whole node as an error segment.
				segType = FMUtil.SEG_WHOLE_TAG;
				break;
			default :
				error = ErrorState.INVALID_CONTENT_ERROR;
				// Mark the whole node as an error segment.
				segType = FMUtil.SEG_WHOLE_TAG;
				break;
		}
		if (error != ErrorState.NONE_ERROR) {
			Segment errorSeg = FMUtil.getSegment((IDOMNode) child, segType);
			if (errorSeg != null)
				reporter.report(MessageFactory.createMessage(new ErrorInfoImpl(error, errorSeg, child)));
		}
	}
}

Back to the top