diff --git a/js/core/markdown.js b/js/core/markdown.js index 530d912b4e..0a005e92f3 100644 --- a/js/core/markdown.js +++ b/js/core/markdown.js @@ -261,7 +261,11 @@ define([ processBlockLevelElements(newBody); var dirtyHTML = toHTML(newBody.innerHTML); // Markdown parsing sometimes inserts empty p tags - var cleanHTML = dirtyHTML.replace(/

\s*<\/p>/gm, ""); + var cleanHTML = dirtyHTML + .replace(/

\s*<\/p>/gm, "") + // beautifer has a bad time with "\n" - //

- // Next line has 2 spaces hidden! - // __
- // - // - var trimmedRight = rawText.trimRight(); - var trimBy = this.calculateLeftPad(trimmedRight) || baseCol; - if (!trimBy) { - return null; //nothing to do - } - var exp = "^ {" + trimBy + "}"; - var startTrim = new RegExp(exp, "gm"); - var trimmedText = (trimBy) ? rawText.replace(startTrim, "") : rawText; - var newNode = textNode.ownerDocument.createTextNode(trimmedText); - // We can then swap the old with the new - return { - oldNode: textNode, - newNode: newNode, - }; - }.bind(this)) - .filter(function(nodes) { - return nodes; - }) - .forEach(function(nodes) { - var oldNode = nodes.oldNode; - var newNode = nodes.newNode; - oldNode.parentElement.replaceChild(newNode, oldNode); - }); + if(baseColumn){ + Array + .from(doc.body.childNodes) + .filter(isTextNode) + .filter(function(textNode) { + // 🎵 Hey, processor! Leave those pre's alone! 🎵 + return !filterParentIsPre(textNode); + }) + .filter(function(textNode) { + // we don't care about last nodes that are just white space + var isLastChild = textNode.parentElement.lastChild === textNode; + var isJustWS = isWhiteSpace(textNode); + return !(isLastChild && isJustWS); + }) + .map(function toTrimmedTextNode(textNode) { + var rawText = textNode.textContent; + // We remove tailing space on the right, which is just there + // to pad out tags like: + //
+ //
+ // Next line has 2 spaces hidden! + // __
+ //
+ // + var trimmedRight = rawText.trimRight(); + var trimBy = this.calculateLeftPad(trimmedRight) || baseColumn; + if (!trimBy) { + return null; //nothing to do + } + var exp = "^ {" + trimBy + "}"; + var startTrim = new RegExp(exp, "gm"); + var trimmedText = (trimBy) ? rawText.replace(startTrim, "") : rawText; + var newNode = textNode.ownerDocument.createTextNode(trimmedText); + // We can then swap the old with the new + return { + oldNode: textNode, + newNode: newNode, + }; + }.bind(this)) + .filter(function(nodes) { + return nodes; + }) + .forEach(function(nodes) { + var oldNode = nodes.oldNode; + var newNode = nodes.newNode; + oldNode.parentElement.replaceChild(newNode, oldNode); + }); + } var nodeIterator = doc.createNodeIterator(doc.body, NodeFilter.SHOW_TEXT, filterLastChildIsPadding); var iterable = this.toESIterable(nodeIterator.nextNode.bind(nodeIterator)); // Remove trailing whitespace nodes diff --git a/tests/spec/core/utils-spec.js b/tests/spec/core/utils-spec.js index f2d0c72df1..a75b07962b 100644 --- a/tests/spec/core/utils-spec.js +++ b/tests/spec/core/utils-spec.js @@ -8,6 +8,37 @@ describe("Core - Utils", function() { }); }); + describe("calculateLeftPad()", function(){ + it("throws given invalid input", function(){ + expect(function(){ + expect(utils.calculateLeftPad()); + }).toThrow(); + expect(function(){ + expect(utils.calculateLeftPad({})); + }).toThrow(); + expect(function(){ + expect(utils.calculateLeftPad(123)); + }).toThrow(); + expect(function(){ + expect(utils.calculateLeftPad(null)); + }).toThrow(); + }); + it("calculates the smallest left padding of multiline text", function(done){ + expect(utils.calculateLeftPad("")).toEqual(0); + expect(utils.calculateLeftPad("\n \n ")).toEqual(2); + expect(utils.calculateLeftPad(" ")).toEqual(25); + expect(utils.calculateLeftPad(" a ")).toEqual(1); + expect(utils.calculateLeftPad(" \n a ")).toEqual(1); + expect(utils.calculateLeftPad(" \n a ")).toEqual(1); + expect(utils.calculateLeftPad("\n \n \n ")).toEqual(4); + expect(utils.calculateLeftPad("\n \n \n ")).toEqual(2); + expect(utils.calculateLeftPad("\n \n \n \n ")).toEqual(2); + expect(utils.calculateLeftPad("\n\n\n\n\n\n\n\n\n\n")).toEqual(0); + expect(utils.calculateLeftPad(" \n\n\n\n\n \n\n\n\n\n ")).toEqual(2); + done(); + }); + }); + describe("makeOwnerSwapper()", function() { it("throws if passed something that is not a node", function(done) { expect(function() {