Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact: refactor org.eclipse.tm4e.core.internal.theme package #695

Merged
merged 9 commits into from
Feb 12, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) 2024 Vegard IT GmbH and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Sebastian Thomschke (Vegard IT) - initial implementation
*/
package org.eclipse.tm4e.core.internal.theme.css;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.eclipse.tm4e.core.theme.RGB;
import org.eclipse.tm4e.core.theme.css.CSSParser;
import org.junit.jupiter.api.Test;

class CSSParserTest {

@Test
void testCSSParser() throws Exception {
final var parser = new CSSParser("""
.invalid { background-color: rgb(255,128,128); }
.storage.invalid { background-color: rgb(255,0,0); }
""");

assertEquals(null, parser.getBestStyle("undefined"));
assertEquals(new RGB(255,128,128), parser.getBestStyle("invalid").getBackgroundColor());
assertEquals(new RGB(255,0,0), parser.getBestStyle("storage", "invalid").getBackgroundColor());
assertEquals(new RGB(255,0,0), parser.getBestStyle("storage" , "modifier", "invalid", "deprecated").getBackgroundColor());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,59 +25,57 @@ public class Style implements IStyle {
private boolean underline;
private boolean strikeThrough;

public void setColor(final RGB color) {
this.color = color;
}

@Nullable
@Override
public RGB getColor() {
public @Nullable RGB getColor() {
return color;
}

@Nullable
@Override
public RGB getBackgroundColor() {
return backgroundColor;
public void setColor(final @Nullable RGB value) {
color = value;
}

public void setBackgroundColor(final RGB backgroundColor) {
this.backgroundColor = backgroundColor;
@Override
public @Nullable RGB getBackgroundColor() {
return backgroundColor;
}

public void setBold(final boolean bold) {
this.bold = bold;
public void setBackgroundColor(final @Nullable RGB value) {
backgroundColor = value;
}

@Override
public boolean isBold() {
return bold;
}

public void setItalic(final boolean italic) {
this.italic = italic;
public void setBold(final boolean enabled) {
bold = enabled;
}

@Override
public boolean isItalic() {
return italic;
}

public void setItalic(final boolean enabled) {
italic = enabled;
}

@Override
public boolean isUnderline() {
return underline;
}

public void setUnderline(final boolean underline) {
this.underline = underline;
public void setUnderline(final boolean enabled) {
underline = enabled;
}

@Override
public boolean isStrikeThrough() {
return strikeThrough;
}

public void setStrikeThrough(final boolean strikeThrough) {
this.strikeThrough = strikeThrough;
public void setStrikeThrough(final boolean enabled) {
strikeThrough = enabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,4 @@ public Condition getFirstCondition() {
public Condition getSecondCondition() {
return secondCondition;
}

@Override
public int getSpecificity() {
return firstCondition.getSpecificity() + secondCondition.getSpecificity();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ public int nbClass() {
}

@Override
public int nbMatch(final String... names) {
return firstCondition.nbMatch(names) + secondCondition.nbMatch(names);
public int nbMatch(final String... cssClassNames) {
return firstCondition.nbMatch(cssClassNames) + secondCondition.nbMatch(cssClassNames);
}

@Override
public String toString() {
return "(" + getFirstCondition() + " and " + getSecondCondition() + ")";
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,22 @@
package org.eclipse.tm4e.core.internal.theme.css;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tm4e.core.internal.theme.css.util.AbstractAttributeCondition;

final class CSSClassCondition extends CSSAttributeCondition {
final class CSSClassCondition extends AbstractAttributeCondition implements ExtendedCondition {

CSSClassCondition(@Nullable final String localName, final String namespaceURI, final String value) {
super(localName, namespaceURI, true, value);
protected CSSClassCondition(final @Nullable String localName, final @Nullable String namespaceURI, final String value) {
super(localName, namespaceURI, value);
}

@Override
public short getConditionType() {
return SAC_CLASS_CONDITION;
}

@Override
public boolean getSpecified() {
return true;
}

@Override
Expand All @@ -25,13 +36,18 @@ public int nbClass() {
}

@Override
public int nbMatch(final String... names) {
public int nbMatch(final String... cssClassNames) {
final String value = getValue();
for (final String name : names) {
for (final String name : cssClassNames) {
if (name.equals(value)) {
return 1;
}
}
return 0;
}

@Override
public String toString() {
return "CSSClass=='" + getValue() + "'";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,15 @@
package org.eclipse.tm4e.core.internal.theme.css;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.tm4e.core.internal.theme.css.util.AbstractConditionFactory;
import org.w3c.css.sac.AttributeCondition;
import org.w3c.css.sac.CSSException;
import org.w3c.css.sac.CombinatorCondition;
import org.w3c.css.sac.Condition;
import org.w3c.css.sac.ConditionFactory;
import org.w3c.css.sac.ContentCondition;
import org.w3c.css.sac.LangCondition;
import org.w3c.css.sac.NegativeCondition;
import org.w3c.css.sac.PositionalCondition;

@NonNullByDefault({})
public final class CSSConditionFactory implements ConditionFactory {

private static final String NOT_IMPLEMENTED_IN_CSS2 = "Not implemented in CSS2";
public final class CSSConditionFactory extends AbstractConditionFactory {

public static final ConditionFactory INSTANCE = new CSSConditionFactory();

Expand All @@ -34,72 +29,8 @@ public CombinatorCondition createAndCondition(final Condition first, final Condi
return new CSSAndCondition((ExtendedCondition) first, (ExtendedCondition) second);
}

@Override
public AttributeCondition createAttributeCondition(final String localName, final String namespaceURI, final boolean specified,
final String value) throws CSSException {
return new CSSAttributeCondition(localName, namespaceURI, specified, value);
}

@Override
public AttributeCondition createBeginHyphenAttributeCondition(final String arg0, final String arg1, final boolean arg2,
final String arg3) throws CSSException {
throw new CSSException(NOT_IMPLEMENTED_IN_CSS2);
}

@Override
public AttributeCondition createClassCondition(final String namespaceURI, final String value) throws CSSException {
return new CSSClassCondition(null, "class", value);
}

@Override
public ContentCondition createContentCondition(final String arg0) throws CSSException {
throw new CSSException(NOT_IMPLEMENTED_IN_CSS2);
}

@Override
public AttributeCondition createIdCondition(final String arg0) throws CSSException {
throw new CSSException(NOT_IMPLEMENTED_IN_CSS2);
}

@Override
public LangCondition createLangCondition(final String arg0) throws CSSException {
throw new CSSException(NOT_IMPLEMENTED_IN_CSS2);
}

@Override
public NegativeCondition createNegativeCondition(final Condition arg0) throws CSSException {
throw new CSSException(NOT_IMPLEMENTED_IN_CSS2);
}

@Override
public AttributeCondition createOneOfAttributeCondition(final String arg0, final String arg1, final boolean arg2, final String arg3)
throws CSSException {
throw new CSSException(NOT_IMPLEMENTED_IN_CSS2);
}

@Override
public Condition createOnlyChildCondition() throws CSSException {
throw new CSSException(NOT_IMPLEMENTED_IN_CSS2);
}

@Override
public Condition createOnlyTypeCondition() throws CSSException {
throw new CSSException(NOT_IMPLEMENTED_IN_CSS2);
}

@Override
public CombinatorCondition createOrCondition(final Condition arg0, final Condition arg1) throws CSSException {
throw new CSSException(NOT_IMPLEMENTED_IN_CSS2);
}

@Override
public PositionalCondition createPositionalCondition(final int arg0, final boolean arg1, final boolean arg2)
throws CSSException {
throw new CSSException(NOT_IMPLEMENTED_IN_CSS2);
}

@Override
public AttributeCondition createPseudoClassCondition(final String arg0, final String arg1) throws CSSException {
throw new CSSException(NOT_IMPLEMENTED_IN_CSS2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,13 @@ public SimpleSelector getSimpleSelector() {
return selector;
}

@Override
public int getSpecificity() {
return selector.getSpecificity() + condition.getSpecificity();
}

@Override
public int nbClass() {
return selector.nbClass() + condition.nbClass();
}

@Override
public int nbMatch(final String... names) {
return selector.nbMatch(names) + condition.nbMatch(names);
public int nbMatch(final String... cssClassNames) {
return selector.nbMatch(cssClassNames) + condition.nbMatch(cssClassNames);
}
}
Loading
Loading