+ @iframe.style.height = "#{h}px"
+ @wrap2.scrollTop = s
+ # true if n is text node with only one caracter, and the only child of a tag
+ is_only_char_in_tag: (n, i) ->
+ return false unless n.type is 'text'
+ return false unless n.text.length is 1
+ return false if n.parent is @tree_parent
+ return false unless n.parent.children.length is 1
+ return true
+ # true if n is text node with just a space in it, and the only child of a tag
+ is_lone_space: (n, i) ->
+ return false unless n.type is 'text'
+ return false unless n.text is ' '
+ return false if n.parent is @tree_parent
+ return false unless n.parent.children.length is 1
+ return true
+ # detect special case: typing before a space that's the only thing in a block/doc
+ # reason: enter key creates blocks with just a space in them
+ insert_should_replace: (n, i) ->
+ return false unless i is 0
+ return false unless n.text is ' '
+ return true if n.parent is @tree_parent
+ if n.parent.children.length is 1
+ if n.parent.children[0] is n
+ # n is only child
+ return true
+ return false
+ # after calling this, you MUST call changed() and text_cleanup()
+ insert_character: (n, i, char) ->
+ return if n.parent is @tree_parent # FIXME implement text nodes at top level
+ # insert the character
+ if @insert_should_replace n, i
+ n.text = char
+ else if i is 0
+ n.text = char + n.text
+ else if i is n.text.length
+ # replace the space
+ n.text += char
+ else
+ n.text =
+ n.text.substr(0, i) +
+ char +
+ n.text.substr(i)
+ n.el.nodeValue = n.text
+ # WARNING: after calling this, you MUST call changed() and text_cleanup()
+ remove_character: (n, i) ->
+ n.text = n.text.substr(0, i) + n.text.substr(i + 1)
+ n.el.nodeValue = n.text
+ computed_style: (n, prop) ->
+ if n.type is 'text'
+ n = n.parent
+ style = @iframe.contentWindow.getComputedStyle n.el, null
+ return style.getPropertyValue prop
+ # returns the new white-space value that will preserve spaces for node n
+ preserve_space: (n, ideal_target) ->
+ if n.type is 'text'
+ target = n.parent
+ else
+ target = n
+ while target isnt ideal_target and not target.el.style.whiteSpace
+ unless target?
+ console.log "bug #967123"
+ return
+ target = target.parent
+ ws = ws_props[target.el.style.whiteSpace]?.to_preserve
+ ws ?= 'pre-wrap'
+ target.el.style.whiteSpace = ws
+ @update_style_from_el target
+ return ws
+ update_style_from_el: (n) ->
+ style = n.el.getAttribute 'style'
+ if style?
+ n.attrs.style = style
+ else
+ if n.attrs.style?
+ delete n.attrs.style
+ # remove whitespace that would be trimmed
+ # replace whitespace that would collapse with a single space
+ # FIXME remove whitespace from after <br> (but not before)
+ # FIXME rewrite to
+ # check computed white-space prop on txt parents
+ # batch replace txt node contents (ie don't loop for each char)
+ collapse_whitespace: (tree = @tree) ->
+ prev = cur = next = null
+ prev_i = cur_i = next_i = 0
+ prev_pos = pos = next_pos = null
+ prev_px = cur_px = next_px = null
+ first = true
+ removed_char = null
+
+ tree_remove_empty_text_nodes(tree)
+
+ iterate = (tree, cb) ->
+ for n in tree
+ if n.type is 'text'
+ i = 0
+ while i < n.text.length # don't foreach, cb might remove chars
+ advance = cb n, i
+ if advance
+ i += 1
+ if n.type is 'tag'
+ block = is_display_block n.el
+ if block
+ cb null
+ if n.children.length > 0
+ iterate n.children, cb
+ if block
+ cb null
+ # remove cur char
+ remove = (undo) ->
+ if undo
+ cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + removed_char + (cur.text.substr cur_i)
+ if next is cur # in same text node
+ next_i += 1
+ return -1
+ else
+ removed_char = cur.text.charAt(cur_i)
+ cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + (cur.text.substr cur_i + 1)
+ if next is cur # in same text node
+ if next_i is 0
+ throw "how is this possible?"
+ next_i -= 1
+ return 1
+ replace_with_space = (undo) ->
+ if undo
+ cur.text = (cur.text.substr 0, cur_i) + removed_char + (cur.text.substr cur_i + 1)
+ cur.el.textContent = cur.text
+ else
+ removed_char = cur.text.charAt(cur_i)
+ if removed_char isnt ' '
+ cur.text = (cur.text.substr 0, cur_i) + ' ' + (cur.text.substr cur_i + 1)
+ cur.el.textContent = cur.text
+ return 0
+ # return true if cur was removed from the dom (ie re-use same prev)
+ operate = ->
+ # cur definitately set
+ # prev and/or next might be null, indicating the start/end of a display:block
+ return false unless is_space_code cur.text.charCodeAt cur_i
+ fixers = [remove, replace_with_space]
+ # check for common case: single whitespace surrounded by non-whitespace chars
+ if prev? and next?
+ unless (is_space_code prev.text.charCodeAt prev_i) or (is_space_code next.text.charCodeAt next_i)
+ dbg = cur.text.charCodeAt cur_i
+ if cur.text.charAt(cur_i) is ' ' # perens required
+ # single space can't collapse, doesn't need fixin'
+ return false
+ else
+ # tab, newline, etc, can't collapse, but maybe should be replaced
+ fixers = [replace_with_space]
+ bounds = text_range_bounds cur.el, cur_i, cur_i + 1
+ # consistent cases:
+ # 1. zero rects returned by getClientRects() means collapsed space
+ if bounds is null
+ return remove()
+ # 2. width greater than zero means visible space
+ if bounds.w > 0
+ # has bounds, don't try removing
+ fixers = [replace_with_space]
+ # now the weird edge cases...
+ #
+ # firefox and chromium both report zero width for characters at the end
+ # of a line where the text wraps (automatically, due to word-wrap) to
+ # the next line. These do not appear to be distinguishable from
+ # collapsed spaces via the range/bounds api, so...
+ #
+ # remove it from the dom, and if prev or next moves, put it back.
+ #
+ # this block (try changing it, put it back if something moves) is also
+ # used on collapsable whitespace characters besides space. In this case
+ # the character is replaced with a normal space character instead of
+ # removed
+ if prev? and not prev_px?
+ prev_px = new_cursor_position n: prev, i: prev_i
+ if next? and not next_px?
+ next_px = new_cursor_position n: next, i: next_i
+ #if prev is null and next is null
+ # parent_px = cur.parent.el.getBoundingClientRect()
+ undo_arg = true # just for readabality
+ removed = 0
+ for fixer in fixers
+ break if removed > 0
+ removed += fixer()
+ need_undo = false
+ if prev?
+ if prev_px?
+ new_prev_px = new_cursor_position n: prev, i: prev_i
+ if new_prev_px?
+ if new_prev_px.x isnt prev_px.x or new_prev_px.y isnt prev_px.y
+ need_undo = true
+ else
+ need_undo = true
+ else
+ console.log "this shouldn't happen, we remove spaces that don't locate"
+ if next? and not need_undo
+ if next_px?
+ new_next_px = new_cursor_position n: next, i: next_i
+ if new_next_px?
+ if new_next_px.x isnt next_px.x or new_next_px.y isnt next_px.y
+ need_undo = true
+ else
+ need_undo = true
+ #else
+ # console.log "removing space becase space after it is collapsed"
+ if need_undo
+ removed += fixer undo_arg
+ if removed > 0
+ return true
+ else
+ return false
+ # pass null at start/end of display:block
+ queue = (n, i) ->
+ next = n
+ next_i = i
+ next_px = null
+ advance = true
+ if cur?
+ removed = operate()
+ # don't advance (to the next character next time) if we removed a
+ # character from the same text node as ``next``, because doing so
+ # renumbers the indexes in that string
+ if removed and cur is next
+ advance = false
+ else
+ removed = false
+ unless removed
+ prev = cur
+ prev_i = cur_i
+ prev_px = cur_px
+ cur = next
+ cur_i = next_i
+ cur_px = next_px
+ return advance
+ queue null
+ iterate tree, queue
+ queue null
+
+ tree_remove_empty_text_nodes(tree)
+ return
+ # call this after you insert or remove inline nodes. It will:
+ # merge consecutive text nodes
+ # remove empty text nodes
+ # adjust white-space property
+ # note: this assumes that all whitespace in text nodes should be displayed
+ # (ie not collapse or be trimmed) and will change the white-space property
+ # as needed to achieve this.
+ text_cleanup: (n) ->
+ if @is_display_block n
+ block = n
+ else
+ block = @find_block_parent n
+ return unless block?
+ run = @get_text_run block
+ return unless run?
+ # merge consecutive text nodes
+ if run.length > 1
+ i = 1
+ prev = run[0]
+ while i < run.length
+ n = run[i]
+ if prev.type is 'text' and n.type is 'text'
+ if prev.parent is n.parent
+ prev_i = n.parent.children.indexOf prev
+ n_i = n.parent.children.indexOf n
+ if n_i is prev_i + 1
+ prev.text = prev.text + n.text
+ prev.el.textContent = prev.text
+ @remove_node n
+ run.splice i, 1
+ continue # don't increment i or change prev
+ i += 1
+ prev = n
+ # remove empty text nodes
+ i = 0
+ while i < run.length
+ n = run[i]
+ if n.type is 'text'
+ if n.text is ''
+ @remove_node n
+ # FIXME maybe remove parents recursively if this makes them empty
+ run.splice i, 1
+ continue # don't increment i
+ i += 1
+ # note: inline tags can have white-space:pre-line/etc
+ # note: inline-blocks have their whitespace collapsed independantly of outer run
+ # note: inline-blocks are treated like non-whitespace char even if empty
+ if block.el.style.whiteSpace?
+ ws = block.el.style.whiteSpace
+ if ws_props[ws]
+ if ws_props[ws].space
+ if ws_props[ws].to_collapse is 'normal'
+ block.el.style.whiteSpace = null
+ else
+ block.el.style.whiteSpace = ws_props[ws].to_collapse
+ @update_style_from_el block
+ # note: space after <br> colapses, but not space before
+ # check for spaces that would collapse without help
+ eats_start_sp = true # if the next node starts with space it collapses (unless pre)
+ prev = null
+ for n in run
+ if n.type is 'tag'
+ if n.name is 'br'
+ eats_start_sp = true
+ else
+ eats_start_sp = false
+ else # TEXT
+ need_preserve = false
+ if n.type isnt 'text'
+ console.log "bug #232308"
+ return
+ if eats_start_sp
+ if is_space_code n.text.charCodeAt 0
+ need_preserve = true
+ unless need_preserve
+ need_preserve = multi_sp_regex.test n.text
+ if need_preserve
+ # do we have it already?
+ ws = @computed_style n, 'white-space' # FIXME implement this
+ unless ws_props[ws]?.space
+ # 2nd arg is ideal target for css rule
+ ws = @preserve_space n, block
+ eats_start_sp = false
+ else
+ if is_space_code n.text.charCodeAt(n.text.length - 1)
+ ws = @computed_style n, 'white-space' # FIXME implement this
+ if ws_props[ws]?.space
+ eats_start_sp = false
+ else
+ eats_start_sp = true
+ else
+ eats_start_sp = false
+ # check if text ends with a collapsable space
+ if run.length > 0
+ last = run[run.length - 1]
+ if last.type is 'text'
+ if eats_start_sp
+ @preserve_space last, block
+ return
+ css_clear: (n, prop) ->
+ return unless n.attrs.style?
+ return if n.attrs.style is ''
+ css_delimiter_regex = new RegExp('\s*;\s*', 'g') # FIXME make this global
+ styles = n.attrs.style.trim().split css_delimiter
+ return unless styles.length > 0
+ if styles[styles.length - 1] is ''
+ styles.pop()
+ return unless styles.length > 0
+ i = 0
+ while i < styles.length
+ if styles[i].substr(0, 12) is 'white-space:'
+ styles.splice i, 1
+ else
+ i += 1
+ return
+ # WARNING: after calling this one or more times, you MUST:
+ # if it's inline: call @text_cleanup
+ # call @changed()
+ remove_node: (n) ->
+ i = n.parent.children.indexOf n
+ if i is -1
+ throw "BUG #9187112313"
+ n.el.parentNode.removeChild n.el
+ n.parent.children.splice i, 1
+ return
+ # remove a node from the tree/dom, insert into new_parent before insert_before?end
+ # WARNING: after calling this one or more times, you MUST:
+ # if it's inline: call @text_cleanup
+ # call @changed()
+ move_node: (n, new_parent, insert_before = null) ->
+ i = n.parent.children.indexOf n
+ if i is -1
+ throw "Error: tried to remove node, but it's not in it's parents list of children"
+ return
+ if insert_before?
+ before_i = new_parent.children.indexOf insert_before
+ if i is -1
+ throw "Error: tried to move a node to be before a non-existent node"
+ insert_before = insert_before.el
+ @remove_node n
+ if insert_before?
+ new_parent.el.insertBefore n.el, insert_before
+ new_parent.children.splice before_i, 0, n
+ else
+ new_parent.el.appendChild n.el, insert_before
+ new_parent.children.push n
+ n.parent = new_parent
+ return