1 # Copyright 2015 Jason Woofenden
2 # This file implements an WYSIWYG editor in the browser (no contenteditable)
4 # This program is free software: you can redistribute it and/or modify it under
5 # the terms of the GNU Affero General Public License as published by the Free
6 # Software Foundation, either version 3 of the License, or (at your option) any
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 timeout = (ms, cb) -> return setTimeout cb, ms
22 if (window.requestAnimationFrame?)
23 window.requestAnimationFrame cb
27 this_url_sans_path = ->
28 ret = "#{window.location.href}"
29 clip = ret.lastIndexOf '#'
31 ret = ret.substr 0, clip
32 clip = ret.lastIndexOf '?'
34 ret = ret.substr 0, clip
35 clip = ret.lastIndexOf '/'
37 ret = ret.substr 0, clip + 1
40 # xml 1.0 spec, chromium and firefox accept these, plus lots of unicode chars
41 valid_attr_regex = new RegExp '^[a-zA-Z_:][-a-zA-Z0-9_:.]*$'
42 # html5 spec is much more lax, but chromium won't let me make at attribute with the name "4"
43 js_attr_regex = new RegExp '^[oO][nN].'
45 debug_dot_at = (doc, x, y) ->
47 el = doc.createElement 'div'
48 el.setAttribute 'style', "position: absolute; left: #{x}px; top: #{y}px; width: 1px; height: 3px; background-color: red"
49 doc.body.appendChild el
50 #console.log(new Error().stack)
52 # text nodes don't have getBoundingClientRect(), so use selection api to find
54 get_el_bounds = window.bounds = (el) ->
55 if el.getBoundingClientRect?
56 rect = el.getBoundingClientRect()
58 # text nodes don't have getBoundingClientRect(), so use range api
59 range = el.ownerDocument.createRange()
60 range.selectNodeContents el
61 rect = range.getBoundingClientRect()
62 doc = el.ownerDocument.documentElement
63 win = el.ownerDocument.defaultView
64 y_fix = win.pageYOffset - doc.clientTop
65 x_fix = win.pageXOffset - doc.clientLeft
69 w: rect.width ? (rect.right - rect.left)
70 h: rect.height ? (rect.top - rect.bottom)
73 is_display_block = (el) ->
75 return el.currentStyle.display is 'block'
77 return window.getComputedStyle(el, null).getPropertyValue('display') is 'block'
79 # Pass return value from dom event handlers to this.
80 # If they return false, this will addinionally stop propagation and default.
81 event_return = (e, bool) ->
88 # Warning: currently assumes you're asking about a single character
89 # Note: chromium returns multiple bounding rects for a space at a line-break
90 # Note: chromium's getBoundingClientRect() is broken (when zero-area client rects)
91 # Note: sometimes returns null (eg for whitespace that is not visible)
92 text_range_bounds = (el, start, end) ->
93 range = document.createRange()
94 range.setStart el, start
96 rects = range.getClientRects()
101 doc = el.ownerDocument.documentElement
102 win = el.ownerDocument.defaultView
103 y_fix = win.pageYOffset - doc.clientTop
104 x_fix = win.pageXOffset - doc.clientLeft
108 w: rect.width ? (rect.right - rect.left)
109 h: rect.height ? (rect.top - rect.bottom)
111 bounding: range.getBoundingClientRect()
115 constructor: (args) ->
126 range = document.createRange()
127 if @n.text.length is 0
128 ret = text_range_bounds @n.el, 0, 0
129 else if @i is @n.text.length
130 ret = text_range_bounds @n.el, @i - 1, @i
134 ret = text_range_bounds @n.el, @i, @i + 1
145 new_cursor_position = (args) ->
146 ret = new CursorPosition args
151 # encode text so it can be safely placed inside an html attribute
152 enc_attr_regex = new RegExp '(&)|(")|(\u00A0)', 'g'
154 return txt.replace enc_attr_regex, (match, amp, quote) ->
155 return '&' if (amp)
156 return '"' if (quote)
158 enc_text_regex = new RegExp '(&)|(<)|(\u00A0)', 'g'
160 return txt.replace enc_text_regex, (match, amp, lt) ->
161 return '&' if (amp)
162 return '<' if (lt)
182 # TODO make these always pretty-print (on the inside) like blocks
183 no_text_elements = { # these elements never contain text
193 domify = (doc, hash) ->
194 for tag, attrs of hash
196 return document.createTextNode attrs
197 el = document.createElement tag
206 outer_css = (args) ->
209 inner_padding = args.inner_padding ? overlay_padding
210 frame_width = args.frame_width ? inner_padding
211 # TODO editor controls height...
212 occupy = (left, top = left, right = left, bottom = top) ->
215 return Math.max(left, top, right, bottom)
220 ret += 'color: black;'
221 ret += 'background: white;'
224 ret += "border: #{occupy 1}px solid black;"
225 ret += "padding: #{occupy frame_width}px;"
228 ret += "border: #{occupy 1}px solid black;"
229 ret += "padding: #{occupy inner_padding}px;"
230 ret += "padding-right: #{inner_padding + occupy 0, 0, 15, 0}px;" # for scroll bar
231 ret += "width: #{w}px;"
232 ret += "height: #{h}px;"
233 ret += 'overflow-x: hidden;'
234 ret += 'overflow-y: scroll;'
237 ret += 'position: relative;'
238 ret += "width: #{w}px;"
239 ret += "min-height: #{h}px;"
242 ret += 'box-sizing: border-box;'
244 ret += 'border: none;'
246 ret += "width: #{w}px;"
247 #ret += "height: #{h}px;" # height auto-set when content set/changed
248 ret += '-ms-user-select: none;'
249 ret += '-webkit-user-select: none;'
250 ret += '-moz-user-select: none;'
251 ret += 'user-select: none;'
254 ret += 'position: absolute;'
255 ret += "left: -#{inner_padding}px;"
256 ret += "top: -#{inner_padding}px;"
257 ret += "right: -#{inner_padding}px;"
258 ret += "bottom: -#{inner_padding}px;"
259 ret += 'overflow: hidden;'
262 ret += 'position: absolute;'
263 ret += 'background: rgba(100,100,100,0.2);'
266 ret += 'position: absolute;'
268 ret += 'background: linear-gradient(0deg, rgba(0,0,0,1), rgba(255,255,255,1), rgba(0,0,0,1), rgba(255,255,255,1), rgba(0,0,0,1), rgba(255,255,255,1), rgba(0,0,0,1), rgba(255,255,255,1), rgba(0,0,0,1));'
269 ret += 'background-size: 200% 200%;'
270 ret += '-webkit-animation: blink 1s linear normal infinite;'
271 ret += 'animation: blink 1s linear normal infinite;'
273 ret += '@-webkit-keyframes blink {'
274 ret += '0%{background-position:0% 0%}'
275 ret += '100%{background-position:0% -100%}'
277 ret += '@keyframes blink { '
278 ret += '0%{background-position:0% 0%}'
279 ret += '100%{background-position:0% -100%}'
283 ret += 'position: absolute;'
284 ret += 'border: 1px solid rgba(0,0,0,0.1);'
285 ret += 'outline: 1px solid rgba(255,255,255,0.1);' # in case there's a black background
288 ret += 'z-index: 10;'
289 ret += 'position: absolute;'
290 ret += 'font-size: 8px;'
291 ret += 'white-space: pre;'
292 ret += 'background: rgba(255,255,255,0.4);'
293 ret += '-ms-user-select: none;'
294 ret += '-webkit-user-select: none;'
295 ret += '-moz-user-select: none;'
296 ret += 'user-select: none;'
303 '20': true # capslock
305 '144': true # numlock
307 '91': true # windows "start" key
308 # key codes: (valid on keydown, not keypress)
313 KEY_BACKSPACE = 8 # <--
314 KEY_DELETE = 46 # -->
323 control_key_codes = # we react to these, but they aren't typing
339 instantiate_tree = (tree, parent) ->
344 c.el = parent.ownerDocument.createTextNode c.text
345 parent.appendChild c.el
347 if c.name in ['script', 'object', 'iframe', 'link']
348 # TODO put placeholders instead
351 # TODO create in correct namespace
352 c.el = parent.ownerDocument.createElement c.name
354 # FIXME if attr_whitelist[k]?
355 if valid_attr_regex.test k
356 unless js_attr_regex.test k
357 c.el.setAttribute k, v
358 parent.appendChild c.el
360 instantiate_tree c.children, c.el
364 traverse_tree = (tree, cb) ->
370 done = traverse_tree c.children, cb
374 first_cursor_position = (tree) ->
376 traverse_tree tree, (node, state) ->
377 if node.type is 'text'
378 cursor = new_cursor_position n: node, i: 0
383 return found # maybe null
385 # this will fail when text has non-locatable cursor positions
386 find_next_cursor_position = (tree, cursor) ->
387 if cursor.n.type is 'text' and cursor.n.text.length > cursor.i
388 new_cursor = new_cursor_position n: cursor.n, i: cursor.i + 1
393 traverse_tree tree, (node, state) ->
394 if node.type is 'text' and state_before is false
395 new_cursor = new_cursor_position n: node, i: 0
406 last_cursor_position = (tree) ->
408 traverse_tree tree, (node) ->
409 if node.type is 'text'
410 cursor = new_cursor_position n: node, i: node.text.length
414 return found # maybe null
416 # this will fail when text has non-locatable cursor positions
417 find_prev_cursor_position = (tree, cursor) ->
418 if cursor.n.type is 'text' and cursor.i > 0
419 new_cursor = new_cursor_position n: cursor.n, i: cursor.i - 1
424 traverse_tree tree, (node) ->
426 found = found_prev # maybe null
428 if node.type is 'text'
429 new_cursor = new_cursor_position n: node, i: node.text.length
431 found_prev = new_cursor
433 return found # maybe null
435 xy_to_cursor = (tree, xy) ->
437 if n.type is 'tag' or n.type is 'text'
438 bounds = get_el_bounds n.el
439 continue if xy.x < bounds.x
440 continue if xy.x > bounds.x + bounds.w
441 continue if xy.y < bounds.y
442 continue if xy.y > bounds.y + bounds.h
444 ret = xy_to_cursor n.children, xy
447 # click is within bounding box that contains all text.
448 if n.text.length is 0
449 ret = new_cursor_position n: n, i: 0
452 before = new_cursor_position n: n, i: 0
453 continue unless before?
454 after = new_cursor_position n: n, i: n.text.length
455 continue unless after?
456 if xy.y < before.y + before.h and xy.x < before.x
457 # console.log 'before first char on first line'
459 if xy.y > after.y and xy.x > after.x
460 # console.log 'after last char on last line'
463 console.log "Warning: click in text bounding box but above first line"
464 continue # above first line (runaround?)
465 if xy.y > after.y + after.h
466 console.log "Warning: click in text bounding box but below last line", xy.y, after.y, after.h
467 continue # below last line (shouldn't happen?)
468 while after.i - before.i > 1
469 guess_i = Math.round((before.i + after.i) / 2)
470 cur = new_cursor_position n: n, i: guess_i
472 console.log "error: failed to find cursor pixel location for", n, guess_i
475 if xy.y < cur.y or (xy.y <= cur.y + cur.h and xy.x < cur.x)
479 continue unless before? # signals failure to find a cursor position
480 # which one is closest?
481 if Math.abs(before.x - xy.x) < Math.abs(after.x - xy.x)
487 # browsers collapse these (html5 spec calls these "space characters")
488 is_space_code = (char_code) ->
490 when 9, 10, 12, 13, 32
494 return is_space_code chr.charCodeAt 0
496 tree_remove_empty_text_nodes = (tree) ->
498 traverse_tree tree, (n) ->
500 if n.text.length is 0
504 # don't completely empty the tree
506 if tree[0].type is 'text'
507 console.log "oop, leaving a blank node because it's the only thing"
509 n.el.parentNode.removeChild n.el
510 for c, i in n.parent.children
512 n.parent.children.splice i, 1
515 # pass a array of nodes (from parser library, ie it should have .el and .text)
516 tree_dedup_space = (tree) ->
517 prev = cur = next = null
518 prev_i = cur_i = next_i = 0
519 prev_pos = pos = next_pos = null
520 prev_px = cur_px = next_px = null
524 tree_remove_empty_text_nodes(tree)
526 iterate = (tree, cb) ->
530 while i < n.text.length # don't foreach, cb might remove chars
535 block = is_display_block n.el
538 if n.children.length > 0
539 iterate n.children, cb
544 removed_char = cur.text.charAt(cur_i)
545 cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + (cur.text.substr cur_i + 1)
546 if next is cur # in same text node
548 throw "how is this possible?"
553 cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + removed_char + (cur.text.substr cur_i)
554 if next is cur # in same text node
557 # return true if cur was removed from the dom (ie re-use same prev)
559 # cur definitately set
560 # prev and/or next might be null, indicating the start/end of a display:block
561 return false unless is_space_code cur.text.charCodeAt cur_i
562 bounds = text_range_bounds cur.el, cur_i, cur_i + 1
564 # 1. zero rects returned by getClientRects() means collapsed space
567 # 2. width greater than zero means visible space
570 # now the weird edge cases...
572 # firefox and chromium both report zero width for characters at the end
573 # of a line where the text wraps (automatically, due to word-wrap) to
574 # the next line. These do not appear to be distinguishable from
575 # collapsed spaces via the range/bounds api, so...
577 # remove it from the dom, and if prev or next moves, put it back.
578 if prev? and not prev_px?
579 prev_px = new_cursor_position n: prev, i: prev_i
580 if next? and not next_px?
581 next_px = new_cursor_position n: next, i: next_i
582 #if prev is null and next is null
583 # parent_px = cur.parent.el.getBoundingClientRect()
587 new_prev_px = new_cursor_position n: prev, i: prev_i
588 if new_prev_px.x isnt prev_px.x or new_prev_px.y isnt prev_px.y
591 console.log "this shouldn't happen, we remove spaces that don't locate"
594 new_next_px = new_cursor_position n: next, i: next_i
595 if new_next_px.x isnt next_px.x or new_next_px.y isnt next_px.y
598 # console.log "removing space becase space after it is collapsed"
600 # pass null at start/end of display:block
608 # don't advance (to the next character next time) if we removed a
609 # character from the same text node as ``next``, because doing so
610 # renumbers the indexes in that string
611 if removed and cur is next
627 tree_remove_empty_text_nodes(tree)
629 class PeachHTML5Editor
630 # Options: (all optional)
631 # editor_id: "id" attribute for outer-most element created by/for editor
632 # css_file: filename of a css file to style editable content
633 # on_init: callback for when the editable content is in place
634 constructor: (in_el, options) ->
635 @options = options ? {}
639 @init_1_called = false # when iframes have loaded
640 @outer_iframe # iframe to hold editor
641 @outer_idoc # "document" object for @outer_iframe
642 @wrap2 = null # scrollbar is on this
643 @iframe = null # iframe to hold editable content
644 @idoc = null # "document" object for @iframe
647 @cursor_visible = false
648 @poll_for_blur_timeout = null
650 @iframe_height = null
651 opt_fragment = @options.fragment ? true
654 @parser_opts.fragment = 'body'
656 @outer_iframe = domify document, iframe: {}
657 outer_iframe_style = 'border: none !important; margin: 0 !important; padding: 0 !important; height: 100% !important; width: 100% !important;'
658 if @options.editor_id?
659 @outer_iframe.setAttribute 'id', @options.editor_id
660 @outer_iframe.onload = =>
661 @outer_idoc = @outer_iframe.contentDocument
662 icss = domify @outer_idoc, style: children: [
663 domify @outer_idoc, text: css
665 @outer_idoc.head.appendChild icss
666 @iframe = domify @outer_idoc, iframe: sandbox: 'allow-same-origin allow-scripts'
669 timeout 200, => # firefox never fires this onload
670 @init_1() unless @init_1_called
671 @outer_idoc.body.appendChild(
672 domify @outer_idoc, div: id: 'wrap1', children: [
673 domify @outer_idoc, div: style: "position: absolute; top: 0; left: 1px; font-size: 10px", children: [ domify @outer_idoc, text: "Peach HTML5 Editor" ]
674 @wrap2 = domify @outer_idoc, div: id: 'wrap2', children: [
675 domify @outer_idoc, div: id: 'wrap3', children: [
677 @overlay = domify @outer_idoc, div: id: 'overlay'
682 outer_wrap = domify document, div: class: 'peach_html5_editor'
683 @in_el.parentNode.appendChild outer_wrap
684 outer_bounds = get_el_bounds outer_wrap
685 if outer_bounds.w < 300
687 if outer_bounds.h < 300
689 outer_iframe_style += "width: #{outer_bounds.w}px; height: #{outer_bounds.h}px;"
690 @outer_iframe.setAttribute 'style', outer_iframe_style
691 css = outer_css w: outer_bounds.w, h: outer_bounds.h
692 outer_wrap.appendChild @outer_iframe
693 init_1: -> # @iframe has loaded (but not it's css)
694 @idoc = @iframe.contentDocument
695 @init_1_called = true
696 # chromium doesn't resolve relative urls as though they were at the same domain
697 # so add a <base> tag
698 @idoc.head.appendChild domify @idoc, base: href: this_url_sans_path()
699 # don't let @iframe have scrollbars
700 @idoc.head.appendChild domify @idoc, style: children: [domify @idoc, text: "body { overflow: hidden; }"]
703 istyle = domify @idoc, link: rel: 'stylesheet', href: @options.css_file
706 @idoc.head.appendChild istyle
709 init_2: -> # @iframe and it's css file(s) are ready
710 @overlay.onclick = (e) =>
712 return event_return e, @onclick e
713 @overlay.ondoubleclick = (e) =>
715 return event_return e, @ondoubleclick e
716 @outer_idoc.body.onkeyup = (e) =>
718 return event_return e, @onkeyup e
719 @outer_idoc.body.onkeydown = (e) =>
721 return event_return e, @onkeydown e
722 @outer_idoc.body.onkeypress = (e) =>
724 return event_return e, @onkeypress e
725 @load_html @in_el.value
728 overlay_event_to_inner_xy: (e) ->
729 unless @wrap2_offset?
730 @wrap2_offset = get_el_bounds @wrap2
731 x = e.pageX - overlay_padding
732 y = e.pageY - overlay_padding + @wrap2.scrollTop
733 return x: x - @wrap2_offset.x, y: y - @wrap2_offset.y
735 xy = @overlay_event_to_inner_xy e
736 new_cursor = xy_to_cursor @tree, xy
738 @move_cursor new_cursor
742 ondoubleclick: (e) ->
746 return false if ignore_key_codes[e.keyCode]?
747 #return false if control_key_codes[e.keyCode]?
750 return false if ignore_key_codes[e.keyCode]?
751 #return false if control_key_codes[e.keyCode]?
755 new_cursor = find_prev_cursor_position @tree, @cursor
757 new_cursor = first_cursor_position @tree
759 @move_cursor new_cursor
763 new_cursor = find_next_cursor_position @tree, @cursor
765 new_cursor = last_cursor_position @tree
767 @move_cursor new_cursor
772 # go prev until we're higher on y axis
773 while new_cursor.y >= @cursor.y
774 new_cursor = find_prev_cursor_position @tree, new_cursor
775 return false unless new_cursor?
776 # done early if we're already left of old cursor position
777 if new_cursor.x <= @cursor.x
778 @move_cursor new_cursor
780 target_y = new_cursor.y
781 # search leftward, until we find the closest position
782 # new_cursor is the prev-most position we've checked
783 # prev_cursor is the older value, so it's not as prev as new_cursor
784 while new_cursor.x > @cursor.x and new_cursor.y is target_y
785 prev_cursor = new_cursor
786 new_cursor = find_prev_cursor_position @tree, new_cursor
787 break unless new_cursor?
788 # move cursor to prev_cursor or new_cursor
790 if new_cursor.y is target_y
791 # both valid, and on the same line, use closest
792 if (@cursor.x - new_cursor.x) < (prev_cursor.x - @cursor.x)
793 @move_cursor new_cursor
795 @move_cursor prev_cursor
797 # new_cursor on wrong line, use prev_cursor
798 @move_cursor prev_cursor
800 # can't go any further prev, use prev_cursor
801 @move_cursor prev_cursor
803 # move cursor to first position in document
804 new_cursor = first_cursor_position @tree
806 @move_cursor new_cursor
811 # go next until we move on the y axis
812 while new_cursor.y <= @cursor.y
813 new_cursor = find_next_cursor_position @tree, new_cursor
814 return false unless new_cursor?
815 # done early if we're already right of old cursor position
816 if new_cursor.x >= @cursor.x
817 # this would be strange, but could happen due to runaround
818 @move_cursor new_cursor
820 target_y = new_cursor.y
821 # search rightward, until we find the closest position
822 # new_cursor is the next-most position we've checked
823 # prev_cursor is the older value, so it's not as next as new_cursor
824 while new_cursor.x < @cursor.x and new_cursor.y is target_y
825 prev_cursor = new_cursor
826 new_cursor = find_next_cursor_position @tree, new_cursor
827 break unless new_cursor?
828 # move cursor to prev_cursor or new_cursor
830 if new_cursor.y is target_y
831 # both valid, and on the same line, use closest
832 if (new_cursor.x - @cursor.x) < (@cursor.x - prev_cursor.x)
833 @move_cursor new_cursor
835 @move_cursor prev_cursor
837 # new_cursor on wrong line, use prev_cursor
838 @move_cursor prev_cursor
840 # can't go any further prev, use prev_cursor
841 @move_cursor prev_cursor
843 # move cursor to first position in document
844 new_cursor = last_cursor_position @tree
846 @move_cursor new_cursor
851 return false unless @cursor?
852 return false unless @cursor.i > 0
853 @cursor.n.text = @cursor.n.text.substr(0, @cursor.i - 1) + @cursor.n.text.substr(@cursor.i)
854 @cursor.n.el.nodeValue = @cursor.n.text
855 new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i - 1
857 @move_cursor new_cursor
863 return false unless @cursor?
864 return false unless @cursor.i < @cursor.n.text.length
865 @cursor.n.text = @cursor.n.text.substr(0, @cursor.i) + @cursor.n.text.substr(@cursor.i + 1)
866 @cursor.n.el.nodeValue = @cursor.n.text
867 new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i
869 @move_cursor new_cursor
890 return false if ignore_key_codes[e.keyCode]?
891 # return false if control_key_codes[e.keyCode]? # handled in keydown
892 char = e.charCode ? e.keyCode
894 char = String.fromCharCode char
896 @cursor.n.text = char + @cursor.n.text
897 else if @cursor.i is @cursor.n.text.length - 1
898 @cursor.n.text += char
901 @cursor.n.text.substr(0, @cursor.i) +
903 @cursor.n.text.substr(@cursor.i)
904 @cursor.n.el.nodeValue = @cursor.n.text
905 new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i + 1
907 # probably pressed space, and browser isn't displaying it
908 # FIXME insert instead, rip it out later if possible, etc.
911 @cursor.n.text.substr(0, @cursor.i) +
912 @cursor.n.text.substr(@cursor.i + 1)
913 @cursor.n.el.nodeValue = @cursor.n.text
915 @move_cursor new_cursor
918 clear_dom: -> # remove all the editable content (and cursor, overlays, etc)
919 while @idoc.body.childNodes.length
920 @idoc.body.removeChild @idoc.body.childNodes[0]
924 @tree = peach_parser.parse html, @parser_opts
926 instantiate_tree @tree, @idoc.body
927 tree_dedup_space @tree
930 @in_el.onchange = null
931 @in_el.value = @pretty_html @tree
933 @load_html @in_el.value
934 @adjust_iframe_height()
935 adjust_iframe_height: ->
936 h = parseInt(@idoc.body.scrollHeight, 10)
937 if @iframe_height isnt h
940 @iframe.style.height = "0"
941 @iframe.style.height = "#{h}px"
943 kill_cursor: -> # remove it, forget where it was
945 @cursor_el.parentNode.removeChild @cursor_el
946 @cursor_visible = false
949 move_cursor: (cursor) ->
951 unless @cursor_visible
952 @cursor_el = domify @outer_idoc, div: id: 'cursor'
953 @overlay.appendChild @cursor_el
954 @cursor_visible = true
955 @cursor_el.style.left = "#{cursor.x + overlay_padding - 1}px"
960 @cursor_el.style.top = "#{cursor.y + overlay_padding + Math.round(height * .07)}px"
961 @cursor_el.style.height = "#{Math.round height * 0.82}px"
964 while @matting.length > 0
965 @overlay.removeChild @matting[0]
968 prev_bounds = x: 0, y: 0, w: 0, h: 0
974 bounds = get_el_bounds n.el
975 return unless bounds?
976 if bounds.x is prev_bounds.x and bounds.y is prev_bounds.y and bounds.w is prev_bounds.w and bounds.h is prev_bounds.h
979 ann_box = domify @outer_idoc, div: class: 'ann_box', style: "left: #{bounds.x - 1 + overlay_padding}px; top: #{bounds.y - 2 + overlay_padding}px; width: #{bounds.w}px; height: #{bounds.h}px" # outline: 1000px solid rgba(0,153,255,#{alpha});
980 @overlay.appendChild ann_box
981 @matting.push ann_box
982 ann_tag = domify @outer_idoc, div: class: 'ann_tag', style: "left: #{bounds.x + 1 + overlay_padding}px; top: #{bounds.y - 7 + overlay_padding}px", children: [domify @outer_idoc, text: " #{n.name} "]
983 @overlay.appendChild ann_tag
984 @matting.push ann_tag
987 pretty_html: (tree, indent = '', parent_flags = pre_ish: false, block: true, want_nl: false) ->
989 want_nl = parent_flags.want_nl
990 prev_in_flow_is_text = false
991 prev_in_flow_is_block = false
994 inner_flags = want_nl: true
1001 if n.el.currentStyle?
1002 cs = n.el.currentStyle
1003 whitespace = cs['white-space']
1004 display = cs['display']
1005 position = cs['position']
1007 visibility = cs['visibility']
1009 cs = @iframe.contentWindow.getComputedStyle(n.el, null)
1010 whitespace = cs.getPropertyValue 'white-space'
1011 display = cs.getPropertyValue 'display'
1012 position = cs.getPropertyValue 'position'
1013 float = cs.getPropertyValue 'float'
1014 visibility = cs.getPropertyValue 'visibility'
1015 if n.name is 'textarea'
1016 inner_flags.pre_ish = true
1018 inner_flags.pre_ish = whitespace.substr(0, 3) is 'pre'
1020 when 'left', 'right'
1024 when 'absolute', 'fixed'
1027 if 'display' is 'none'
1031 when 'hidden', 'collapse'
1036 when 'inline', 'none'
1037 inner_flags.block = false
1038 is_block = in_flow_block = false
1040 inner_flags.block = true
1041 is_block = in_flow_block = false
1042 else # block, table, etc
1043 inner_flags.block = true
1045 in_flow_block = in_flow
1050 in_flow_block = false
1051 else # 'comment', 'doctype'
1055 in_flow_block = false
1056 # print whitespace if we can
1057 unless parent_flags.pre_ish
1058 unless prev_in_flow_is_text and is_br
1059 if (i is 0 and parent_flags.block) or in_flow_block or prev_in_flow_is_block
1072 if n.attrs[k].length > 0
1073 ret += "=\"#{enc_attr n.attrs[k]}\""
1075 unless void_elements[n.name]?
1076 if inner_flags.block
1077 next_indent = indent + ' '
1079 next_indent = indent
1080 if n.children.length
1081 ret += @pretty_html n.children, next_indent, inner_flags
1082 ret += "</#{n.name}>"
1084 ret += enc_text n.text
1086 ret += "<!--#{n.text}-->" # TODO encode?
1088 ret += "<!DOCTYPE #{n.name}"
1089 if n.public_identifier? and n.public_identifier.length > 0
1090 ret += " \"#{n.public_identifier}\""
1091 if n.system_identifier? and n.system_identifier.length > 0
1092 ret += " \"#{n.system_identifier}\""
1096 prev_in_flow_is_text = is_text
1097 prev_in_flow_is_block = is_block or (in_flow and is_br)
1099 # output final newline if allowed
1100 unless parent_flags.pre_ish
1101 if prev_in_flow_is_block or parent_flags.block
1102 ret += "\n#{indent.substr 4}"
1107 @editor_is_focused = true
1110 return if @poll_for_blur_timeout? # already polling
1111 @poll_for_blur_timeout = timeout 150, =>
1112 next_frame => # pause polling when browser knows we're not active/visible/etc.
1113 @poll_for_blur_timeout = null
1114 if document.activeElement is @outer_iframe
1117 @editor_is_focused = false
1120 window.peach_html5_editor = (args...) ->
1121 return new PeachHTML5Editor args...
1123 # test in browser: peach_html5_editor(document.getElementsByTagName('textarea')[0])