summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-05-06 18:13:00 +0100
committerMichael Williamson <mike@zwobble.org>2013-05-06 18:13:00 +0100
commitf31e3c05a5bf4dc50a7f7f9146186a8653aeff3e (patch)
treec0d62aed0dfa2ec45f221db2b19db0e5be6efff4
parent2525686027ba49f212b617167d8f98b0990ea5c0 (diff)
Avoid stack overflows with long documents
-rw-r--r--lib/document-to-html.js4
-rw-r--r--tests/document-to-html.tests.js12
2 files changed, 15 insertions, 1 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js
index 94c8b6a..5c4db50 100644
--- a/lib/document-to-html.js
+++ b/lib/document-to-html.js
@@ -42,7 +42,9 @@ function DocumentConverter(options) {
function convertParagraph(element, html, messages, callback) {
html.satisfyPath(styleForParagraph(element, messages));
- convertElements(element.children, html, messages, callback);
+ process.nextTick(function() {
+ convertElements(element.children, html, messages, callback);
+ });
}
function styleForParagraph(element, messages) {
diff --git a/tests/document-to-html.tests.js b/tests/document-to-html.tests.js
index 447e0b5..fd6ef1a 100644
--- a/tests/document-to-html.tests.js
+++ b/tests/document-to-html.tests.js
@@ -237,6 +237,18 @@ describe('DocumentConverter', function() {
assert.equal(result.value, '<img alt="Not an image at all!" />');
});
});
+
+ test('long documents do not cause stack overflow', function() {
+ var paragraphs = [];
+ for (var i = 0; i < 1000; i++) {
+ paragraphs.push(paragraphOfText("Hello."));
+ }
+ var document = new documents.Document(paragraphs);
+ var converter = new DocumentConverter();
+ return converter.convertToHtml(document).then(function(result) {
+ assert.equal(result.value.indexOf("<p>Hello.</p>"), 0);
+ });
+ });
});
function paragraphOfText(text, styleName) {