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: 4068f52615d7daa20f1554316e2edfeef97b0077 (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
/*****************************************************************************
 * 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.css.ui.internal.contentassist;



import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.eclipse.wst.css.core.internal.provisional.document.ICSSModel;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule;
import org.eclipse.wst.css.ui.internal.image.CSSImageType;
import org.eclipse.wst.html.core.internal.contentmodel.HTMLCMDocumentFactory;
import org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration;
import org.eclipse.wst.xml.core.internal.contentmodel.CMDocument;
import org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration;
import org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap;
import org.eclipse.wst.xml.core.internal.document.DocumentTypeAdapter;
import org.eclipse.wst.xml.core.internal.provisional.contentmodel.CMDocType;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

class CSSProposalGeneratorForHTMLTag extends CSSProposalGenerator {

	/**
	 * CSSStyleRuleProposalGenerator constructor comment.
	 * 
	 */
	CSSProposalGeneratorForHTMLTag(CSSContentAssistContext context) {
		super(context);

		if (fHTMLTags == null) {
			fHTMLTags = setupHTMLTags();
		}
	}

	/**
	 * getCandidates method comment.
	 */
	protected Iterator getCandidates() {
		List candidates = new ArrayList();

		if (checkLeadingColon()) {
			return candidates.iterator();
		}

		// XHTML requires lower case
		boolean bLowerCase = false;
		if (fContext.getModel().getStyleSheetType() == ICSSModel.EMBEDDED) {
			Node domNode = fContext.getModel().getOwnerDOMNode();
			if (domNode != null && !(domNode instanceof Document)) {
				domNode = domNode.getOwnerDocument();
				if (domNode instanceof IDOMDocument) {
					DocumentTypeAdapter adapter = (DocumentTypeAdapter) ((IDOMDocument) domNode).getAdapterFor(DocumentTypeAdapter.class);
					if (adapter != null)
						bLowerCase = (adapter.getTagNameCase() == DocumentTypeAdapter.LOWER_CASE);
				}
			}
		}


		int length = fHTMLTags.length;
		for (int i = 0; i < length; i++) {
			String tagText = fHTMLTags[i];
			if (bLowerCase) {
				tagText = tagText.toLowerCase();
			}
			if (!isMatch(tagText)) {
				continue;
			}

			int cursorPos = 0;
			StringBuffer buf = new StringBuffer();
			buf.append(tagText);
			cursorPos += tagText.length();
			boolean inRule = (fContext.getTargetNode() instanceof ICSSStyleRule);
			if (!inRule || fContext.getTextToReplace().length() == 0) {
				buf.append(" ");//$NON-NLS-1$
				cursorPos += 1;
			}
			if (!inRule) {
				StringAndOffset sao = generateBraces();
				buf.append(sao.fString);
				cursorPos += sao.fOffset;
			}

			CSSCACandidate item = new CSSCACandidate();
			item.setReplacementString(buf.toString());
			item.setCursorPosition(cursorPos);
			item.setDisplayString(tagText);
			item.setImageType(CSSImageType.SELECTOR_TAG);
			candidates.add(item);
		}

		return candidates.iterator();
	}

	/**
	 *  
	 */
	private static String[] setupHTMLTags() {
		CMDocument cmdoc = HTMLCMDocumentFactory.getCMDocument(CMDocType.HTML_DOC_TYPE);
		CMNamedNodeMap elements = cmdoc.getElements();
		Vector names = new Vector();
		int nElements = elements.getLength();
		for (int i = 0; i < nElements; i++) {
			CMElementDeclaration edec = (CMElementDeclaration) elements.item(i);
			if (isAttrDefined(edec, HTML40Namespace.ATTR_NAME_STYLE)) {
				names.add(edec.getElementName());
			}
		}
		Collections.sort(names);
		String[] tags = new String[names.size()];
		Iterator iNames = names.iterator();
		for (int i = 0; iNames.hasNext(); i++) {
			tags[i] = (String) iNames.next();
		}
		return tags;
	}

	/**
	 *  
	 */
	private static boolean isAttrDefined(CMElementDeclaration edec, String attrName) {
		if (edec == null) {
			return false;
		}
		CMNamedNodeMap attrs = edec.getAttributes();
		if (attrs == null) {
			return false;
		}
		for (int i = 0; i < attrs.getLength(); i++) {
			CMAttributeDeclaration attr = (CMAttributeDeclaration) attrs.item(i);
			if (attr == null) {
				continue;
			}
			if (attr.getAttrName().equalsIgnoreCase(attrName)) {
				return true;
			}
		}
		return false;
	}

	private static String[] fHTMLTags = null;
}

Back to the top