Skip to content

Commit

Permalink
handle comments and childless elements
Browse files Browse the repository at this point in the history
fixes slab#89
  • Loading branch information
jhchen committed May 11, 2014
1 parent 85890d8 commit 631100c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/document.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class Document
@lines.remove(line)

setHTML: (html) ->
html = Normalizer.stripComments(html)
html = Normalizer.stripWhitespace(html)
@root.innerHTML = html
@lines = new LinkedList()
Expand Down
11 changes: 7 additions & 4 deletions src/normalizer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ Normalizer =
break
curNode = curNode.nextSibling

stripComments: (html) ->
html = html.replace(/<!--[\s\S]*?-->/g, '')

stripWhitespace: (html) ->
# Remove leading and tailing whitespace
html = html.replace(/^\s+/, '').replace(/\s+$/, '')
Expand All @@ -123,15 +126,15 @@ Normalizer =
node.removeAttribute('style')

whitelistTags: (node) ->
return unless DOM.isElement(node)
return node unless DOM.isElement(node)
node = DOM.switchTag(node, Normalizer.ALIASES[node.tagName]) if Normalizer.ALIASES[node.tagName]?
if !Normalizer.TAGS[node.tagName]?
if DOM.BLOCK_TAGS[node.tagName]?
node = DOM.switchTag(node, DOM.DEFAULT_BLOCK_TAG)
else if node.hasAttributes()
node = DOM.switchTag(node, DOM.DEFAULT_INLINE_TAG)
else
else if !node.hasAttributes() and node.firstChild?
node = DOM.unwrap(node)
else
node = DOM.switchTag(node, DOM.DEFAULT_INLINE_TAG)
return node

# Wrap inline nodes with block tags
Expand Down
7 changes: 7 additions & 0 deletions test/unit/document.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,13 @@ describe('Document', ->
<p><b></b><br></p>
', true)
)

it('setHTML() with comment', ->
@doc.setHTML('
<!-- HTML Comment -->
<p>Test</p>
')
)
)

describe('toDelta()', ->
Expand Down

0 comments on commit 631100c

Please sign in to comment.