JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
editor: start on html serializer
[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 # encode text so it can be safely placed inside an html attribute
18 enc_attr = (txt) ->
19         # FIXME implement
20         return txt
21
22 void_elements = {
23         area: true
24         base: true
25         br: true
26         col: true
27         embed: true
28         hr: true
29         img: true
30         input: true
31         keygen: true
32         link: true
33         meta: true
34         param: true
35         source: true
36         track: true
37         wbr: true
38 }
39 dom_to_html = (dom) ->
40         ret = ''
41         for el in dom
42                 switch el.type
43                         when wheic_parser.TYPE_TAG
44                                 ret += '<' + el.name
45                                 attr_keys = []
46                                 for k of el.attrs
47                                         attr_keys.push k
48                                 attr_keys.sort()
49                                 for k in attr_keys
50                                         ret += " #{k}=\"#{enc_attr el.attrs[k]}\""
51                                 ret += '>'
52                                 unless ret.name in void_elements
53                                         if el.children.length
54                                                 ret += dom_to_html el.children
55                                         ret += "</#{el.name}>"
56                         when wheic_parser.TYPE_TEXT
57                                 ret += el.text
58                         when wheic_parser.TYPE_COMMENT
59                                 ret += "<!--#{el.text}-->"
60                         when wheic_parser.TYPE_DOCTYPE
61                                 ret += "<!DOCTYPE #{el.name}"
62                                 if el.public_identifier? and el.public_identifier.length > 0
63                                         ret += " \"#{el.public_identifier}\""
64                                 if el.system_identifier? and el.system_identifier.length > 0
65                                         ret += " \"#{el.system_identifier}\""
66         return ret
67
68 make_wysiwyg = (el, options = {}) ->
69         opt_fragment = options.fragment ? true
70         parser_opts = {}
71         if opt_fragment
72                 parser_opts.fragment = 'body'
73         dom = wheic_parser.parse(el.value, parser_opts)
74         el.value = dom_to_html dom
75
76 window.wheic = make_wysiwyg
77
78 # test in browser: wheic(document.getElementsByTagName('textarea')[0])