blob: 0a92aafd51c62140af5dd897b511a6d031fc6964 (
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
|
/*******************************************************************************
* 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.core.internal.util;
import org.eclipse.wst.css.core.internal.document.CSSSelectorListImpl;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSSelector;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorItem;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorList;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSSimpleSelector;
public class SelectorValidator {
/**
* Constructor for SelectorValidator.
*/
public SelectorValidator(String text) {
super();
fText = text;
}
/**
* Returns true if the text consists of one CLASS selector. syntax check
* is a little loose (.123 is passed)
*
* ".class" -> true ".123" -> true ".class , .class2" -> false
* ".class.class2" -> false ".123{}" -> false
*/
public boolean isClass() {
ICSSSimpleSelector selector = getOnlyOneSimpleSelector();
if (selector != null) {
return (selector.getName().length() == 0 && selector.getNumOfAttributes() == 0 && selector.getNumOfClasses() == 1 && selector.getNumOfIDs() == 0 && selector.getNumOfPseudoNames() == 0);
}
return false;
}
/**
* Returns true if the text consists of one ID selector.
*
* "#ID" -> true "H1#myID" -> false "#abc{}" -> false
*/
public boolean isID() {
ICSSSimpleSelector selector = getOnlyOneSimpleSelector();
if (selector != null) {
return (selector.getName().length() == 0 && selector.getNumOfAttributes() == 0 && selector.getNumOfClasses() == 0 && selector.getNumOfIDs() == 1 && selector.getNumOfPseudoNames() == 0);
}
return false;
}
/**
* overall check
*
* "P#hoge98 + *:hover > A:link, A.external:visited" -> true "H1 H2 {}" ->
* false
*/
public boolean isValid() {
parse();
return (fSelectorList != null && fSelectorList.getErrorCount() == 0);
}
private ICSSSimpleSelector getOnlyOneSimpleSelector() {
parse();
if (fSelectorList != null && fSelectorList.getLength() == 1) {
ICSSSelector selector = fSelectorList.getSelector(0);
int nItem = selector.getLength();
if (nItem == 1) {
ICSSSelectorItem item = selector.getItem(0);
if (item instanceof ICSSSimpleSelector) {
return (ICSSSimpleSelector) item;
}
}
}
return null;
}
private void parse() {
if (fSelectorList == null) {
if (fText != null) {
fSelectorList = new CSSSelectorListImpl(fText);
}
}
}
private String fText = null;
private ICSSSelectorList fSelectorList = null;
}
|