diff options
| -rw-r--r-- | lib/documents.js | 10 | ||||
| -rw-r--r-- | tests/documents.tests.js | 10 |
2 files changed, 19 insertions, 1 deletions
diff --git a/lib/documents.js b/lib/documents.js index 5d2d0b7..b91c3b1 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -47,7 +47,7 @@ function DocumentConverter(options) { return element.children.map(elementToHtml).join(""); }, "text": function(element) { - return element.value; + return escapeHtml(element.value); } }; return { @@ -83,3 +83,11 @@ function Text(value) { value: value }; } + +function escapeHtml(value) { + return value + .replace(/&/g, '&') + .replace(/"/g, '"') + .replace(/</g, '<') + .replace(/>/g, '>'); +} diff --git a/tests/documents.tests.js b/tests/documents.tests.js index a5e81af..7161377 100644 --- a/tests/documents.tests.js +++ b/tests/documents.tests.js @@ -16,6 +16,16 @@ describe('DocumentConverter', function() { }); }); + test('text is HTML-escaped', function() { + var document = new documents.Document([ + paragraphOfText("1 < 2") + ]); + var converter = new DocumentConverter(); + return converter.convertToHtml(document).then(function(result) { + assert.equal("<p>1 < 2</p>", result.html); + }); + }); + test('should convert document containing multiple paragraphs to multiple p elements', function() { var document = new documents.Document([ paragraphOfText("Hello."), |
