From 1113b8d7a8761f972a5584ed985c7114d071509c Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 18 Oct 2013 15:20:27 -0400 Subject: [PATCH] FIX: Don't double sanitize values, allow blockquotes with leading text --- .../javascripts/discourse/dialects/dialect.js | 88 ++++++++++++------- .../javascripts/discourse/dialects/html.js | 28 +++++- .../discourse/dialects/newline_dialect.js | 4 +- test/javascripts/components/markdown_test.js | 9 ++ 4 files changed, 92 insertions(+), 37 deletions(-) diff --git a/app/assets/javascripts/discourse/dialects/dialect.js b/app/assets/javascripts/discourse/dialects/dialect.js index a5ea056f906..d39b93f2ddc 100644 --- a/app/assets/javascripts/discourse/dialects/dialect.js +++ b/app/assets/javascripts/discourse/dialects/dialect.js @@ -7,7 +7,8 @@ var parser = window.BetterMarkdown, MD = parser.Markdown, dialect = MD.dialects.Discourse = MD.subclassDialect( MD.dialects.Gruber ), - initialized = false; + initialized = false, + emitters = []; /** Initialize our dialects for processing. @@ -21,6 +22,51 @@ function initializeDialects() { initialized = true; } +/** + Process the text nodes in the JsonML tree, calling any emitters that have + been added. + + @method processTextNodes + @param {Array} node the JsonML tree + @param {Object} event the parse node event data +**/ +function processTextNodes(node, event) { + if (node.length < 2) { return; } + + if (node[0] === '__RAW') { + return; + } + + var skipSanitize = []; + for (var j=1; j", + endTagIndex = first ? block.indexOf(endTag) : block.lastIndexOf(endTag); + + if (endTagIndex !== -1) { + endTagIndex += endTag.length; + + var leading = block.substr(0, endTagIndex), + trailing = block.substr(endTagIndex).replace(/^\s+/, ''); + + if (trailing.length) { + next.unshift(trailing); + } + + return [ leading ]; + } + }; Discourse.Dialect.registerBlock('html', function(block, next) { - var m = /^<([^>]+)\>/.exec(block); + // Fix manual blockquote paragraphing even though it's not strictly correct + var split = splitAtLast('blockquote', block, next, true); + if (split) { return split; } + + var m = /^<([^>]+)\>/m.exec(block); if (m && m[1]) { var tag = m[1].split(/\s/); if (tag && tag[0] && blockTags.indexOf(tag[0]) !== -1) { + split = splitAtLast(tag[0], block, next); + if (split) { return split; } return [ block.toString() ]; } } diff --git a/app/assets/javascripts/discourse/dialects/newline_dialect.js b/app/assets/javascripts/discourse/dialects/newline_dialect.js index 22c90f65ccf..d08a8a30f33 100644 --- a/app/assets/javascripts/discourse/dialects/newline_dialect.js +++ b/app/assets/javascripts/discourse/dialects/newline_dialect.js @@ -10,10 +10,11 @@ Discourse.Dialect.postProcessText(function (text, event) { if (linebreaks || (insideCounts.pre > 0)) { return; } if (text === "\n") { - // If the tage is just a new line, replace it with a `
` + // If the tag is just a new line, replace it with a `
` return [['br']]; } else { + // If the text node contains new lines, perhaps with text between them, insert the // `
` tags. var split = text.split(/\n+/); @@ -25,6 +26,7 @@ Discourse.Dialect.postProcessText(function (text, event) { if (i !== split.length-1) { replacement.push(['br']); } } } + return replacement; } } diff --git a/test/javascripts/components/markdown_test.js b/test/javascripts/components/markdown_test.js index 4e465dfd999..239748467f2 100644 --- a/test/javascripts/components/markdown_test.js +++ b/test/javascripts/components/markdown_test.js @@ -54,6 +54,14 @@ test("Line Breaks", function() { cooked("[] first choice\n[] second choice", "

[] first choice
[] second choice

", "it handles new lines correctly with [] options"); + + cooked("
evil
\ntrout", + "
evil
\n\n

trout

", + "it doesn't insert
after blockquotes"); + + cooked("leading
evil
\ntrout", + "leading
evil
\n\n

trout

", + "it doesn't insert
after blockquotes with leading text"); }); test("Paragraphs for HTML", function() { @@ -311,6 +319,7 @@ test("sanitize", function() { "

disney reddit

", "we can embed proper links"); + cooked("
a\n
\n", "
a\n\n
\n\n
", "it does not double sanitize"); }); test("URLs in BBCode tags", function() {