JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fix text node encodings when formatting to html
[peach-html5-editor.git] / editor.coffee
index da7b393..94a42c2 100644 (file)
@@ -102,6 +102,12 @@ enc_attr = (txt) ->
                return '&' if (amp)
                return '"' if (quote)
                return ' '
+enc_text_regex = new RegExp '(&)|(<)|(\u00A0)', 'g'
+enc_text = (txt) ->
+       return txt.replace enc_text_regex, (match, amp, lt) ->
+               return '&amp;' if (amp)
+               return '&lt;' if (lt)
+               return '&nbsp;'
 
 void_elements = {
        area: true
@@ -140,7 +146,7 @@ dom_to_html = (dom) ->
                                                ret += dom_to_html el.children
                                        ret += "</#{el.name}>"
                        when TYPE_TEXT
-                               ret += el.text
+                               ret += enc_text el.text
                        when TYPE_COMMENT
                                ret += "<!--#{el.text}-->"
                        when TYPE_DOCTYPE
@@ -352,8 +358,8 @@ tree_dedup_space = (tree) ->
                        if n.type is TYPE_TEXT
                                i = 0
                                while i < n.text.length # don't foreach, cb might remove chars
-                                       removed = cb n, i
-                                       unless removed
+                                       advance = cb n, i
+                                       if advance
                                                i += 1
                        if n.type is TYPE_TAG
                                block = is_display_block n.el
@@ -434,8 +440,14 @@ tree_dedup_space = (tree) ->
                next = n
                next_i = i
                next_px = null
+               advance = true
                if cur?
                        removed = operate()
+                       # don't advance (to the next character next time) if we removed a
+                       # character from the same text node as ``next``, because doing so
+                       # renumbers the indexes in that string
+                       if removed and cur is next
+                               advance = false
                else
                        removed = false
                unless removed
@@ -445,7 +457,7 @@ tree_dedup_space = (tree) ->
                cur = next
                cur_i = next_i
                cur_px = next_px
-               return removed
+               return advance
        queue null
        iterate tree, queue
        queue null