From cd79d48d9799b715188a63461f2023eb4187ad39 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 14 Dec 2013 15:43:30 +0000 Subject: Consider attributes when matching elements --- lib/html-paths.js | 2 +- tests/html-paths.tests.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/html-paths.js b/lib/html-paths.js index 9ecb6dd..d2456c2 100644 --- a/lib/html-paths.js +++ b/lib/html-paths.js @@ -41,7 +41,7 @@ function Element(tagName, attributes, options) { } Element.prototype.matchesElement = function(element) { - return this.tagNames[element.tagName]; + return this.tagNames[element.tagName] && _.isEqual(this.attributes || {}, element.attributes || {}); } diff --git a/tests/html-paths.tests.js b/tests/html-paths.tests.js index 7e3ed86..76a77ef 100644 --- a/tests/html-paths.tests.js +++ b/tests/html-paths.tests.js @@ -11,5 +11,12 @@ describe("html-paths", function() { assert.ok(pathPart.matchesElement({tagName: "ol"})); assert.ok(!pathPart.matchesElement({tagName: "p"})); }); + + test("element matches if attributes are the same", function() { + var pathPart = htmlPaths.element(["p"], {"class": "tip"}); + assert.ok(!pathPart.matchesElement({tagName: "p"})); + assert.ok(!pathPart.matchesElement({tagName: "p", attributes: {"class": "tip help"}})); + assert.ok(pathPart.matchesElement({tagName: "p", attributes: {"class": "tip"}})); + }); }); -- cgit v1.2.3