diff options
| author | Michael Williamson <mike@zwobble.org> | 2013-03-29 23:04:44 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2013-03-29 23:04:44 +0000 |
| commit | 354a6ba8f34108236879c8af4280abea74409be5 (patch) | |
| tree | 03d9c404a3609a450068d9d78a7d875b90af3a54 | |
| parent | e51abdc358cef3655f074f4ab0e6000623ce4749 (diff) | |
Allow reading of multiple classes on style
| -rw-r--r-- | lib/style-reader.js | 15 | ||||
| -rw-r--r-- | tests/style-reader.tests.js | 14 |
2 files changed, 28 insertions, 1 deletions
diff --git a/lib/style-reader.js b/lib/style-reader.js index d2b6fbc..ca4e18a 100644 --- a/lib/style-reader.js +++ b/lib/style-reader.js @@ -4,5 +4,18 @@ exports.read = read; function read(string) { - return styles.elements(string.split(/\s+/)); + var elementStrings = string.split(/\s+/); + var elements = elementStrings.map(readElement); + return styles.elements(elements); +} + +function readElement(string) { + var parts = string.split("."); + var tagName = parts[0]; + var classNames = parts.slice(1); + var options = {}; + if (classNames.length > 0) { + options["class"] = classNames.join(" "); + } + return styles.element(tagName, options); } diff --git a/tests/style-reader.tests.js b/tests/style-reader.tests.js index 18aa263..10047dc 100644 --- a/tests/style-reader.tests.js +++ b/tests/style-reader.tests.js @@ -12,4 +12,18 @@ describe('styleReader.read', function() { test('reads nested elements', function() { assert.deepEqual(styleReader.read("ul li"), styles.elements(["ul", "li"])); }); + + test('reads class on element', function() { + var expected = styles.elements([ + styles.element("p", {"class": "tip"}) + ]) + assert.deepEqual(styleReader.read("p.tip"), expected); + }); + + test('reads multiple classes on element', function() { + var expected = styles.elements([ + styles.element("p", {"class": "tip help"}) + ]) + assert.deepEqual(styleReader.read("p.tip.help"), expected); + }); }) |
