diff options
| author | Michael Williamson <mike@zwobble.org> | 2013-05-04 16:13:56 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2013-05-04 16:13:56 +0100 |
| commit | efca7fab18dfc9d6cc384bd133ecb57016784260 (patch) | |
| tree | 8daaf8ff47bfe6cd3deed07b1395d0bec82b3c82 | |
| parent | 8cf5cf90366a4f5f8e0efaf3bb39c0c4f8f512e0 (diff) | |
Allow HTML path element part to match multiple tag names
| -rw-r--r-- | lib/html-generator.js | 2 | ||||
| -rw-r--r-- | lib/html-paths.js | 29 | ||||
| -rw-r--r-- | tests/html-paths.tests.js | 15 |
3 files changed, 40 insertions, 6 deletions
diff --git a/lib/html-generator.js b/lib/html-generator.js index 3705767..62c58a8 100644 --- a/lib/html-generator.js +++ b/lib/html-generator.js @@ -47,7 +47,7 @@ function HtmlGenerator() { } function stylePartMatchesStackElement(stylePart, stackElement) { - return stackElement && stylePart.tagName === stackElement.tagName; + return stackElement && stylePart.matchesElement(stackElement); } function open(htmlPathElement) { diff --git a/lib/html-paths.js b/lib/html-paths.js index 1d3b1f4..9ecb6dd 100644 --- a/lib/html-paths.js +++ b/lib/html-paths.js @@ -20,10 +20,29 @@ function elements(elementStyles) { function element(tagName, attributes, options) { options = options || {}; - return { - tagName: tagName, - attributes: attributes || {}, - fresh: options.fresh - }; + return new Element(tagName, attributes, options); } +function Element(tagName, attributes, options) { + var tagNames = {}; + if (_.isArray(tagName)) { + tagName.forEach(function(tagName) { + tagNames[tagName] = true; + }); + tagName = tagName[0]; + } else { + tagNames[tagName] = true; + } + + this.tagName = tagName; + this.tagNames = tagNames; + this.attributes = attributes || {}; + this.fresh = options.fresh; +} + +Element.prototype.matchesElement = function(element) { + return this.tagNames[element.tagName]; +} + + + diff --git a/tests/html-paths.tests.js b/tests/html-paths.tests.js new file mode 100644 index 0000000..7e3ed86 --- /dev/null +++ b/tests/html-paths.tests.js @@ -0,0 +1,15 @@ +var assert = require("assert"); + +var test = require("./testing").test; +var htmlPaths = require("../lib/html-paths"); + + +describe("html-paths", function() { + test("element can match multiple tag names", function() { + var pathPart = htmlPaths.element(["ul", "ol"]); + assert.ok(pathPart.matchesElement({tagName: "ul"})); + assert.ok(pathPart.matchesElement({tagName: "ol"})); + assert.ok(!pathPart.matchesElement({tagName: "p"})); + }); +}); + |
