X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=editor.coffee;h=d9b8ac531c399d26d7fd4366adda0948d021917b;hb=a8e360112070562e780d12d52eec7ab054a1c6a5;hp=c70ebbed30f75a36a4383468e9bdf9f9065d4932;hpb=e1cb7bde7741f06ee23ba076557a300cf66b603f;p=peach-html5-editor.git diff --git a/editor.coffee b/editor.coffee index c70ebbe..d9b8ac5 100644 --- a/editor.coffee +++ b/editor.coffee @@ -139,11 +139,20 @@ 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 + is_tiny_block = false + if el.children.length is 1 + if el.children[0].type is TYPE_TEXT + if el.children[0].text.length < 35 + is_tiny_block = true + 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 +163,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 and not is_tiny_block + ret += "\n" + ret += dom_to_html el.children, next_indent, is_block and not is_tiny_block + if is_block and not is_tiny_block + 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