Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed outermost element id selection bug

* Also added tests
  • Loading branch information...
commit 6617a60d590e4bdaf7821765973d903817ba0baf 1 parent ee61349
@siddMahen authored
Showing with 24 additions and 16 deletions.
  1. +1 −16 lib/soupselect.js
  2. +23 −0 test/test-regress-01.js
View
17 lib/soupselect.js
@@ -118,22 +118,7 @@ var _select = function(dom, selector) {
var id_selector = tokens[i].split('#', 2)[1];
// need to stop on the first id found (in bad HTML)...
- var el = null;
- for ( var k = 0; k < currentContext.length; k++ ) {
-
- // the document has no child elements but tags do so we search children to avoid
- // returning the current element via a false positive
- if ( typeof currentContext[k].children !== 'undefined' ) {
- el = domUtils.getElementById(id_selector, currentContext[k].children, true);
- } else {
- el = domUtils.getElementById(id_selector, currentContext[k], true);
- }
-
- if ( el ) {
- found.push(el);
- break;
- }
- }
+ found.push(domUtils.getElementById(id_selector, currentContext, true));
if (!found[0]) {
currentContext = [];
View
23 test/test-regress-01.js
@@ -0,0 +1,23 @@
+var htmlparser = require("htmlparser2"),
+ assert = require("assert"),
+ soupselect = require("../lib/soupselect");
+
+fruits = '"<ul id = "fruits">'+
+ '<li id = "apple">Apple</li>'+
+ '<li class = "orange">Orange</li>'+
+ '<li class = "pear">Pear</li>'+
+ '</ul>"';
+
+
+var handler = new htmlparser.DefaultHandler();
+ parser = new htmlparser.Parser(handler);
+
+parser.parseComplete(fruits);
+var dom = handler.dom;
+
+suite("#select()", function(){
+ test("should return the outermost element when asked for it's id", function(){
+ assert.deepEqual([dom[1]], soupselect.select(dom, "#fruits"));
+ });
+});
+
Please sign in to comment.
Something went wrong with that request. Please try again.