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

Nested At-Rules #376

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions org/w3c/css/css/CssRuleList.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@

package org.w3c.css.css;

import java.util.ArrayList;
import java.util.List;

import org.w3c.css.parser.AtRule;
import org.w3c.css.util.Messages;

import java.util.ArrayList;

public class CssRuleList {
public class CssRuleList implements ICssStyleRuleOrCssRuleList {

AtRule atRule;
ArrayList<CssStyleRule> rulelist;
ArrayList<ICssStyleRuleOrCssRuleList> rulelist;
public String pseudopage;
String indent;

public CssRuleList() {
atRule = null;
rulelist = new ArrayList<CssStyleRule>();
rulelist = new ArrayList<>();
indent = new String();
}

Expand All @@ -28,9 +29,40 @@ public void addStyleRule(CssStyleRule stylerule) {
}

public ArrayList<CssStyleRule> getStyleRules() {
ArrayList<CssStyleRule> r = new ArrayList<>(this.rulelist.size());
for(ICssStyleRuleOrCssRuleList u: this.rulelist) {
if (u instanceof CssStyleRule) {
r.add((CssStyleRule) u);
}
}
return r;
}

public List<ICssStyleRuleOrCssRuleList> getStyleRulesTree() {
return rulelist;
}

public List<CssStyleRule> getStyleRulesAllInTree() {
ArrayList<CssStyleRule> r = new ArrayList<>();
this.appendStyleRulesTree(r);
return r;
}

protected void appendStyleRulesTree(List<CssStyleRule> r) {
for(ICssStyleRuleOrCssRuleList u: this.rulelist) {
if (u instanceof CssStyleRule) {
r.add((CssStyleRule) u);
}else if (u instanceof CssRuleList) {
CssRuleList l = (CssRuleList) u;
l.appendStyleRulesTree(r);
}
}
}

/**
* This should be rather named setAtRule
* @param atRule
*/
public void addAtRule(AtRule atRule) {
this.atRule = atRule;
}
Expand Down Expand Up @@ -64,7 +96,7 @@ public String toString() {
ret.append(atRuleString);
ret.append(" {\n\n");
}
for (CssStyleRule styleRule : rulelist) {
for (ICssStyleRuleOrCssRuleList styleRule : rulelist) {
ret.append(styleRule);
}
if (atRuleString.length() != 0) {
Expand All @@ -74,4 +106,8 @@ public String toString() {
return ret.toString();
}

public void addSubRulelist(CssRuleList rulelist) {
this.rulelist.add(rulelist);
}

}
2 changes: 1 addition & 1 deletion org/w3c/css/css/CssStyleRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import java.util.ArrayList;

public class CssStyleRule {
public class CssStyleRule implements ICssStyleRuleOrCssRuleList {

public CssStyleRule(String indent, String selectors,
ArrayList<CssProperty> properties, boolean important) {
Expand Down
5 changes: 5 additions & 0 deletions org/w3c/css/css/ICssStyleRuleOrCssRuleList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.w3c.css.css;

public interface ICssStyleRuleOrCssRuleList {

}
37 changes: 28 additions & 9 deletions org/w3c/css/css/StyleSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Stack;
import java.util.Vector;

/**
* This class contains a style sheet with all rules, errors and warnings.
Expand All @@ -34,7 +37,9 @@ public class StyleSheet {
private Errors errors;
private Warnings warnings;
private String type;
private ArrayList<CssRuleList> atRuleList;
private ArrayList<CssRuleList> atRuleListFlat = new ArrayList<>();
private ArrayList<CssRuleList> atRuleListTree = new ArrayList<>();
private Stack<CssRuleList> ruleInsertStack = new Stack<CssRuleList>();
private boolean doNotAddRule;
private boolean doNotAddAtRule;
private static final boolean debug = false;
Expand All @@ -48,7 +53,6 @@ public StyleSheet() {
errors = new Errors();
warnings = new Warnings();
cascading = new CssCascadingOrder();
atRuleList = new ArrayList<>();
customProperties = new HashMap<>();
}

Expand Down Expand Up @@ -239,14 +243,23 @@ public void addCharSet(String charset) {
public void newAtRule(AtRule atRule) {
CssRuleList rulelist = new CssRuleList();
rulelist.addAtRule(atRule);
atRuleList.add(rulelist);

atRuleListFlat.add(rulelist);
if (! ruleInsertStack.isEmpty()) {
ruleInsertStack.peek().addSubRulelist(rulelist);
} else {
atRuleListTree.add(rulelist);
}
ruleInsertStack.add(rulelist);

indent += " ";
}

public void endOfAtRule() {
if (!doNotAddAtRule) {
CssRuleList rulelist = new CssRuleList();
atRuleList.add(rulelist); //for the new set of rules
// CssRuleList rulelist = new CssRuleList();
// atRuleList.add(rulelist); //for the new set of rules
if (! ruleInsertStack.isEmpty()) ruleInsertStack.pop();
}
important = false;
selectortext = "";
Expand Down Expand Up @@ -282,13 +295,15 @@ public void endOfRule() {
if (!doNotAddRule) {
CssStyleRule stylerule = new CssStyleRule(indent, selectortext,
properties, important);
if (!atRuleList.isEmpty()) {
rulelist = atRuleList.remove(atRuleList.size() - 1);
if (!ruleInsertStack.isEmpty()) {
rulelist = ruleInsertStack.peek();
} else {
rulelist = new CssRuleList();
ruleInsertStack.add(rulelist);
atRuleListTree.add(rulelist);
atRuleListFlat.add(rulelist);
}
rulelist.addStyleRule(stylerule);
atRuleList.add(rulelist);
}
selectortext = "";
doNotAddRule = false;
Expand All @@ -303,12 +318,16 @@ public void removeThisAtRule() {
}

public ArrayList<CssRuleList> newGetRules() {
return atRuleList;
return atRuleListFlat;
}
public List<CssRuleList> newGetRulesTree() {
return this.atRuleListTree;
}

String selectortext;
boolean important;
ArrayList<CssProperty> properties;
String indent = new String();
public String charset;

}