diff --git a/src/selection.coffee b/src/selection.coffee index 306eb0bced..8bc194f126 100644 --- a/src/selection.coffee +++ b/src/selection.coffee @@ -1,8 +1,9 @@ -_ = require('lodash') -DOM = require('./dom') -Leaf = require('./leaf') -Range = require('./lib/range') -Utils = require('./utils') +_ = require('lodash') +DOM = require('./dom') +Leaf = require('./leaf') +Normalizer = require('./normalizer') +Range = require('./lib/range') +Utils = require('./utils') class Selection @@ -86,6 +87,11 @@ class Selection node = node.childNodes[offset] offset = 0 else if node.childNodes.length == 0 + # TODO revisit fix for encoding edge case

|

+ unless Normalizer.TAGS[node.tagName]? + text = node.ownerDocument.createTextNode('') + node.appendChild(text) + node = text return [node, 0] else node = node.lastChild diff --git a/src/utils.coffee b/src/utils.coffee index 9503504d61..68da9a6112 100644 --- a/src/utils.coffee +++ b/src/utils.coffee @@ -47,8 +47,12 @@ Utils = return version and maxVersion >= version mergeNodes: (newNode, oldNode) -> - DOM.moveChildren(newNode, oldNode) - DOM.normalize(newNode) + if DOM.isElement(newNode) + DOM.moveChildren(newNode, oldNode) + DOM.normalize(newNode) + else + text = DOM.getText(newNode) + DOM.getText(oldNode) + DOM.setText(newNode, text) DOM.removeNode(oldNode) # refNode is node after split point, root is parent of eldest node we want split (root will not be split) diff --git a/test/unit/document.coffee b/test/unit/document.coffee index 96ac6cda67..014925091d 100644 --- a/test/unit/document.coffee +++ b/test/unit/document.coffee @@ -5,27 +5,27 @@ describe('Document', -> describe('constructor', -> tests = - # 'blank': - # initial: '' - # expected: '' - # 'no change': - # initial: '

Test

' - # expected: '

Test

' - # 'text': - # initial: 'Test' - # expected: '

Test

' - # 'inline': - # initial: 'Test' - # expected: '

Test

' + 'blank': + initial: '' + expected: '' + 'no change': + initial: '

Test

' + expected: '

Test

' + 'text': + initial: 'Test' + expected: '

Test

' + 'inline': + initial: 'Test' + expected: '

Test

' 'block pulling': initial: '
Test
Test
' expected: '

Test

Test

' - # 'with breaks': - # initial: '

A
B
C

' - # expected: '

A

B

C

' - # 'pull and break': - # initial: '
A
B
C
' - # expected: '

A

B

C

' + 'with breaks': + initial: '

A
B
C

' + expected: '

A

B

C

' + 'pull and break': + initial: '
A
B
C
' + expected: '

A

B

C

' _.each(tests, (test, name) -> it(name, -> diff --git a/test/unit/normalizer.coffee b/test/unit/normalizer.coffee index 92c47802f6..fd861250e0 100644 --- a/test/unit/normalizer.coffee +++ b/test/unit/normalizer.coffee @@ -113,10 +113,13 @@ describe('Normalizer', -> 'wrap orphaned text node': initial: '01' expected: '01' + 'merge text nodes': + initial: 'A B.' + expected: 'A B.' _.each(tests, (test, name) -> it(name, -> - @container.innerHTML = "
#{test.initial}
" + @container.innerHTML = "

#{test.initial}

" Quill.Normalizer.optimizeLine(@container.firstChild) expect(@container.firstChild).toEqualHTML(test.expected) ) diff --git a/test/unit/selection.coffee b/test/unit/selection.coffee index 6ea20dfdc3..20cdfc7307 100644 --- a/test/unit/selection.coffee +++ b/test/unit/selection.coffee @@ -94,7 +94,8 @@ describe('Selection', -> quill.root.innerHTML = '' quill.editor.doc.rebuild() [resultNode, resultIndex] = quill.editor.selection._encodePosition(quill.root, 0) - expect(resultNode).toEqual(quill.root) + expect(Quill.DOM.isTextNode(resultNode)).toBe(true) + expect(Quill.DOM.getText(resultNode)).toEqual('') expect(resultIndex).toEqual(0) ) diff --git a/test/unit/utils.coffee b/test/unit/utils.coffee index 5ebbbd59f7..00b29402e8 100644 --- a/test/unit/utils.coffee +++ b/test/unit/utils.coffee @@ -146,10 +146,22 @@ describe('Utils', -> it('merge and normalize', -> @container.innerHTML = 'OneTwo' + expect(@container.childNodes.length).toEqual(2) Quill.Utils.mergeNodes(@container.firstChild, @container.lastChild) expect(@container).toEqualHTML('OneTwo') + expect(@container.childNodes.length).toEqual(1) expect(@container.firstChild.childNodes.length).toEqual(1) ) + + it('merge text nodes', -> + @container.innerHTML = '' + @container.appendChild(document.createTextNode('One')) + @container.appendChild(document.createTextNode('Two')) + expect(@container.childNodes.length).toEqual(2) + Quill.Utils.mergeNodes(@container.firstChild, @container.lastChild) + expect(@container).toEqualHTML('OneTwo') + expect(@container.childNodes.length).toEqual(1) + ) ) describe('splitAncestors()', ->