- block = @find_block_parent n
- changed = false
- n = @cursor.n.parent
- while n and n isnt block
- changed = true
- while n.children.length > 0
- @move_node n.children[0], n.parent, n
- @remove_node n
- n = n.parent
- # replace @cursor.n with a single (preserved) space
- if @cursor.n.text != ' '
- changed = true
- @cursor.n.text = @cursor.n.el.textContent = ' '
- if changed
- @text_cleanup @cursor.n
- # place the cursor to the left of that space
- new_cursor = new_cursor_position n: @cursor.n, i: 0
- else # emptying a text node (but not a whole text run)
- # figure out where cursor should land
- block = @find_block_parent @cursor.n
- new_cursor = find_prev_cursor_position @tree, n: @cursor.n, i: 0
- ncb = @find_block_parent new_cursor
- if ncb isnt block
- new_cursor = find_next_cursor_position @tree, n: @cursor.n, i: 1
- # delete text node
- @remove_node @cursor.n
- # delete any inline parents
- n = @cursor.n.parent
- while n and n isnt block
- while n.children.length > 0
- @move_node n.children[0], n.parent, n
- @remove_node n
- n = n.parent
- # update cursor dest in case things moved around
- if new_cursor?
+ block = @find_block_parent @cursor.n
+ changed = false
+ n = @cursor.n.parent
+ # note: this doesn't use _backspace_node_helper because:
+ # 1. we don't want to delete the target node (we're replacing it's contents)
+ # 2. we want to track whether anything was removed
+ # 3. we know already know there's no other text from this run anywhere
+ while n and n isnt block
+ changed = true
+ while n.children.length > 0
+ @move_node n.children[0], n.parent, n
+ @remove_node n
+ n = n.parent
+ # replace @cursor.n with a single (preserved) space
+ if @cursor.n.text != ' '
+ changed = true
+ @cursor.n.text = @cursor.n.el.textContent = ' '
+ if changed
+ @text_cleanup @cursor.n
+ # place the cursor to the left of that space
+ new_cursor = new_cursor_position n: @cursor.n, i: 0
+ else # emptying a text node (but not a whole text run)
+ # figure out where cursor should land
+ block = @find_block_parent @cursor.n
+ new_cursor = find_prev_cursor_position @tree, n: @cursor.n, i: 0
+ ncb = @find_block_parent new_cursor.n
+ if ncb isnt block
+ new_cursor = find_next_cursor_position @tree, n: @cursor.n, i: 1
+ # delete text node and cleanup emptied parents
+ run_i = run.indexOf @cursor.n
+ @_backspace_node_helper @cursor.n, run, run_i
+ # see if new adjacent siblings should merge
+ # TODO make smarter
+ if run_i > 0 and run_i + 1 < run.length
+ if run[run_i - 1].type is 'text' and run[run_i + 1].type is 'text'
+ merge_state = n: run[run_i + 1]
+ @_merge_left merge_state
+ if merge_state.moved_cursor
+ new_cursor = merge_state
+ # update whitespace preservation
+ @text_cleanup(block)
+ # update cursor x/y in case things moved around
+ if new_cursor?
+ if new_cursor.n.el.parentNode # still in dom after cleanup