JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
preserve <script> tags and whitespace in plaintext elements
[peach-html5-editor.git] / parser.js
index de9e9ed..1d8a53a 100644 (file)
--- a/parser.js
+++ b/parser.js
@@ -1327,28 +1327,20 @@ parse_html = function (args_html, args) {
                                // the list of active formatting elements, then remove node from
                                // the list of active formatting elements.
                                node_in_afe = false
-                               for (i = 0; i < afe.length; ++i) {
-                                       t = afe[i]
-                                       if (t === node) {
-                                               if (inner > 3) {
-                                                       afe.splice(i, 1)
-                                               } else {
-                                                       node_in_afe = true
-                                               }
-                                               break
+                               if ((i = afe.indexOf(node)) !== -1) {
+                                       if (inner > 3) {
+                                               afe.splice(i, 1)
+                                       } else {
+                                               node_in_afe = true
                                        }
                                }
                                // 6. If node is not in the list of active formatting elements,
                                // then remove node from the stack of open elements and then go
                                // back to the step labeled inner loop.
                                if (!node_in_afe) {
-                                       for (i = 0; i < open_els.length; ++i) {
-                                               t = open_els[i]
-                                               if (t === node) {
-                                                       node_above = open_els[i + 1]
-                                                       open_els.splice(i, 1)
-                                                       break
-                                               }
+                                       if ((i = open_els.indexOf(node)) !== -1) {
+                                               node_above = open_els[i + 1]
+                                               open_els.splice(i, 1)
                                        }
                                        continue
                                }
@@ -1360,51 +1352,31 @@ parse_html = function (args_html, args) {
                                // elements with an entry for the new element, and let node be
                                // the new element.
                                new_node = token_to_element(node.token, NS_HTML, ca)
-                               for (i = 0; i < afe.length; ++i) {
-                                       t = afe[i]
-                                       if (t === node) {
-                                               afe[i] = new_node
-                                               break
-                                       }
+                               if ((i = afe.indexOf(node)) !== -1) {
+                                       afe[i] = new_node
                                }
-                               for (i = 0; i < open_els.length; ++i) {
-                                       t = open_els[i]
-                                       if (t === node) {
-                                               node_above = open_els[i + 1]
-                                               open_els[i] = new_node
-                                               break
-                                       }
+                               if ((i = open_els.indexOf(node)) !== -1) {
+                                       node_above = open_els[i + 1]
+                                       open_els[i] = new_node
                                }
                                node = new_node
                                // 8. If last node is furthest block, then move the
                                // aforementioned bookmark to be immediately after the new node
                                // in the list of active formatting elements.
                                if (last_node === fb) {
-                                       for (i = 0; i < afe.length; ++i) {
-                                               t = afe[i]
-                                               if (t === bookmark) {
-                                                       afe.splice(i, 1)
-                                                       break
-                                               }
+                                       if ((i = afe.indexOf(bookmark)) !== -1) {
+                                               afe.splice(i, 1)
                                        }
-                                       for (i = 0; i < afe.length; ++i) {
-                                               t = afe[i]
-                                               if (t === node) {
-                                                       // "after" means lower
-                                                       afe.splice(i, 0, bookmark) // "after as <-
-                                                       break
-                                               }
+                                       if ((i = afe.indexOf(node)) !== -1) {
+                                               // "after" means lower
+                                               afe.splice(i, 0, bookmark) // "after as <-
                                        }
                                }
                                // 9. Insert last node into node, first removing it from its
                                // previous parent node if any.
                                if (last_node.parent != null) {
-                                       for (i = 0; i < last_node.parent.children.length; ++i) {
-                                               c = last_node.parent.children[i]
-                                               if (c === last_node) {
-                                                       last_node.parent.children.splice(i, 1)
-                                                       break
-                                               }
+                                       if ((i = last_node.parent.children.indexOf(last_node)) !== -1) {
+                                               last_node.parent.children.splice(i, 1)
                                        }
                                }
                                node.children.push(last_node)
@@ -1422,12 +1394,8 @@ parse_html = function (args_html, args) {
                        //   * last_node is fb
                        //   * last_node is still in the tree (not a duplicate)
                        if (last_node.parent != null) {
-                               for (i = 0; i < last_node.parent.children.length; ++i) {
-                                       c = last_node.parent.children[i]
-                                       if (c === last_node) {
-                                               last_node.parent.children.splice(i, 1)
-                                               break
-                                       }
+                               if ((i = last_node.parent.children.indexOf(last_node)) !== -1) {
+                                       last_node.parent.children.splice(i, 1)
                                }
                        }
                        // can't use standard insert token thing, because it's already in
@@ -1453,36 +1421,20 @@ parse_html = function (args_html, args) {
                        // elements, and insert the new element into the list of active
                        // formatting elements at the position of the aforementioned
                        // bookmark.
-                       for (i = 0; i < afe.length; ++i) {
-                               t = afe[i]
-                               if (t === fe) {
-                                       afe.splice(i, 1)
-                                       break
-                               }
+                       if ((i = afe.indexOf(fe)) !== -1) {
+                               afe.splice(i, 1)
                        }
-                       for (i = 0; i < afe.length; ++i) {
-                               t = afe[i]
-                               if (t === bookmark) {
-                                       afe[i] = new_element
-                                       break
-                               }
+                       if ((i = afe.indexOf(bookmark)) !== -1) {
+                               afe[i] = new_element
                        }
                        // 19. Remove formatting element from the stack of open elements,
                        // and insert the new element into the stack of open elements
                        // immediately below the position of furthest block in that stack.
-                       for (i = 0; i < open_els.length; ++i) {
-                               t = open_els[i]
-                               if (t === fe) {
-                                       open_els.splice(i, 1)
-                                       break
-                               }
+                       if ((i = open_els.indexOf(fe)) !== -1) {
+                               open_els.splice(i, 1)
                        }
-                       for (i = 0; i < open_els.length; ++i) {
-                               t = open_els[i]
-                               if (t === fb) {
-                                       open_els.splice(i, 0, new_element)
-                                       break
-                               }
+                       if ((i = open_els.indexOf(fb)) !== -1) {
+                               open_els.splice(i, 0, new_element)
                        }
                        // 20. Jump back to the step labeled outer loop.
                }
@@ -1721,6 +1673,7 @@ parse_html = function (args_html, args) {
                        position = adjusted_insertion_location()
                }
                position[0].children.splice(position[1], 0, t)
+               t.parent = position[0]
                return
        }
 
@@ -1967,6 +1920,7 @@ parse_html = function (args_html, args) {
                        el.flag('parser-inserted', true)
                        // fixfull frament case
                        ail[0].children.splice(ail[1], 0, el)
+                       el.parent = ail[0]
                        open_els.unshift(el)
                        tok_state = tok_state_script_data
                        original_ins_mode = ins_mode // make sure orig... is defined
@@ -6070,10 +6024,15 @@ parse_html = function (args_html, args) {
        return doc.children
 }
 
+var this_module = {
+       parse: parse_html,
+       Node: Node,
+}
+
 if (context === 'module') {
-       module.exports = parse_html
+       module.exports = this_module
 } else {
-       window.peach_parser = parse_html
+       window.peach_parser = this_module
 }
 
 }).call(this)