diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-11-19 12:03:32 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-11-19 12:03:32 +0000 |
| commit | 6bef85a8fde03b50de4aa78064ab4c32ad54041c (patch) | |
| tree | 84bc1ab1c337f0ca045b1cb34487ec07f4c84c76 | |
| parent | 68d8c8140fb6ef1118fa2815243817f8e4f55b59 (diff) | |
Introduce matcher for style name
| -rw-r--r-- | lib/style-reader.js | 2 | ||||
| -rw-r--r-- | lib/styles/document-matchers.js | 14 | ||||
| -rw-r--r-- | test/document-to-html.tests.js | 6 | ||||
| -rw-r--r-- | test/style-reader.tests.js | 2 | ||||
| -rw-r--r-- | test/styles/document-matchers.tests.js | 9 |
5 files changed, 26 insertions, 7 deletions
diff --git a/lib/style-reader.js b/lib/style-reader.js index 2889554..fe80006 100644 --- a/lib/style-reader.js +++ b/lib/style-reader.js @@ -74,7 +74,7 @@ function documentMatcherRule() { lop.rules.tokenOfType("close-square-bracket") ).head(), function(styleName) { - return {styleName: styleName}; + return {styleName: documentMatchers.equalTo(styleName)}; } ); diff --git a/lib/styles/document-matchers.js b/lib/styles/document-matchers.js index bdc213c..38ac049 100644 --- a/lib/styles/document-matchers.js +++ b/lib/styles/document-matchers.js @@ -5,6 +5,7 @@ exports.italic = new Matcher("italic"); exports.underline = new Matcher("underline"); exports.strikethrough = new Matcher("strikethrough"); exports.commentReference = new Matcher("commentReference"); +exports.equalTo = equalTo; function paragraph(options) { @@ -29,7 +30,7 @@ function Matcher(elementType, options) { Matcher.prototype.matches = function(element) { return element.type === this._elementType && (this._styleId === undefined || element.styleId === this._styleId) && - (this._styleName === undefined || (element.styleName && element.styleName.toUpperCase() === this._styleName.toUpperCase())) && + (this._styleName === undefined || (element.styleName && this._styleName.operator(this._styleName.operand, element.styleName))) && (this._listIndex === undefined || isList(element, this._listIndex, this._listIsOrdered)); }; @@ -38,3 +39,14 @@ function isList(element, levelIndex, isOrdered) { element.numbering.level == levelIndex && element.numbering.isOrdered == isOrdered; } + +function equalTo(value) { + return { + operator: equals, + operand: value + }; +} + +function equals(first, second) { + return first.toUpperCase() === second.toUpperCase(); +} diff --git a/test/document-to-html.tests.js b/test/document-to-html.tests.js index b7a0251..2543779 100644 --- a/test/document-to-html.tests.js +++ b/test/document-to-html.tests.js @@ -69,7 +69,7 @@ test('uses style mappings to pick HTML element for docx paragraph', function() { var converter = new DocumentConverter({ styleMap: [ { - from: documentMatchers.paragraph({styleName: "Heading 1"}), + from: documentMatchers.paragraph({styleName: documentMatchers.equalTo("Heading 1")}), to: htmlPaths.topLevelElement("h1") } ] @@ -86,7 +86,7 @@ test('mappings for style names are case insensitive', function() { var converter = new DocumentConverter({ styleMap: [ { - from: documentMatchers.paragraph({styleName: "Heading 1"}), + from: documentMatchers.paragraph({styleName: documentMatchers.equalTo("Heading 1")}), to: htmlPaths.topLevelElement("h1") } ] @@ -301,7 +301,7 @@ test('run styles are converted to HTML if mapping exists', function() { var converter = new DocumentConverter({ styleMap: [ { - from: documentMatchers.run({styleName: "Heading 1 Char"}), + from: documentMatchers.run({styleName: documentMatchers.equalTo("Heading 1 Char")}), to: htmlPaths.elements(["strong"]) } ] diff --git a/test/style-reader.tests.js b/test/style-reader.tests.js index 372e649..85b85f1 100644 --- a/test/style-reader.tests.js +++ b/test/style-reader.tests.js @@ -99,7 +99,7 @@ test("styleReader.readDocumentMatcher", { "reads paragraph with style name": function() { assertDocumentMatcher( "p[style-name='Heading 1']", - documentMatchers.paragraph({styleName: "Heading 1"}) + documentMatchers.paragraph({styleName: documentMatchers.equalTo("Heading 1")}) ); }, diff --git a/test/styles/document-matchers.tests.js b/test/styles/document-matchers.tests.js index e36298c..9f2864b 100644 --- a/test/styles/document-matchers.tests.js +++ b/test/styles/document-matchers.tests.js @@ -19,12 +19,19 @@ test("paragraph style ID only matches paragraphs with that style ID", function() }); test("paragraph style name only matches paragraphs with that style name", function() { - var matcher = documentMatchers.paragraph({styleName: "Heading 1"}); + var matcher = documentMatchers.paragraph({styleName: documentMatchers.equalTo("Heading 1")}); assert.ok(!matcher.matches(new Paragraph())); assert.ok(matcher.matches(paragraphWithStyle("Heading1", "Heading 1"))); assert.ok(!matcher.matches(paragraphWithStyle("Heading2", "Heading 2"))); }); +test("paragraph style name matching is case insensitive", function() { + var matcher = documentMatchers.paragraph({styleName: documentMatchers.equalTo("heading 1")}); + assert.ok(!matcher.matches(new Paragraph())); + assert.ok(matcher.matches(paragraphWithStyle("Heading1", "heaDING 1"))); + assert.ok(!matcher.matches(paragraphWithStyle("Heading2", "heaDING 2"))); +}); + test("ordered-list(index) matches an ordered list with specified level index", function() { var matcher = documentMatchers.paragraph({list: {isOrdered: true, levelIndex: 1}}); assert.ok(!matcher.matches(new Paragraph())); |
