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
18 enc_attr_regex = new RegExp '(&)|(")|(\u00A0)', 'g'
20 return txt.replace enc_attr_regex, (match, amp, quote) ->
21 return '&' if (amp)
22 return '"' if (quote)
42 dom_to_html = (dom) ->
46 when wheic_parser.TYPE_TAG
54 if el.attrs[k].length > 0
55 ret += "=\"#{enc_attr el.attrs[k]}\""
57 unless void_elements[el.name]
59 ret += dom_to_html el.children
60 ret += "</#{el.name}>"
61 when wheic_parser.TYPE_TEXT
63 when wheic_parser.TYPE_COMMENT
64 ret += "<!--#{el.text}-->"
65 when wheic_parser.TYPE_DOCTYPE
66 ret += "<!DOCTYPE #{el.name}"
67 if el.public_identifier? and el.public_identifier.length > 0
68 ret += " \"#{el.public_identifier}\""
69 if el.system_identifier? and el.system_identifier.length > 0
70 ret += " \"#{el.system_identifier}\""
74 wysiwyg = (el, options = {}) ->
75 opt_fragment = options.fragment ? true
78 parser_opts.fragment = 'body'
80 dom: wheic_parser.parse el.value, parser_opts
81 iframe: document.createElement('iframe')
83 @dom = wheic_parser.parse el.value, parser_opts
85 idoc = editor_instance.iframe.contentDocument
86 if options.stylesheet # TODO test this
87 istyle = idoc.createElement 'style'
88 istyle.setAttribute 'src', options.stylesheet
89 idoc.head.appendChild istyle
90 el.parentNode.appendChild editor_instance.iframe
91 return editor_instance
95 dom_to_html: dom_to_html
98 # test in browser: wheic(document.getElementsByTagName('textarea')[0])