diff --git a/src/main/java/org/jsoup/select/Evaluator.java b/src/main/java/org/jsoup/select/Evaluator.java index 3303684783..2f31c12103 100644 --- a/src/main/java/org/jsoup/select/Evaluator.java +++ b/src/main/java/org/jsoup/select/Evaluator.java @@ -80,6 +80,27 @@ public String toString() { } } + /** + * Evaluator for element namespace + */ + public static final class Namespace extends Evaluator { + private final String nameSpace; + + public Namespace(String nameSpace) { + this.nameSpace = nameSpace; + } + + @Override + public boolean matches(Element root, Element element) { + return (element.normalName().startsWith(nameSpace)); + } + + @Override + public String toString() { + return String.format("%s", nameSpace); + } + } + /** * Evaluator for element id */ diff --git a/src/main/java/org/jsoup/select/QueryParser.java b/src/main/java/org/jsoup/select/QueryParser.java index b049df3d74..ed47527883 100644 --- a/src/main/java/org/jsoup/select/QueryParser.java +++ b/src/main/java/org/jsoup/select/QueryParser.java @@ -162,6 +162,8 @@ private void findElements() { byId(); else if (tq.matchChomp(".")) byClass(); + else if (tq.toString().endsWith("|*")) + byNamespace(); else if (tq.matchesWord() || tq.matches("*|")) byTag(); else if (tq.matches("[")) @@ -262,6 +264,12 @@ private void byTag() { } } + private void byNamespace() { + String nameSpace = normalize(tq.consumeElementSelector()); + Validate.notEmpty(nameSpace); + evals.add(new Evaluator.Namespace(nameSpace.replace("|", ":"))); + } + private void byAttribute() { TokenQueue cq = new TokenQueue(tq.chompBalanced('[', ']')); // content queue String key = cq.consumeToAny(AttributeEvals); // eq, not, start, end, contain, match, (no val) @@ -312,7 +320,7 @@ private void indexGreaterThan() { private void indexEquals() { evals.add(new Evaluator.IndexEquals(consumeIndex())); } - + //pseudo selectors :first-child, :last-child, :nth-child, ... private static final Pattern NTH_AB = Pattern.compile("(([+-])?(\\d+)?)n(\\s*([+-])?\\s*\\d+)?", Pattern.CASE_INSENSITIVE); private static final Pattern NTH_B = Pattern.compile("([+-])?(\\d+)"); diff --git a/src/main/java/org/jsoup/select/Selector.java b/src/main/java/org/jsoup/select/Selector.java index 71d0bc2a19..cf3adc9ce1 100644 --- a/src/main/java/org/jsoup/select/Selector.java +++ b/src/main/java/org/jsoup/select/Selector.java @@ -27,6 +27,7 @@ *
tag
div
*|E
*|name
finds <fb:name>
and <name>
elementsns|E
fb|name
finds <fb:name>
elementsns|*
fb|*
finds <fb:name>
and <fb:school>
elements#id
div#wrap
, #logo
.class
div.left
, .result
[attr]
a[href]
, [title]