Skip to content

Commit

Permalink
Fix parsing text nodes following block elements
Browse files Browse the repository at this point in the history
Fixes #663
  • Loading branch information
javan committed Aug 14, 2019
1 parent 692e2fd commit d4edd23
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/trix/models/html_parser.coffee
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions test/src/unit/html_parser_test.coffee
Expand Up @@ -111,6 +111,11 @@ testGroup "Trix.HTMLParser", ->
expectedHTML = """<blockquote><!--block-->abc</blockquote><div><!--block--><strong>123</strong></div>"""
assert.documentHTMLEqual Trix.HTMLParser.parse(html).getDocument(), expectedHTML

test "parses text nodes following block elements", ->
html = """<ul><li>a</li></ul>b<blockquote>c</blockquote>d"""
expectedHTML = """<ul><li><!--block-->a</li></ul><div><!--block-->b</div><blockquote><!--block-->c</blockquote><div><!--block-->d</div>"""
assert.documentHTMLEqual Trix.HTMLParser.parse(html).getDocument(), expectedHTML

test "parses whitespace-only text nodes without a containing block element", ->
html = """a <strong>b</strong> <em>c</em>"""
expectedHTML = """<div><!--block-->a <strong>b</strong> <em>c</em></div>"""
Expand Down

0 comments on commit d4edd23

Please sign in to comment.