Skip to content

Commit 2e9f0a4

Browse files
committed
localName of the ElementSelector is always lowercase
1 parent d06f2c2 commit 2e9f0a4

File tree

5 files changed

+38
-24
lines changed

5 files changed

+38
-24
lines changed

src/main/java/com/gargoylesoftware/css/dom/CSSStyleSheetImpl.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.gargoylesoftware.css.parser.CSSOMParser;
3939
import com.gargoylesoftware.css.parser.InputSource;
4040
import com.gargoylesoftware.css.parser.media.MediaQueryList;
41+
import com.gargoylesoftware.css.parser.selector.ElementSelector;
4142
import com.gargoylesoftware.css.parser.selector.Selector;
4243
import com.gargoylesoftware.css.util.LangUtils;
4344
import com.gargoylesoftware.css.util.ThrowCssExceptionErrorHandler;
@@ -380,17 +381,19 @@ public static class CSSStyleSheetRuleIndex {
380381
private final Map<String, List<SelectorEntry>> classSelectors_ = new HashMap<>();
381382
private final List<SelectorEntry> otherSelectors_ = new ArrayList<>();
382383

383-
public void addElementSelector(final String name, final Selector s, final CSSStyleRuleImpl styleRule) {
384-
List<SelectorEntry> entries = elementSelectors_.get(name);
384+
public void addElementSelector(final ElementSelector elementSelector,
385+
final Selector s, final CSSStyleRuleImpl styleRule) {
386+
final String elementName = elementSelector.getLocalName();
387+
List<SelectorEntry> entries = elementSelectors_.get(elementName);
385388
if (entries == null) {
386389
entries = new ArrayList<SelectorEntry>();
387-
elementSelectors_.put(name, entries);
390+
elementSelectors_.put(elementName, entries);
388391
}
389392
final SelectorEntry selectorEntry = new SelectorEntry(s, styleRule);
390393
entries.add(selectorEntry);
391394
}
392395

393-
public void addClassSelector(final String elementName, final String className,
396+
public void addClassSelector(final ElementSelector elementSelector, final String className,
394397
final Selector s, final CSSStyleRuleImpl styleRule) {
395398
List<SelectorEntry> entries = classSelectors_.get(className);
396399
if (entries == null) {
@@ -400,6 +403,7 @@ public void addClassSelector(final String elementName, final String className,
400403
SelectorEntry selectorEntry = new SelectorEntry(s, styleRule);
401404
entries.add(selectorEntry);
402405

406+
final String elementName = elementSelector.getLocalName();
403407
if (elementName != null) {
404408
final String key = elementName + "." + className;
405409
entries = classSelectors_.get(key);

src/main/java/com/gargoylesoftware/css/parser/selector/ElementSelector.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.io.Serializable;
1818
import java.util.ArrayList;
1919
import java.util.List;
20+
import java.util.Locale;
2021

2122
import com.gargoylesoftware.css.parser.Locator;
2223
import com.gargoylesoftware.css.parser.condition.Condition;
@@ -30,7 +31,12 @@ public class ElementSelector extends AbstractSelector implements SimpleSelector,
3031
private List<Condition> conditions_;
3132

3233
public ElementSelector(final String localName, final Locator locator) {
33-
localName_ = localName;
34+
if (localName != null) {
35+
localName_ = localName.toLowerCase(Locale.ROOT);
36+
}
37+
else {
38+
localName_ = null;
39+
}
3440
setLocator(locator);
3541
}
3642

@@ -44,6 +50,9 @@ public SimpleSelector getSimpleSelector() {
4450
return this;
4551
}
4652

53+
/**
54+
* @return the local name in lower case.
55+
*/
4756
public String getLocalName() {
4857
return localName_;
4958
}

src/test/java/com/gargoylesoftware/css/TestException.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,28 @@ public void test() throws Exception {
5757

5858
Assert.assertEquals("@charset \"US-ASCII\";", rules.item(0).getCssText());
5959
Assert.assertEquals("@import url(http://www.steadystate.com/primary.css);", rules.item(1).getCssText());
60-
Assert.assertEquals("P { color: blue }", rules.item(2).getCssText());
60+
Assert.assertEquals("p { color: blue }", rules.item(2).getCssText());
6161

6262
stylesheet.deleteRule(1);
6363

6464
Assert.assertEquals(2, rules.getLength());
6565
Assert.assertEquals("@charset \"US-ASCII\";", rules.item(0).getCssText());
66-
Assert.assertEquals("P { color: blue }", rules.item(1).getCssText());
66+
Assert.assertEquals("p { color: blue }", rules.item(1).getCssText());
6767

6868
CSSRule rule = rules.item(1);
69-
rule.setCssText("H2 { smell: strong }");
70-
Assert.assertEquals("H2 { smell: strong }", rules.item(1).getCssText());
69+
rule.setCssText("h2 { smell: strong }");
70+
Assert.assertEquals("h2 { smell: strong }", rules.item(1).getCssText());
7171

72-
final int n = stylesheet.insertRule("@media speech { H1 { voice: male } }", 1);
72+
final int n = stylesheet.insertRule("@media speech { h1 { voice: male } }", 1);
7373
Assert.assertEquals(1, n);
7474

7575
Assert.assertEquals(3, rules.getLength());
7676
Assert.assertEquals("@charset \"US-ASCII\";", rules.item(0).getCssText());
77-
Assert.assertEquals("@media speech {H1 { voice: male } }", rules.item(1).getCssText());
78-
Assert.assertEquals("H2 { smell: strong }", rules.item(2).getCssText());
77+
Assert.assertEquals("@media speech {h1 { voice: male } }", rules.item(1).getCssText());
78+
Assert.assertEquals("h2 { smell: strong }", rules.item(2).getCssText());
7979

8080
rule = rules.item(1);
81-
((CSSMediaRule) rule).insertRule("P { voice: female }", 1);
81+
((CSSMediaRule) rule).insertRule("p { voice: female }", 1);
8282
Assert.assertEquals("speech", ((CSSMediaRule) rule).getMedia().getMediaText());
8383

8484
// TODO

src/test/java/com/gargoylesoftware/css/parser/CSS3ParserTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,15 @@ public void selectorLang() throws Exception {
170170
Assert.assertEquals("de", ((LangCondition) selector.getConditions().get(0)).getLang());
171171

172172
rule = rules.item(2);
173-
Assert.assertEquals("*:lang(fr) > Q { }", rule.getCssText());
173+
Assert.assertEquals("*:lang(fr) > q { }", rule.getCssText());
174174
Assert.assertEquals(CSSRule.STYLE_RULE, rule.getType());
175175
ChildSelector childSelector = (ChildSelector) ((CSSStyleRuleImpl) rule).getSelectors().get(0);
176176
selector = (ElementSelector) childSelector.getAncestorSelector();
177177
Assert.assertEquals(ConditionType.LANG_CONDITION, selector.getConditions().get(0).getConditionType());
178178
Assert.assertEquals("fr", ((LangCondition) selector.getConditions().get(0)).getLang());
179179

180180
rule = rules.item(3);
181-
Assert.assertEquals("*:lang(de) > Q { }", rule.getCssText());
181+
Assert.assertEquals("*:lang(de) > q { }", rule.getCssText());
182182
Assert.assertEquals(CSSRule.STYLE_RULE, rule.getType());
183183
childSelector = (ChildSelector) ((CSSStyleRuleImpl) rule).getSelectors().get(0);
184184
selector = (ElementSelector) childSelector.getAncestorSelector();
@@ -1124,32 +1124,32 @@ public void counter() throws Exception {
11241124
Assert.assertEquals(5, rules.getLength());
11251125

11261126
CSSRule rule = rules.item(0);
1127-
Assert.assertEquals("H1:before { content: counter(chno, upper-latin) \". \" }", rule.getCssText());
1127+
Assert.assertEquals("h1:before { content: counter(chno, upper-latin) \". \" }", rule.getCssText());
11281128
Assert.assertEquals(CSSRule.STYLE_RULE, rule.getType());
11291129
CSSValueImpl value = (CSSValueImpl) ((CSSStyleRule) rule).getStyle().getPropertyCSSValue("content");
11301130
Assert.assertEquals("counter(chno, upper-latin)", ((CSSValueImpl) value.item(0)).getCounterValue().toString());
11311131

11321132
rule = rules.item(1);
1133-
Assert.assertEquals("H2:before { content: counter(section, upper-roman) \" - \" }", rule.getCssText());
1133+
Assert.assertEquals("h2:before { content: counter(section, upper-roman) \" - \" }", rule.getCssText());
11341134
Assert.assertEquals(CSSRule.STYLE_RULE, rule.getType());
11351135
value = (CSSValueImpl) ((CSSStyleRule) rule).getStyle().getPropertyCSSValue("content");
11361136
Assert.assertEquals("counter(section, upper-roman)",
11371137
((CSSValueImpl) value.item(0)).getCounterValue().toString());
11381138

11391139
rule = rules.item(2);
1140-
Assert.assertEquals("BLOCKQUOTE:after { content: \" [\" counter(bq, lower-greek) \"]\" }", rule.getCssText());
1140+
Assert.assertEquals("blockquote:after { content: \" [\" counter(bq, lower-greek) \"]\" }", rule.getCssText());
11411141
Assert.assertEquals(CSSRule.STYLE_RULE, rule.getType());
11421142
value = (CSSValueImpl) ((CSSStyleRule) rule).getStyle().getPropertyCSSValue("content");
11431143
Assert.assertEquals("counter(bq, lower-greek)", ((CSSValueImpl) value.item(1)).getCounterValue().toString());
11441144

11451145
rule = rules.item(3);
1146-
Assert.assertEquals("DIV.note:before { content: counter(notecntr, disc) \" \" }", rule.getCssText());
1146+
Assert.assertEquals("div.note:before { content: counter(notecntr, disc) \" \" }", rule.getCssText());
11471147
Assert.assertEquals(CSSRule.STYLE_RULE, rule.getType());
11481148
value = (CSSValueImpl) ((CSSStyleRule) rule).getStyle().getPropertyCSSValue("content");
11491149
Assert.assertEquals("counter(notecntr, disc)", ((CSSValueImpl) value.item(0)).getCounterValue().toString());
11501150

11511151
rule = rules.item(4);
1152-
Assert.assertEquals("P:before { content: counter(p, none) }", rule.getCssText());
1152+
Assert.assertEquals("p:before { content: counter(p, none) }", rule.getCssText());
11531153
Assert.assertEquals(CSSRule.STYLE_RULE, rule.getType());
11541154
value = (CSSValueImpl) ((CSSStyleRule) rule).getStyle().getPropertyCSSValue("content");
11551155
Assert.assertEquals("counter(p, none)", value.getCounterValue().toString());
@@ -1170,7 +1170,7 @@ public void counters() throws Exception {
11701170
Assert.assertEquals(1, rules.getLength());
11711171

11721172
final CSSRule rule = rules.item(0);
1173-
Assert.assertEquals("LI:before { content: counters(item, \".\") \" \"; counter-increment: item }",
1173+
Assert.assertEquals("li:before { content: counters(item, \".\") \" \"; counter-increment: item }",
11741174
rule.getCssText());
11751175
Assert.assertEquals(CSSRule.STYLE_RULE, rule.getType());
11761176
final CSSValueImpl value = (CSSValueImpl) ((CSSStyleRule) rule).getStyle().getPropertyCSSValue("content");
@@ -2725,7 +2725,7 @@ public void cdoCdc() throws Exception {
27252725
Assert.assertEquals(15, rules.getLength());
27262726

27272727
CSSRule rule = rules.item(0);
2728-
Assert.assertEquals("OL { list-style-type: lower-alpha }", rule.getCssText());
2728+
Assert.assertEquals("ol { list-style-type: lower-alpha }", rule.getCssText());
27292729

27302730
rule = rules.item(1);
27312731
Assert.assertEquals("*.a { color: green; background: white none }", rule.getCssText());

src/test/java/com/gargoylesoftware/css/parser/CSSOMParserTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.io.Reader;
1818
import java.io.StringReader;
19+
import java.util.Locale;
1920

2021
import org.junit.Assert;
2122
import org.junit.Test;
@@ -82,7 +83,7 @@ public void parseStyleSheet() throws Exception {
8283
Assert.assertEquals(CSSRule.STYLE_RULE, rule.getType());
8384

8485
final CSSStyleRule sr = (CSSStyleRule) rule;
85-
Assert.assertEquals(testSelector_, sr.getSelectorText());
86+
Assert.assertEquals(testSelector_.toLowerCase(Locale.ROOT), sr.getSelectorText());
8687

8788
final CSSStyleDeclaration style = sr.getStyle();
8889
Assert.assertEquals(testItem_, style.item(0));
@@ -122,7 +123,7 @@ public void parseSelectors() throws Exception {
122123
final InputSource is = new InputSource(r);
123124
final SelectorList sl = new CSSOMParser().parseSelectors(is);
124125

125-
Assert.assertEquals(testSelector_, sl.get(0).toString());
126+
Assert.assertEquals(testSelector_.toLowerCase(Locale.ROOT), sl.get(0).toString());
126127
}
127128

128129
/**

0 commit comments

Comments
 (0)