blob: ba347c92f150ce38dce0e9cfb351553dadaf4b05 [file] [log] [blame]
nitind958d79a2004-11-23 19:23:00 +00001/*******************************************************************************
2 * Copyright (c) 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
david_williamsaaadf2b2005-04-11 07:28:31 +000011package org.eclipse.wst.html.core.internal.htmlcss;
nitind958d79a2004-11-23 19:23:00 +000012
13
14
amywufecd7a92006-05-10 07:22:58 +000015import com.ibm.icu.util.StringTokenizer;
nitind958d79a2004-11-23 19:23:00 +000016
david_williams63219a22005-04-10 01:59:51 +000017import org.eclipse.wst.css.core.internal.provisional.adapters.IStyleSelectorAdapter;
18import org.eclipse.wst.css.core.internal.provisional.document.ICSSSimpleSelector;
david_williams4ad020f2005-04-18 08:00:30 +000019import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
nitind958d79a2004-11-23 19:23:00 +000020import org.w3c.dom.Element;
21
22/**
23 * Insert the type's description here.
24 */
25public class HTMLStyleSelectorAdapter implements IStyleSelectorAdapter {
26
27 static private HTMLStyleSelectorAdapter instance;
28 private Object toMatch = IStyleSelectorAdapter.class;
29
nitind958d79a2004-11-23 19:23:00 +000030 public synchronized static HTMLStyleSelectorAdapter getInstance() {
31 if (instance == null) {
32 instance = new HTMLStyleSelectorAdapter();
33 }
34 return instance;
35 }
36
nitind958d79a2004-11-23 19:23:00 +000037 public boolean isAdapterForType(Object type) {
38 return type == toMatch;
39 }
40
nitind958d79a2004-11-23 19:23:00 +000041 public boolean match(ICSSSimpleSelector selector, Element element, String pseudoName) {
42 if (element == null)
43 return false;
44 int i;
45 String key;
46
47 // PseudoName
48 i = selector.getNumOfPseudoNames();
49 if (i > 0) {
50 if (pseudoName == null || pseudoName.length() == 0)
51 return false;
52 for (i = i - 1; i >= 0; i--) {
53 if (pseudoName.equalsIgnoreCase(selector.getPseudoName(i))) {
54 break;
55 }
56 }
57 if (i < 0)
58 return false;
59 }
60
61 // check tag name
62 if (!selector.isUniversal() && !element.getNodeName().equalsIgnoreCase(selector.getName()))
63 return false;
64
65 // check id
66 i = selector.getNumOfIDs();
67 if (i > 0) {
68 if (i > 1)
69 return false;
70 key = element.getAttribute("id");//$NON-NLS-1$
71 if (key == null)
72 return false;
73 if (!selector.getID(0).equals(key))
74 return false;
75 }
76
77 // check class
78 i = selector.getNumOfClasses();
79 if (i > 0) {
80 key = element.getAttribute("class");//$NON-NLS-1$
81 if (key == null)
82 return false;
83 StringTokenizer tokenizer = new StringTokenizer(key);
84 for (i = i - 1; i >= 0; i--) {
85 boolean ok = false;
86 while (tokenizer.hasMoreTokens()) {
87 if (selector.getClass(i).equals(tokenizer.nextToken())) {
88 ok = true;
89 break;
90 }
91 }
92 if (!ok)
93 return false;
94 }
95 }
96
97 // check attributes
98 for (i = selector.getNumOfAttributes() - 1; i >= 0; i--) {
99 StringTokenizer tokenizer = new StringTokenizer(selector.getAttribute(i), "=~| \t\r\n\f");//$NON-NLS-1$
100 int countTokens = tokenizer.countTokens();
101 if (countTokens > 0) {
102 String attrValue = element.getAttribute(tokenizer.nextToken());
103 if (attrValue == null)
104 return false;
105 if (countTokens > 1) {
106 String token = tokenizer.nextToken("= \t\r\n\f");//$NON-NLS-1$
107 StringTokenizer attrValueTokenizer = null;
108 if (token.equals("~")) {//$NON-NLS-1$
109 attrValueTokenizer = new StringTokenizer(attrValue);
110 }
111 else if (token.equals("|")) {//$NON-NLS-1$
112 attrValueTokenizer = new StringTokenizer(attrValue, "-");//$NON-NLS-1$
113 }
114 if (attrValueTokenizer != null) {
115 if (tokenizer.hasMoreTokens()) {
116 token = tokenizer.nextToken();
117 boolean ok = false;
118 while (attrValueTokenizer.hasMoreTokens()) {
119 if (token.equals(attrValueTokenizer.nextToken())) {
120 ok = true;
121 break;
122 }
123 }
124 if (!ok)
125 return false;
126 }
127 }
128 else {
129 if (!attrValue.equals(token))
130 return false;
131 }
132 }
133 }
134 }
135
136 return true;
137 }
138
nitind958d79a2004-11-23 19:23:00 +0000139 public void notifyChanged(INodeNotifier notifier, int eventType, Object changedFeature, Object oldValue, Object newValue, int pos) {
140 }
141}