-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Add selecting elements by namespace (#1811) #1847
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I would just extend the byTag method to support this. |
||
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+)"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1399,6 +1399,16 @@ public void testNamespacedElements() { | |
assertEquals("html > body > fb|comments", els.get(0).cssSelector()); | ||
} | ||
|
||
@Test | ||
public void testSelectByNamespace() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be in SelectorTest, and needs to be more detailed -- e.g. to catch the example I pointed out, and validate other variations work. |
||
String html = "<html><body><ns:p>p in namespace ns</ns:p><ns:img>img in namespace ns</ns:img></body></html>"; | ||
Document doc = Jsoup.parse(html); | ||
Elements els = doc.select("ns|*"); | ||
assertEquals(2, els.size()); | ||
assertEquals("html > body > ns|p", els.get(0).cssSelector()); | ||
assertEquals("html > body > ns|img", els.get(1).cssSelector()); | ||
} | ||
|
||
@Test | ||
public void testChainedRemoveAttributes() { | ||
String html = "<a one two three four>Text</a>"; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the right way to do this. The TokenQueue must be consumed token by token. With this implementation, queries like
ns|* div
(or anything after the namespace wildcard selector) will fail. You will need an appropriate matcher in TokenQueue. I think you could update the current tag matcher.