JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
code cleanup (no IE8 support)
authorJason Woofenden <jason@jasonwoof.com>
Wed, 7 Jun 2017 03:29:20 +0000 (23:29 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Wed, 7 Jun 2017 03:29:20 +0000 (23:29 -0400)
editor.js
parser.js

index 977ca9e..72e8506 100644 (file)
--- a/editor.js
+++ b/editor.js
@@ -429,7 +429,7 @@ function instantiate_tree (tree, parent) {
                        case 'tag':
                                if (c.name === 'script' || c.name === 'object' || c.name === 'iframe' || c.name === 'link') {
                                        // TODO put placeholders instead
-                                       remove.unshift(i)
+                                       remove.unshift(i) // add to beginning so they are removed last first
                                        continue
                                }
                                // TODO create in correct namespace
@@ -450,13 +450,11 @@ function instantiate_tree (tree, parent) {
                                }
                }
        }
-       results = []
+       // these are in reverse order so we remove highest indexes first
        for (i = 0; i < remove.length; i++) {
-               // FIXME this deletes the wrong node when siblings are removed
                index = remove[i]
-               results.push(tree.splice(index, 1))
+               tree.splice(index, 1)
        }
-       return results
 }
 
 function traverse_tree (tree, cb) {
index 1bbc57b..4493ed1 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.
                }