Skip to content

Commit

Permalink
Added Evaluator toString tests, fixed formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jhy committed Dec 14, 2024
1 parent d5bbe25 commit 34fb153
Show file tree
Hide file tree
Showing 2 changed files with 311 additions and 47 deletions.
95 changes: 48 additions & 47 deletions src/main/java/org/jsoup/select/Evaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public boolean matches(Element root, Element element) {

@Override
public String toString() {
return String.format("%s", tagName);
return String.format("%s|*", tagName);
}
}

Expand All @@ -126,7 +126,7 @@ public boolean matches(Element root, Element element) {

@Override
public String toString() {
return String.format("%s", tagName);
return String.format("*|%s", tagName);
}
}

Expand Down Expand Up @@ -527,38 +527,40 @@ public String toString() {


public static abstract class CssNthEvaluator extends Evaluator {
protected final int a, b;
protected final int step, offset;

public CssNthEvaluator(int a, int b) {
this.a = a;
this.b = b;
}
public CssNthEvaluator(int b) {
this(0,b);
}
public CssNthEvaluator(int step, int offset) {
this.step = step;
this.offset = offset;
}

@Override
public boolean matches(Element root, Element element) {
final Element p = element.parent();
if (p == null || (p instanceof Document)) return false;
public CssNthEvaluator(int offset) {
this(0, offset);
}

final int pos = calculatePosition(root, element);
if (a == 0) return pos == b;
@Override
public boolean matches(Element root, Element element) {
final Element p = element.parent();
if (p == null || (p instanceof Document)) return false;

return (pos-b)*a >= 0 && (pos-b)%a==0;
}
final int pos = calculatePosition(root, element);
if (step == 0) return pos == offset;

@Override
public String toString() {
if (a == 0)
return String.format(":%s(%d)",getPseudoClass(), b);
if (b == 0)
return String.format(":%s(%dn)",getPseudoClass(), a);
return String.format(":%s(%dn%+d)", getPseudoClass(),a, b);
}
return (pos - offset) * step >= 0 && (pos - offset) % step == 0;
}

@Override
public String toString() {
if (step == 0)
return String.format(":%s(%d)", getPseudoClass(), offset);
if (offset == 0)
return String.format(":%s(%dn)", getPseudoClass(), step);
return String.format(":%s(%dn%+d)", getPseudoClass(), step, offset);
}

protected abstract String getPseudoClass();

protected abstract String getPseudoClass();
protected abstract int calculatePosition(Element root, Element element);
protected abstract int calculatePosition(Element root, Element element);
}


Expand All @@ -568,19 +570,19 @@ public String toString() {
* @see IndexEquals
*/
public static final class IsNthChild extends CssNthEvaluator {
public IsNthChild(int step, int offset) {
super(step, offset);
}

public IsNthChild(int a, int b) {
super(a,b);
}

@Override protected int calculatePosition(Element root, Element element) {
return element.elementSiblingIndex()+1;
}

@Override
protected int calculatePosition(Element root, Element element) {
return element.elementSiblingIndex() + 1;
}

@Override protected String getPseudoClass() {
return "nth-child";
}
@Override
protected String getPseudoClass() {
return "nth-child";
}
}

/**
Expand All @@ -589,9 +591,9 @@ public IsNthChild(int a, int b) {
* @see IndexEquals
*/
public static final class IsNthLastChild extends CssNthEvaluator {
public IsNthLastChild(int a, int b) {
super(a,b);
}
public IsNthLastChild(int step, int offset) {
super(step, offset);
}

@Override
protected int calculatePosition(Element root, Element element) {
Expand All @@ -611,8 +613,8 @@ protected String getPseudoClass() {
*
*/
public static class IsNthOfType extends CssNthEvaluator {
public IsNthOfType(int a, int b) {
super(a, b);
public IsNthOfType(int step, int offset) {
super(step, offset);
}

@Override protected int calculatePosition(Element root, Element element) {
Expand All @@ -637,9 +639,8 @@ protected String getPseudoClass() {
}

public static class IsNthLastOfType extends CssNthEvaluator {

public IsNthLastOfType(int a, int b) {
super(a, b);
public IsNthLastOfType(int step, int offset) {
super(step, offset);
}

@Override
Expand Down
Loading

0 comments on commit 34fb153

Please sign in to comment.