+find_loc_cursor_position = (tree, loc) ->
+ for c in tree
+ if c.type is TYPE_TAG or c.type is TYPE_TEXT
+ bounds = get_el_bounds c.el
+ continue if loc.x < bounds.x
+ continue if loc.x > bounds.x + bounds.w
+ continue if loc.y < bounds.y
+ continue if loc.y > bounds.y + bounds.h
+ if c.children.length
+ ret = find_loc_cursor_position c.children, loc
+ return ret if ret?
+ if c.type is TYPE_TEXT
+ # click is within bounding box that contains all text.
+ return [c, 0] if c.text.length is 0
+ before_i = 0
+ before = cursor_to_xyh c, before_i
+ unless before?
+ console.log "error: failed to find cursor pixel location for start of", c
+ return
+ after_i = c.text.length
+ after = cursor_to_xyh c, after_i
+ unless after?
+ console.log "error: failed to find cursor pixel location for end of", c
+ return
+ if loc.y < before.y + before.h and loc.x < before.x
+ # console.log 'before first char on first line'
+ continue
+ if loc.y > after.y and loc.x > after.x
+ # console.log 'after last char on last line'
+ continue
+ if loc.y < before.y
+ console.log "Warning: click in bounding box but above first line"
+ continue # above first line (runaround?)
+ if loc.y > after.y + after.h
+ console.log "Warning: click in bounding box but below last line", loc.y, after.y, after.h
+ continue # below last line (shouldn't happen?)
+ while after_i - before_i > 1
+ cur_i = Math.round((before_i + after_i) / 2)
+ cur = cursor_to_xyh c, cur_i
+ unless loc?
+ console.log "error: failed to find cursor pixel location for", c, cur_i
+ return
+ if loc.y < cur.y or (loc.y <= cur.y + cur.h and loc.x < cur.x)
+ after_i = cur_i
+ after = cur
+ else
+ before_i = cur_i
+ before = cur
+ # which one is closest?
+ if Math.abs(before.x - loc.x) < Math.abs(after.x - loc.x)
+ return [c, before_i]
+ else
+ return [c, after_i]
+ return null
+
+# browsers collapse these (html5 spec calls these "space characters")
+is_space_code = (char_code) ->
+ switch char_code
+ when 9, 10, 12, 13, 32
+ return true
+ return false
+is_space = (chr) ->
+ return is_space_code chr.charCodeAt 0
+
+# warning: contains browser-specific hackery
+is_space_significant = (n, i) ->
+ range = document.createRange()
+ range.setStart n.el, i
+ range.setEnd n.el, i + 1
+ rects = range.getClientRects()
+ bounding_rect = range.getBoundingClientRect()
+ if rects.length is 0
+ return false
+ 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
+