1 # Copyright 2015 Jason Woofenden
2 # This file implements an WYSIWYG editor in the browser (no contenteditable)
4 # This program is free software: you can redistribute it and/or modify it under
5 # the terms of the GNU Affero General Public License as published by the Free
6 # Software Foundation, either version 3 of the License, or (at your option) any
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 timeout = (ms, cb) -> return setTimeout cb, ms
22 if (window.requestAnimationFrame?)
23 window.requestAnimationFrame cb
27 this_url_sans_path = ->
28 ret = "#{window.location.href}"
29 clip = ret.lastIndexOf '#'
31 ret = ret.substr 0, clip
32 clip = ret.lastIndexOf '?'
34 ret = ret.substr 0, clip
35 clip = ret.lastIndexOf '/'
37 ret = ret.substr 0, clip + 1
40 # xml 1.0 spec, chromium and firefox accept these, plus lots of unicode chars
41 valid_attr_regex = new RegExp '^[a-zA-Z_:][-a-zA-Z0-9_:.]*$'
42 # html5 spec is much more lax, but chromium won't let me make at attribute with the name "4"
43 js_attr_regex = new RegExp '^[oO][nN].'
45 debug_dot_at = (doc, x, y) ->
47 el = doc.createElement 'div'
48 el.setAttribute 'style', "position: absolute; left: #{x}px; top: #{y}px; width: 1px; height: 3px; background-color: red"
49 doc.body.appendChild el
50 #console.log(new Error().stack)
52 # text nodes don't have getBoundingClientRect(), so use selection api to find
54 get_el_bounds = (el) ->
55 if el.getBoundingClientRect?
56 rect = el.getBoundingClientRect()
58 # text nodes don't have getBoundingClientRect(), so use range api
59 range = el.ownerDocument.createRange()
60 range.selectNodeContents el
61 rect = range.getBoundingClientRect()
62 doc = el.ownerDocument.documentElement
63 win = el.ownerDocument.defaultView
64 y_fix = win.pageYOffset - doc.clientTop
65 x_fix = win.pageXOffset - doc.clientLeft
69 w: rect.width ? (rect.right - rect.left)
70 h: rect.height ? (rect.top - rect.bottom)
73 is_display_block = (el) ->
75 return el.currentStyle.display is 'block'
77 return window.getComputedStyle(el, null).getPropertyValue('display') is 'block'
79 # Pass return value from dom event handlers to this.
80 # If they return false, this will addinionally stop propagation and default.
81 event_return = (e, bool) ->
88 # Warning: currently assumes you're asking about a single character
89 # Note: chromium returns multiple bounding rects for a space at a line-break
90 # Note: chromium's getBoundingClientRect() is broken (when zero-area client rects)
91 # Note: sometimes returns null (eg for whitespace that is not visible)
92 text_range_bounds = (el, start, end) ->
93 range = document.createRange()
94 range.setStart el, start
96 rects = range.getClientRects()
101 doc = el.ownerDocument.documentElement
102 win = el.ownerDocument.defaultView
103 y_fix = win.pageYOffset - doc.clientTop
104 x_fix = win.pageXOffset - doc.clientLeft
108 w: rect.width ? (rect.right - rect.left)
109 h: rect.height ? (rect.top - rect.bottom)
111 bounding: range.getBoundingClientRect()
114 # figure out the x/y coordinates of where the cursor should be if it's at
115 # position ``i`` within text node ``n``
116 # sometimes returns null (eg for whitespace that is not visible)
117 window.cursor_to_xyh = cursor_to_xyh = (n, i) ->
118 range = document.createRange()
119 if n.text.length is 0
120 ret = text_range_bounds n.el, 0, 0
121 else if i is n.text.length
122 ret = text_range_bounds n.el, i - 1, i
126 ret = text_range_bounds n.el, i, i + 1
128 debug_dot_at n.el.ownerDocument, ret.x, ret.y
131 # encode text so it can be safely placed inside an html attribute
132 enc_attr_regex = new RegExp '(&)|(")|(\u00A0)', 'g'
134 return txt.replace enc_attr_regex, (match, amp, quote) ->
135 return '&' if (amp)
136 return '"' if (quote)
138 enc_text_regex = new RegExp '(&)|(<)|(\u00A0)', 'g'
140 return txt.replace enc_text_regex, (match, amp, lt) ->
141 return '&' if (amp)
142 return '<' if (lt)
162 # TODO make these always pretty-print (on the inside) like blocks
163 no_text_elements = { # these elements never contain text
173 domify = (doc, hash) ->
174 for tag, attrs of hash
176 return document.createTextNode attrs
177 el = document.createElement tag
186 outer_css = (args) ->
189 inner_padding = args.inner_padding ? overlay_padding
190 frame_width = args.frame_width ? inner_padding
191 # TODO editor controls height...
192 occupy = (left, top = left, right = left, bottom = top) ->
195 return Math.max(left, top, right, bottom)
200 ret += 'color: black;'
201 ret += 'background: white;'
204 ret += "border: #{occupy 1}px solid black;"
205 ret += "padding: #{occupy frame_width}px;"
208 ret += "border: #{occupy 1}px solid black;"
209 ret += "padding: #{occupy inner_padding}px;"
210 ret += "padding-right: #{inner_padding + occupy 0, 0, 15, 0}px;" # for scroll bar
211 ret += "width: #{w}px;"
212 ret += "height: #{h}px;"
213 ret += 'overflow-x: hidden;'
214 ret += 'overflow-y: scroll;'
217 ret += 'position: relative;'
218 ret += "width: #{w}px;"
219 ret += "min-height: #{h}px;"
222 ret += 'box-sizing: border-box;'
224 ret += 'border: none;'
226 ret += "width: #{w}px;"
227 #ret += "height: #{h}px;" # height auto-set when content set/changed
228 ret += '-ms-user-select: none;'
229 ret += '-webkit-user-select: none;'
230 ret += '-moz-user-select: none;'
231 ret += 'user-select: none;'
234 ret += 'position: absolute;'
235 ret += "left: -#{inner_padding}px;"
236 ret += "top: -#{inner_padding}px;"
237 ret += "right: -#{inner_padding}px;"
238 ret += "bottom: -#{inner_padding}px;"
239 ret += 'overflow: hidden;'
242 ret += 'position: absolute;'
243 ret += 'background: rgba(100,100,100,0.2);'
246 ret += 'position: absolute;'
248 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));'
249 ret += 'background-size: 200% 200%;'
250 ret += '-webkit-animation: blink 1s linear normal infinite;'
251 ret += 'animation: blink 1s linear normal infinite;'
253 ret += '@-webkit-keyframes blink {'
254 ret += '0%{background-position:0% 0%}'
255 ret += '100%{background-position:0% -100%}'
257 ret += '@keyframes blink { '
258 ret += '0%{background-position:0% 0%}'
259 ret += '100%{background-position:0% -100%}'
263 ret += 'position: absolute;'
264 ret += 'border: 1px solid rgba(0,0,0,0.1);'
265 ret += 'outline: 1px solid rgba(255,255,255,0.1);' # in case there's a black background
268 ret += 'z-index: 10;'
269 ret += 'position: absolute;'
270 ret += 'font-size: 8px;'
271 ret += 'white-space: pre;'
272 ret += 'background: rgba(255,255,255,0.4);'
273 ret += '-ms-user-select: none;'
274 ret += '-webkit-user-select: none;'
275 ret += '-moz-user-select: none;'
276 ret += 'user-select: none;'
283 '20': true # capslock
285 '144': true # numlock
287 '91': true # windows "start" key
288 # key codes: (valid on keydown, not keypress)
293 KEY_BACKSPACE = 8 # <--
294 KEY_DELETE = 46 # -->
303 control_key_codes = # we react to these, but they aren't typing
319 instantiate_tree = (tree, parent) ->
324 c.el = parent.ownerDocument.createTextNode c.text
325 parent.appendChild c.el
327 if c.name in ['script', 'object', 'iframe', 'link']
328 # TODO put placeholders instead
331 # TODO create in correct namespace
332 c.el = parent.ownerDocument.createElement c.name
334 # FIXME if attr_whitelist[k]?
335 if valid_attr_regex.test k
336 unless js_attr_regex.test k
337 c.el.setAttribute k, v
338 parent.appendChild c.el
340 instantiate_tree c.children, c.el
344 traverse_tree = (tree, cb) ->
350 done = traverse_tree c.children, cb
354 find_next_cursor_position = (tree, n, i) ->
355 if n.type is 'text' and n.text.length > i
356 orig_xyh = cursor_to_xyh n, i
358 console.log "ERROR: couldn't find xy for current cursor location"
360 for next_i in [i+1 .. n.text.length] # inclusive is valid (after last char)
361 next_xyh = cursor_to_xyh n, next_i
363 if next_xyh.x > orig_xyh.x or next_xyh.y > orig_xyh.y
367 traverse_tree tree, (node, state) ->
368 if node.type is 'text' and state_before is false
369 if cursor_to_xyh(node, 0)?
379 find_prev_cursor_position = (tree, n, i) ->
380 if n? and n.type is 'text' and i > 0
381 orig_xyh = cursor_to_xyh n, i
383 console.log "ERROR: couldn't find xy for current cursor location"
385 for prev_i in [i-1 .. 0]
386 prev_xyh = cursor_to_xyh n, prev_i
388 if prev_xyh.x < orig_xyh.x or prev_xyh.y < orig_xyh.y
393 traverse_tree tree, (node) ->
394 if node.type is 'text'
399 found = found_prev # null if n is the first text node
404 if cursor_to_xyh found, found.text.length # text visible?
405 return [found, found.text.length]
406 return find_prev_cursor_position tree, found, 0
409 find_loc_cursor_position = (tree, loc) ->
411 if c.type is 'tag' or c.type is 'text'
412 bounds = get_el_bounds c.el
413 continue if loc.x < bounds.x
414 continue if loc.x > bounds.x + bounds.w
415 continue if loc.y < bounds.y
416 continue if loc.y > bounds.y + bounds.h
418 ret = find_loc_cursor_position c.children, loc
421 # click is within bounding box that contains all text.
422 return [c, 0] if c.text.length is 0
424 before = cursor_to_xyh c, before_i
426 console.log "error: failed to find cursor pixel location for start of", c
428 after_i = c.text.length
429 after = cursor_to_xyh c, after_i
431 console.log "error: failed to find cursor pixel location for end of", c
433 if loc.y < before.y + before.h and loc.x < before.x
434 # console.log 'before first char on first line'
436 if loc.y > after.y and loc.x > after.x
437 # console.log 'after last char on last line'
440 console.log "Warning: click in bounding box but above first line"
441 continue # above first line (runaround?)
442 if loc.y > after.y + after.h
443 console.log "Warning: click in bounding box but below last line", loc.y, after.y, after.h
444 continue # below last line (shouldn't happen?)
445 while after_i - before_i > 1
446 cur_i = Math.round((before_i + after_i) / 2)
447 cur = cursor_to_xyh c, cur_i
449 console.log "error: failed to find cursor pixel location for", c, cur_i
451 if loc.y < cur.y or (loc.y <= cur.y + cur.h and loc.x < cur.x)
457 # which one is closest?
458 if Math.abs(before.x - loc.x) < Math.abs(after.x - loc.x)
464 # browsers collapse these (html5 spec calls these "space characters")
465 is_space_code = (char_code) ->
467 when 9, 10, 12, 13, 32
471 return is_space_code chr.charCodeAt 0
473 tree_remove_empty_text_nodes = (tree) ->
475 traverse_tree tree, (n) ->
477 if n.text.length is 0
481 # don't completely empty the tree
483 if tree[0].type is 'text'
484 console.log "oop, leaving a blank node because it's the only thing"
486 n.el.parentNode.removeChild n.el
487 for c, i in n.parent.children
489 n.parent.children.splice i, 1
492 # pass a array of nodes (from parser library, ie it should have .el and .text)
493 tree_dedup_space = (tree) ->
494 prev = cur = next = null
495 prev_i = cur_i = next_i = 0
496 prev_pos = pos = next_pos = null
497 prev_px = cur_px = next_px = null
501 tree_remove_empty_text_nodes(tree)
503 iterate = (tree, cb) ->
507 while i < n.text.length # don't foreach, cb might remove chars
512 block = is_display_block n.el
515 if n.children.length > 0
516 iterate n.children, cb
521 removed_char = cur.text.charAt(cur_i)
522 cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + (cur.text.substr cur_i + 1)
523 if next is cur # in same text node
525 throw "how is this possible?"
530 cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + removed_char + (cur.text.substr cur_i)
531 if next is cur # in same text node
534 # return true if cur was removed from the dom (ie re-use same prev)
536 # cur definitately set
537 # prev and/or next might be null, indicating the start/end of a display:block
538 return false unless is_space_code cur.text.charCodeAt cur_i
539 bounds = text_range_bounds cur.el, cur_i, cur_i + 1
541 # 1. zero rects returned by getClientRects() means collapsed space
544 # 2. width greater than zero means visible space
547 # now the weird edge cases...
549 # firefox and chromium both report zero width for characters at the end
550 # of a line where the text wraps (automatically, due to word-wrap) to
551 # the next line. These do not appear to be distinguishable from
552 # collapsed spaces via the range/bounds api, so...
554 # remove it from the dom, and if prev or next moves, put it back.
555 if prev? and not prev_px?
556 prev_px = cursor_to_xyh prev, prev_i
557 if next? and not next_px?
558 next_px = cursor_to_xyh next, next_i
559 #if prev is null and next is null
560 # parent_px = cur.parent.el.getBoundingClientRect()
564 new_prev_px = cursor_to_xyh prev, prev_i
565 if new_prev_px.x isnt prev_px.x or new_prev_px.y isnt prev_px.y
568 console.log "this shouldn't happen, we remove spaces that don't locate"
571 new_next_px = cursor_to_xyh next, next_i
572 if new_next_px.x isnt next_px.x or new_next_px.y isnt next_px.y
575 # console.log "removing space becase space after it is collapsed"
577 # pass null at start/end of display:block
585 # don't advance (to the next character next time) if we removed a
586 # character from the same text node as ``next``, because doing so
587 # renumbers the indexes in that string
588 if removed and cur is next
604 tree_remove_empty_text_nodes(tree)
606 class PeachHTML5Editor
607 # Options: (all optional)
608 # editor_id: "id" attribute for outer-most element created by/for editor
609 # css_file: filename of a css file to style editable content
610 # on_init: callback for when the editable content is in place
611 constructor: (in_el, options) ->
612 @options = options ? {}
616 @init_1_called = false # when iframes have loaded
617 @outer_iframe # iframe to hold editor
618 @outer_idoc # "document" object for @outer_iframe
619 @wrap2 = null # scrollbar is on this
620 @iframe = null # iframe to hold editable content
621 @idoc = null # "document" object for @iframe
624 @cursor_visible = false
625 @poll_for_blur_timeout = null
626 @iframe_offset = null
627 @iframe_height = null
628 opt_fragment = @options.fragment ? true
631 @parser_opts.fragment = 'body'
633 @outer_iframe = domify document, iframe: {}
634 outer_iframe_style = 'border: none !important; margin: 0 !important; padding: 0 !important; height: 100% !important; width: 100% !important;'
635 if @options.editor_id?
636 @outer_iframe.setAttribute 'id', @options.editor_id
637 @outer_iframe.onload = =>
638 @outer_idoc = @outer_iframe.contentDocument
639 icss = domify @outer_idoc, style: children: [
640 domify @outer_idoc, text: css
642 @outer_idoc.head.appendChild icss
643 @iframe = domify @outer_idoc, iframe: sandbox: 'allow-same-origin allow-scripts'
646 timeout 200, => # firefox never fires this onload
647 @init_1() unless @init_1_called
648 @outer_idoc.body.appendChild(
649 domify @outer_idoc, div: id: 'wrap1', children: [
650 domify @outer_idoc, div: style: "position: absolute; top: 0; left: 1px; font-size: 10px", children: [ domify @outer_idoc, text: "Peach HTML5 Editor" ]
651 @wrap2 = domify @outer_idoc, div: id: 'wrap2', children: [
652 domify @outer_idoc, div: id: 'wrap3', children: [
654 @overlay = domify @outer_idoc, div: id: 'overlay'
659 outer_wrap = domify document, div: class: 'peach_html5_editor'
660 @in_el.parentNode.appendChild outer_wrap
661 outer_bounds = get_el_bounds outer_wrap
662 if outer_bounds.w < 300
664 if outer_bounds.h < 300
666 outer_iframe_style += "width: #{outer_bounds.w}px; height: #{outer_bounds.h}px;"
667 @outer_iframe.setAttribute 'style', outer_iframe_style
668 css = outer_css w: outer_bounds.w, h: outer_bounds.h
669 outer_wrap.appendChild @outer_iframe
670 init_1: -> # @iframe has loaded (but not it's css)
671 @idoc = @iframe.contentDocument
672 @init_1_called = true
673 # chromium doesn't resolve relative urls as though they were at the same domain
674 # so add a <base> tag
675 @idoc.head.appendChild domify @idoc, base: href: this_url_sans_path()
676 # don't let @iframe have scrollbars
677 @idoc.head.appendChild domify @idoc, style: children: [domify @idoc, text: "body { overflow: hidden; }"]
680 istyle = domify @idoc, link: rel: 'stylesheet', href: @options.css_file
683 @idoc.head.appendChild istyle
686 init_2: -> # @iframe and it's css file(s) are ready
687 @overlay.onclick = (e) =>
689 return event_return e, @onclick e
690 @overlay.ondoubleclick = (e) =>
692 return event_return e, @ondoubleclick e
693 @outer_idoc.body.onkeyup = (e) =>
695 return event_return e, @onkeyup e
696 @outer_idoc.body.onkeydown = (e) =>
698 return event_return e, @onkeydown e
699 @outer_idoc.body.onkeypress = (e) =>
701 return event_return e, @onkeypress e
702 @load_html @in_el.value
705 overlay_event_to_inner_xy: (e) ->
706 unless @iframe_offset?
707 @iframe_offset = get_el_bounds @iframe
708 x = e.pageX # TODO ?cross-browserify
709 y = e.pageY + @wrap2.scrollTop # TODO ?cross-browserify
710 # TODO adjust for scrolling
711 return x: x - @iframe_offset.x, y: y - @iframe_offset.y
713 xy = @overlay_event_to_inner_xy e
714 new_cursor = find_loc_cursor_position @tree, xy
716 @move_cursor new_cursor
720 ondoubleclick: (e) ->
724 return false if ignore_key_codes[e.keyCode]?
725 #return false if control_key_codes[e.keyCode]?
728 return false if ignore_key_codes[e.keyCode]?
729 #return false if control_key_codes[e.keyCode]?
733 new_cursor = find_prev_cursor_position @tree, @cursor...
735 @move_cursor new_cursor
738 new_cursor = find_next_cursor_position @tree, c, -1
740 @move_cursor new_cursor
747 new_cursor = find_next_cursor_position @tree, @cursor...
749 @move_cursor new_cursor
752 new_cursor = find_prev_cursor_position @tree, c, -1
754 @move_cursor new_cursor
762 return false unless @cursor?
763 return false unless @cursor[1] > 0
764 @cursor[0].text = @cursor[0].text.substr(0, @cursor[1] - 1) + @cursor[0].text.substr(@cursor[1])
765 @cursor[0].el.nodeValue = @cursor[0].text
766 @move_cursor [@cursor[0], @cursor[1] - 1]
770 return false unless @cursor?
771 return false unless @cursor[1] < @cursor[0].text.length
772 @cursor[0].text = @cursor[0].text.substr(0, @cursor[1]) + @cursor[0].text.substr(@cursor[1] + 1)
773 @cursor[0].el.nodeValue = @cursor[0].text
774 @move_cursor [@cursor[0], @cursor[1]]
793 return false if ignore_key_codes[e.keyCode]?
794 # return false if control_key_codes[e.keyCode]? # handled in keydown
795 char = e.charCode ? e.keyCode
797 char = String.fromCharCode char
799 @cursor[0].text = char + @cursor[0].text
800 else if @cursor[1] is @cursor[0].text.length - 1
801 @cursor[0].text += char
804 @cursor[0].text.substr(0, @cursor[1]) +
806 @cursor[0].text.substr(@cursor[1])
807 @cursor[0].el.nodeValue = @cursor[0].text
808 @move_cursor [@cursor[0], @cursor[1] + 1]
811 clear_dom: -> # remove all the editable content (and cursor, overlays, etc)
812 while @idoc.body.childNodes.length
813 @idoc.body.removeChild @idoc.body.childNodes[0]
817 @tree = peach_parser.parse html, @parser_opts
819 instantiate_tree @tree, @idoc.body
820 tree_dedup_space @tree
823 @in_el.onchange = null
824 @in_el.value = @pretty_html @tree
826 @load_html @in_el.value
827 @adjust_iframe_height()
828 adjust_iframe_height: ->
829 h = parseInt(@idoc.body.scrollHeight, 10)
830 if @iframe_height isnt h
833 @iframe.style.height = "0"
834 @iframe.style.height = "#{h}px"
836 kill_cursor: -> # remove it, forget where it was
838 @cursor_el.parentNode.removeChild @cursor_el
839 @cursor_visible = false
842 move_cursor: (cursor) ->
843 loc = cursor_to_xyh cursor[0], cursor[1]
845 console.log "error: tried to move cursor to position that has no pixel location", cursor[0], cursor[1]
848 # replace cursor element, to reset blink animation
850 @cursor_el.parentNode.removeChild @cursor_el
851 @cursor_el = domify @outer_idoc, div: id: 'cursor'
852 @overlay.appendChild @cursor_el
853 @cursor_visible = true
854 @cursor_el.style.left = "#{loc.x + overlay_padding - 1}px"
859 @cursor_el.style.top = "#{loc.y + overlay_padding + Math.round(height * .07)}px"
860 @cursor_el.style.height = "#{Math.round height * 0.82}px"
863 while @matting.length > 0
864 @overlay.removeChild @matting[0]
867 prev_bounds = x: 0, y: 0, w: 0, h: 0
873 bounds = get_el_bounds n.el
874 return unless bounds?
875 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
878 matt = 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});
879 @overlay.appendChild matt
881 ann = 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} "]
882 @overlay.appendChild ann
886 pretty_html: (tree, indent = '', parent_flags = pre_ish: false, block: true, want_nl: false) ->
888 want_nl = parent_flags.want_nl
889 prev_in_flow_is_text = false
890 prev_in_flow_is_block = false
893 inner_flags = want_nl: true
900 if n.el.currentStyle?
901 cs = n.el.currentStyle
902 whitespace = cs['white-space']
903 display = cs['display']
904 position = cs['position']
906 visibility = cs['visibility']
908 cs = @iframe.contentWindow.getComputedStyle(n.el, null)
909 whitespace = cs.getPropertyValue 'white-space'
910 display = cs.getPropertyValue 'display'
911 position = cs.getPropertyValue 'position'
912 float = cs.getPropertyValue 'float'
913 visibility = cs.getPropertyValue 'visibility'
914 if n.name is 'textarea'
915 inner_flags.pre_ish = true
917 inner_flags.pre_ish = whitespace.substr(0, 3) is 'pre'
923 when 'absolute', 'fixed'
926 if 'display' is 'none'
930 when 'hidden', 'collapse'
935 when 'inline', 'none'
936 inner_flags.block = false
937 is_block = in_flow_block = false
939 inner_flags.block = true
940 is_block = in_flow_block = false
941 else # block, table, etc
942 inner_flags.block = true
944 in_flow_block = in_flow
949 in_flow_block = false
950 else # 'comment', 'doctype'
954 in_flow_block = false
955 # print whitespace if we can
956 unless parent_flags.pre_ish
957 unless prev_in_flow_is_text and is_br
958 if (i is 0 and parent_flags.block) or in_flow_block or prev_in_flow_is_block
971 if n.attrs[k].length > 0
972 ret += "=\"#{enc_attr n.attrs[k]}\""
974 unless void_elements[n.name]?
976 next_indent = indent + ' '
980 ret += @pretty_html n.children, next_indent, inner_flags
981 ret += "</#{n.name}>"
983 ret += enc_text n.text
985 ret += "<!--#{n.text}-->" # TODO encode?
987 ret += "<!DOCTYPE #{n.name}"
988 if n.public_identifier? and n.public_identifier.length > 0
989 ret += " \"#{n.public_identifier}\""
990 if n.system_identifier? and n.system_identifier.length > 0
991 ret += " \"#{n.system_identifier}\""
995 prev_in_flow_is_text = is_text
996 prev_in_flow_is_block = is_block or (in_flow and is_br)
998 # output final newline if allowed
999 unless parent_flags.pre_ish
1000 if prev_in_flow_is_block or parent_flags.block
1001 ret += "\n#{indent.substr 4}"
1006 @editor_is_focused = true
1009 return if @poll_for_blur_timeout? # already polling
1010 @poll_for_blur_timeout = timeout 150, =>
1011 next_frame => # pause polling when browser knows we're not active/visible/etc.
1012 @poll_for_blur_timeout = null
1013 if document.activeElement is @outer_iframe
1016 @editor_is_focused = false
1019 window.peach_html5_editor = (args...) ->
1020 return new PeachHTML5Editor args...
1022 # test in browser: peach_html5_editor(document.getElementsByTagName('textarea')[0])