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 (scrolling)
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 str_has_ws_run = (str) ->
77 return multi_sp_regex.test str
79 # text nodes don't have getBoundingClientRect(), so use selection api to find
81 get_el_bounds = window.bounds = (el) ->
82 if el.getBoundingClientRect?
83 rect = el.getBoundingClientRect()
85 # text nodes don't have getBoundingClientRect(), so use range api
86 range = el.ownerDocument.createRange()
87 range.selectNodeContents el
88 rect = range.getBoundingClientRect()
89 doc = el.ownerDocument.documentElement
90 win = el.ownerDocument.defaultView
91 y_fix = win.pageYOffset - doc.clientTop
92 x_fix = win.pageXOffset - doc.clientLeft
96 w: rect.width ? (rect.right - rect.left)
97 h: rect.height ? (rect.top - rect.bottom)
100 is_display_block = (el) ->
102 return el.currentStyle.display is 'block'
104 return window.getComputedStyle(el, null).getPropertyValue('display') is 'block'
106 # Pass return value from dom event handlers to this.
107 # If they return false, this will addinionally stop propagation and default.
108 event_return = (e, bool) ->
110 if e.stopPropagation?
115 # Warning: currently assumes you're asking about a single character
116 # Note: chromium returns multiple bounding rects for a space at a line-break
117 # Note: chromium's getBoundingClientRect() is broken (when zero-area client rects)
118 # Note: sometimes returns null (eg for whitespace that is not visible)
119 text_range_bounds = (el, start, end) ->
120 range = document.createRange()
121 range.setStart el, start
123 rects = range.getClientRects()
126 if rects[1].width > rects[0].width
134 doc = el.ownerDocument.documentElement
135 win = el.ownerDocument.defaultView
136 y_fix = win.pageYOffset - doc.clientTop
137 x_fix = win.pageXOffset - doc.clientLeft
141 w: rect.width ? (rect.right - rect.left)
142 h: rect.height ? (rect.top - rect.bottom)
144 bounding: range.getBoundingClientRect()
148 constructor: (args) ->
159 range = document.createRange()
160 if @n.text.length is 0
161 ret = text_range_bounds @n.el, 0, 0
162 else if @i is @n.text.length
163 ret = text_range_bounds @n.el, @i - 1, @i
167 ret = text_range_bounds @n.el, @i, @i + 1
178 new_cursor_position = (args) ->
179 ret = new CursorPosition args
184 # encode text so it can be safely placed inside an html attribute
185 enc_attr_regex = new RegExp '(&)|(")|(\u00A0)', 'g'
187 return txt.replace enc_attr_regex, (match, amp, quote) ->
188 return '&' if (amp)
189 return '"' if (quote)
191 enc_text_regex = new RegExp '(&)|(<)|(\u00A0)', 'g'
193 return txt.replace enc_text_regex, (match, amp, lt) ->
194 return '&' if (amp)
195 return '<' if (lt)
215 # TODO make these always pretty-print (on the inside) like blocks
216 no_text_elements = { # these elements never contain text
226 domify = (doc, hash) ->
227 for tag, attrs of hash
229 return document.createTextNode attrs
230 el = document.createElement tag
243 '20': true # capslock
245 '144': true # numlock
247 '91': true # windows "start" key
248 # key codes: (valid on keydown, not keypress)
253 KEY_BACKSPACE = 8 # <--
254 KEY_DELETE = 46 # -->
263 control_key_codes = # we react to these, but they aren't typing
279 instantiate_tree = (tree, parent) ->
284 c.el = parent.ownerDocument.createTextNode c.text
285 parent.appendChild c.el
287 if c.name in ['script', 'object', 'iframe', 'link']
288 # TODO put placeholders instead
291 # TODO create in correct namespace
292 c.el = parent.ownerDocument.createElement c.name
294 # FIXME if attr_whitelist[k]?
295 if valid_attr_regex.test k
296 unless js_attr_regex.test k
297 c.el.setAttribute k, v
298 parent.appendChild c.el
300 instantiate_tree c.children, c.el
304 traverse_tree = (tree, cb) ->
310 done = traverse_tree c.children, cb
314 first_cursor_position = (tree) ->
316 traverse_tree tree, (node, state) ->
317 if node.type is 'text'
318 cursor = new_cursor_position n: node, i: 0
321 return true # done traversing
322 return false # not done traversing
323 return found # maybe null
325 # this will fail when text has non-locatable cursor positions
326 find_next_cursor_position = (tree, cursor) ->
327 if cursor.n.type is 'text' and cursor.n.text.length > cursor.i
328 new_cursor = new_cursor_position n: cursor.n, i: cursor.i + 1
333 traverse_tree tree, (node, state) ->
334 if node.type is 'text' and state_before is false
335 new_cursor = new_cursor_position n: node, i: 0
338 return true # done traversing
341 return false # not done traversing
346 last_cursor_position = (tree) ->
348 traverse_tree tree, (node) ->
349 if node.type is 'text'
350 cursor = new_cursor_position n: node, i: node.text.length
353 return false # not done traversing
354 return found # maybe null
356 # this will fail when text has non-locatable cursor positions
357 find_prev_cursor_position = (tree, cursor) ->
358 if cursor.n.type is 'text' and cursor.i > 0
359 new_cursor = new_cursor_position n: cursor.n, i: cursor.i - 1
364 traverse_tree tree, (node) ->
366 found = found_prev # maybe null
367 return true # done traversing
368 if node.type is 'text'
369 new_cursor = new_cursor_position n: node, i: node.text.length
371 found_prev = new_cursor
372 return false # not done traversing
373 return found # maybe null
375 find_up_cursor_position = (tree, cursor, ideal_x) ->
377 # go prev until we're higher on y axis
378 while new_cursor.y >= cursor.y
379 new_cursor = find_prev_cursor_position tree, new_cursor
380 return null unless new_cursor?
381 # done early if we're already left of old cursor position
382 if new_cursor.x <= ideal_x
384 target_y = new_cursor.y
385 # search leftward, until we find the closest position
386 # new_cursor is the prev-most position we've checked
387 # prev_cursor is the older value, so it's not as prev as new_cursor
388 while new_cursor.x > ideal_x and new_cursor.y is target_y
389 prev_cursor = new_cursor
390 new_cursor = find_prev_cursor_position tree, new_cursor
391 break unless new_cursor?
392 # move cursor to prev_cursor or new_cursor
394 if new_cursor.y is target_y
395 # both valid, and on the same line, use closest
396 if (ideal_x - new_cursor.x) < (prev_cursor.x - ideal_x)
401 # new_cursor on wrong line, use prev_cursor
404 # can't go any further prev, use prev_cursor
407 find_down_cursor_position = (tree, cursor, ideal_x) ->
409 # go next until we move on the y axis
410 while new_cursor.y <= cursor.y
411 new_cursor = find_next_cursor_position tree, new_cursor
412 return null unless new_cursor?
413 # done early if we're already right of old cursor position
414 if new_cursor.x >= ideal_x
415 # this would be strange, but could happen due to runaround
417 target_y = new_cursor.y
418 # search rightward, until we find the closest position
419 # new_cursor is the next-most position we've checked
420 # prev_cursor is the older value, so it's not as next as new_cursor
421 while new_cursor.x < ideal_x and new_cursor.y is target_y
422 prev_cursor = new_cursor
423 new_cursor = find_next_cursor_position tree, new_cursor
424 break unless new_cursor?
425 # move cursor to prev_cursor or new_cursor
427 if new_cursor.y is target_y
428 # both valid, and on the same line, use closest
429 if (new_cursor.x - ideal_x) < (ideal_x - prev_cursor.x)
434 # new_cursor on wrong line, use prev_cursor
437 # can't go any further prev, use prev_cursor
440 xy_to_cursor = (tree, xy) ->
442 if n.type is 'tag' or n.type is 'text'
443 bounds = get_el_bounds n.el
444 continue if xy.x < bounds.x
445 continue if xy.x > bounds.x + bounds.w
446 continue if xy.y < bounds.y
447 continue if xy.y > bounds.y + bounds.h
449 ret = xy_to_cursor n.children, xy
452 # click is within bounding box that contains all text.
453 if n.text.length is 0
454 ret = new_cursor_position n: n, i: 0
457 before = new_cursor_position n: n, i: 0
458 continue unless before?
459 after = new_cursor_position n: n, i: n.text.length
460 continue unless after?
461 if xy.y < before.y + before.h and xy.x < before.x
462 # console.log 'before first char on first line'
464 if xy.y > after.y and xy.x > after.x
465 # console.log 'after last char on last line'
468 console.log "Warning: click in text bounding box but above first line"
469 continue # above first line (runaround?)
470 if xy.y > after.y + after.h
471 console.log "Warning: click in text bounding box but below last line", xy.y, after.y, after.h
472 continue # below last line (shouldn't happen?)
473 while after.i - before.i > 1
474 guess_i = Math.round((before.i + after.i) / 2)
475 cur = new_cursor_position n: n, i: guess_i
477 console.log "error: failed to find cursor pixel location for", n, guess_i
480 if xy.y < cur.y or (xy.y <= cur.y + cur.h and xy.x < cur.x)
484 continue unless before? # signals failure to find a cursor position
485 # which one is closest?
486 if Math.abs(before.x - xy.x) < Math.abs(after.x - xy.x)
492 # browsers collapse these (html5 spec calls these "space characters")
493 is_space_code = (char_code) ->
495 when 9, 10, 12, 13, 32
499 return is_space_code chr.charCodeAt 0
501 tree_remove_empty_text_nodes = (tree) ->
503 traverse_tree tree, (n) ->
505 if n.text.length is 0
507 return false # not done traversing
509 # don't completely empty the tree
511 if tree[0].type is 'text'
512 console.log "oop, leaving a blank node because it's the only thing"
514 n.el.parentNode.removeChild n.el
515 for c, i in n.parent.children
517 n.parent.children.splice i, 1
520 class PeachHTML5Editor
521 # Options: (all optional)
522 # editor_id: "id" attribute for outer-most element created by/for editor
523 # css_file: filename of a css file to style editable content
524 # on_init: callback for when the editable content is in place
525 constructor: (in_el, options) ->
526 @options = options ? {}
528 @tree = null # array of Nodes, all editable content
529 @tree_parent = null # @tree is this.children. .el might === @idoc.body
531 @init_1_called = false # when iframes have loaded
532 @outer_iframe # iframe to hold editor
533 @outer_idoc # "document" object for @outer_iframe
534 @wrap2 = null # scrollbar is on this
536 @wrap2_height = null # including padding
537 @iframe = null # iframe to hold editable content
538 @idoc = null # "document" object for @iframe
541 @cursor_visible = false
542 @cursor_ideal_x = null
543 @poll_for_blur_timeout = null
544 opt_fragment = @options.fragment ? true
547 @parser_opts.fragment = 'body'
549 @outer_iframe = domify document, iframe: {}
550 outer_iframe_style = 'border: none !important; margin: 0 !important; padding: 0 !important; height: 100% !important; width: 100% !important;'
551 if @options.editor_id?
552 @outer_iframe.setAttribute 'id', @options.editor_id
553 @outer_iframe.onload = =>
554 @outer_idoc = @outer_iframe.contentDocument
555 icss = domify @outer_idoc, style: children: [
556 domify @outer_idoc, text: css
558 @outer_idoc.head.appendChild icss
559 @iframe = domify @outer_idoc, iframe: sandbox: 'allow-same-origin allow-scripts'
562 timeout 200, => # firefox never fires this onload
563 @init_1() unless @init_1_called
564 @outer_idoc.body.appendChild(
565 domify @outer_idoc, div: id: 'wrap1', children: [
566 domify @outer_idoc, div: style: "position: absolute; top: 0; left: 1px; font-size: 10px", children: [ domify @outer_idoc, text: "Peach HTML5 Editor" ]
567 @wrap2 = domify @outer_idoc, div: id: 'wrap2', children: [
568 domify @outer_idoc, div: id: 'wrap3', children: [
570 @overlay = domify @outer_idoc, div: id: 'overlay'
575 outer_wrap = domify document, div: class: 'peach_html5_editor'
576 @in_el.parentNode.appendChild outer_wrap
577 outer_bounds = get_el_bounds outer_wrap
578 if outer_bounds.w < 300
580 if outer_bounds.h < 300
582 outer_iframe_style += "width: #{outer_bounds.w}px; height: #{outer_bounds.h}px;"
583 @outer_iframe.setAttribute 'style', outer_iframe_style
584 css = @generate_outer_css w: outer_bounds.w, h: outer_bounds.h
585 outer_wrap.appendChild @outer_iframe
586 init_1: -> # @iframe has loaded (but not it's css)
587 @idoc = @iframe.contentDocument
588 @init_1_called = true
589 # chromium doesn't resolve relative urls as though they were at the same domain
590 # so add a <base> tag
591 @idoc.head.appendChild domify @idoc, base: href: this_url_sans_path()
592 # don't let @iframe have scrollbars
593 @idoc.head.appendChild domify @idoc, style: children: [domify @idoc, text: "body { overflow: hidden; }"]
596 istyle = domify @idoc, link: rel: 'stylesheet', href: @options.css_file
599 @idoc.head.appendChild istyle
602 init_2: -> # @iframe and it's css file(s) are ready
603 @overlay.onclick = (e) =>
605 return event_return e, @onclick e
606 @overlay.ondoubleclick = (e) =>
608 return event_return e, @ondoubleclick e
609 @outer_idoc.body.onkeyup = (e) =>
611 return event_return e, @onkeyup e
612 @outer_idoc.body.onkeydown = (e) =>
614 return event_return e, @onkeydown e
615 @outer_idoc.body.onkeypress = (e) =>
617 return event_return e, @onkeypress e
618 @load_html @in_el.value
621 generate_outer_css: (args) ->
624 inner_padding = args.inner_padding ? overlay_padding
625 frame_width = args.frame_width ? inner_padding
626 occupy = (left, top = left, right = left, bottom = top) ->
629 return Math.max(left, top, right, bottom)
634 ret += 'color: black;'
635 ret += 'background: white;'
638 ret += "border: #{occupy 1}px solid black;"
639 ret += "padding: #{occupy frame_width}px;"
642 ret += "border: #{occupy 1}px solid black;"
643 @wrap2_height = h # including padding because padding scrolls
644 ret += "padding: #{occupy inner_padding}px;"
645 ret += "padding-right: #{inner_padding + occupy 0, 0, 15, 0}px;" # for scroll bar
646 ret += "width: #{w}px;"
647 ret += "height: #{h}px;"
648 ret += 'overflow-x: hidden;'
649 ret += 'overflow-y: scroll;'
652 ret += 'position: relative;'
653 ret += "width: #{w}px;"
654 ret += "min-height: #{h}px;"
657 ret += 'box-sizing: border-box;'
659 ret += 'border: none;'
661 ret += "width: #{w}px;"
662 #ret += "height: #{h}px;" # height auto-set when content set/changed
663 ret += '-ms-user-select: none;'
664 ret += '-webkit-user-select: none;'
665 ret += '-moz-user-select: none;'
666 ret += 'user-select: none;'
669 ret += 'position: absolute;'
670 ret += "left: -#{inner_padding}px;"
671 ret += "top: -#{inner_padding}px;"
672 ret += "right: -#{inner_padding}px;"
673 ret += "bottom: -#{inner_padding}px;"
674 ret += 'overflow: hidden;'
677 ret += 'position: absolute;'
678 ret += 'background: rgba(100,100,100,0.2);'
681 ret += 'position: absolute;'
683 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));'
684 ret += 'background-size: 200% 200%;'
685 ret += '-webkit-animation: blink 1s linear normal infinite;'
686 ret += 'animation: blink 1s linear normal infinite;'
688 ret += '@-webkit-keyframes blink {'
689 ret += '0%{background-position:0% 0%}'
690 ret += '100%{background-position:0% -100%}'
692 ret += '@keyframes blink { '
693 ret += '0%{background-position:0% 0%}'
694 ret += '100%{background-position:0% -100%}'
698 ret += 'position: absolute;'
699 ret += 'border: 1px solid rgba(0,0,0,0.1);'
700 ret += 'outline: 1px solid rgba(255,255,255,0.1);' # in case there's a black background
703 ret += 'z-index: 10;'
704 ret += 'position: absolute;'
705 ret += 'font-size: 8px;'
706 ret += 'white-space: pre;'
707 ret += 'background: rgba(255,255,255,0.4);'
708 ret += '-ms-user-select: none;'
709 ret += '-webkit-user-select: none;'
710 ret += '-moz-user-select: none;'
711 ret += 'user-select: none;'
714 overlay_event_to_inner_xy: (e) ->
715 unless @wrap2_offset?
716 @wrap2_offset = get_el_bounds @wrap2
717 x = e.pageX - overlay_padding
718 y = e.pageY - overlay_padding + @wrap2.scrollTop
719 return x: x - @wrap2_offset.x, y: y - @wrap2_offset.y
721 xy = @overlay_event_to_inner_xy e
722 new_cursor = xy_to_cursor @tree, xy
724 @move_cursor new_cursor
728 ondoubleclick: (e) ->
732 return false if ignore_key_codes[e.keyCode]?
733 #return false if control_key_codes[e.keyCode]?
736 return false if ignore_key_codes[e.keyCode]?
737 #return false if control_key_codes[e.keyCode]?
741 new_cursor = find_prev_cursor_position @tree, @cursor
743 new_cursor = first_cursor_position @tree
745 @move_cursor new_cursor
749 new_cursor = find_next_cursor_position @tree, @cursor
751 new_cursor = last_cursor_position @tree
753 @move_cursor new_cursor
757 new_cursor = find_up_cursor_position @tree, @cursor, @cursor_ideal_x
759 saved_ideal_x = @cursor_ideal_x
760 @move_cursor new_cursor
761 @cursor_ideal_x = saved_ideal_x
763 # move cursor to first position in document
764 new_cursor = first_cursor_position @tree
766 @move_cursor new_cursor
770 new_cursor = find_down_cursor_position @tree, @cursor, @cursor_ideal_x
772 saved_ideal_x = @cursor_ideal_x
773 @move_cursor new_cursor
774 @cursor_ideal_x = saved_ideal_x
776 # move cursor to first position in document
777 new_cursor = last_cursor_position @tree
779 @move_cursor new_cursor
782 new_cursor = last_cursor_position @tree
784 @move_cursor new_cursor
790 return false unless @cursor?
791 new_cursor = find_next_cursor_position @tree, n: @cursor.n, i: @cursor.i
792 # try moving cursor right and then running backspace code
793 # TODO replace this hack with a real implementation
795 # try to detect common case where cursor goes inside an block,
796 # but doesn't pass a character (and advance one more in that case)
797 if new_cursor.n isnt @cursor.n and new_cursor.i is 0
798 if new_cursor.n.type is 'text' and new_cursor.n.text.length > 0
799 if new_cursor.n.parent?
800 unless @is_display_block new_cursor.n.parent
801 # FIXME should test run sibling
802 new_cursor = new_cursor_position n: new_cursor.n, i: new_cursor.i + 1
804 if new_cursor.n isnt @cursor.n or new_cursor.i isnt @cursor.i
805 @move_cursor new_cursor
815 new_cursor = first_cursor_position @tree
817 @move_cursor new_cursor
831 return false if ignore_key_codes[e.keyCode]?
832 char = e.charCode ? e.keyCode
834 char = String.fromCharCode char
835 @insert_character @cursor.n, @cursor.i, char
836 @text_cleanup @cursor.n
838 new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i + 1
840 @move_cursor new_cursor
842 console.log "ERROR: couldn't find cursor position after insert"
845 on_key_enter: (e) -> # enter key pressed
846 return unless @cursor_visible
847 cur_block = @cursor.n
849 if cur_block.type is 'tag'
850 if is_display_block cur_block.el
852 return unless cur_block.parent?
853 cur_block = cur_block.parent
854 # find array to insert new element into
855 if cur_block.parent is @tree_parent # top-level
856 parent_el = @idoc.body
859 parent_el = cur_block.parent.el
860 pc = cur_block.parent.children
861 # find index of current block in its parent
863 break if n is cur_block
864 i += 1 # we want to be after it
869 # TODO if content after cursor
870 # TODO new block is empty
871 new_text = new peach_parser.Node 'text', text: ' '
872 new_node = new peach_parser.Node 'tag', name: 'p', parent: cur_block.parent, attrs: {style: 'white-space: pre-wrap'}, children: [new_text]
873 new_text.parent = new_node
874 new_text.el = domify @idoc, text: ' '
875 new_node.el = domify @idoc, p: style: 'white-space: pre-wrap', children: [new_text.el]
876 pc.splice i, 0, new_node
877 parent_el.insertBefore new_node.el, before
879 new_cursor = new_cursor_position n: new_text, i: 0
880 throw 'bork bork' unless new_cursor?
881 @move_cursor new_cursor
882 # TODO move content past cursor into this new block
883 # unlike the global function, this takes a Node, not an element
884 is_display_block: (n) ->
885 # TODO stop calling global function, merge it into here, use iframe's window object
886 return false unless n.type is 'tag'
887 return is_display_block n.el
888 find_block_parent: (n) ->
891 return null unless n?
892 return n if @is_display_block n
893 return n if n is @tree_parent
895 # return a flat array of nodes (text, <br>, and later also inline-block)
896 # that are flowing/wrapping together. n can be the containing block, or any
900 if @is_display_block n
903 block = @find_block_parent n
904 return ret unless block?
905 traverse_tree block.children, (n) =>
908 else if n.type is 'tag'
912 disp = @computed_style n
913 if disp is 'inline-block'
915 return false # not done traversing
917 node_is_decendant: (young, old) ->
918 while young? and young != @tree_parent
919 return true if young is old
922 # helper for on_key_backspace
923 _merge_left: (state) ->
924 # the node prev to n was not prev to it a moment ago, merge with it if reasonable
925 pi = state.n.parent.children.indexOf(state.n)
927 prev = state.n.parent.children[pi - 1]
928 if prev.type is 'text'
929 state.i = prev.text.length
930 prev.text = prev.el.textContent = prev.text + state.n.text
934 state.moved_cursor = true
935 # else # TODO merge possible consecutive matching inline tags at @cursor
937 # helper for on_key_backspace
938 # remove n from the dom, also remove its inline parents that are emptied by removing n
939 _backspace_node_helper: (n, run = @get_text_run(n), run_i = run.indexOf(n)) ->
940 block = @find_block_parent n
943 # delete any inline parents
945 while n? and n isnt block
946 # bail if the previous node in this run is also inside the same parent
948 break if @node_is_decendant run[run_i - 1], n
949 # bail if the next node in this run is also inside the same parent
950 if run_i + 1 < run.length
951 break if @node_is_decendant run[run_i + 1], n
952 # move any sibling nodes to parent. These nodes are not in the text run
953 while n.children.length > 0
954 @move_node n.children[0], n.parent, n
955 # remove (now completely empty) inline parent
957 # proceed to outer parent
960 on_key_backspace: (e) ->
961 return unless @cursor?
965 if @cursor.i is 0 # cursor is at start of text node
966 run ?= @get_text_run @cursor.n
967 run_i = run.indexOf(@cursor.n)
968 if run_i is 0 # if at start of text run
969 block = @find_block_parent @cursor.n
970 prev_cursor = find_prev_cursor_position @tree, n: @cursor.n, i: 0
971 if prev_cursor is null # if in first text run of document
972 # do nothing (there's nothing text-like to the left of the cursor)
974 # else merge with prev/outer text run
975 pcb = @find_block_parent prev_cursor.n
976 while block.children.length > 0
977 @move_node block.children[0], pcb
979 # merge possible consecutive text nodes at @cursor
980 merge_state = n: @cursor.n
981 @_merge_left merge_state
982 @text_cleanup merge_state.n
983 new_cursor = new_cursor_position n: merge_state.n, i: merge_state.i
984 else # at start of text node, but not start of text run
985 prev = run[run_i - 1]
986 if prev.type is 'text' # if previous in text run is text
987 if prev.text.length is 1 # if emptying prev (in text run)
988 @_backspace_node_helper prev, run, run_i
989 merge_state = n: @cursor.n, i: @cursor.i
990 @_merge_left merge_state
991 @text_cleanup merge_state.n
992 new_cursor = new_cursor_position n: merge_state.n, i: merge_state.i
993 else # prev in run is text with muliple chars
994 # delete last character in prev
995 prev.text = prev.text.substr(0, prev.text.length - 1)
996 prev.el.textContent = prev.text
997 @text_cleanup @cursor.n
998 new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i
999 else if prev.name is 'br' or prev.name is 'hr'
1000 @_backspace_node_helper prev, run, run_i
1001 merge_state = n: @cursor.n, i: @cursor.i
1002 @_merge_left merge_state
1003 @text_cleanup merge_state.n
1004 new_cursor = new_cursor_position n: merge_state.n, i: merge_state.i
1005 # FIXME implement this:
1006 # else # if prev (in run) is inline-block
1007 # if that inline-block has text in it
1008 # delete last char in prev inlineblock
1009 # if that empties it
1013 # move cursor inside
1015 # delete prev (inline) block
1017 # auto-delete this @cursor.parent(s) if this empties them
1018 else # cursor is not at start of text node
1019 run ?= @get_text_run @cursor.n
1020 if @cursor.n.text.length is 1 # if emptying text node
1021 if run.length is 1 # if emptying text run (of text/br/hr/inline-block)
1022 # remove inline-parents of @cursor.n
1023 block = @find_block_parent @cursor.n
1025 n = @cursor.n.parent
1026 # note: this doesn't use _backspace_node_helper because:
1027 # 1. we don't want to delete the target node (we're replacing it's contents)
1028 # 2. we want to track whether anything was removed
1029 # 3. we know already know there's no other text from this run anywhere
1030 while n and n isnt block
1032 while n.children.length > 0
1033 @move_node n.children[0], n.parent, n
1036 # replace @cursor.n with a single (preserved) space
1037 if @cursor.n.text != ' '
1039 @cursor.n.text = @cursor.n.el.textContent = ' '
1041 @text_cleanup @cursor.n
1042 # place the cursor to the left of that space
1043 new_cursor = new_cursor_position n: @cursor.n, i: 0
1044 else # emptying a text node (but not a whole text run)
1045 # figure out where cursor should land
1046 block = @find_block_parent @cursor.n
1047 new_cursor = find_prev_cursor_position @tree, n: @cursor.n, i: 0
1048 ncb = @find_block_parent new_cursor.n
1050 new_cursor = find_next_cursor_position @tree, n: @cursor.n, i: 1
1051 # delete text node and cleanup emptied parents
1052 run_i = run.indexOf @cursor.n
1053 @_backspace_node_helper @cursor.n, run, run_i
1054 # see if new adjacent siblings should merge
1056 if run_i > 0 and run_i + 1 < run.length
1057 if run[run_i - 1].type is 'text' and run[run_i + 1].type is 'text'
1058 merge_state = n: run[run_i + 1]
1059 @_merge_left merge_state
1060 if merge_state.moved_cursor
1061 new_cursor = merge_state
1062 # update whitespace preservation
1063 @text_cleanup(block)
1064 # update cursor x/y in case things moved around
1066 if new_cursor.n.el.parentNode # still in dom after cleanup
1067 new_cursor = new_cursor_position n: new_cursor.n, i: new_cursor.i
1070 else # there's a char left of cursor that we can delete without emptying anything
1072 need_text_cleanup = true
1073 if @cursor.i > 1 and @cursor.i < @cursor.n.text.length
1074 pre = @cursor.n.text.substr(@cursor.i - 2, 3)
1075 post = pre.charAt(0) + pre.charAt(2)
1076 if str_has_ws_run(pre) is str_has_ws_run(post)
1077 need_text_cleanup = false
1078 @remove_character(@cursor.n, @cursor.i - 1)
1079 # call text_cleanup if whe created/removed a whitespace run
1080 if need_text_cleanup
1081 @text_cleanup @cursor.n
1082 new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i - 1
1083 # mark document changed and move the cursor
1087 @move_cursor new_cursor
1091 on_page_up_key: (e) ->
1092 if @wrap2.scrollTop is 0
1093 return unless @cursor?
1094 new_cursor = first_cursor_position @tree
1096 if new_cursor.n isnt @cursor.n or new_cursor.i isnt @cursor.i
1097 @move_cursor new_cursor
1100 screen_y = @cursor.y - @wrap2.scrollTop
1101 scroll_amount = @wrap2_height - breathing_room
1102 @wrap2.scrollTop = Math.max 0, @wrap2.scrollTop - scroll_amount
1104 @move_cursor_into_view screen_y + @wrap2.scrollTop
1105 on_page_down_key: (e) ->
1106 lowest_scrollpos = @wrap2.scrollHeight - @wrap2_height
1107 if @wrap2.scrollTop is lowest_scrollpos
1108 return unless @cursor?
1109 new_cursor = last_cursor_position @tree
1111 if new_cursor.n isnt @cursor.n or new_cursor.i isnt @cursor.i
1112 @move_cursor new_cursor
1115 screen_y = @cursor.y - @wrap2.scrollTop
1116 scroll_amount = @wrap2_height - breathing_room
1117 @wrap2.scrollTop = Math.min lowest_scrollpos, @wrap2.scrollTop + scroll_amount
1119 @move_cursor_into_view screen_y + @wrap2.scrollTop
1121 move_cursor_into_view: (y_target) ->
1122 return if y_target is @cursor.y
1124 y_min = @wrap2.scrollTop
1125 unless @wrap2.scrollTop is 0
1126 y_min += breathing_room
1127 y_max = @wrap2.scrollTop + @wrap2_height
1128 unless @wrap2.scrollTop is @wrap2.scrollHeight - @wrap2_height # downmost
1129 y_max -= breathing_room
1130 y_target = Math.min y_target, y_max
1131 y_target = Math.max y_target, y_min
1132 if y_target < @cursor.y
1133 finder = find_up_cursor_position
1134 far_enough = (cur, target_y) ->
1135 return cur.y + cur.h <= target_y
1137 finder = find_down_cursor_position
1138 far_enough = (cur, y_target) ->
1139 return cur.y >= y_target
1141 cur = finder @tree, was, @cursor_ideal_x
1143 break if far_enough cur, y_target
1148 if was.y + was.h > y_max
1150 else if was.y < y_min
1153 if cur.y + cur.h > y_max
1155 else if cur.y < y_min
1158 # both valid, pick best
1161 else if was.y + was.h > y_max
1163 else if cur.y - y_target < y_target - was.y
1168 new_cursor = was ? cur
1170 saved_ideal_x = @cursor_ideal_x
1171 @move_cursor new_cursor
1172 @cursor_ideal_x = saved_ideal_x
1174 clear_dom: -> # remove all the editable content (and cursor, overlays, etc)
1175 while @idoc.body.childNodes.length
1176 @idoc.body.removeChild @idoc.body.childNodes[0]
1179 load_html: (html) ->
1180 @tree = peach_parser.parse html, @parser_opts
1181 if !@tree[0]?.parent
1182 @tree = peach_parser.parse '<p style="white-space: pre-wrap"> </p>', @parser_opts
1183 @tree_parent = @tree[0]?.parent
1184 @tree_parent.el = @idoc.body
1186 instantiate_tree @tree, @tree_parent.el
1187 @collapse_whitespace @tree
1190 @in_el.onchange = null
1191 @in_el.value = @pretty_html @tree
1192 @in_el.onchange = =>
1193 @load_html @in_el.value
1194 @adjust_iframe_height()
1195 adjust_iframe_height: ->
1196 s = @wrap2.scrollTop
1197 # when the content gets shorter, the idoc's body tag will continue to
1198 # report the old (too big) height in Chrome. The workaround is to
1199 # shrink the iframe before the content height:
1200 @iframe.style.height = "10px"
1201 h = parseInt(@idoc.body.scrollHeight, 10)
1202 @iframe.style.height = "#{h}px"
1203 @wrap2.scrollTop = s
1204 # does this node have whitespace that would be collapsed by white-space: normal?
1205 # note: this checks direct text children, and does _not_ recurse into child tags
1206 # tag is a node with type:"tag"
1207 # FIXME use new textrun api
1208 has_collapsable_space: (tag) ->
1209 for n in tag.children
1211 for i in [0...n.text.length]
1212 code = n.text.charCodeAt i
1213 if code isnt 32 and is_space_code code
1216 # check for double spaces that don't surround insert location
1218 if n.text.substr(i - 1, 2) is ' '
1220 if n.text.length > 0
1221 if is_space_code n.text.charCodeAt 0
1223 if is_space_code n.text.charCodeAt n.text.length - 1
1226 # true if n is text node with only one caracter, and the only child of a tag
1227 is_only_char_in_tag: (n, i) ->
1228 return false unless n.type is 'text'
1229 return false unless n.text.length is 1
1230 return false if n.parent is @tree_parent
1231 return false unless n.parent.children.length is 1
1233 # true if n is text node with just a space in it, and the only child of a tag
1234 is_lone_space: (n, i) ->
1235 return false unless n.type is 'text'
1236 return false unless n.text is ' '
1237 return false if n.parent is @tree_parent
1238 return false unless n.parent.children.length is 1
1240 # detect special case: typing before a space that's the only thing in a block/doc
1241 # reason: enter key creates blocks with just a space in them
1242 insert_should_replace: (n, i) ->
1243 return false unless i is 0
1244 return false unless n.text is ' '
1245 return true if n.parent is @tree_parent
1246 if n.parent.children.length is 1
1247 if n.parent.children[0] is n
1251 # after calling this, you MUST call changed() and text_cleanup()
1252 insert_character: (n, i, char) ->
1253 return if n.parent is @tree_parent # FIXME implement text nodes at top level
1254 # insert the character
1255 if @insert_should_replace n, i
1258 n.text = char + n.text
1259 else if i is n.text.length
1264 n.text.substr(0, i) +
1267 n.el.nodeValue = n.text
1268 # WARNING: after calling this, you MUST call changed() and text_cleanup()
1269 remove_character: (n, i) ->
1270 n.text = n.text.substr(0, i) + n.text.substr(i + 1)
1271 n.el.nodeValue = n.text
1272 computed_style: (n, prop) ->
1275 style = @iframe.contentWindow.getComputedStyle n.el, null
1276 return style.getPropertyValue prop
1277 # returns the new white-space value that will preserve spaces for node n
1278 preserve_space: (n, ideal_target) ->
1283 while target isnt ideal_target and not target.el.style.whiteSpace
1285 console.log "bug #967123"
1287 target = target.parent
1288 ws = ws_props[target.el.style.whiteSpace]?.to_preserve
1290 target.el.style.whiteSpace = ws
1291 @update_style_from_el target
1293 update_style_from_el: (n) ->
1294 style = n.el.getAttribute 'style'
1296 n.attrs.style = style
1299 delete n.attrs.style
1300 # remove whitespace that would be trimmed
1301 # replace whitespace that would collapse with a single space
1302 # FIXME remove whitespace from after <br> (but not before)
1303 collapse_whitespace: (tree = @tree) ->
1304 prev = cur = next = null
1305 prev_i = cur_i = next_i = 0
1306 prev_pos = pos = next_pos = null
1307 prev_px = cur_px = next_px = null
1311 tree_remove_empty_text_nodes(tree)
1313 iterate = (tree, cb) ->
1317 while i < n.text.length # don't foreach, cb might remove chars
1322 block = is_display_block n.el
1325 if n.children.length > 0
1326 iterate n.children, cb
1332 cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + removed_char + (cur.text.substr cur_i)
1333 if next is cur # in same text node
1337 removed_char = cur.text.charAt(cur_i)
1338 cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + (cur.text.substr cur_i + 1)
1339 if next is cur # in same text node
1341 throw "how is this possible?"
1344 replace_with_space = (undo) ->
1346 cur.text = (cur.text.substr 0, cur_i) + removed_char + (cur.text.substr cur_i + 1)
1347 cur.el.textContent = cur.text
1349 removed_char = cur.text.charAt(cur_i)
1350 if removed_char isnt ' '
1351 cur.text = (cur.text.substr 0, cur_i) + ' ' + (cur.text.substr cur_i + 1)
1352 cur.el.textContent = cur.text
1354 # return true if cur was removed from the dom (ie re-use same prev)
1356 # cur definitately set
1357 # prev and/or next might be null, indicating the start/end of a display:block
1358 return false unless is_space_code cur.text.charCodeAt cur_i
1359 fixers = [remove, replace_with_space]
1360 # check for common case: single whitespace surrounded by non-whitespace chars
1362 unless (is_space_code prev.text.charCodeAt prev_i) or (is_space_code next.text.charCodeAt next_i)
1363 dbg = cur.text.charCodeAt cur_i
1364 if cur.text.charAt(cur_i) is ' ' # perens required
1365 # single space can't collapse, doesn't need fixin'
1368 # tab, newline, etc, can't collapse, but maybe should be replaced
1369 fixers = [replace_with_space]
1370 bounds = text_range_bounds cur.el, cur_i, cur_i + 1
1372 # 1. zero rects returned by getClientRects() means collapsed space
1375 # 2. width greater than zero means visible space
1377 # has bounds, don't try removing
1378 fixers = [replace_with_space]
1379 # now the weird edge cases...
1381 # firefox and chromium both report zero width for characters at the end
1382 # of a line where the text wraps (automatically, due to word-wrap) to
1383 # the next line. These do not appear to be distinguishable from
1384 # collapsed spaces via the range/bounds api, so...
1386 # remove it from the dom, and if prev or next moves, put it back.
1388 # this block (try changing it, put it back if something moves) is also
1389 # used on collapsable whitespace characters besides space. In this case
1390 # the character is replaced with a normal space character instead of
1392 if prev? and not prev_px?
1393 prev_px = new_cursor_position n: prev, i: prev_i
1394 if next? and not next_px?
1395 next_px = new_cursor_position n: next, i: next_i
1396 #if prev is null and next is null
1397 # parent_px = cur.parent.el.getBoundingClientRect()
1398 undo_arg = true # just for readabality
1401 break if removed > 0
1406 new_prev_px = new_cursor_position n: prev, i: prev_i
1408 if new_prev_px.x isnt prev_px.x or new_prev_px.y isnt prev_px.y
1413 console.log "this shouldn't happen, we remove spaces that don't locate"
1414 if next? and not need_undo
1416 new_next_px = new_cursor_position n: next, i: next_i
1418 if new_next_px.x isnt next_px.x or new_next_px.y isnt next_px.y
1423 # console.log "removing space becase space after it is collapsed"
1425 removed += fixer undo_arg
1430 # pass null at start/end of display:block
1438 # don't advance (to the next character next time) if we removed a
1439 # character from the same text node as ``next``, because doing so
1440 # renumbers the indexes in that string
1441 if removed and cur is next
1457 tree_remove_empty_text_nodes(tree)
1459 # call this after you insert or remove inline nodes. It will:
1460 # merge consecutive text nodes
1461 # remove empty text nodes
1462 # adjust white-space property
1463 # note: this assumes that all whitespace in text nodes should be displayed
1464 # (ie not collapse or be trimmed) and will change the white-space property
1465 # as needed to achieve this.
1466 text_cleanup: (n) ->
1467 if @is_display_block n
1470 block = @find_block_parent n
1471 return unless block?
1472 run = @get_text_run block
1474 # merge consecutive text nodes
1478 while i < run.length
1480 if prev.type is 'text' and n.type is 'text'
1481 if prev.parent is n.parent
1482 prev_i = n.parent.children.indexOf prev
1483 n_i = n.parent.children.indexOf n
1484 if n_i is prev_i + 1
1485 prev.text = prev.text + n.text
1486 prev.el.textContent = prev.text
1489 continue # don't increment i or change prev
1492 # remove empty text nodes
1494 while i < run.length
1499 # FIXME maybe remove parents recursively if this makes them empty
1501 continue # don't increment i
1503 # note: inline tags can have white-space:pre-line/etc
1504 # note: inline-blocks have their whitespace collapsed independantly of outer run
1505 # note: inline-blocks are treated like non-whitespace char even if empty
1506 if block.el.style.whiteSpace?
1507 ws = block.el.style.whiteSpace
1509 if ws_props[ws].space
1510 if ws_props[ws].to_collapse is 'normal'
1511 block.el.style.whiteSpace = null
1513 block.el.style.whiteSpace = ws_props[ws].to_collapse
1514 @update_style_from_el block
1515 # note: space after <br> colapses, but not space before
1516 # check for spaces that would collapse without help
1517 eats_start_sp = true # if the next node starts with space it collapses (unless pre)
1522 eats_start_sp = true
1524 eats_start_sp = false
1526 need_preserve = false
1527 if n.type isnt 'text'
1528 console.log "bug #232308"
1531 if is_space_code n.text.charCodeAt 0
1532 need_preserve = true
1533 unless need_preserve
1534 need_preserve = multi_sp_regex.test n.text
1536 # do we have it already?
1537 ws = @computed_style n, 'white-space' # FIXME implement this
1538 unless ws_props[ws]?.space
1539 # 2nd arg is ideal target for css rule
1540 ws = @preserve_space n, block
1541 eats_start_sp = false
1543 if is_space_code n.text.charCodeAt(n.text.length - 1)
1544 ws = @computed_style n, 'white-space' # FIXME implement this
1545 if ws_props[ws]?.space
1546 eats_start_sp = false
1548 eats_start_sp = true
1550 eats_start_sp = false
1551 # check if text ends with a collapsable space
1553 last = run[run.length - 1]
1554 if last.type is 'text'
1556 @preserve_space last, block
1558 css_clear: (n, prop) ->
1559 return unless n.attrs.style?
1560 return if n.attrs.style is ''
1561 css_delimiter_regex = new RegExp('\s*;\s*', 'g') # FIXME make this global
1562 styles = n.attrs.style.trim().split css_delimiter
1563 return unless styles.length > 0
1564 if styles[styles.length - 1] is ''
1566 return unless styles.length > 0
1568 while i < styles.length
1569 if styles[i].substr(0, 12) is 'white-space:'
1574 # WARNING: after calling this one or more times, you MUST:
1575 # if it's inline: call @text_cleanup
1578 i = n.parent.children.indexOf n
1580 throw "BUG #9187112313"
1581 n.el.parentNode.removeChild n.el
1582 n.parent.children.splice i, 1
1584 # remove a node from the tree/dom, insert into new_parent before insert_before?end
1585 # WARNING: after calling this one or more times, you MUST:
1586 # if it's inline: call @text_cleanup
1588 move_node: (n, new_parent, insert_before = null) ->
1589 i = n.parent.children.indexOf n
1591 throw "Error: tried to remove node, but it's not in it's parents list of children"
1594 before_i = new_parent.children.indexOf insert_before
1596 throw "Error: tried to move a node to be before a non-existent node"
1597 insert_before = insert_before.el
1600 new_parent.el.insertBefore n.el, insert_before
1601 new_parent.children.splice before_i, 0, n
1603 new_parent.el.appendChild n.el, insert_before
1604 new_parent.children.push n
1605 n.parent = new_parent
1607 kill_cursor: -> # remove it, forget where it was
1609 @cursor_el.parentNode.removeChild @cursor_el
1610 @cursor_visible = false
1614 move_cursor: (cursor) ->
1615 @cursor_ideal_x = cursor.x
1617 unless @cursor_visible
1618 @cursor_el = domify @outer_idoc, div: id: 'cursor'
1619 @overlay.appendChild @cursor_el
1620 @cursor_visible = true
1621 @cursor_el.style.left = "#{cursor.x + overlay_padding - 1}px"
1626 @cursor_el.style.top = "#{cursor.y + overlay_padding + Math.round(height * .07)}px"
1627 @cursor_el.style.height = "#{Math.round height * 0.82}px"
1629 @scroll_into_view cursor.y, height
1631 scroll_into_view: (y, h = 0) ->
1632 y += overlay_padding # convert units from @idoc to @wrap2
1633 # very top of document
1634 if y <= breathing_room
1635 @wrap2.scrollTop = 0
1637 # very bottom of document
1638 if y + h >= @wrap2.scrollHeight - breathing_room
1639 @wrap2.scrollTop = @wrap2.scrollHeight - @wrap2_height
1641 # The most scrolled up (lowest value for scrollTop) that would be OK
1642 upmost = y + h + breathing_room - @wrap2_height
1643 upmost = Math.max(upmost, 0)
1644 # the most scrolled down (highest value for scrollTop) that would be OK
1645 downmost = y - breathing_room
1646 downmost = Math.min(downmost, @wrap2.scrollHeight - @wrap2_height)
1647 if upmost > downmost # means h is too big to fit
1648 # scroll so top is visible
1649 @wrap2.scrollTop = downmost
1651 if @wrap2.scrollTop < upmost
1652 @wrap2.scrollTop = upmost
1654 if @wrap2.scrollTop > downmost
1655 @wrap2.scrollTop = downmost
1659 while @matting.length > 0
1660 @overlay.removeChild @matting[0]
1663 prev_bounds = x: 0, y: 0, w: 0, h: 0
1665 while n?.el? and n isnt @tree_parent
1669 bounds = get_el_bounds n.el
1670 return unless bounds?
1671 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
1674 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});
1675 @overlay.appendChild ann_box
1676 @matting.push ann_box
1677 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} "]
1678 @overlay.appendChild ann_tag
1679 @matting.push ann_tag
1682 pretty_html: (tree, indent = '', parent_flags = pre_ish: false, block: true, want_nl: false) ->
1684 want_nl = parent_flags.want_nl
1685 prev_in_flow_is_text = false
1686 prev_in_flow_is_block = false
1689 inner_flags = want_nl: true
1696 if n.el.currentStyle?
1697 cs = n.el.currentStyle
1698 whitespace = cs['white-space']
1699 display = cs['display']
1700 position = cs['position']
1702 visibility = cs['visibility']
1704 cs = @iframe.contentWindow.getComputedStyle(n.el, null)
1705 whitespace = cs.getPropertyValue 'white-space'
1706 display = cs.getPropertyValue 'display'
1707 position = cs.getPropertyValue 'position'
1708 float = cs.getPropertyValue 'float'
1709 visibility = cs.getPropertyValue 'visibility'
1710 if n.name is 'textarea'
1711 inner_flags.pre_ish = true
1713 inner_flags.pre_ish = whitespace.substr(0, 3) is 'pre'
1715 when 'left', 'right'
1719 when 'absolute', 'fixed'
1722 if 'display' is 'none'
1726 when 'hidden', 'collapse'
1731 when 'inline', 'none'
1732 inner_flags.block = false
1733 is_block = in_flow_block = false
1735 inner_flags.block = true
1736 is_block = in_flow_block = false
1737 else # block, table, etc
1738 inner_flags.block = true
1740 in_flow_block = in_flow
1745 in_flow_block = false
1746 else # 'comment', 'doctype'
1750 in_flow_block = false
1751 # print whitespace if we can
1752 unless parent_flags.pre_ish
1753 unless prev_in_flow_is_text and is_br
1754 if (i is 0 and parent_flags.block) or in_flow_block or prev_in_flow_is_block
1767 if n.attrs[k].length > 0
1768 ret += "=\"#{enc_attr n.attrs[k]}\""
1770 unless void_elements[n.name]?
1771 if inner_flags.block
1772 next_indent = indent + ' '
1774 next_indent = indent
1775 if n.children.length
1776 ret += @pretty_html n.children, next_indent, inner_flags
1777 ret += "</#{n.name}>"
1779 ret += enc_text n.text
1781 ret += "<!--#{n.text}-->" # TODO encode?
1783 ret += "<!DOCTYPE #{n.name}"
1784 if n.public_identifier? and n.public_identifier.length > 0
1785 ret += " \"#{n.public_identifier}\""
1786 if n.system_identifier? and n.system_identifier.length > 0
1787 ret += " \"#{n.system_identifier}\""
1791 prev_in_flow_is_text = is_text
1792 prev_in_flow_is_block = is_block or (in_flow and is_br)
1794 # output final newline if allowed
1795 unless parent_flags.pre_ish
1796 if prev_in_flow_is_block or parent_flags.block
1797 ret += "\n#{indent.substr 4}"
1802 @editor_is_focused = true
1805 return if @poll_for_blur_timeout? # already polling
1806 @poll_for_blur_timeout = timeout 150, =>
1807 next_frame => # pause polling when browser knows we're not active/visible/etc.
1808 @poll_for_blur_timeout = null
1809 if document.activeElement is @outer_iframe
1812 @editor_is_focused = false
1815 window.peach_html5_editor = (args...) ->
1816 return new PeachHTML5Editor args...
1818 # test in browser: peach_html5_editor(document.getElementsByTagName('textarea')[0])