diff options
| author | Michael Williamson <mike@zwobble.org> | 2013-04-01 12:55:21 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2013-04-01 12:55:21 +0100 |
| commit | ad75779025dd2fdae2eab851e1be1c6e5b8955ef (patch) | |
| tree | 087f2c0d5db242347096c05808ece9f340f4f94a | |
| parent | c9f133942a541fbcae95720b5b0480b94e36c00f (diff) | |
Generate HTML for bold elements
| -rw-r--r-- | lib/documents.js | 14 | ||||
| -rw-r--r-- | lib/html-generator.js | 14 | ||||
| -rw-r--r-- | tests/documents.tests.js | 34 |
3 files changed, 50 insertions, 12 deletions
diff --git a/lib/documents.js b/lib/documents.js index eebd607..ba2e8ec 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -46,15 +46,23 @@ function DocumentConverter(options) { function styleForParagraph(element) { return paragraphStyleMap[element.properties.styleName] || defaultParagraphStyle; } + + function convertRun(run, html) { + var runHtml = new HtmlGenerator(); + if (run.properties.isBold) { + runHtml.style(styles.elements(["strong"])); + } + convertElements(run.children, runHtml); + runHtml.closeAll(); + html.append(runHtml); + } var elementConverters = { "document": function(document, html) { convertElements(document.children, html); }, "paragraph": convertParagraph, - "run": function(element, html) { - convertElements(element.children, html); - }, + "run": convertRun, "text": function(element, html) { html.text(element.value); } diff --git a/lib/html-generator.js b/lib/html-generator.js index af6cedc..38e3619 100644 --- a/lib/html-generator.js +++ b/lib/html-generator.js @@ -21,6 +21,15 @@ function HtmlGenerator() { return fragments.join(""); } + function append(otherHtmlGenerator) { + if (otherHtmlGenerator._fragments.length > 0) { + writeAll(); + } + otherHtmlGenerator._fragments.forEach(function(fragment) { + fragments.push(fragment); + }); + } + function style(style) { var firstFreshPartIndex = findIndex(style, function(part) { return part.fresh; @@ -82,7 +91,10 @@ function HtmlGenerator() { return { style: style, text: text, - asString: asString + closeAll: popAll, + asString: asString, + append: append, + _fragments: fragments }; } diff --git a/tests/documents.tests.js b/tests/documents.tests.js index 06faf10..9cd3a49 100644 --- a/tests/documents.tests.js +++ b/tests/documents.tests.js @@ -13,7 +13,7 @@ describe('DocumentConverter', function() { ]); var converter = new DocumentConverter(); return converter.convertToHtml(document).then(function(result) { - assert.equal("<p>Hello.</p>", result.html); + assert.equal(result.html, "<p>Hello.</p>"); }); }); @@ -33,7 +33,7 @@ describe('DocumentConverter', function() { ]); var converter = new DocumentConverter({defaultParagraphStyle: styles.topLevelElement("h1")}); return converter.convertToHtml(document).then(function(result) { - assert.equal("<h1>Hello.</h1>", result.html); + assert.equal(result.html, "<h1>Hello.</h1>"); }); }); @@ -43,7 +43,7 @@ describe('DocumentConverter', function() { ]); var converter = new DocumentConverter(); return converter.convertToHtml(document).then(function(result) { - assert.equal("<p>1 < 2</p>", result.html); + assert.equal(result.html, "<p>1 < 2</p>"); }); }); @@ -54,7 +54,7 @@ describe('DocumentConverter', function() { ]); var converter = new DocumentConverter(); return converter.convertToHtml(document).then(function(result) { - assert.equal("<p>Hello.</p><p>Goodbye.</p>", result.html); + assert.equal(result.html, "<p>Hello.</p><p>Goodbye.</p>"); }); }); @@ -68,7 +68,7 @@ describe('DocumentConverter', function() { } }); return converter.convertToHtml(document).then(function(result) { - assert.equal("<h1>Hello.</h1>", result.html); + assert.equal(result.html, "<h1>Hello.</h1>"); }); }); @@ -82,7 +82,25 @@ describe('DocumentConverter', function() { } }); return converter.convertToHtml(document).then(function(result) { - assert.equal("<h1><span>Hello.</span></h1>", result.html); + assert.equal(result.html, "<h1><span>Hello.</span></h1>"); + }); + }); + + test('bold runs are wrapped in <strong> tags', function() { + var run = runOfText("Hello.", {isBold: true}); + var converter = new DocumentConverter(); + return converter.convertToHtml(run).then(function(result) { + assert.equal(result.html, "<strong>Hello.</strong>"); + }); + }); + + test('bold runs can exist inside other tags', function() { + var run = new documents.Paragraph([ + runOfText("Hello.", {isBold: true}) + ]); + var converter = new DocumentConverter(); + return converter.convertToHtml(run).then(function(result) { + assert.equal(result.html, "<p><strong>Hello.</strong></p>"); }); }); }); @@ -94,7 +112,7 @@ function paragraphOfText(text, styleName) { }); } -function runOfText(text) { +function runOfText(text, properties) { var textElement = new documents.Text(text); - return new documents.Run([textElement]); + return new documents.Run([textElement], properties); } |
