Skip to content

Commit 2b1f567

Browse files
committed
Make NestingCondition a Singleton, simplify code
1 parent c4c8fc6 commit 2b1f567

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed

src/main/java/io/sf/carte/doc/style/css/parser/CSSParser.java

+4-14
Original file line numberDiff line numberDiff line change
@@ -3009,20 +3009,12 @@ protected void handleStarHack(int index, CharSequence word) {
30093009
.getParentManager();
30103010
PropertyNameTokenHandler starhackHandler = mgr.new PropertyNameTokenHandler() {
30113011

3012+
/**
3013+
* Callers must check for non-empty buffer.
3014+
*/
30123015
@Override
30133016
void processBuffer(int index, int triggerCp) {
3014-
String raw = buffer.toString();
3015-
if (!isEscapedIdent()) {
3016-
getManager().propertyName = raw;
3017-
buffer.setLength(0);
3018-
} else {
3019-
getManager().propertyName = unescapeBuffer(index);
3020-
if (!parseError && !isValidIdentifier(propertyName)) {
3021-
handleWarning(index - buffer.length(),
3022-
ParseHelper.WARN_PROPERTY_NAME,
3023-
"Suspicious property name: " + raw);
3024-
}
3025-
}
3017+
getManager().propertyName = unescapeBuffer(index);
30263018
setWhitespacePrevCp();
30273019
}
30283020

@@ -3340,7 +3332,6 @@ public void endManagement(int index) {
33403332
SelectorListImpl selist = selectorHandler.getSelectorList();
33413333
if (!selist.isEmpty()) {
33423334
handler.endSelector(selist);
3343-
//selectorHandler.selist = selectorHandler.new ParserSelectorListImpl();
33443335
}
33453336
resetFields();
33463337
yieldManagement(BlockContentsDeclarationManager.this);
@@ -3351,7 +3342,6 @@ public void endOfStream(int len) {
33513342
SelectorListImpl selist = selectorHandler.getSelectorList();
33523343
if (!selist.isEmpty()) {
33533344
handler.endSelector(selist);
3354-
//selectorHandler.selist = selectorHandler.new ParserSelectorListImpl();
33553345
}
33563346
BlockContentsManager.this.endOfStream(len);
33573347
}

src/main/java/io/sf/carte/doc/style/css/parser/NSACSelectorFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ Condition createCondition(Condition.ConditionType type) {
591591
case POSITIONAL:
592592
return createPositionalCondition();
593593
case NESTING:
594-
return new NestingCondition();
594+
return NestingCondition.getInstance();
595595
}
596596
return null;
597597
}

src/main/java/io/sf/carte/doc/style/css/parser/NestingCondition.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ class NestingCondition extends AbstractCondition {
1818

1919
private static final long serialVersionUID = 1L;
2020

21-
NestingCondition() {
21+
private static final NestingCondition instance = new NestingCondition();
22+
23+
private NestingCondition() {
2224
super();
2325
}
2426

27+
public static Condition getInstance() {
28+
return instance;
29+
}
30+
2531
@Override
2632
Condition replace(SelectorList base, MutableBoolean replaced) {
2733
replaced.setTrueValue();
@@ -38,6 +44,22 @@ public ConditionType getConditionType() {
3844
return ConditionType.NESTING;
3945
}
4046

47+
@Override
48+
public int hashCode() {
49+
return ConditionType.NESTING.hashCode();
50+
}
51+
52+
@Override
53+
public boolean equals(Object obj) {
54+
if (this == obj) {
55+
return true;
56+
}
57+
if (obj == null) {
58+
return false;
59+
}
60+
return getClass() == obj.getClass();
61+
}
62+
4163
@Override
4264
public String toString() {
4365
return "&";

src/test/java/io/sf/carte/doc/style/css/parser/SelectorListImplTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,22 @@ public void testReplaceNestedList() {
312312
assertEquals(":is(p:last-child,div.cls)", replaced.toString());
313313
}
314314

315+
@Test
316+
public void testEquals() {
317+
SelectorList selist = parseSelectors("p:last-child,div.cls");
318+
SelectorList selist2 = parseSelectors("p:last-child,div.cls");
319+
320+
assertEquals(selist, selist2);
321+
}
322+
323+
@Test
324+
public void testEqualsNested() {
325+
SelectorList selist = parseSelectors("&:last-child,&.cls");
326+
SelectorList selist2 = parseSelectors("&:last-child,&.cls");
327+
328+
assertEquals(selist, selist2);
329+
}
330+
315331
private SelectorList parseSelectors(String selist) throws CSSException {
316332
try {
317333
return parser.parseSelectors(new StringReader(selist));

0 commit comments

Comments
 (0)