From 40456a0e4aa640192c2be10d6add138292a4a839 Mon Sep 17 00:00:00 2001 From: Soreine Date: Wed, 26 Oct 2016 19:00:59 +0200 Subject: [PATCH] Add tests for splitNode operation --- src/transforms/apply-operation.js | 6 ++-- .../by-key/split-node-by-key/block/index.js | 32 +++++++++++++++++ .../by-key/split-node-by-key/block/input.yaml | 11 ++++++ .../split-node-by-key/block/output.yaml | 12 +++++++ .../by-key/split-node-by-key/text/index.js | 34 +++++++++++++++++++ .../by-key/split-node-by-key/text/input.yaml | 10 ++++++ .../by-key/split-node-by-key/text/output.yaml | 10 ++++++ 7 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 test/transforms/fixtures/by-key/split-node-by-key/block/index.js create mode 100644 test/transforms/fixtures/by-key/split-node-by-key/block/input.yaml create mode 100644 test/transforms/fixtures/by-key/split-node-by-key/block/output.yaml create mode 100644 test/transforms/fixtures/by-key/split-node-by-key/text/index.js create mode 100644 test/transforms/fixtures/by-key/split-node-by-key/text/input.yaml create mode 100644 test/transforms/fixtures/by-key/split-node-by-key/text/output.yaml diff --git a/src/transforms/apply-operation.js b/src/transforms/apply-operation.js index 81ee33080..11d8f5305 100644 --- a/src/transforms/apply-operation.js +++ b/src/transforms/apply-operation.js @@ -403,6 +403,9 @@ function splitNode(state, operation) { const newDocument = document.splitNode(path, offset) // Update selection + let { selection } = state + const { anchorKey, anchorOffset, focusKey, focusOffset } = selection + const node = document.assertPath(path) // The text node that was split const splittedText = node.kind == 'text' @@ -412,9 +415,6 @@ function splitNode(state, operation) { ? offset : offset - node.getOffset(splittedText) - let { selection } = state - const { anchorKey, anchorOffset, focusKey, focusOffset } = selection - // Should we update the selection ? const shouldUpdateAnchor = splittedText.key == anchorKey && textOffset <= anchorOffset const shouldUpdateFocus = splittedText.key == focusKey && textOffset <= focusOffset diff --git a/test/transforms/fixtures/by-key/split-node-by-key/block/index.js b/test/transforms/fixtures/by-key/split-node-by-key/block/index.js new file mode 100644 index 000000000..fc5a6a69f --- /dev/null +++ b/test/transforms/fixtures/by-key/split-node-by-key/block/index.js @@ -0,0 +1,32 @@ + +import assert from 'assert' + +export default function (state) { + const { selection } = state + + const range = selection.merge({ + anchorKey: 'anchor', + anchorOffset: 2, + focusKey: 'anchor', + focusOffset: 5 + }) + + const next = state + .transform() + .moveTo(range) + .splitNodeByKey('key', 3) + .apply() + + + const second = next.document.getTexts().last() + + assert.deepEqual( + next.selection.toJS(), + range.merge({ + focusKey: second.key, + focusOffset: 2 + }).toJS() + ) + + return next +} diff --git a/test/transforms/fixtures/by-key/split-node-by-key/block/input.yaml b/test/transforms/fixtures/by-key/split-node-by-key/block/input.yaml new file mode 100644 index 000000000..564fe1a8f --- /dev/null +++ b/test/transforms/fixtures/by-key/split-node-by-key/block/input.yaml @@ -0,0 +1,11 @@ + +nodes: + - kind: block + key: 'key' + type: paragraph + nodes: + - kind: text + key: 'anchor' + text: Thetext +# Selection +# Th[ete]xt \ No newline at end of file diff --git a/test/transforms/fixtures/by-key/split-node-by-key/block/output.yaml b/test/transforms/fixtures/by-key/split-node-by-key/block/output.yaml new file mode 100644 index 000000000..763d9da44 --- /dev/null +++ b/test/transforms/fixtures/by-key/split-node-by-key/block/output.yaml @@ -0,0 +1,12 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: The + - kind: block + type: paragraph + nodes: + - kind: text + text: text diff --git a/test/transforms/fixtures/by-key/split-node-by-key/text/index.js b/test/transforms/fixtures/by-key/split-node-by-key/text/index.js new file mode 100644 index 000000000..1b2865e2d --- /dev/null +++ b/test/transforms/fixtures/by-key/split-node-by-key/text/index.js @@ -0,0 +1,34 @@ + +import assert from 'assert' + +export default function (state) { + const { selection } = state + + const range = selection.merge({ + anchorKey: 'anchor', + anchorOffset: 4, + focusKey: 'anchor', + focusOffset: 5 + }) + + const next = state + .transform() + .moveTo(range) + .splitNodeByKey('anchor', 3, { normalize: false }) + .apply() + + // The second text + const second = next.document.getTexts().last() + + assert.deepEqual( + next.selection.toJS(), + range.merge({ + anchorKey: second.key, + anchorOffset: 1, + focusKey: second.key, + focusOffset: 2 + }).toJS() + ) + + return next +} diff --git a/test/transforms/fixtures/by-key/split-node-by-key/text/input.yaml b/test/transforms/fixtures/by-key/split-node-by-key/text/input.yaml new file mode 100644 index 000000000..33b7acde9 --- /dev/null +++ b/test/transforms/fixtures/by-key/split-node-by-key/text/input.yaml @@ -0,0 +1,10 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + key: 'anchor' + text: onetwo +# Selection +# onet[w]o \ No newline at end of file diff --git a/test/transforms/fixtures/by-key/split-node-by-key/text/output.yaml b/test/transforms/fixtures/by-key/split-node-by-key/text/output.yaml new file mode 100644 index 000000000..91ceb3594 --- /dev/null +++ b/test/transforms/fixtures/by-key/split-node-by-key/text/output.yaml @@ -0,0 +1,10 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + key: 'anchor' + text: one + - kind: text + text: two