From: Jason Woofenden Date: Wed, 9 Mar 2016 06:48:43 +0000 (-0500) Subject: pretty-print html, break pre-wrap/etc blocks X-Git-Url: https://jasonwoof.com/gitweb/?p=peach-html5-editor.git;a=commitdiff_plain;h=68877825429e0eb91c764d1277d8b6cb8a178f33 pretty-print html, break pre-wrap/etc blocks --- diff --git a/editor.coffee b/editor.coffee index c70ebbe..2d06854 100644 --- a/editor.coffee +++ b/editor.coffee @@ -139,11 +139,14 @@ void_elements = { track: true wbr: true } -dom_to_html = (dom) -> +dom_to_html = (dom, indent = '', parent_is_block = false) -> ret = '' - for el in dom + for el, i in dom switch el.type when TYPE_TAG + is_block = is_display_block el.el + if is_block or (parent_is_block and i is 0) + ret += indent ret += '<' + el.name attr_keys = [] for k of el.attrs @@ -154,12 +157,26 @@ dom_to_html = (dom) -> if el.attrs[k].length > 0 ret += "=\"#{enc_attr el.attrs[k]}\"" ret += '>' - unless void_elements[el.name] + unless void_elements[el.name]? + if is_block + next_indent = indent + ' ' + else + next_indent = indent if el.children.length - ret += dom_to_html el.children + if is_block + ret += "\n" + ret += dom_to_html el.children, next_indent, is_block + if is_block or (parent_is_block and i is dom.length - 1) + ret += indent ret += "" + if is_block or (parent_is_block and i is dom.length - 1) + ret += "\n" when TYPE_TEXT + if parent_is_block and i is 0 + ret += indent ret += enc_text el.text + if parent_is_block and i is dom.length - 1 + ret += "\n" when TYPE_COMMENT ret += "" when TYPE_DOCTYPE