diff --git a/app/assets/javascripts/discourse/dialects/dialect.js b/app/assets/javascripts/discourse/dialects/dialect.js index ddb84b45a8d..749b289fced 100644 --- a/app/assets/javascripts/discourse/dialects/dialect.js +++ b/app/assets/javascripts/discourse/dialects/dialect.js @@ -11,7 +11,8 @@ var parser = window.BetterMarkdown, dialect = MD.dialects.Discourse = DialectHelpers.subclassDialect( MD.dialects.Gruber ), initialized = false, emitters = [], - hoisted; + hoisted, + preProcessors = []; /** Initialize our dialects for processing. @@ -165,6 +166,11 @@ Discourse.Dialect = { **/ cook: function(text, opts) { if (!initialized) { initializeDialects(); } + + preProcessors.forEach(function(p) { + text = p(text); + }); + hoisted = {}; dialect.options = opts; var tree = parser.toHTMLTree(text, 'Discourse'), @@ -188,6 +194,14 @@ Discourse.Dialect = { return result.trim(); }, + /** + Adds a text pre-processor. Use only if necessary, as a dialect + that emits JsonML is much better if possible. + **/ + addPreProcessor: function(preProc) { + preProcessors.push(preProc); + }, + /** Registers an inline replacer function diff --git a/app/assets/javascripts/discourse/lib/markdown.js b/app/assets/javascripts/discourse/lib/markdown.js index 89f9b471fe2..b9861f78147 100644 --- a/app/assets/javascripts/discourse/lib/markdown.js +++ b/app/assets/javascripts/discourse/lib/markdown.js @@ -9,7 +9,8 @@ **/ var _validClasses = {}, _validIframes = [], - _validTags = {}; + _validTags = {}, + _decoratedCaja = false; function validateAttribute(tagName, attribName, value) { var tag = _validTags[tagName]; @@ -198,6 +199,20 @@ Discourse.Markdown = { // Allow things like <3 and <_< text = text.replace(/<([^A-Za-z\/\!]|$)/g, "<$1"); + // The first time, let's add some more whitelisted tags + if (!_decoratedCaja) { + + // Add anything whitelisted to the list of elements if it's not in there + // already. + var elements = window.html4.ELEMENTS; + Ember.keys(_validTags).forEach(function(t) { + if (!elements[t]) { + elements[t] = 0; + } + }); + _decoratedCaja = true; + } + return window.html_sanitize(text, Discourse.Markdown.urlAllowed, validateAttribute); },