From: Jason Woofenden Date: Fri, 22 Jan 2016 01:35:14 +0000 (-0500) Subject: editor: start on html serializer X-Git-Url: https://jasonwoof.com/gitweb/?p=peach-html5-editor.git;a=commitdiff_plain;h=c052fef9484e52df1ac860610ce1620c9a3420c2 editor: start on html serializer --- diff --git a/editor.coffee b/editor.coffee index 4de3b81..32c1d4d 100644 --- a/editor.coffee +++ b/editor.coffee @@ -14,3 +14,65 @@ # 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 = (txt) -> + # FIXME implement + return txt + +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 + +make_wysiwyg = (el, options = {}) -> + opt_fragment = options.fragment ? true + parser_opts = {} + if opt_fragment + parser_opts.fragment = 'body' + dom = wheic_parser.parse(el.value, parser_opts) + el.value = dom_to_html dom + +window.wheic = make_wysiwyg + +# test in browser: wheic(document.getElementsByTagName('textarea')[0]) diff --git a/parser.coffee b/parser.coffee index 804c257..509e492 100644 --- a/parser.coffee +++ b/parser.coffee @@ -322,9 +322,9 @@ legacy_char_refs = { yen: '¥', yuml: 'ÿ' } -void_elements = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'] -raw_text_elements = ['script', 'style'] -escapable_raw_text_elements = ['textarea', 'title'] +#void_elements = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'] +#raw_text_elements = ['script', 'style'] +#escapable_raw_text_elements = ['textarea', 'title'] # http://www.w3.org/TR/SVG/ 1.1 (Second Edition) svg_elements = [ 'a', 'altGlyph', 'altGlyphDef', 'altGlyphItem', 'animate', 'animateColor',