Repository: ianstormtaylor/slate
File: packages/slate/src/core/normalize-node.ts (TypeScript)

Code before:
          } else if (prev.text === '') {
            Transforms.removeNodes(editor, {
              at: path.concat(n - 1),
              voids: true,
            })
            element = Node.get(editor, path) as Element
            n--
          } else if (Text.equals(child, prev, { loose: true })) {
            Transforms.mergeNodes(editor, { at: path.concat(n), voids: true })
            element = Node.get(editor, path) as Element
            n--
          }
        }
      } else {
        if (editor.isInline(child)) {
          // Ensure that inline nodes are surrounded by text nodes.
          if (prev == null || !Text.isText(prev)) {
            const newChild = { text: '' }
            Transforms.insertNodes(editor, newChild, {
              at: path.concat(n),
              voids: true,
            })
            element = Node.get(editor, path) as Element
            n++
          }
          if (n === element.children.length - 1) {
            const newChild = { text: '' }
            Transforms.insertNodes(editor, newChild, {
              at: path.concat(n + 1),
              voids: true,
            })
            element = Node.get(editor, path) as Element
            n++
          }
        } else {
          // Allow only inline nodes to be in other inline nodes, or in parent blocks that only
          // contain inlines and text.
          Transforms.unwrapNodes(editor, { at: path.concat(n), voids: true })
          element = Node.get(editor, path) as Element
          n--
        }
      }
    }
  } else {
    // Since we'll be applying operations while iterating, we also modify `n` when adding/removing nodes
    for (let n = 0; n < element.children.length; n++) {
      const child = element.children[n]

      // Allow only block nodes in the top-level children and parent blocks that only contain block nodes
      if (Text.isText(child) || editor.isInline(child)) {
        if (options?.fallbackElement) {

Review comment (suggestion):
using if instead of else-if here so we can make the before and after nodes in the same pass--this could cause issues if any wrappers around `normalizeNode` rely on doing something in between but I can't imagine why anything reasonably would

Code after:
          } else if (prev.text === '') {
            Transforms.removeNodes(editor, {
              at: path.concat(n - 1),
              voids: true,
            })
            element = Node.get(editor, path) as Element
            n--
          } else if (Text.equals(child, prev, { loose: true })) {
            Transforms.mergeNodes(editor, { at: path.concat(n), voids: true })
            element = Node.get(editor, path) as Element
            n--
          }
        }
      } else {
        if (editor.isInline(child)) {
          // Ensure that inline nodes are surrounded by text nodes.
          if (prev == null || !Node.isText(prev)) {
            const newChild = { text: '' }
            Transforms.insertNodes(editor, newChild, {
              at: path.concat(n),
              voids: true,
            })
            element = Node.get(editor, path) as Element
            n++
          }
          if (n === element.children.length - 1) {
            const newChild = { text: '' }
            Transforms.insertNodes(editor, newChild, {
              at: path.concat(n + 1),
              voids: true,
            })
            element = Node.get(editor, path) as Element
            n++
          }
        } else {
          // Allow only inline nodes to be in other inline nodes, or in parent blocks that only
          // contain inlines and text.
          Transforms.unwrapNodes(editor, { at: path.concat(n), voids: true })
          element = Node.get(editor, path) as Element
          n--
        }
      }
    }
  } else {
    // Since we'll be applying operations while iterating, we also modify `n` when adding/removing nodes
    for (let n = 0; n < element.children.length; n++) {
      const child = element.children[n]

      // Allow only block nodes in the top-level children and parent blocks that only contain block nodes
      if (Node.isText(child) || editor.isInline(child)) {
        if (options?.fallbackElement) {