diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-11-19 12:16:34 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-11-19 12:16:34 +0000 |
| commit | 2df392487052d543fe412383ef6d29886678edbf (patch) | |
| tree | 427c93ee2128c546076c32a9814efa094bde8968 | |
| parent | f3676789408dfaf898823c1fbe3cb7d6888a1fd4 (diff) | |
Support parsing style name prefixes
| -rw-r--r-- | lib/style-reader.js | 43 | ||||
| -rw-r--r-- | lib/styles/parser/tokeniser.js | 1 | ||||
| -rw-r--r-- | test/style-reader.tests.js | 9 |
3 files changed, 39 insertions, 14 deletions
diff --git a/lib/style-reader.js b/lib/style-reader.js index fe80006..20cdb56 100644 --- a/lib/style-reader.js +++ b/lib/style-reader.js @@ -63,21 +63,38 @@ function documentMatcherRule() { return {styleId: styleId}; } ); - - var styleNameRule = lop.rules.then( - lop.rules.sequence( - lop.rules.tokenOfType("open-square-bracket"), - lop.rules.sequence.cut(), - lop.rules.token("identifier", "style-name"), - lop.rules.tokenOfType("equals"), - lop.rules.sequence.capture(stringRule), - lop.rules.tokenOfType("close-square-bracket") - ).head(), - function(styleName) { - return {styleName: documentMatchers.equalTo(styleName)}; - } + + var styleNameMatcherRule = lop.rules.firstOf("style name matcher", + lop.rules.then( + lop.rules.sequence( + lop.rules.tokenOfType("equals"), + lop.rules.sequence.cut(), + lop.rules.sequence.capture(stringRule) + ).head(), + function(styleName) { + return {styleName: documentMatchers.equalTo(styleName)}; + } + ), + lop.rules.then( + lop.rules.sequence( + lop.rules.tokenOfType("startsWith"), + lop.rules.sequence.cut(), + lop.rules.sequence.capture(stringRule) + ).head(), + function(styleName) { + return {styleName: documentMatchers.startsWith(styleName)}; + } + ) ); + var styleNameRule = lop.rules.sequence( + lop.rules.tokenOfType("open-square-bracket"), + lop.rules.sequence.cut(), + lop.rules.token("identifier", "style-name"), + lop.rules.sequence.capture(styleNameMatcherRule), + lop.rules.tokenOfType("close-square-bracket") + ).head(); + var listTypeRule = lop.rules.firstOf("list type", identifierToConstant("ordered-list", {isOrdered: true}), diff --git a/lib/styles/parser/tokeniser.js b/lib/styles/parser/tokeniser.js index c60e134..fa45ff9 100644 --- a/lib/styles/parser/tokeniser.js +++ b/lib/styles/parser/tokeniser.js @@ -15,6 +15,7 @@ function tokenise(string) { {name: "whitespace", regex: /\s+/}, {name: "arrow", regex: /=>/}, {name: "equals", regex: /=/}, + {name: "startsWith", regex: /\^=/}, {name: "open-paren", regex: /\(/}, {name: "close-paren", regex: /\)/}, {name: "open-square-bracket", regex: /\[/}, diff --git a/test/style-reader.tests.js b/test/style-reader.tests.js index 85b85f1..6092bb9 100644 --- a/test/style-reader.tests.js +++ b/test/style-reader.tests.js @@ -96,13 +96,20 @@ test("styleReader.readDocumentMatcher", { ); }, - "reads paragraph with style name": function() { + "reads paragraph with exact style name": function() { assertDocumentMatcher( "p[style-name='Heading 1']", documentMatchers.paragraph({styleName: documentMatchers.equalTo("Heading 1")}) ); }, + "reads paragraph with style name prefix": function() { + assertDocumentMatcher( + "p[style-name^='Heading']", + documentMatchers.paragraph({styleName: documentMatchers.startsWith("Heading")}) + ); + }, + "reads p:ordered-list(1) as ordered list with index of 0": function() { assertDocumentMatcher( "p:ordered-list(1)", |
