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