Skip to content

Commit

Permalink
Issue #11: Fixed bug when parsing <style> element. These rules were i…
Browse files Browse the repository at this point in the history
…ncorrectly being treated as coming from the RenderOptions, instead of the document.

Fixed an issue where CSS selectors with no id or class were not being parsed correctly.
Fixed an issue where the root svg element wasn't matching the :first-child rule (it is the first child of the document)
  • Loading branch information
BigBadaboom committed Jul 11, 2018
1 parent 59de4b0 commit 905b704
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
34 changes: 22 additions & 12 deletions androidsvg/src/main/java/com/caverock/androidsvg/CSSParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ boolean isEmpty()
return this.rules == null || this.rules.isEmpty();
}

int ruleCount()
{
return (this.rules != null) ? this.rules.size() : 0;
}

/*
* Remove all rules that were addres from a give Source.
*/
Expand Down Expand Up @@ -218,7 +223,7 @@ public String toString()

static enum Source
{
Parser,
Document,
RenderOptions
}

Expand All @@ -239,7 +244,7 @@ static class Rule
@Override
public String toString()
{
return String.valueOf(selector) + " {}";
return String.valueOf(selector) + " {...} (src="+this.source+")";
}
}

Expand Down Expand Up @@ -272,16 +277,19 @@ boolean isEmpty()
}

// Methods for accumulating a specificity value as SimpleSelector entries are added.
// Number of ID selectors in the selector
void addedIdAttribute()
{
specificity += 10000;
specificity += 1000000;
}

// Number of class selectors, attributes selectors, and pseudo-classes
void addedAttributeOrPseudo()
{
specificity += 100;
specificity += 1000;
}

// Number of type (element) selectors and pseudo-elements
void addedElement()
{
specificity += 1;
Expand All @@ -293,7 +301,7 @@ public String toString()
StringBuilder sb = new StringBuilder();
for (SimpleSelector sel: selector)
sb.append(sel).append(' ');
return sb.append('(').append(specificity).append(')').toString();
return sb.append('[').append(specificity).append(']').toString();
}
}

Expand All @@ -304,7 +312,7 @@ public String toString()

CSSParser()
{
this(MediaType.screen, Source.Parser);
this(MediaType.screen, Source.Document);
}


Expand Down Expand Up @@ -470,14 +478,14 @@ boolean nextSimpleSelector(Selector selector) throws CSSParseException
throw new CSSParseException("Invalid \"#id\" selector");
selectorPart.addAttrib(ID, AttribOp.EQUALS, value);
selector.addedIdAttribute();
continue;
}

if (selectorPart == null)
break;

// Now check for attribute selection and pseudo selectors
// Now check for attribute selection and pseudo selectors
if (consume('['))
{
if (selectorPart == null)
selectorPart = new SimpleSelector(combinator, null);
skipWhitespace();
String attrName = nextIdentifier();
String attrValue = null;
Expand Down Expand Up @@ -507,6 +515,8 @@ else if (consume("|="))

if (consume(':'))
{
if (selectorPart == null)
selectorPart = new SimpleSelector(combinator, null);
// skip pseudo
int pseudoStart = position;
if (nextIdentifier() != null) {
Expand Down Expand Up @@ -1072,8 +1082,8 @@ else if (sel.combinator == Combinator.CHILD)

private static int getChildPosition(List<SvgContainer> ancestors, int ancestorsPos, SvgElementBase obj)
{
if (ancestorsPos < 0) // Has no parent, so can't have a sibling
return -1;
if (ancestorsPos < 0) // Has no parent, so must be only child of document
return 0;
if (ancestors.get(ancestorsPos) != obj.parent) // parent doesn't match, so obj must be an indirect reference (eg. from a <use>)
return -1;
int childPos = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4483,7 +4483,7 @@ private void style(Attributes attributes) throws SVGParseException

private void parseCSSStyleSheet(String sheet)
{
CSSParser cssp = new CSSParser(MediaType.screen, CSSParser.Source.RenderOptions);
CSSParser cssp = new CSSParser(MediaType.screen, CSSParser.Source.Document);
svgDocument.addCSSRules(cssp.parse(sheet));
}

Expand Down

0 comments on commit 905b704

Please sign in to comment.