diff options
| author | Michael Williamson <mike@zwobble.org> | 2013-05-03 22:01:34 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2013-05-03 22:01:34 +0100 |
| commit | 9f2a2399cdabf91d0766c9a6ddbeed0a4e2597cf (patch) | |
| tree | b94b0e78f88fb1b68a1045ca3b7dba8d1ad0ef1f | |
| parent | 4746d8f52c7287f9f71f393f6a5220130ed3d984 (diff) | |
Add parsing for styles
| -rw-r--r-- | lib/style-reader.js | 39 | ||||
| -rw-r--r-- | tests/style-reader.tests.js | 13 |
2 files changed, 45 insertions, 7 deletions
diff --git a/lib/style-reader.js b/lib/style-reader.js index be5ab20..423d9d0 100644 --- a/lib/style-reader.js +++ b/lib/style-reader.js @@ -6,9 +6,33 @@ var htmlPaths = require("./html-paths"); exports.readHtmlPath = readHtmlPath; exports.readDocumentMatcher = readDocumentMatcher; +exports.readStyle = readStyle; +function readStyle(string) { + return parseString(styleRule(), string); +} + +function styleRule() { + return lop.rules.sequence( + lop.rules.sequence.capture(documentMatcherRule()), + lop.rules.tokenOfType("whitespace"), + lop.rules.tokenOfType("arrow"), + lop.rules.tokenOfType("whitespace"), + lop.rules.sequence.capture(htmlPathRule()) + ).map(function(documentMatcher, htmlPath) { + return { + from: documentMatcher, + to: htmlPath + }; + }); +} + function readDocumentMatcher(string) { + return parseString(documentMatcherRule(), string); +} + +function documentMatcherRule() { var sequence = lop.rules.sequence; var paragraphRule = lop.rules.then( @@ -30,18 +54,19 @@ function readDocumentMatcher(string) { runRule ); - var matcherRule = sequence( + return sequence( sequence.capture(elementTypeRule), sequence.capture(lop.rules.optional(classRule)) ).map(function(createMatcher, styleName) { return createMatcher(styleName.valueOrElse()); }); - - return parseString(matcherRule, string); } - function readHtmlPath(string) { + return parseString(htmlPathRule(), string); +} + +function htmlPathRule() { var capture = lop.rules.sequence.capture; var whitespaceRule = lop.rules.tokenOfType("whitespace"); var freshRule = lop.rules.then( @@ -72,7 +97,7 @@ function readHtmlPath(string) { return htmlPaths.element(tagName, attributes, options); }); - var styleRule = lop.rules.then( + return lop.rules.then( lop.rules.zeroOrMoreWithSeparator( styleElementRule, lop.rules.sequence( @@ -83,7 +108,6 @@ function readHtmlPath(string) { ), htmlPaths.elements ); - return parseString(styleRule, string); } var identifierRule = lop.rules.tokenOfType("identifier"); @@ -117,7 +141,8 @@ function tokenise(string) { {name: "dot", regex: /\./}, {name: "colon", regex: /:/}, {name: "gt", regex: />/}, - {name: "whitespace", regex: /\s+/} + {name: "whitespace", regex: /\s+/}, + {name: "arrow", regex: /=>/} ]); return tokeniser.tokenise(string); } diff --git a/tests/style-reader.tests.js b/tests/style-reader.tests.js index c9bfe5b..d406f48 100644 --- a/tests/style-reader.tests.js +++ b/tests/style-reader.tests.js @@ -7,6 +7,7 @@ var test = require("./testing").test; var readHtmlPath = styleReader.readHtmlPath; var readDocumentMatcher = styleReader.readDocumentMatcher; +var readStyle = styleReader.readStyle; describe('styleReader.readHtmlPath', function() { @@ -59,3 +60,15 @@ describe("styleReader.readDocumentMatcher", function() { ); }); }); + +describe("styleReader.read", function() { + test("document matcher is mapped to HTML path using arrow", function() { + assert.deepEqual( + readStyle("p => h1"), + { + from: documentMatchers.paragraph(), + to: htmlPaths.elements(["h1"]) + } + ); + }); +}); |
