X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=editor.coffee;h=c01a2b18e91110c8d44e4d23e8ee07b6a77899bc;hb=d3c2a37281dcca53f823094406e3c59b1947688f;hp=4de3b8179d8c312fb1e024070b3e9a21c71e0a3a;hpb=a7cff3a244ba086034aed2f284235b6bcb6b7f98;p=peach-html5-editor.git diff --git a/editor.coffee b/editor.coffee index 4de3b81..c01a2b1 100644 --- a/editor.coffee +++ b/editor.coffee @@ -14,3 +14,72 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +# encode text so it can be safely placed inside an html attribute +enc_attr_regex = new RegExp '(&)|(")|(\u00A0)', 'g' +enc_attr = (txt) -> + return txt.replace enc_attr_regex, (match, amp, quote) -> + return '&' if (amp) + return '"' if (quote) + return ' ' + +void_elements = { + area: true + base: true + br: true + col: true + embed: true + hr: true + img: true + input: true + keygen: true + link: true + meta: true + param: true + source: true + track: true + wbr: true +} +dom_to_html = (dom) -> + ret = '' + for el in dom + switch el.type + when wheic_parser.TYPE_TAG + ret += '<' + el.name + attr_keys = [] + for k of el.attrs + attr_keys.push k + attr_keys.sort() + for k in attr_keys + ret += " #{k}=\"#{enc_attr el.attrs[k]}\"" + ret += '>' + unless ret.name in void_elements + if el.children.length + ret += dom_to_html el.children + ret += "" + when wheic_parser.TYPE_TEXT + ret += el.text + when wheic_parser.TYPE_COMMENT + ret += "" + when wheic_parser.TYPE_DOCTYPE + ret += " 0 + ret += " \"#{el.public_identifier}\"" + if el.system_identifier? and el.system_identifier.length > 0 + ret += " \"#{el.system_identifier}\"" + return ret + +wysiwyg = (el, options = {}) -> + opt_fragment = options.fragment ? true + parser_opts = {} + if opt_fragment + parser_opts.fragment = 'body' + editor_instance = dom: wheic_parser.parse(el.value, parser_opts) + # el.value = dom_to_html dom + return editor_instance + +window.wheic = { + wysiwyg: wysiwyg + dom_to_html: dom_to_html +} + +# test in browser: wheic(document.getElementsByTagName('textarea')[0])