From 8409e490c9372ba65c561e47f47d42dc3f23b83e Mon Sep 17 00:00:00 2001
From: Ian Storm Taylor <ian@ianstormtaylor.com>
Date: Thu, 23 Aug 2018 13:53:29 -0700
Subject: [PATCH] tweak maximum schema iterations logic, addresses #2118

---
 packages/slate/src/changes/with-schema.js     |  6 ++-
 packages/slate/test/index.js                  |  7 ++-
 .../merge-adjacent-texts-max-iterations.js    | 53 +++++++++++++++++++
 3 files changed, 64 insertions(+), 2 deletions(-)
 create mode 100644 packages/slate/test/schema/core/merge-adjacent-texts-max-iterations.js

diff --git a/packages/slate/src/changes/with-schema.js b/packages/slate/src/changes/with-schema.js
index 51ab684b3..edce6e4a3 100644
--- a/packages/slate/src/changes/with-schema.js
+++ b/packages/slate/src/changes/with-schema.js
@@ -155,7 +155,11 @@ function normalizeNodeAndChildren(change, node, schema) {
  */
 
 function normalizeNode(change, node, schema) {
-  const max = schema.stack.plugins.length + schema.rules.length + 1
+  const max =
+    schema.stack.plugins.length +
+    schema.rules.length +
+    (node.object === 'text' ? 1 : node.nodes.size)
+
   let iterations = 0
 
   function iterate(c, n) {
diff --git a/packages/slate/test/index.js b/packages/slate/test/index.js
index 462b2ee74..7689cd954 100644
--- a/packages/slate/test/index.js
+++ b/packages/slate/test/index.js
@@ -90,8 +90,13 @@ describe('slate', () => {
   })
 
   fixtures(__dirname, 'schema', ({ module }) => {
-    const { input, output, schema } = module
+    let { input, output, schema } = module
     const s = Schema.create(schema)
+
+    if (!Value.isValue(input)) {
+      input = Value.fromJSON(input)
+    }
+
     let expected = output
     let actual = input
       .change()
diff --git a/packages/slate/test/schema/core/merge-adjacent-texts-max-iterations.js b/packages/slate/test/schema/core/merge-adjacent-texts-max-iterations.js
new file mode 100644
index 000000000..8fdb58b16
--- /dev/null
+++ b/packages/slate/test/schema/core/merge-adjacent-texts-max-iterations.js
@@ -0,0 +1,53 @@
+export const schema = {}
+
+export const input = {
+  object: 'value',
+  document: {
+    object: 'document',
+    data: {},
+    nodes: [
+      {
+        object: 'block',
+        type: 'paragraph',
+        data: {},
+        nodes: Array.from({ length: 100 }).map(() => ({
+          object: 'text',
+          leaves: [
+            {
+              object: 'leaf',
+              text: 'a',
+              marks: [],
+            },
+          ],
+        })),
+      },
+    ],
+  },
+}
+
+export const output = {
+  object: 'value',
+  document: {
+    object: 'document',
+    data: {},
+    nodes: [
+      {
+        object: 'block',
+        type: 'paragraph',
+        data: {},
+        nodes: [
+          {
+            object: 'text',
+            leaves: [
+              {
+                object: 'leaf',
+                text: 'a'.repeat(100),
+                marks: [],
+              },
+            ],
+          },
+        ],
+      },
+    ],
+  },
+}