+ if rects.length > 1
+ # chromium returns two rects in both these cases:
+ # 1. a space that is word-wrapped. one rect on each line. Note that
+ # chromium does _not_ do this for _all_ spaces that are word wrapped.
+ # 2. the last (insignificant) space in a sequence of collapsing spaces
+ # in this case the rects are identical.
+ if rects[1].top > rects[0].top
+ return true
+ width = rects[0].width ? (rects[0].right - rects[0].left)
+ if width > 0
+ return true
+ # firefox reports the space that's word-wrapped as zero width
+ if n.text.length > i + 1
+ range.setStart n.el, i + 1
+ range.setEnd n.el, i + 2
+ next_rects = range.getClientRects()
+ if next_rects.length > 0
+ if next_rects[0].top > rects[0].top
+ # next character is lower on the screen, so this must be a word-wrap space
+ return true
+ else
+ # FIXME detect word-wrap in last character
+ # could be followed by an inline block with no starting space
+ # FIXME chromium gets here for a significant space at the begining of a
+ # text node that word-wraps
+ return false
+
+# pass a node (from parser library, ie it should have .el and .text)
+remove_insignificant_whitespace = (n) ->
+ changed = false
+ if n.type is TYPE_TEXT
+ i = 0
+ while i < n.text.length
+ if is_space_code n.text.charCodeAt i
+ if is_space_significant n, i
+ i += 1
+ else
+ n.el.textContent = n.text = (n.text.substr 0, i) + (n.text.substr i + 1)
+ changed = true
+ else
+ i += 1
+ if n.children.length > 0
+ for c in n.children
+ if remove_insignificant_whitespace c
+ changed = true
+ return changed
+
+class PeachHTML5Editor
+ constructor: (in_el, options = {}) ->
+ @in_el = in_el
+ @tree = []
+ @iframe = domify iframe: class: 'peach_html5_editor'
+ @cursor = null
+ @cursor_el = null
+ @cursor_visible = false
+ opt_fragment = options.fragment ? true
+ @parser_opts = {}
+ if opt_fragment
+ @parser_opts.fragment = 'body'
+
+ @iframe.onload = =>
+ @idoc = @iframe.contentDocument
+
+ ignore_key_codes =
+ '18': true # alt
+ '20': true # capslock
+ '17': true # ctrl
+ '144': true # numlock
+ '16': true # shift
+ '91': true # windows "start" key
+ control_key_codes = # we react to these, but they aren't typing
+ '37': KEY_LEFT
+ '38': KEY_UP
+ '39': KEY_RIGHT
+ '40': KEY_DOWN
+ '35': KEY_END
+ '8': KEY_BACKSPACE
+ '46': KEY_DELETE
+ '13': KEY_ENTER
+ '27': KEY_ESCAPE
+ '36': KEY_HOME
+ '45': KEY_INSERT
+ '33': KEY_PAGE_UP
+ '34': KEY_PAGE_DOWN
+ '9': KEY_TAB
+
+ @idoc.body.onclick = (e) =>
+ # idoc.body.offset().left/top
+ new_cursor = find_loc_cursor_position @tree, x: e.pageX, y: e.pageY
+ if new_cursor?
+ @move_cursor new_cursor
+ @idoc.body.onkeyup = (e) =>
+ return if e.ctrlKey
+ return false if ignore_key_codes[e.keyCode]?
+ #return false if control_key_codes[e.keyCode]?
+ @idoc.body.onkeydown = (e) =>
+ return if e.ctrlKey
+ return false if ignore_key_codes[e.keyCode]?
+ #return false if control_key_codes[e.keyCode]?
+ switch e.keyCode
+ when KEY_LEFT
+ if @cursor?
+ new_cursor = find_prev_cursor_position @tree, @cursor...
+ if new_cursor?
+ @move_cursor new_cursor
+ else
+ for c in @tree
+ new_cursor = find_next_cursor_position @tree, c, -1
+ if new_cursor?
+ @move_cursor new_cursor
+ break
+ return false
+ when KEY_UP
+ return false
+ when KEY_RIGHT
+ if @cursor?
+ new_cursor = find_next_cursor_position @tree, @cursor...
+ if new_cursor?
+ @move_cursor new_cursor
+ else
+ for c in @tree
+ new_cursor = find_prev_cursor_position @tree, c, -1
+ if new_cursor?
+ @move_cursor new_cursor
+ break
+ return false
+ when KEY_DOWN
+ return false
+ when KEY_END
+ return false
+ when KEY_BACKSPACE
+ return false unless @cursor?
+ return false unless @cursor[1] > 0
+ @cursor[0].text = @cursor[0].text.substr(0, @cursor[1] - 1) + @cursor[0].text.substr(@cursor[1])
+ @cursor[0].el.nodeValue = @cursor[0].text
+ @move_cursor [@cursor[0], @cursor[1] - 1]
+ return false
+ when KEY_DELETE
+ return false unless @cursor?
+ return false unless @cursor[1] < @cursor[0].text.length
+ @cursor[0].text = @cursor[0].text.substr(0, @cursor[1]) + @cursor[0].text.substr(@cursor[1] + 1)
+ @cursor[0].el.nodeValue = @cursor[0].text
+ @move_cursor [@cursor[0], @cursor[1]]
+ return false
+ when KEY_ENTER
+ return false
+ when KEY_ESCAPE
+ return false
+ when KEY_HOME
+ return false
+ when KEY_INSERT
+ return false
+ when KEY_PAGE_UP
+ return false
+ when KEY_PAGE_DOWN
+ return false
+ when KEY_TAB
+ return false
+ @idoc.body.onkeypress = (e) =>
+ return if e.ctrlKey
+ return false if ignore_key_codes[e.keyCode]?
+ return false if control_key_codes[e.keyCode]? # handled in keydown
+ char = e.charCode ? e.keyCode
+ if char and @cursor?
+ char = String.fromCharCode char
+ if @cursor[1] is 0
+ @cursor[0].text = char + @cursor[0].text
+ else if @cursor[1] is @cursor[0].text.length - 1
+ @cursor[0].text += char
+ else
+ @cursor[0].text =
+ @cursor[0].text.substr(0, @cursor[1]) +
+ char +
+ @cursor[0].text.substr(@cursor[1])
+ @cursor[0].el.nodeValue = @cursor[0].text
+ @move_cursor [@cursor[0], @cursor[1] + 1]
+ @changed()
+ return false
+ if options.stylesheet # TODO test this
+ istyle = @idoc.createElement 'style'
+ istyle.setAttribute 'src', options.stylesheet
+ @idoc.head.appendChild istyle
+ icss = @idoc.createElement 'style'
+ icss.appendChild @idoc.createTextNode css
+ @idoc.head.appendChild icss
+ @load_html @in_el.value
+
+ @in_el.parentNode.appendChild @iframe
+ clear_dom: ->
+ # FIXME add parent node, so we don't empty body and delete cursor_el
+ while @idoc.body.childNodes.length
+ @idoc.body.removeChild @idoc.body.childNodes[0]
+ @cursor_visible = false
+ return
+ load_html: (html) ->
+ @tree = peach_parser.parse html, @parser_opts
+ @clear_dom()
+ instantiate_tree @tree, @idoc.body
+ remove_insignificant_whitespace type: TYPE_TAG, children: @tree
+ @changed()
+ changed: ->
+ # FIXME don't export cursor placeholder (when cursor is between space characters)
+ @in_el.onchange = null
+ @in_el.value = dom_to_html @tree
+ @in_el.onchange = =>
+ @load_html @in_el.value
+ move_cursor: (cursor) ->
+ loc = cursor_to_xyh cursor[0], cursor[1]
+ unless loc?
+ console.log "error: tried to move cursor to position that has no pixel location", cursor[0], cursor[1]
+ return
+ @cursor = cursor
+ # replace cursor, to reset blink animation
+ if @cursor_visible
+ @cursor_el.parentNode.removeChild @cursor_el
+ @cursor_el = domify div: id: 'peach_html5_editor_cursor'
+ @idoc.body.appendChild @cursor_el
+ @cursor_visible = true
+ # TODO figure out x,y coords for cursor
+ @cursor_el.style.left = "#{loc.x}px"
+ @cursor_el.style.top = "#{loc.y}px"
+
+window.peach_html5_editor = (args...) ->
+ return new PeachHTML5Editor args...