diff --git a/src/trix/models/html_parser.coffee b/src/trix/models/html_parser.coffee index ad0cf0146..3e42cd32a 100644 --- a/src/trix/models/html_parser.coffee +++ b/src/trix/models/html_parser.coffee @@ -54,11 +54,18 @@ class Trix.HTMLParser extends Trix.BasicObject processNode: (node) -> switch node.nodeType when Node.TEXT_NODE - @processTextNode(node) + unless @isInsignificantTextNode(node) + @appendBlockForTextNode(node) + @processTextNode(node) when Node.ELEMENT_NODE @appendBlockForElement(node) @processElement(node) + appendBlockForTextNode: (node) -> + if @currentBlockElement and not elementContainsNode(@currentBlockElement, node) + @currentBlock = @appendEmptyBlock() + @currentBlockElement = null + appendBlockForElement: (element) -> elementIsBlockElement = @isBlockElement(element) currentBlockContainsElement = elementContainsNode(@currentBlockElement, element) @@ -87,13 +94,12 @@ class Trix.HTMLParser extends Trix.BasicObject null processTextNode: (node) -> - unless @isInsignificantTextNode(node) - string = node.data - unless elementCanDisplayPreformattedText(node.parentNode) - string = squishBreakableWhitespace(string) - if stringEndsWithWhitespace(node.previousSibling?.textContent) - string = leftTrimBreakableWhitespace(string) - @appendStringWithAttributes(string, @getTextAttributes(node.parentNode)) + string = node.data + unless elementCanDisplayPreformattedText(node.parentNode) + string = squishBreakableWhitespace(string) + if stringEndsWithWhitespace(node.previousSibling?.textContent) + string = leftTrimBreakableWhitespace(string) + @appendStringWithAttributes(string, @getTextAttributes(node.parentNode)) processElement: (element) -> if nodeIsAttachmentElement(element) diff --git a/test/src/unit/html_parser_test.coffee b/test/src/unit/html_parser_test.coffee index 7be1e53d5..12271510b 100644 --- a/test/src/unit/html_parser_test.coffee +++ b/test/src/unit/html_parser_test.coffee @@ -111,6 +111,11 @@ testGroup "Trix.HTMLParser", -> expectedHTML = """
abc
123
""" assert.documentHTMLEqual Trix.HTMLParser.parse(html).getDocument(), expectedHTML + test "parses text nodes following block elements", -> + html = """b
c
d""" + expectedHTML = """
b
c
d
""" + assert.documentHTMLEqual Trix.HTMLParser.parse(html).getDocument(), expectedHTML + test "parses whitespace-only text nodes without a containing block element", -> html = """a b c""" expectedHTML = """
a b c
"""