JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fix text node encodings when formatting to html
[peach-html5-editor.git] / editor.coffee
1 # Copyright 2015 Jason Woofenden
2 # This file implements an WYSIWYG editor in the browser (no contenteditable)
3 #
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
7 # later version.
8 #
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
12 # details.
13 #
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/>.
16
17 TYPE_TAG = peach_parser.TYPE_TAG
18 TYPE_TEXT = peach_parser.TYPE_TEXT
19 TYPE_COMMENT = peach_parser.TYPE_COMMENT
20 TYPE_DOCTYPE = peach_parser.TYPE_DOCTYPE
21
22 debug_dot_at = (doc, x, y) ->
23         el = doc.createElement 'div'
24         el.setAttribute 'style', "position: absolute; left: #{x}px; top: #{y}px; width: 1px; height: 3px; background-color: red"
25         doc.body.appendChild el
26         #console.log(new Error().stack)
27
28 # text nodes don't have getBoundingClientRect(), so use selection api to find
29 # it.
30 get_el_bounds = (el) ->
31         if el.getBoundingClientRect?
32                 rect = el.getBoundingClientRect()
33         else
34                 # text nodes don't have getBoundingClientRect(), so use range api
35                 range = el.ownerDocument.createRange()
36                 range.selectNodeContents el
37                 rect = range.getBoundingClientRect()
38         doc = el.ownerDocument.documentElement
39         win = el.ownerDocument.defaultView
40         y_fix = win.pageYOffset - doc.clientTop
41         x_fix = win.pageXOffset - doc.clientLeft
42         return {
43                 x: rect.left + x_fix
44                 y: rect.top + y_fix
45                 w: rect.width ? (rect.right - rect.left)
46                 h: rect.height ? (rect.top - rect.bottom)
47         }
48
49 is_display_block = (el) ->
50         if el.currentStyle?
51                 return el.currentStyle.display is 'block'
52         else
53                 return window.getComputedStyle(el, null).getPropertyValue('display') is 'block'
54
55 # Warning: currently assumes you're asking about a single character
56 # Note: chromium returns multiple bounding rects for a space at a line-break
57 # Note: chromium's getBoundingClientRect() is broken (when zero-area client rects)
58 # Note: sometimes returns null (eg for whitespace that is not visible)
59 text_range_bounds = (el, start, end) ->
60         range = document.createRange()
61         range.setStart el, start
62         range.setEnd el, end
63         rects = range.getClientRects()
64         if rects.length > 0
65                 rect = rects[0]
66         else
67                 return null
68         doc = el.ownerDocument.documentElement
69         win = el.ownerDocument.defaultView
70         y_fix = win.pageYOffset - doc.clientTop
71         x_fix = win.pageXOffset - doc.clientLeft
72         return {
73                 x: rect.left + x_fix
74                 y: rect.top + y_fix
75                 w: rect.width ? (rect.right - rect.left)
76                 h: rect.height ? (rect.top - rect.bottom)
77                 rects: rects
78                 bounding: range.getBoundingClientRect()
79         }
80
81 # figure out the x/y coordinates of where the cursor should be if it's at
82 # position ``i`` within text node ``n``
83 # sometimes returns null (eg for whitespace that is not visible)
84 window.cursor_to_xyh = cursor_to_xyh = (n, i) ->
85         range = document.createRange()
86         if n.text.length is 0
87                 ret = text_range_bounds n.el, 0, 0
88         if i is n.text.length
89                 ret = text_range_bounds n.el, i - 1, i
90                 if ret?
91                         ret.x += ret.w
92         else
93                 ret = text_range_bounds n.el, i, i + 1
94         if ret?
95                 debug_dot_at n.el.ownerDocument, ret.x, ret.y
96         return ret
97
98 # encode text so it can be safely placed inside an html attribute
99 enc_attr_regex = new RegExp '(&)|(")|(\u00A0)', 'g'
100 enc_attr = (txt) ->
101         return txt.replace enc_attr_regex, (match, amp, quote) ->
102                 return '&amp;' if (amp)
103                 return '&quot;' if (quote)
104                 return '&nbsp;'
105 enc_text_regex = new RegExp '(&)|(<)|(\u00A0)', 'g'
106 enc_text = (txt) ->
107         return txt.replace enc_text_regex, (match, amp, lt) ->
108                 return '&amp;' if (amp)
109                 return '&lt;' if (lt)
110                 return '&nbsp;'
111
112 void_elements = {
113         area: true
114         base: true
115         br: true
116         col: true
117         embed: true
118         hr: true
119         img: true
120         input: true
121         keygen: true
122         link: true
123         meta: true
124         param: true
125         source: true
126         track: true
127         wbr: true
128 }
129 dom_to_html = (dom) ->
130         ret = ''
131         for el in dom
132                 switch el.type
133                         when TYPE_TAG
134                                 ret += '<' + el.name
135                                 attr_keys = []
136                                 for k of el.attrs
137                                         attr_keys.unshift k
138                                 #attr_keys.sort()
139                                 for k in attr_keys
140                                         ret += " #{k}"
141                                         if el.attrs[k].length > 0
142                                                 ret += "=\"#{enc_attr el.attrs[k]}\""
143                                 ret += '>'
144                                 unless void_elements[el.name]
145                                         if el.children.length
146                                                 ret += dom_to_html el.children
147                                         ret += "</#{el.name}>"
148                         when TYPE_TEXT
149                                 ret += enc_text el.text
150                         when TYPE_COMMENT
151                                 ret += "<!--#{el.text}-->"
152                         when TYPE_DOCTYPE
153                                 ret += "<!DOCTYPE #{el.name}"
154                                 if el.public_identifier? and el.public_identifier.length > 0
155                                         ret += " \"#{el.public_identifier}\""
156                                 if el.system_identifier? and el.system_identifier.length > 0
157                                         ret += " \"#{el.system_identifier}\""
158                                 ret += ">\n"
159         return ret
160
161 domify = (h) ->
162         for tag, attrs of h
163                 if tag is 'text'
164                         return document.createTextNode attrs
165                 el = document.createElement tag
166                 for k, v of attrs
167                         if k is 'children'
168                                 for child in v
169                                         el.appendChild child
170                         else
171                                 el.setAttribute k, v
172         return el
173
174 css = ''
175 css += 'div#peach_html5_editor_cursor {'
176 css +=     'position: absolute;'
177 css +=     'height: 1em;'
178 css +=     'width: 2px;'
179 css +=     'margin-left: -1px;'
180 css +=     'margin-right: -1px;'
181 css +=     'background: #444;'
182 css +=     '-webkit-animation: blink 1s steps(2, start) infinite;'
183 css +=     'animation: blink 1s steps(2, start) infinite;'
184 css += '}'
185 css += '@-webkit-keyframes blink {'
186 css +=     'to { visibility: hidden; }'
187 css += '}'
188 css += '@keyframes blink {'
189 css +=     'to { visibility: hidden; }'
190 css += '}'
191
192 # key codes:
193 KEY_LEFT = 37
194 KEY_UP = 38
195 KEY_RIGHT = 39
196 KEY_DOWN = 40
197 KEY_BACKSPACE = 8 # <--
198 KEY_DELETE = 46 # -->
199 KEY_END = 35
200 KEY_ENTER = 13
201 KEY_ESCAPE = 27
202 KEY_HOME = 36
203 KEY_INSERT = 45
204 KEY_PAGE_UP = 33
205 KEY_PAGE_DOWN = 34
206 KEY_TAB = 9
207
208 instantiate_tree = (tree, parent) ->
209         for c in tree
210                 switch c.type
211                         when TYPE_TEXT
212                                 c.el = parent.ownerDocument.createTextNode c.text
213                                 parent.appendChild c.el
214                         when TYPE_TAG
215                                 # TODO create in correct namespace
216                                 c.el = parent.ownerDocument.createElement c.name
217                                 for k, v of c.attrs
218                                         # FIXME if attr_whitelist[k]?
219                                         c.el.setAttribute k, v
220                                 parent.appendChild c.el
221                                 if c.children.length
222                                         instantiate_tree c.children, c.el
223
224 traverse_tree = (tree, state, cb) ->
225         for c in tree
226                 cb c, state
227                 break if state.done?
228                 if c.children.length
229                         traverse_tree c.children, state, cb
230                         break if state.done?
231         return state
232 # find the next element in tree (and decendants) that is after n and can contain text
233 # TODO make it so cursor can go places that don't have text but could
234 find_next_cursor_position = (tree, n, i) ->
235         if n? and n.type is TYPE_TEXT and n.text.length > i
236                 orig_xyh = cursor_to_xyh n, i
237                 unless orig_xyh?
238                         console.log "ERROR: couldn't find xy for current cursor location"
239                         return
240                 for next_i in [i+1 .. n.text.length] # inclusive is valid (after last char)
241                         next_xyh = cursor_to_xyh n, next_i
242                         if next_xyh?
243                                 if next_xyh.x > orig_xyh.x or next_xyh.y > orig_xyh.y
244                                         return [n, next_i]
245         found = traverse_tree tree, before: n?, (node, state) ->
246                 if node.type is TYPE_TEXT and state.before is false
247                         state.node = node
248                         state.done = true
249                 if node is n
250                         state.before = false
251         if found.node?
252                 return [found.node, 0]
253         return null
254
255 # TODO make it so cursor can go places that don't have text but could
256 find_prev_cursor_position = (tree, n, i) ->
257         if n? and n.type is TYPE_TEXT and i > 0
258                 orig_xyh = cursor_to_xyh n, i
259                 unless orig_xyh?
260                         console.log "ERROR: couldn't find xy for current cursor location"
261                         return
262                 for prev_i in [i-1 .. 0]
263                         prev_xyh = cursor_to_xyh n, prev_i
264                         if prev_xyh?
265                                 if prev_xyh.x < orig_xyh.x or prev_xyh.y < orig_xyh.y
266                                         return [n, prev_i]
267                 return [n, i - 1]
268         found = traverse_tree tree, before: n?, (node, state) ->
269                 if node.type is TYPE_TEXT
270                         unless n?
271                                 state.node = node
272                                 state.done = true
273                         if node is n
274                                 if state.prev?
275                                         state.node = state.prev
276                                 state.done = true
277                         if node
278                                 state.prev = node
279         if found.node?
280                 return [found.node, found.node.text.length]
281         return null
282
283 find_loc_cursor_position = (tree, loc) ->
284         for c in tree
285                 if c.type is TYPE_TAG or c.type is TYPE_TEXT
286                         bounds = get_el_bounds c.el
287                         continue if loc.x < bounds.x
288                         continue if loc.x > bounds.x + bounds.w
289                         continue if loc.y < bounds.y
290                         continue if loc.y > bounds.y + bounds.h
291                         if c.children.length
292                                 ret = find_loc_cursor_position c.children, loc
293                                 return ret if ret?
294                         if c.type is TYPE_TEXT
295                                 # click is within bounding box that contains all text.
296                                 return [c, 0] if c.text.length is 0
297                                 before_i = 0
298                                 before = cursor_to_xyh c, before_i
299                                 unless before?
300                                         console.log "error: failed to find cursor pixel location for start of", c
301                                         return
302                                 after_i = c.text.length
303                                 after = cursor_to_xyh c, after_i
304                                 unless after?
305                                         console.log "error: failed to find cursor pixel location for end of", c
306                                         return
307                                 if loc.y < before.y + before.h and loc.x < before.x
308                                         # console.log 'before first char on first line'
309                                         continue
310                                 if loc.y > after.y and loc.x > after.x
311                                         # console.log 'after last char on last line'
312                                         continue
313                                 if loc.y < before.y
314                                         console.log "Warning: click in bounding box but above first line"
315                                         continue # above first line (runaround?)
316                                 if loc.y > after.y + after.h
317                                         console.log "Warning: click in bounding box but below last line", loc.y, after.y, after.h
318                                         continue # below last line (shouldn't happen?)
319                                 while after_i - before_i > 1
320                                         cur_i = Math.round((before_i + after_i) / 2)
321                                         cur = cursor_to_xyh c, cur_i
322                                         unless loc?
323                                                 console.log "error: failed to find cursor pixel location for", c, cur_i
324                                                 return
325                                         if loc.y < cur.y or (loc.y <= cur.y + cur.h and loc.x < cur.x)
326                                                 after_i = cur_i
327                                                 after = cur
328                                         else
329                                                 before_i = cur_i
330                                                 before = cur
331                                 # which one is closest?
332                                 if Math.abs(before.x - loc.x) < Math.abs(after.x - loc.x)
333                                         return [c, before_i]
334                                 else
335                                         return [c, after_i]
336         return null
337
338 # browsers collapse these (html5 spec calls these "space characters")
339 is_space_code = (char_code) ->
340         switch char_code
341                 when 9, 10, 12, 13, 32
342                         return true
343         return false
344 is_space = (chr) ->
345         return is_space_code chr.charCodeAt 0
346
347 # pass a array of nodes (from parser library, ie it should have .el and .text)
348 tree_dedup_space = (tree) ->
349         prev = cur = next = null
350         prev_i = cur_i = next_i = 0
351         prev_pos = pos = next_pos = null
352         prev_px = cur_px = next_px = null
353         first = true
354         removed_char = null
355
356         iterate = (tree, cb) ->
357                 for n in tree
358                         if n.type is TYPE_TEXT
359                                 i = 0
360                                 while i < n.text.length # don't foreach, cb might remove chars
361                                         advance = cb n, i
362                                         if advance
363                                                 i += 1
364                         if n.type is TYPE_TAG
365                                 block = is_display_block n.el
366                                 if block
367                                         cb null
368                                 if n.children.length > 0
369                                         iterate n.children, cb
370                                 if block
371                                         cb null
372         # remove cur char
373         remove = ->
374                 removed_char = cur.text.charAt(cur_i)
375                 cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + (cur.text.substr cur_i + 1)
376                 if next is cur # in same text node
377                         if next_i is 0
378                                 throw "how is this possible?"
379                         next_i -= 1
380                 return true
381         # undo remove()
382         put_it_back = ->
383                 cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + removed_char + (cur.text.substr cur_i)
384                 if next is cur # in same text node
385                         next_i += 1
386                 return false
387         # return true if cur was removed from the dom (ie re-use same prev)
388         operate = ->
389                 # cur definitately set
390                 # prev and/or next might be null, indicating the start/end of a display:block
391                 return false unless is_space_code cur.text.charCodeAt cur_i
392                 bounds = text_range_bounds cur.el, cur_i, cur_i + 1
393                 # consistent cases:
394                 # 1. zero rects returned by getClientRects() means collapsed space
395                 if bounds is null
396                         return remove()
397                 # 2. width greater than zero means visible space
398                 if bounds.w > 0
399                         return false
400                 # now the weird edge cases...
401                 #
402                 # firefox and chromium both report zero width for characters at the end
403                 # of a line where the text wraps (automatically, due to word-wrap) to
404                 # the next line. These do not appear to be distinguishable from
405                 # collapsed spaces via the range/bounds api, so...
406                 #
407                 # remove it from the dom, and if prev or next moves, put it back.
408                 if prev? and not prev_px?
409                         prev_px = cursor_to_xyh prev, prev_i
410                 if next? and not next_px?
411                         next_px = cursor_to_xyh next, next_i
412                 #if prev is null and next is null
413                 #       parent_px = cur.parent.el.getBoundingClientRect()
414                 remove()
415                 if prev?
416                         if prev_px?
417                                 new_prev_px = cursor_to_xyh prev, prev_i
418                                 if new_prev_px.x isnt prev_px.x or new_prev_px.y isnt prev_px.y
419                                         return put_it_back()
420                         else
421                                 console.log "this shouldn't happen, we remove spaces that don't locate"
422                 if next?
423                         if next_px?
424                                 new_next_px = cursor_to_xyh next, next_i
425                                 if new_next_px.x isnt next_px.x or new_next_px.y isnt next_px.y
426                                         return put_it_back()
427                         #else
428                         #       console.log "removing space becase space after it is collapsed"
429                 # if there's no prev or next (single space inside a block-level element?) check
430                 # TODO scrapt this, or fix it so it works when there's no parent
431                 # if prev is null and next is null
432                 #       new_parent_px = cur.parent.el.getBoundingClientRect()
433                 #       if new_parent_px.left isnt parent_px.left or new_parent_px.top isnt parent_px.top or new_parent_px.right isnt parent_px.right or new_parent_px.bottom isnt parent_px.bottom
434                 #               console.log "WEIRD: parent moved"
435                 #               return put_it_back()
436                 # we didn't put it back
437                 return true
438         # pass null at start/end of display:block
439         queue = (n, i) ->
440                 next = n
441                 next_i = i
442                 next_px = null
443                 advance = true
444                 if cur?
445                         removed = operate()
446                         # don't advance (to the next character next time) if we removed a
447                         # character from the same text node as ``next``, because doing so
448                         # renumbers the indexes in that string
449                         if removed and cur is next
450                                 advance = false
451                 else
452                         removed = false
453                 unless removed
454                         prev = cur
455                         prev_i = cur_i
456                         prev_px = cur_px
457                 cur = next
458                 cur_i = next_i
459                 cur_px = next_px
460                 return advance
461         queue null
462         iterate tree, queue
463         queue null
464
465 class PeachHTML5Editor
466         constructor: (in_el, options = {}) ->
467                 @in_el = in_el
468                 @tree = []
469                 @iframe = domify iframe: class: 'peach_html5_editor'
470                 @cursor = null
471                 @cursor_el = null
472                 @cursor_visible = false
473                 opt_fragment = options.fragment ? true
474                 @parser_opts = {}
475                 if opt_fragment
476                         @parser_opts.fragment = 'body'
477
478                 @iframe.onload = =>
479                         @idoc = @iframe.contentDocument
480
481                         ignore_key_codes =
482                                 '18': true # alt
483                                 '20': true # capslock
484                                 '17': true # ctrl
485                                 '144': true # numlock
486                                 '16': true # shift
487                                 '91': true # windows "start" key
488                         control_key_codes = # we react to these, but they aren't typing
489                                 '37': KEY_LEFT
490                                 '38': KEY_UP
491                                 '39': KEY_RIGHT
492                                 '40': KEY_DOWN
493                                 '35': KEY_END
494                                 '8':  KEY_BACKSPACE
495                                 '46': KEY_DELETE
496                                 '13': KEY_ENTER
497                                 '27': KEY_ESCAPE
498                                 '36': KEY_HOME
499                                 '45': KEY_INSERT
500                                 '33': KEY_PAGE_UP
501                                 '34': KEY_PAGE_DOWN
502                                 '9':  KEY_TAB
503
504                         @idoc.body.onclick = (e) =>
505                                 # idoc.body.offset().left/top
506                                 new_cursor = find_loc_cursor_position @tree, x: e.pageX, y: e.pageY
507                                 if new_cursor?
508                                         @move_cursor new_cursor
509                         @idoc.body.onkeyup = (e) =>
510                                 return if e.ctrlKey
511                                 return false if ignore_key_codes[e.keyCode]?
512                                 #return false if control_key_codes[e.keyCode]?
513                         @idoc.body.onkeydown = (e) =>
514                                 return if e.ctrlKey
515                                 return false if ignore_key_codes[e.keyCode]?
516                                 #return false if control_key_codes[e.keyCode]?
517                                 switch e.keyCode
518                                         when KEY_LEFT
519                                                 if @cursor?
520                                                         new_cursor = find_prev_cursor_position @tree, @cursor...
521                                                         if new_cursor?
522                                                                 @move_cursor new_cursor
523                                                 else
524                                                         for c in @tree
525                                                                 new_cursor = find_next_cursor_position @tree, c, -1
526                                                                 if new_cursor?
527                                                                         @move_cursor new_cursor
528                                                                         break
529                                                 return false
530                                         when KEY_UP
531                                                 return false
532                                         when KEY_RIGHT
533                                                 if @cursor?
534                                                         new_cursor = find_next_cursor_position @tree, @cursor...
535                                                         if new_cursor?
536                                                                 @move_cursor new_cursor
537                                                 else
538                                                         for c in @tree
539                                                                 new_cursor = find_prev_cursor_position @tree, c, -1
540                                                                 if new_cursor?
541                                                                         @move_cursor new_cursor
542                                                                         break
543                                                 return false
544                                         when KEY_DOWN
545                                                 return false
546                                         when KEY_END
547                                                 return false
548                                         when KEY_BACKSPACE
549                                                 return false unless @cursor?
550                                                 return false unless @cursor[1] > 0
551                                                 @cursor[0].text = @cursor[0].text.substr(0, @cursor[1] - 1) + @cursor[0].text.substr(@cursor[1])
552                                                 @cursor[0].el.nodeValue = @cursor[0].text
553                                                 @move_cursor [@cursor[0], @cursor[1] - 1]
554                                                 return false
555                                         when KEY_DELETE
556                                                 return false unless @cursor?
557                                                 return false unless @cursor[1] < @cursor[0].text.length
558                                                 @cursor[0].text = @cursor[0].text.substr(0, @cursor[1]) + @cursor[0].text.substr(@cursor[1] + 1)
559                                                 @cursor[0].el.nodeValue = @cursor[0].text
560                                                 @move_cursor [@cursor[0], @cursor[1]]
561                                                 return false
562                                         when KEY_ENTER
563                                                 return false
564                                         when KEY_ESCAPE
565                                                 return false
566                                         when KEY_HOME
567                                                 return false
568                                         when KEY_INSERT
569                                                 return false
570                                         when KEY_PAGE_UP
571                                                 return false
572                                         when KEY_PAGE_DOWN
573                                                 return false
574                                         when KEY_TAB
575                                                 return false
576                         @idoc.body.onkeypress = (e) =>
577                                 return if e.ctrlKey
578                                 return false if ignore_key_codes[e.keyCode]?
579                                 return false if control_key_codes[e.keyCode]? # handled in keydown
580                                 char = e.charCode ? e.keyCode
581                                 if char and @cursor?
582                                         char = String.fromCharCode char
583                                         if @cursor[1] is 0
584                                                 @cursor[0].text = char + @cursor[0].text
585                                         else if @cursor[1] is @cursor[0].text.length - 1
586                                                 @cursor[0].text += char
587                                         else
588                                                 @cursor[0].text =
589                                                         @cursor[0].text.substr(0, @cursor[1]) +
590                                                         char +
591                                                         @cursor[0].text.substr(@cursor[1])
592                                         @cursor[0].el.nodeValue = @cursor[0].text
593                                         @move_cursor [@cursor[0], @cursor[1] + 1]
594                                         @changed()
595                                 return false
596                         if options.stylesheet # TODO test this
597                                 istyle = @idoc.createElement 'style'
598                                 istyle.setAttribute 'src', options.stylesheet
599                                 @idoc.head.appendChild istyle
600                         icss = @idoc.createElement 'style'
601                         icss.appendChild @idoc.createTextNode css
602                         @idoc.head.appendChild icss
603                         @load_html @in_el.value
604
605                 @in_el.parentNode.appendChild @iframe
606         clear_dom: ->
607                 # FIXME add parent node, so we don't empty body and delete cursor_el
608                 while @idoc.body.childNodes.length
609                         @idoc.body.removeChild @idoc.body.childNodes[0]
610                 @cursor_visible = false
611                 return
612         load_html: (html) ->
613                 @tree = peach_parser.parse html, @parser_opts
614                 @clear_dom()
615                 instantiate_tree @tree, @idoc.body
616                 tree_dedup_space @tree
617                 @changed()
618         changed: ->
619                 # FIXME don't export cursor placeholder (when cursor is between space characters)
620                 @in_el.onchange = null
621                 @in_el.value = dom_to_html @tree
622                 @in_el.onchange = =>
623                         @load_html @in_el.value
624         move_cursor: (cursor) ->
625                 loc = cursor_to_xyh cursor[0], cursor[1]
626                 unless loc?
627                         console.log "error: tried to move cursor to position that has no pixel location", cursor[0], cursor[1]
628                         return
629                 @cursor = cursor
630                 # replace cursor, to reset blink animation
631                 if @cursor_visible
632                         @cursor_el.parentNode.removeChild @cursor_el
633                 @cursor_el = domify div: id: 'peach_html5_editor_cursor'
634                 @idoc.body.appendChild @cursor_el
635                 @cursor_visible = true
636                 # TODO figure out x,y coords for cursor
637                 @cursor_el.style.left = "#{loc.x}px"
638                 @cursor_el.style.top = "#{loc.y}px"
639
640 window.peach_html5_editor = (args...) ->
641         return new PeachHTML5Editor args...
642
643 # test in browser: peach_html5_editor(document.getElementsByTagName('textarea')[0])