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/>.
17 # encode text so it can be safely placed inside an html attribute
39 dom_to_html = (dom) ->
43 when wheic_parser.TYPE_TAG
50 ret += " #{k}=\"#{enc_attr el.attrs[k]}\""
52 unless ret.name in void_elements
54 ret += dom_to_html el.children
55 ret += "</#{el.name}>"
56 when wheic_parser.TYPE_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}\""
68 make_wysiwyg = (el, options = {}) ->
69 opt_fragment = options.fragment ? true
72 parser_opts.fragment = 'body'
73 dom = wheic_parser.parse(el.value, parser_opts)
74 el.value = dom_to_html dom
76 window.wheic = make_wysiwyg
78 # test in browser: wheic(document.getElementsByTagName('textarea')[0])