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/>.
19 breathing_room = 30 # minimum pixels above/below cursor
21 timeout = (ms, cb) -> return setTimeout cb, ms
23 if (window.requestAnimationFrame?)
24 window.requestAnimationFrame cb
28 this_url_sans_path = ->
29 ret = "#{window.location.href}"
30 clip = ret.lastIndexOf '#'
32 ret = ret.substr 0, clip
33 clip = ret.lastIndexOf '?'
35 ret = ret.substr 0, clip
36 clip = ret.lastIndexOf '/'
38 ret = ret.substr 0, clip + 1
41 # table too look up the properties of various values for css's white-space
44 space: false # spaces are not preserved/rendered
45 newline: false # newlines are not preserved/rendered
46 wrap: true # text is word-wrapped
47 to_preserve: 'pre-wrap' # to preservespaces, change white-space to this
57 to_preserve: 'pre-wrap'
69 # xml 1.0 spec, chromium and firefox accept these, plus lots of unicode chars
70 valid_attr_regex = new RegExp '^[a-zA-Z_:][-a-zA-Z0-9_:.]*$'
71 # html5 spec is much more lax, but chromium won't let me make at attribute with the name "4"
72 js_attr_regex = new RegExp '^[oO][nN].'
73 # html5 spec says that only these characters are collapsable
74 multi_sp_regex = new RegExp '[\u0020\u0009\u000a\u000c\u000d][\u0020\u0009\u000a\u000c\u000d]'
76 debug_dot_at = (doc, x, y) ->
78 el = doc.createElement 'div'
79 el.setAttribute 'style', "position: absolute; left: #{x}px; top: #{y}px; width: 1px; height: 3px; background-color: red"
80 doc.body.appendChild el
81 #console.log(new Error().stack)
83 # text nodes don't have getBoundingClientRect(), so use selection api to find
85 get_el_bounds = window.bounds = (el) ->
86 if el.getBoundingClientRect?
87 rect = el.getBoundingClientRect()
89 # text nodes don't have getBoundingClientRect(), so use range api
90 range = el.ownerDocument.createRange()
91 range.selectNodeContents el
92 rect = range.getBoundingClientRect()
93 doc = el.ownerDocument.documentElement
94 win = el.ownerDocument.defaultView
95 y_fix = win.pageYOffset - doc.clientTop
96 x_fix = win.pageXOffset - doc.clientLeft
100 w: rect.width ? (rect.right - rect.left)
101 h: rect.height ? (rect.top - rect.bottom)
104 is_display_block = (el) ->
106 return el.currentStyle.display is 'block'
108 return window.getComputedStyle(el, null).getPropertyValue('display') is 'block'
110 # Pass return value from dom event handlers to this.
111 # If they return false, this will addinionally stop propagation and default.
112 event_return = (e, bool) ->
114 if e.stopPropagation?
119 # Warning: currently assumes you're asking about a single character
120 # Note: chromium returns multiple bounding rects for a space at a line-break
121 # Note: chromium's getBoundingClientRect() is broken (when zero-area client rects)
122 # Note: sometimes returns null (eg for whitespace that is not visible)
123 text_range_bounds = (el, start, end) ->
124 range = document.createRange()
125 range.setStart el, start
127 rects = range.getClientRects()
130 if rects[1].width > rects[0].width
138 doc = el.ownerDocument.documentElement
139 win = el.ownerDocument.defaultView
140 y_fix = win.pageYOffset - doc.clientTop
141 x_fix = win.pageXOffset - doc.clientLeft
145 w: rect.width ? (rect.right - rect.left)
146 h: rect.height ? (rect.top - rect.bottom)
148 bounding: range.getBoundingClientRect()
152 constructor: (args) ->
163 range = document.createRange()
164 if @n.text.length is 0
165 ret = text_range_bounds @n.el, 0, 0
166 else if @i is @n.text.length
167 ret = text_range_bounds @n.el, @i - 1, @i
171 ret = text_range_bounds @n.el, @i, @i + 1
182 new_cursor_position = (args) ->
183 ret = new CursorPosition args
188 # encode text so it can be safely placed inside an html attribute
189 enc_attr_regex = new RegExp '(&)|(")|(\u00A0)', 'g'
191 return txt.replace enc_attr_regex, (match, amp, quote) ->
192 return '&' if (amp)
193 return '"' if (quote)
195 enc_text_regex = new RegExp '(&)|(<)|(\u00A0)', 'g'
197 return txt.replace enc_text_regex, (match, amp, lt) ->
198 return '&' if (amp)
199 return '<' if (lt)
219 # TODO make these always pretty-print (on the inside) like blocks
220 no_text_elements = { # these elements never contain text
230 domify = (doc, hash) ->
231 for tag, attrs of hash
233 return document.createTextNode attrs
234 el = document.createElement tag
247 '20': true # capslock
249 '144': true # numlock
251 '91': true # windows "start" key
252 # key codes: (valid on keydown, not keypress)
257 KEY_BACKSPACE = 8 # <--
258 KEY_DELETE = 46 # -->
267 control_key_codes = # we react to these, but they aren't typing
283 instantiate_tree = (tree, parent) ->
288 c.el = parent.ownerDocument.createTextNode c.text
289 parent.appendChild c.el
291 if c.name in ['script', 'object', 'iframe', 'link']
292 # TODO put placeholders instead
295 # TODO create in correct namespace
296 c.el = parent.ownerDocument.createElement c.name
298 # FIXME if attr_whitelist[k]?
299 if valid_attr_regex.test k
300 unless js_attr_regex.test k
301 c.el.setAttribute k, v
302 parent.appendChild c.el
304 instantiate_tree c.children, c.el
308 traverse_tree = (tree, cb) ->
314 done = traverse_tree c.children, cb
318 first_cursor_position = (tree) ->
320 traverse_tree tree, (node, state) ->
321 if node.type is 'text'
322 cursor = new_cursor_position n: node, i: 0
325 return true # done traversing
326 return false # not done traversing
327 return found # maybe null
329 # this will fail when text has non-locatable cursor positions
330 find_next_cursor_position = (tree, cursor) ->
331 if cursor.n.type is 'text' and cursor.n.text.length > cursor.i
332 new_cursor = new_cursor_position n: cursor.n, i: cursor.i + 1
337 traverse_tree tree, (node, state) ->
338 if node.type is 'text' and state_before is false
339 new_cursor = new_cursor_position n: node, i: 0
342 return true # done traversing
345 return false # not done traversing
350 last_cursor_position = (tree) ->
352 traverse_tree tree, (node) ->
353 if node.type is 'text'
354 cursor = new_cursor_position n: node, i: node.text.length
357 return false # not done traversing
358 return found # maybe null
360 # this will fail when text has non-locatable cursor positions
361 find_prev_cursor_position = (tree, cursor) ->
362 if cursor.n.type is 'text' and cursor.i > 0
363 new_cursor = new_cursor_position n: cursor.n, i: cursor.i - 1
368 traverse_tree tree, (node) ->
370 found = found_prev # maybe null
371 return true # done traversing
372 if node.type is 'text'
373 new_cursor = new_cursor_position n: node, i: node.text.length
375 found_prev = new_cursor
376 return false # not done traversing
377 return found # maybe null
379 find_up_cursor_position = (tree, cursor, ideal_x) ->
381 # go prev until we're higher on y axis
382 while new_cursor.y >= cursor.y
383 new_cursor = find_prev_cursor_position tree, new_cursor
384 return null unless new_cursor?
385 # done early if we're already left of old cursor position
386 if new_cursor.x <= ideal_x
388 target_y = new_cursor.y
389 # search leftward, until we find the closest position
390 # new_cursor is the prev-most position we've checked
391 # prev_cursor is the older value, so it's not as prev as new_cursor
392 while new_cursor.x > ideal_x and new_cursor.y is target_y
393 prev_cursor = new_cursor
394 new_cursor = find_prev_cursor_position tree, new_cursor
395 break unless new_cursor?
396 # move cursor to prev_cursor or new_cursor
398 if new_cursor.y is target_y
399 # both valid, and on the same line, use closest
400 if (ideal_x - new_cursor.x) < (prev_cursor.x - ideal_x)
405 # new_cursor on wrong line, use prev_cursor
408 # can't go any further prev, use prev_cursor
411 find_down_cursor_position = (tree, cursor, ideal_x) ->
413 # go next until we move on the y axis
414 while new_cursor.y <= cursor.y
415 new_cursor = find_next_cursor_position tree, new_cursor
416 return null unless new_cursor?
417 # done early if we're already right of old cursor position
418 if new_cursor.x >= ideal_x
419 # this would be strange, but could happen due to runaround
421 target_y = new_cursor.y
422 # search rightward, until we find the closest position
423 # new_cursor is the next-most position we've checked
424 # prev_cursor is the older value, so it's not as next as new_cursor
425 while new_cursor.x < ideal_x and new_cursor.y is target_y
426 prev_cursor = new_cursor
427 new_cursor = find_next_cursor_position tree, new_cursor
428 break unless new_cursor?
429 # move cursor to prev_cursor or new_cursor
431 if new_cursor.y is target_y
432 # both valid, and on the same line, use closest
433 if (new_cursor.x - ideal_x) < (ideal_x - prev_cursor.x)
438 # new_cursor on wrong line, use prev_cursor
441 # can't go any further prev, use prev_cursor
444 xy_to_cursor = (tree, xy) ->
446 if n.type is 'tag' or n.type is 'text'
447 bounds = get_el_bounds n.el
448 continue if xy.x < bounds.x
449 continue if xy.x > bounds.x + bounds.w
450 continue if xy.y < bounds.y
451 continue if xy.y > bounds.y + bounds.h
453 ret = xy_to_cursor n.children, xy
456 # click is within bounding box that contains all text.
457 if n.text.length is 0
458 ret = new_cursor_position n: n, i: 0
461 before = new_cursor_position n: n, i: 0
462 continue unless before?
463 after = new_cursor_position n: n, i: n.text.length
464 continue unless after?
465 if xy.y < before.y + before.h and xy.x < before.x
466 # console.log 'before first char on first line'
468 if xy.y > after.y and xy.x > after.x
469 # console.log 'after last char on last line'
472 console.log "Warning: click in text bounding box but above first line"
473 continue # above first line (runaround?)
474 if xy.y > after.y + after.h
475 console.log "Warning: click in text bounding box but below last line", xy.y, after.y, after.h
476 continue # below last line (shouldn't happen?)
477 while after.i - before.i > 1
478 guess_i = Math.round((before.i + after.i) / 2)
479 cur = new_cursor_position n: n, i: guess_i
481 console.log "error: failed to find cursor pixel location for", n, guess_i
484 if xy.y < cur.y or (xy.y <= cur.y + cur.h and xy.x < cur.x)
488 continue unless before? # signals failure to find a cursor position
489 # which one is closest?
490 if Math.abs(before.x - xy.x) < Math.abs(after.x - xy.x)
496 # browsers collapse these (html5 spec calls these "space characters")
497 is_space_code = (char_code) ->
499 when 9, 10, 12, 13, 32
503 return is_space_code chr.charCodeAt 0
505 tree_remove_empty_text_nodes = (tree) ->
507 traverse_tree tree, (n) ->
509 if n.text.length is 0
511 return false # not done traversing
513 # don't completely empty the tree
515 if tree[0].type is 'text'
516 console.log "oop, leaving a blank node because it's the only thing"
518 n.el.parentNode.removeChild n.el
519 for c, i in n.parent.children
521 n.parent.children.splice i, 1
524 # pass a array of nodes (from parser library, ie it should have .el and .text)
525 tree_dedup_space = (tree) ->
526 prev = cur = next = null
527 prev_i = cur_i = next_i = 0
528 prev_pos = pos = next_pos = null
529 prev_px = cur_px = next_px = null
533 tree_remove_empty_text_nodes(tree)
535 iterate = (tree, cb) ->
539 while i < n.text.length # don't foreach, cb might remove chars
544 block = is_display_block n.el
547 if n.children.length > 0
548 iterate n.children, cb
554 cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + removed_char + (cur.text.substr cur_i)
555 if next is cur # in same text node
559 removed_char = cur.text.charAt(cur_i)
560 cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + (cur.text.substr cur_i + 1)
561 if next is cur # in same text node
563 throw "how is this possible?"
566 replace_with_space = (undo) ->
568 cur.text = (cur.text.substr 0, cur_i) + removed_char + (cur.text.substr cur_i + 1)
569 cur.el.textContent = cur.text
571 removed_char = cur.text.charAt(cur_i)
572 if removed_char isnt ' '
573 cur.text = (cur.text.substr 0, cur_i) + ' ' + (cur.text.substr cur_i + 1)
574 cur.el.textContent = cur.text
576 # return true if cur was removed from the dom (ie re-use same prev)
578 # cur definitately set
579 # prev and/or next might be null, indicating the start/end of a display:block
580 return false unless is_space_code cur.text.charCodeAt cur_i
581 fixers = [remove, replace_with_space]
582 # check for common case: single whitespace surrounded by non-whitespace chars
584 unless (is_space_code prev.text.charCodeAt prev_i) or (is_space_code next.text.charCodeAt next_i)
585 dbg = cur.text.charCodeAt cur_i
586 if cur.text.charAt(cur_i) is ' ' # perens required
587 # single space can't collapse, doesn't need fixin'
590 # tab, newline, etc, can't collapse, but maybe should be replaced
591 fixers = [replace_with_space]
592 bounds = text_range_bounds cur.el, cur_i, cur_i + 1
594 # 1. zero rects returned by getClientRects() means collapsed space
597 # 2. width greater than zero means visible space
599 # has bounds, don't try removing
600 fixers = [replace_with_space]
601 # now the weird edge cases...
603 # firefox and chromium both report zero width for characters at the end
604 # of a line where the text wraps (automatically, due to word-wrap) to
605 # the next line. These do not appear to be distinguishable from
606 # collapsed spaces via the range/bounds api, so...
608 # remove it from the dom, and if prev or next moves, put it back.
610 # this block (try changing it, put it back if something moves) is also
611 # used on collapsable whitespace characters besides space. In this case
612 # the character is replaced with a normal space character instead of
614 if prev? and not prev_px?
615 prev_px = new_cursor_position n: prev, i: prev_i
616 if next? and not next_px?
617 next_px = new_cursor_position n: next, i: next_i
618 #if prev is null and next is null
619 # parent_px = cur.parent.el.getBoundingClientRect()
620 undo_arg = true # just for readabality
628 new_prev_px = new_cursor_position n: prev, i: prev_i
630 if new_prev_px.x isnt prev_px.x or new_prev_px.y isnt prev_px.y
635 console.log "this shouldn't happen, we remove spaces that don't locate"
636 if next? and not need_undo
638 new_next_px = new_cursor_position n: next, i: next_i
640 if new_next_px.x isnt next_px.x or new_next_px.y isnt next_px.y
645 # console.log "removing space becase space after it is collapsed"
647 removed += fixer undo_arg
652 # pass null at start/end of display:block
660 # don't advance (to the next character next time) if we removed a
661 # character from the same text node as ``next``, because doing so
662 # renumbers the indexes in that string
663 if removed and cur is next
679 tree_remove_empty_text_nodes(tree)
681 class PeachHTML5Editor
682 # Options: (all optional)
683 # editor_id: "id" attribute for outer-most element created by/for editor
684 # css_file: filename of a css file to style editable content
685 # on_init: callback for when the editable content is in place
686 constructor: (in_el, options) ->
687 @options = options ? {}
689 @tree = null # array of Nodes, all editable content
690 @tree_parent = null # @tree is this.children. .el might === @idoc.body
692 @init_1_called = false # when iframes have loaded
693 @outer_iframe # iframe to hold editor
694 @outer_idoc # "document" object for @outer_iframe
695 @wrap2 = null # scrollbar is on this
697 @wrap2_height = null # including padding
698 @iframe = null # iframe to hold editable content
699 @idoc = null # "document" object for @iframe
702 @cursor_visible = false
703 @cursor_ideal_x = null
704 @poll_for_blur_timeout = null
705 opt_fragment = @options.fragment ? true
708 @parser_opts.fragment = 'body'
710 @outer_iframe = domify document, iframe: {}
711 outer_iframe_style = 'border: none !important; margin: 0 !important; padding: 0 !important; height: 100% !important; width: 100% !important;'
712 if @options.editor_id?
713 @outer_iframe.setAttribute 'id', @options.editor_id
714 @outer_iframe.onload = =>
715 @outer_idoc = @outer_iframe.contentDocument
716 icss = domify @outer_idoc, style: children: [
717 domify @outer_idoc, text: css
719 @outer_idoc.head.appendChild icss
720 @iframe = domify @outer_idoc, iframe: sandbox: 'allow-same-origin allow-scripts'
723 timeout 200, => # firefox never fires this onload
724 @init_1() unless @init_1_called
725 @outer_idoc.body.appendChild(
726 domify @outer_idoc, div: id: 'wrap1', children: [
727 domify @outer_idoc, div: style: "position: absolute; top: 0; left: 1px; font-size: 10px", children: [ domify @outer_idoc, text: "Peach HTML5 Editor" ]
728 @wrap2 = domify @outer_idoc, div: id: 'wrap2', children: [
729 domify @outer_idoc, div: id: 'wrap3', children: [
731 @overlay = domify @outer_idoc, div: id: 'overlay'
736 outer_wrap = domify document, div: class: 'peach_html5_editor'
737 @in_el.parentNode.appendChild outer_wrap
738 outer_bounds = get_el_bounds outer_wrap
739 if outer_bounds.w < 300
741 if outer_bounds.h < 300
743 outer_iframe_style += "width: #{outer_bounds.w}px; height: #{outer_bounds.h}px;"
744 @outer_iframe.setAttribute 'style', outer_iframe_style
745 css = @generate_outer_css w: outer_bounds.w, h: outer_bounds.h
746 outer_wrap.appendChild @outer_iframe
747 init_1: -> # @iframe has loaded (but not it's css)
748 @idoc = @iframe.contentDocument
749 @init_1_called = true
750 # chromium doesn't resolve relative urls as though they were at the same domain
751 # so add a <base> tag
752 @idoc.head.appendChild domify @idoc, base: href: this_url_sans_path()
753 # don't let @iframe have scrollbars
754 @idoc.head.appendChild domify @idoc, style: children: [domify @idoc, text: "body { overflow: hidden; }"]
757 istyle = domify @idoc, link: rel: 'stylesheet', href: @options.css_file
760 @idoc.head.appendChild istyle
763 init_2: -> # @iframe and it's css file(s) are ready
764 @overlay.onclick = (e) =>
766 return event_return e, @onclick e
767 @overlay.ondoubleclick = (e) =>
769 return event_return e, @ondoubleclick e
770 @outer_idoc.body.onkeyup = (e) =>
772 return event_return e, @onkeyup e
773 @outer_idoc.body.onkeydown = (e) =>
775 return event_return e, @onkeydown e
776 @outer_idoc.body.onkeypress = (e) =>
778 return event_return e, @onkeypress e
779 @load_html @in_el.value
782 generate_outer_css: (args) ->
785 inner_padding = args.inner_padding ? overlay_padding
786 frame_width = args.frame_width ? inner_padding
787 occupy = (left, top = left, right = left, bottom = top) ->
790 return Math.max(left, top, right, bottom)
795 ret += 'color: black;'
796 ret += 'background: white;'
799 ret += "border: #{occupy 1}px solid black;"
800 ret += "padding: #{occupy frame_width}px;"
803 ret += "border: #{occupy 1}px solid black;"
804 @wrap2_height = h # including padding because padding scrolls
805 ret += "padding: #{occupy inner_padding}px;"
806 ret += "padding-right: #{inner_padding + occupy 0, 0, 15, 0}px;" # for scroll bar
807 ret += "width: #{w}px;"
808 ret += "height: #{h}px;"
809 ret += 'overflow-x: hidden;'
810 ret += 'overflow-y: scroll;'
813 ret += 'position: relative;'
814 ret += "width: #{w}px;"
815 ret += "min-height: #{h}px;"
818 ret += 'box-sizing: border-box;'
820 ret += 'border: none;'
822 ret += "width: #{w}px;"
823 #ret += "height: #{h}px;" # height auto-set when content set/changed
824 ret += '-ms-user-select: none;'
825 ret += '-webkit-user-select: none;'
826 ret += '-moz-user-select: none;'
827 ret += 'user-select: none;'
830 ret += 'position: absolute;'
831 ret += "left: -#{inner_padding}px;"
832 ret += "top: -#{inner_padding}px;"
833 ret += "right: -#{inner_padding}px;"
834 ret += "bottom: -#{inner_padding}px;"
835 ret += 'overflow: hidden;'
838 ret += 'position: absolute;'
839 ret += 'background: rgba(100,100,100,0.2);'
842 ret += 'position: absolute;'
844 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));'
845 ret += 'background-size: 200% 200%;'
846 ret += '-webkit-animation: blink 1s linear normal infinite;'
847 ret += 'animation: blink 1s linear normal infinite;'
849 ret += '@-webkit-keyframes blink {'
850 ret += '0%{background-position:0% 0%}'
851 ret += '100%{background-position:0% -100%}'
853 ret += '@keyframes blink { '
854 ret += '0%{background-position:0% 0%}'
855 ret += '100%{background-position:0% -100%}'
859 ret += 'position: absolute;'
860 ret += 'border: 1px solid rgba(0,0,0,0.1);'
861 ret += 'outline: 1px solid rgba(255,255,255,0.1);' # in case there's a black background
864 ret += 'z-index: 10;'
865 ret += 'position: absolute;'
866 ret += 'font-size: 8px;'
867 ret += 'white-space: pre;'
868 ret += 'background: rgba(255,255,255,0.4);'
869 ret += '-ms-user-select: none;'
870 ret += '-webkit-user-select: none;'
871 ret += '-moz-user-select: none;'
872 ret += 'user-select: none;'
875 overlay_event_to_inner_xy: (e) ->
876 unless @wrap2_offset?
877 @wrap2_offset = get_el_bounds @wrap2
878 x = e.pageX - overlay_padding
879 y = e.pageY - overlay_padding + @wrap2.scrollTop
880 return x: x - @wrap2_offset.x, y: y - @wrap2_offset.y
882 xy = @overlay_event_to_inner_xy e
883 new_cursor = xy_to_cursor @tree, xy
885 @move_cursor new_cursor
889 ondoubleclick: (e) ->
893 return false if ignore_key_codes[e.keyCode]?
894 #return false if control_key_codes[e.keyCode]?
897 return false if ignore_key_codes[e.keyCode]?
898 #return false if control_key_codes[e.keyCode]?
902 new_cursor = find_prev_cursor_position @tree, @cursor
904 new_cursor = first_cursor_position @tree
906 @move_cursor new_cursor
910 new_cursor = find_next_cursor_position @tree, @cursor
912 new_cursor = last_cursor_position @tree
914 @move_cursor new_cursor
918 new_cursor = find_up_cursor_position @tree, @cursor, @cursor_ideal_x
920 saved_ideal_x = @cursor_ideal_x
921 @move_cursor new_cursor
922 @cursor_ideal_x = saved_ideal_x
924 # move cursor to first position in document
925 new_cursor = first_cursor_position @tree
927 @move_cursor new_cursor
931 new_cursor = find_down_cursor_position @tree, @cursor, @cursor_ideal_x
933 saved_ideal_x = @cursor_ideal_x
934 @move_cursor new_cursor
935 @cursor_ideal_x = saved_ideal_x
937 # move cursor to first position in document
938 new_cursor = last_cursor_position @tree
940 @move_cursor new_cursor
943 new_cursor = last_cursor_position @tree
945 @move_cursor new_cursor
951 return false unless @cursor?
952 return false unless @cursor.i < @cursor.n.text.length
953 @remove_character @cursor.n, @cursor.i
954 @adjust_whitespace_style @cursor.n
956 new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i
958 @move_cursor new_cursor
969 new_cursor = first_cursor_position @tree
971 @move_cursor new_cursor
985 return false if ignore_key_codes[e.keyCode]?
986 char = e.charCode ? e.keyCode
988 char = String.fromCharCode char
989 @insert_character @cursor.n, @cursor.i, char
990 @adjust_whitespace_style @cursor.n
992 new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i + 1
994 @move_cursor new_cursor
996 console.log "ERROR: couldn't find cursor position after insert"
999 on_key_enter: (e) -> # enter key pressed
1000 return unless @cursor_visible
1001 cur_block = @cursor.n
1003 if cur_block.type is 'tag'
1004 if is_display_block cur_block.el
1006 return unless cur_block.parent?
1007 cur_block = cur_block.parent
1008 # find array to insert new element into
1009 if cur_block.parent is @tree_parent # top-level
1010 parent_el = @idoc.body
1013 parent_el = cur_block.parent.el
1014 pc = cur_block.parent.children
1015 # find index of current block in its parent
1017 break if n is cur_block
1018 i += 1 # we want to be after it
1023 # TODO if content after cursor
1024 # TODO new block is empty
1025 new_text = new peach_parser.Node 'text', text: ' '
1026 new_node = new peach_parser.Node 'tag', name: 'p', parent: cur_block.parent, attrs: {style: 'white-space: pre-wrap'}, children: [new_text]
1027 new_text.parent = new_node
1028 new_text.el = domify @idoc, text: ' '
1029 new_node.el = domify @idoc, p: style: 'white-space: pre-wrap', children: [new_text.el]
1030 pc.splice i, 0, new_node
1031 parent_el.insertBefore new_node.el, before
1033 new_cursor = new_cursor_position n: new_text, i: 0
1034 throw 'bork bork' unless new_cursor?
1035 @move_cursor new_cursor
1036 # TODO move content past cursor into this new block
1037 # unlike the global function, this takes a Node, not an element
1038 is_display_block: (n) ->
1039 # TODO stop calling global function, merge it into here, use iframe's window object
1040 return false unless n.type is 'tag'
1041 return is_display_block n.el
1042 find_block_parent: (n) ->
1045 return null unless n?
1046 return n if @is_display_block n
1047 return n if n is @tree_parent
1049 # return a flat array of nodes (text, <br>, and later also inline-block)
1050 # that are flowing/wrapping together. n can be the containing block, or any
1051 # element inside it.
1052 get_text_run: (n) ->
1053 if @is_display_block n
1056 block = @find_block_parent n
1057 return unless block?
1059 traverse_tree n.children, (n) =>
1062 else if n.type is 'tag'
1066 disp = @computed_style n
1067 if disp is 'inline-block'
1069 return false # not done traversing
1071 on_key_backspace: (e) ->
1072 return false unless @cursor?
1073 if @is_lone_space @cursor.n # false if it's not in a tag
1075 # don't delete the space, because then it would collapse
1076 # instead leave a space after the cursor
1077 new_cursor = new_cursor_position n: @cursor.n, i: 0
1079 @move_cursor new_cursor
1083 # cursor at the begining of an element that contains only a space
1084 parent = @cursor.n.parent
1085 new_cursor = find_prev_cursor_position @tree, @cursor
1087 if new_cursor.n is @cursor.n or new_cursor.n is parent
1089 tag = @cursor.n.parent
1090 if tag is @tree_parent
1091 console.log "top-level text not supported" # FIXME
1093 for n, i in tag.parent.children
1095 tag.parent.el.removeChild tag.el
1096 tag.parent.children.splice i, 1
1100 # re-check, in case it moved or is invalid now
1101 new_cursor = new_cursor_position n: new_cursor.n, i: new_cursor.i
1103 @move_cursor new_cursor
1105 new_cursor = first_cursor_position @tree
1107 @move_cursor new_cursor
1111 else if @cursor.i is 0 # start of text chunk
1112 # FIXME clean this up: use new code for text runs
1113 # FIXME handle backspacing a <br> even if it's near a inline tag boundary
1114 # determine if cursor is at start of text run (text formatted inline)
1115 block = @find_block_parent @cursor.n
1117 at_block_start = true
1118 prev_pos = find_prev_cursor_position @tree, @cursor
1120 # if the cursor can't go back, then there's probably nowhere we can merge into
1121 # TODO consider case of nested blocks. should backspace remove one?
1123 prev_pos_block = @find_block_parent prev_pos.n
1124 if prev_pos_block is block
1125 # context: there is text before the cursor within the same block.
1126 # FIXME clean up this hack for looking for <br> (see above)
1127 cursor_text_pi = @cursor.n.parent.children.indexOf @cursor.n
1128 if cursor_text_pi > 0
1129 prev_node = @cursor.n.parent.children[cursor_text_pi - 1]
1130 if prev_node.type is 'tag' and prev_node.name is 'br'
1131 @remove_node prev_node
1132 @text_cleanup @cursor.n.parent
1134 new_cursor = new_cursor_position n: prev_pos.n, i: prev_pos.i
1136 @move_cursor new_cursor
1140 # note: find_prev_cursor_position just crossed a boundary, not a character
1141 # prev_pos is within the same block, try deleting there
1142 @move_cursor prev_pos
1143 # FIXME cleanup: don't call @move_cursor twice if the next line succeeds
1144 return @on_key_backspace()
1145 # context: backspace pressed at start of a display:block
1146 return if block is @tree_parent # top level text
1147 parent = block.parent
1148 parent_i = parent.children.indexOf block
1150 throw "BUG #98270918347"
1153 # no previous sibling to merge into, so instead move contents into parent
1157 # FIXME prev_sib should be the previous in-flow element
1158 # ie it should skip comments, hidden things, floating things, etc.
1159 prev_sib = parent.children[parent_i - 1]
1160 if @is_display_block prev_sib
1162 before = null # null means append
1166 if dest is @tree_parent
1167 # don't remove outer-most blocks
1169 while block.children.length > 0
1170 n = block.children[block.children.length - 1]
1171 @move_node n, dest, before
1176 new_cursor = new_cursor_position n: prev_pos.n, i: prev_pos.i
1178 @move_cursor new_cursor
1183 # TODO handle case of removing last char
1185 if @is_only_char_in_tag @cursor.n
1186 if @is_display_block @cursor.n.parent
1187 @cursor.n.el.textContent = @cursor.n.text = ' '
1189 console.log "unimplemented: delete last char in inline" # FIXME
1192 @remove_character @cursor.n, @cursor.i - 1
1193 @adjust_whitespace_style @cursor.n
1195 new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i - 1
1197 @move_cursor new_cursor
1201 on_page_up_key: (e) ->
1202 if @wrap2.scrollTop is 0
1203 return unless @cursor?
1204 new_cursor = first_cursor_position @tree
1206 if new_cursor.n isnt @cursor.n or new_cursor.i isnt @cursor.i
1207 @move_cursor new_cursor
1210 screen_y = @cursor.y - @wrap2.scrollTop
1211 scroll_amount = @wrap2_height - breathing_room
1212 @wrap2.scrollTop = Math.max 0, @wrap2.scrollTop - scroll_amount
1214 @move_cursor_into_view screen_y + @wrap2.scrollTop
1215 on_page_down_key: (e) ->
1216 lowest_scrollpos = @wrap2.scrollHeight - @wrap2_height
1217 if @wrap2.scrollTop is lowest_scrollpos
1218 return unless @cursor?
1219 new_cursor = last_cursor_position @tree
1221 if new_cursor.n isnt @cursor.n or new_cursor.i isnt @cursor.i
1222 @move_cursor new_cursor
1225 screen_y = @cursor.y - @wrap2.scrollTop
1226 scroll_amount = @wrap2_height - breathing_room
1227 @wrap2.scrollTop = Math.min lowest_scrollpos, @wrap2.scrollTop + scroll_amount
1229 @move_cursor_into_view screen_y + @wrap2.scrollTop
1231 move_cursor_into_view: (y_target) ->
1232 return if y_target is @cursor.y
1234 y_min = @wrap2.scrollTop
1235 unless @wrap2.scrollTop is 0
1236 y_min += breathing_room
1237 y_max = @wrap2.scrollTop + @wrap2_height
1238 unless @wrap2.scrollTop is @wrap2.scrollHeight - @wrap2_height # downmost
1239 y_max -= breathing_room
1240 y_target = Math.min y_target, y_max
1241 y_target = Math.max y_target, y_min
1242 if y_target < @cursor.y
1243 finder = find_up_cursor_position
1244 far_enough = (cur, target_y) ->
1245 return cur.y + cur.h <= target_y
1247 finder = find_down_cursor_position
1248 far_enough = (cur, y_target) ->
1249 return cur.y >= y_target
1251 cur = finder @tree, was, @cursor_ideal_x
1253 break if far_enough cur, y_target
1258 if was.y + was.h > y_max
1260 else if was.y < y_min
1263 if cur.y + cur.h > y_max
1265 else if cur.y < y_min
1268 # both valid, pick best
1271 else if was.y + was.h > y_max
1273 else if cur.y - y_target < y_target - was.y
1278 new_cursor = was ? cur
1280 saved_ideal_x = @cursor_ideal_x
1281 @move_cursor new_cursor
1282 @cursor_ideal_x = saved_ideal_x
1284 clear_dom: -> # remove all the editable content (and cursor, overlays, etc)
1285 while @idoc.body.childNodes.length
1286 @idoc.body.removeChild @idoc.body.childNodes[0]
1289 load_html: (html) ->
1290 @tree = peach_parser.parse html, @parser_opts
1291 if !@tree[0]?.parent
1292 @tree = peach_parser.parse '<p style="white-space: pre-wrap"> </p>', @parser_opts
1293 @tree_parent = @tree[0]?.parent
1294 @tree_parent.el = @idoc.body
1296 instantiate_tree @tree, @tree_parent.el
1297 tree_dedup_space @tree
1300 @in_el.onchange = null
1301 @in_el.value = @pretty_html @tree
1302 @in_el.onchange = =>
1303 @load_html @in_el.value
1304 @adjust_iframe_height()
1305 adjust_iframe_height: ->
1306 s = @wrap2.scrollTop
1307 # when the content gets shorter, the idoc's body tag will continue to
1308 # report the old (too big) height in Chrome. The workaround is to
1309 # shrink the iframe before the content height:
1310 @iframe.style.height = "10px"
1311 h = parseInt(@idoc.body.scrollHeight, 10)
1312 @iframe.style.height = "#{h}px"
1313 @wrap2.scrollTop = s
1314 # does this node have whitespace that would be collapsed by white-space: normal?
1315 # note: this checks direct text children, and does _not_ recurse into child tags
1316 # tag is a node with type:"tag"
1317 # FIXME use new textrun api
1318 has_collapsable_space: (tag) ->
1319 for n in tag.children
1321 for i in [0...n.text.length]
1322 code = n.text.charCodeAt i
1323 if code isnt 32 and is_space_code code
1326 # check for double spaces that don't surround insert location
1328 if n.text.substr(i - 1, 2) is ' '
1330 if n.text.length > 0
1331 if is_space_code n.text.charCodeAt 0
1333 if is_space_code n.text.charCodeAt n.text.length - 1
1336 # add/remove "white-space: pre[-wrap]" to/from style="" on tags with direct
1337 # child text nodes with multiple spaces in a row, or spaces at the
1340 # text inside child tags are not consulted. Child tags are expected to have
1341 # this function applied to them when their content changes.
1343 # FIXME stop using this and delete it. use @text_cleanup instead
1344 adjust_whitespace_style: (n) ->
1346 break if @is_display_block n
1349 return if n is @tree_parent
1350 # which css rule should be used to preserve spaces (should we need to)
1351 style = @iframe.contentWindow.getComputedStyle n.el, null
1352 ws = style.getPropertyValue 'white-space'
1353 if ws_props[ws].space
1356 preserve_rule = ws_props[ws].to_preserve
1357 preserve_rule = "white-space: #{preserve_rule}"
1358 if @has_collapsable_space n
1359 # make sure preserve_rule exists
1360 if n.el.style['white-space']
1361 # FIXME check that it matches
1364 n.attrs.style += "; #{preserve_rule}"
1366 n.attrs.style = preserve_rule
1367 n.el.setAttribute 'style', n.attrs.style
1369 # remove preserve_rule if it exists
1370 return unless n.attrs.style?
1371 # FIXME don't assume whitespace is just so
1372 if n.attrs.style is "white-space: #{ws}"
1373 delete n.attrs.style
1374 n.el.removeAttribute 'style'
1376 # FIXME find it in the middle and at the start
1377 needle = "; white-space: #{ws}"
1378 if needle is n.attrs.style.substr n.attrs.style.length - needle
1379 n.attrs.style = n.attrs.style.substr 0, n.attrs.style.length - needle
1380 n.el.setAttribute n.attrs.style
1381 # true if n is text node with only one caracter, and the only child of a tag
1382 is_only_char_in_tag: (n, i) ->
1383 return false unless n.type is 'text'
1384 return false unless n.text.length is 1
1385 return false if n.parent is @tree_parent
1386 return false unless n.parent.children.length is 1
1388 # true if n is text node with just a space in it, and the only child of a tag
1389 is_lone_space: (n, i) ->
1390 return false unless n.type is 'text'
1391 return false unless n.text is ' '
1392 return false if n.parent is @tree_parent
1393 return false unless n.parent.children.length is 1
1395 # detect special case: typing before a space that's the only thing in a block/doc
1396 # reason: enter key creates blocks with just a space in them
1397 insert_should_replace: (n, i) ->
1398 return false unless i is 0
1399 return false unless n.text is ' '
1400 return true if n.parent is @tree_parent
1401 if n.parent.children.length is 1
1402 if n.parent.children[0] is n
1406 # after calling this, you MUST call changed() and adjust_whitespace_style()
1407 insert_character: (n, i, char) ->
1408 return if n.parent is @tree_parent # FIXME implement text nodes at top level
1409 # insert the character
1410 if @insert_should_replace n, i
1413 n.text = char + n.text
1414 else if i is n.text.length
1419 n.text.substr(0, i) +
1422 n.el.nodeValue = n.text
1423 # WARNING: after calling this, you MUST call changed() and adjust_whitespace_style()
1424 remove_character: (n, i) ->
1425 n.text = n.text.substr(0, i) + n.text.substr(i + 1)
1426 n.el.nodeValue = n.text
1427 computed_style: (n, prop) ->
1430 style = @iframe.contentWindow.getComputedStyle n.el, null
1431 return style.getPropertyValue prop
1432 # returns the new white-space value that will preserve spaces for node n
1433 preserve_space: (n, ideal_target) ->
1438 while target isnt ideal_target and not target.el.style.whiteSpace
1440 console.log "bug #967123"
1442 target = target.parent
1443 ws = ws_props[target.el.style.whiteSpace]?.to_preserve
1445 target.el.style.whiteSpace = ws
1446 @update_style_from_el target
1448 update_style_from_el: (n) ->
1449 style = n.el.getAttribute 'style'
1451 n.attrs.style = style
1454 delete n.attrs.style
1455 # call this after you insert or remove inline nodes. It will:
1456 # merge consecutive text nodes
1457 # remove empty text nodes
1458 # adjust white-space property
1459 text_cleanup: (n) ->
1460 if @is_display_block n
1463 block = @find_block_parent n
1464 return unless block?
1465 run = @get_text_run block
1467 # merge consecutive text nodes
1471 while i < run.length
1473 if prev.type is 'text' and n.type is 'text'
1474 if prev.parent is n.parent
1475 prev_i = n.parent.children.indexOf prev
1476 n_i = n.parent.children.indexOf n
1477 if n_i is prev_i + 1
1478 prev.text = prev.text + n.text
1479 prev.el.textContent = prev.text
1482 continue # don't increment i or change prev
1485 # remove empty text nodes
1487 while i < run.length
1492 # FIXME maybe remove parents recursively if this makes them empty
1494 continue # don't increment i
1496 # note: inline tags can have white-space:pre-line/etc
1497 # note: inline-blocks have their whitespace collapsed independantly of outer run
1498 # note: inline-blocks are treated like non-whitespace char even if empty
1499 if block.el.style.whiteSpace?
1500 ws = block.el.style.whiteSpace
1502 if ws_props[ws].space
1503 if ws_props[ws].to_collapse is 'normal'
1504 block.el.style.whiteSpace = null
1506 block.el.style.whiteSpace = ws_props[ws].to_collapse
1507 @update_style_from_el block
1508 # note: space after <br> colapses, but not space before
1509 # check for spaces that would collapse without help
1510 eats_start_sp = true # if the next node starts with space it collapses (unless pre)
1515 eats_start_sp = true
1517 eats_start_sp = false
1519 need_preserve = false
1520 if n.type isnt 'text'
1521 console.log "bug #232308"
1524 if is_space_code n.text.charCodeAt 0
1525 need_preserve = true
1526 unless need_preserve
1527 need_preserve = multi_sp_regex.test n.text
1529 # do we have it already?
1530 ws = @computed_style n, 'white-space' # FIXME implement this
1531 unless ws_props[ws]?.space
1532 # 2nd arg is ideal target for css rule
1533 ws = @preserve_space n, block
1534 eats_start_sp = false
1536 if is_space_code n.text.charCodeAt(n.text.length - 1)
1537 ws = @computed_style n, 'white-space' # FIXME implement this
1538 if ws_props[ws]?.space
1539 eats_start_sp = false
1541 eats_start_sp = true
1543 eats_start_sp = false
1544 # check if text ends with a collapsable space
1546 last = run[run.length - 1]
1547 if last.type is 'text'
1549 @preserve_space last, block
1551 css_clear: (n, prop) ->
1552 return unless n.attrs.style?
1553 return if n.attrs.style is ''
1554 css_delimiter_regex = new RegExp('\s*;\s*', 'g') # FIXME make this global
1555 styles = n.attrs.style.trim().split css_delimiter
1556 return unless styles.length > 0
1557 if styles[styles.length - 1] is ''
1559 return unless styles.length > 0
1561 while i < styles.length
1562 if styles[i].substr(0, 12) is 'white-space:'
1567 # WARNING: after calling this one or more times, you MUST:
1568 # if it's inline: call @text_cleanup
1571 i = n.parent.children.indexOf n
1573 throw "BUG #9187112313"
1574 n.el.parentNode.removeChild n.el
1575 n.parent.children.splice i, 1
1577 # remove a node from the tree/dom, insert into new_parent before insert_before?end
1578 # WARNING: after calling this one or more times, you MUST:
1579 # if it's inline: call @text_cleanup
1581 move_node: (n, new_parent, insert_before = null) ->
1582 i = n.parent.children.indexOf n
1584 throw "Error: tried to remove node, but it's not in it's parents list of children"
1587 before_i = new_parent.children.indexOf insert_before
1589 throw "Error: tried to move a node to be before a non-existent node"
1590 insert_before = insert_before.el
1593 new_parent.el.insertBefore n.el, insert_before
1594 new_parent.children.splice before_i, 0, n
1596 new_parent.el.appendChild n.el, insert_before
1597 new_parent.children.push n
1598 n.parent = new_parent
1600 kill_cursor: -> # remove it, forget where it was
1602 @cursor_el.parentNode.removeChild @cursor_el
1603 @cursor_visible = false
1607 move_cursor: (cursor) ->
1608 @cursor_ideal_x = cursor.x
1610 unless @cursor_visible
1611 @cursor_el = domify @outer_idoc, div: id: 'cursor'
1612 @overlay.appendChild @cursor_el
1613 @cursor_visible = true
1614 @cursor_el.style.left = "#{cursor.x + overlay_padding - 1}px"
1619 @cursor_el.style.top = "#{cursor.y + overlay_padding + Math.round(height * .07)}px"
1620 @cursor_el.style.height = "#{Math.round height * 0.82}px"
1622 @scroll_into_view cursor.y, height
1624 scroll_into_view: (y, h = 0) ->
1625 y += overlay_padding # convert units from @idoc to @wrap2
1626 # very top of document
1627 if y <= breathing_room
1628 @wrap2.scrollTop = 0
1630 # very bottom of document
1631 if y + h >= @wrap2.scrollHeight - breathing_room
1632 @wrap2.scrollTop = @wrap2.scrollHeight - @wrap2_height
1634 # The most scrolled up (lowest value for scrollTop) that would be OK
1635 upmost = y + h + breathing_room - @wrap2_height
1636 upmost = Math.max(upmost, 0)
1637 # the most scrolled down (highest value for scrollTop) that would be OK
1638 downmost = y - breathing_room
1639 downmost = Math.min(downmost, @wrap2.scrollHeight - @wrap2_height)
1640 if upmost > downmost # means h is too big to fit
1641 # scroll so top is visible
1642 @wrap2.scrollTop = downmost
1644 if @wrap2.scrollTop < upmost
1645 @wrap2.scrollTop = upmost
1647 if @wrap2.scrollTop > downmost
1648 @wrap2.scrollTop = downmost
1652 while @matting.length > 0
1653 @overlay.removeChild @matting[0]
1656 prev_bounds = x: 0, y: 0, w: 0, h: 0
1658 while n?.el? and n isnt @tree_parent
1662 bounds = get_el_bounds n.el
1663 return unless bounds?
1664 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
1667 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});
1668 @overlay.appendChild ann_box
1669 @matting.push ann_box
1670 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} "]
1671 @overlay.appendChild ann_tag
1672 @matting.push ann_tag
1675 pretty_html: (tree, indent = '', parent_flags = pre_ish: false, block: true, want_nl: false) ->
1677 want_nl = parent_flags.want_nl
1678 prev_in_flow_is_text = false
1679 prev_in_flow_is_block = false
1682 inner_flags = want_nl: true
1689 if n.el.currentStyle?
1690 cs = n.el.currentStyle
1691 whitespace = cs['white-space']
1692 display = cs['display']
1693 position = cs['position']
1695 visibility = cs['visibility']
1697 cs = @iframe.contentWindow.getComputedStyle(n.el, null)
1698 whitespace = cs.getPropertyValue 'white-space'
1699 display = cs.getPropertyValue 'display'
1700 position = cs.getPropertyValue 'position'
1701 float = cs.getPropertyValue 'float'
1702 visibility = cs.getPropertyValue 'visibility'
1703 if n.name is 'textarea'
1704 inner_flags.pre_ish = true
1706 inner_flags.pre_ish = whitespace.substr(0, 3) is 'pre'
1708 when 'left', 'right'
1712 when 'absolute', 'fixed'
1715 if 'display' is 'none'
1719 when 'hidden', 'collapse'
1724 when 'inline', 'none'
1725 inner_flags.block = false
1726 is_block = in_flow_block = false
1728 inner_flags.block = true
1729 is_block = in_flow_block = false
1730 else # block, table, etc
1731 inner_flags.block = true
1733 in_flow_block = in_flow
1738 in_flow_block = false
1739 else # 'comment', 'doctype'
1743 in_flow_block = false
1744 # print whitespace if we can
1745 unless parent_flags.pre_ish
1746 unless prev_in_flow_is_text and is_br
1747 if (i is 0 and parent_flags.block) or in_flow_block or prev_in_flow_is_block
1760 if n.attrs[k].length > 0
1761 ret += "=\"#{enc_attr n.attrs[k]}\""
1763 unless void_elements[n.name]?
1764 if inner_flags.block
1765 next_indent = indent + ' '
1767 next_indent = indent
1768 if n.children.length
1769 ret += @pretty_html n.children, next_indent, inner_flags
1770 ret += "</#{n.name}>"
1772 ret += enc_text n.text
1774 ret += "<!--#{n.text}-->" # TODO encode?
1776 ret += "<!DOCTYPE #{n.name}"
1777 if n.public_identifier? and n.public_identifier.length > 0
1778 ret += " \"#{n.public_identifier}\""
1779 if n.system_identifier? and n.system_identifier.length > 0
1780 ret += " \"#{n.system_identifier}\""
1784 prev_in_flow_is_text = is_text
1785 prev_in_flow_is_block = is_block or (in_flow and is_br)
1787 # output final newline if allowed
1788 unless parent_flags.pre_ish
1789 if prev_in_flow_is_block or parent_flags.block
1790 ret += "\n#{indent.substr 4}"
1795 @editor_is_focused = true
1798 return if @poll_for_blur_timeout? # already polling
1799 @poll_for_blur_timeout = timeout 150, =>
1800 next_frame => # pause polling when browser knows we're not active/visible/etc.
1801 @poll_for_blur_timeout = null
1802 if document.activeElement is @outer_iframe
1805 @editor_is_focused = false
1808 window.peach_html5_editor = (args...) ->
1809 return new PeachHTML5Editor args...
1811 # test in browser: peach_html5_editor(document.getElementsByTagName('textarea')[0])