element-web/test/editor/caret-test.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

161 lines
7.7 KiB
TypeScript
Raw Normal View History

/*
Copyright 2019 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2021-06-29 15:11:58 +03:00
import { getLineAndNodePosition } from "../../src/editor/caret";
import EditorModel from "../../src/editor/model";
2021-06-29 15:11:58 +03:00
import { createPartCreator } from "./mock";
describe("editor/caret: DOM position for caret", function () {
describe("basic text handling", function () {
it("at end of single line", function () {
const pc = createPartCreator();
const model = new EditorModel([pc.plain("hello")], pc);
2021-06-29 15:11:58 +03:00
const { offset, lineIndex, nodeIndex } = getLineAndNodePosition(model, { index: 0, offset: 5 });
expect(lineIndex).toBe(0);
expect(nodeIndex).toBe(0);
expect(offset).toBe(5);
});
it("at start of single line", function () {
const pc = createPartCreator();
const model = new EditorModel([pc.plain("hello")], pc);
2021-06-29 15:11:58 +03:00
const { offset, lineIndex, nodeIndex } = getLineAndNodePosition(model, { index: 0, offset: 0 });
expect(lineIndex).toBe(0);
expect(nodeIndex).toBe(0);
expect(offset).toBe(0);
});
it("at middle of single line", function () {
const pc = createPartCreator();
const model = new EditorModel([pc.plain("hello")], pc);
2021-06-29 15:11:58 +03:00
const { offset, lineIndex, nodeIndex } = getLineAndNodePosition(model, { index: 0, offset: 2 });
expect(lineIndex).toBe(0);
expect(nodeIndex).toBe(0);
expect(offset).toBe(2);
});
});
describe("handling line breaks", function () {
it("at end of last line", function () {
const pc = createPartCreator();
const model = new EditorModel([pc.plain("hello"), pc.newline(), pc.plain("world")], pc);
2021-06-29 15:11:58 +03:00
const { offset, lineIndex, nodeIndex } = getLineAndNodePosition(model, { index: 2, offset: 5 });
expect(lineIndex).toBe(1);
expect(nodeIndex).toBe(0);
expect(offset).toBe(5);
});
it("at start of last line", function () {
const pc = createPartCreator();
const model = new EditorModel([pc.plain("hello"), pc.newline(), pc.plain("world")], pc);
2021-06-29 15:11:58 +03:00
const { offset, lineIndex, nodeIndex } = getLineAndNodePosition(model, { index: 2, offset: 0 });
expect(lineIndex).toBe(1);
expect(nodeIndex).toBe(0);
expect(offset).toBe(0);
});
it("in empty line", function () {
const pc = createPartCreator();
const model = new EditorModel([pc.plain("hello"), pc.newline(), pc.newline(), pc.plain("world")], pc);
2021-06-29 15:11:58 +03:00
const { offset, lineIndex, nodeIndex } = getLineAndNodePosition(model, { index: 1, offset: 1 });
expect(lineIndex).toBe(1);
expect(nodeIndex).toBe(-1);
expect(offset).toBe(0);
});
it("after empty line", function () {
const pc = createPartCreator();
const model = new EditorModel([pc.plain("hello"), pc.newline(), pc.newline(), pc.plain("world")], pc);
2021-06-29 15:11:58 +03:00
const { offset, lineIndex, nodeIndex } = getLineAndNodePosition(model, { index: 3, offset: 0 });
expect(lineIndex).toBe(2);
expect(nodeIndex).toBe(0);
expect(offset).toBe(0);
});
});
describe("handling non-editable parts and caret nodes", function () {
it("at start of non-editable part (with plain text around)", function () {
const pc = createPartCreator();
const model = new EditorModel(
[pc.plain("hello"), pc.userPill("Alice", "@alice:hs.tld"), pc.plain("!")],
pc,
Unit test typescriptification - editor utils (#8226) * editor/mock ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/serialize-test.js -> test/editor/serialize-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix ts in serialize-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/range-test.js -> test/editor/range-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/position-test.js -> test/editor/position-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix position-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/operations-test.js -> test/editor/operations-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/model-test.js -> test/editor/model-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix ts in model-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/history-test.js -> test/editor/history-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix ts in history-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/diff-test.js -> test/editor/diff-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/deserialize-test.js -> test/editor/deserialize-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * ts in deserialize-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/caret-test.js -> test/editor/caret-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * ts caret-test Signed-off-by: Kerry Archibald <kerrya@element.io>
2022-04-06 14:40:13 +03:00
);
2021-06-29 15:11:58 +03:00
const { offset, lineIndex, nodeIndex } = getLineAndNodePosition(model, { index: 1, offset: 0 });
expect(lineIndex).toBe(0);
expect(nodeIndex).toBe(0);
expect(offset).toBe(5);
});
it("in middle of non-editable part (with plain text around)", function () {
const pc = createPartCreator();
const model = new EditorModel(
[pc.plain("hello"), pc.userPill("Alice", "@alice:hs.tld"), pc.plain("!")],
pc,
Unit test typescriptification - editor utils (#8226) * editor/mock ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/serialize-test.js -> test/editor/serialize-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix ts in serialize-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/range-test.js -> test/editor/range-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/position-test.js -> test/editor/position-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix position-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/operations-test.js -> test/editor/operations-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/model-test.js -> test/editor/model-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix ts in model-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/history-test.js -> test/editor/history-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix ts in history-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/diff-test.js -> test/editor/diff-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/deserialize-test.js -> test/editor/deserialize-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * ts in deserialize-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/caret-test.js -> test/editor/caret-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * ts caret-test Signed-off-by: Kerry Archibald <kerrya@element.io>
2022-04-06 14:40:13 +03:00
);
2021-06-29 15:11:58 +03:00
const { offset, lineIndex, nodeIndex } = getLineAndNodePosition(model, { index: 1, offset: 2 });
expect(lineIndex).toBe(0);
expect(nodeIndex).toBe(2);
expect(offset).toBe(0);
});
it("at start of non-editable part (without plain text around)", function () {
const pc = createPartCreator();
const model = new EditorModel([pc.userPill("Alice", "@alice:hs.tld")], pc);
2021-06-29 15:11:58 +03:00
const { offset, lineIndex, nodeIndex } = getLineAndNodePosition(model, { index: 0, offset: 0 });
expect(lineIndex).toBe(0);
//presumed nodes on line are (caret, pill, caret)
expect(nodeIndex).toBe(0);
expect(offset).toBe(0);
});
it("in middle of non-editable part (without plain text around)", function () {
const pc = createPartCreator();
const model = new EditorModel([pc.userPill("Alice", "@alice:hs.tld")], pc);
2021-06-29 15:11:58 +03:00
const { offset, lineIndex, nodeIndex } = getLineAndNodePosition(model, { index: 0, offset: 1 });
expect(lineIndex).toBe(0);
//presumed nodes on line are (caret, pill, caret)
expect(nodeIndex).toBe(2);
expect(offset).toBe(0);
});
it("in middle of a first non-editable part, with another one following", function () {
const pc = createPartCreator();
const model = new EditorModel(
[pc.userPill("Alice", "@alice:hs.tld"), pc.userPill("Bob", "@bob:hs.tld")],
Unit test typescriptification - editor utils (#8226) * editor/mock ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/serialize-test.js -> test/editor/serialize-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix ts in serialize-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/range-test.js -> test/editor/range-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/position-test.js -> test/editor/position-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix position-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/operations-test.js -> test/editor/operations-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/model-test.js -> test/editor/model-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix ts in model-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/history-test.js -> test/editor/history-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix ts in history-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/diff-test.js -> test/editor/diff-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/deserialize-test.js -> test/editor/deserialize-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * ts in deserialize-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/caret-test.js -> test/editor/caret-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * ts caret-test Signed-off-by: Kerry Archibald <kerrya@element.io>
2022-04-06 14:40:13 +03:00
pc,
);
2021-06-29 15:11:58 +03:00
const { offset, lineIndex, nodeIndex } = getLineAndNodePosition(model, { index: 0, offset: 1 });
expect(lineIndex).toBe(0);
//presumed nodes on line are (caret, pill, caret, pill, caret)
expect(nodeIndex).toBe(2);
expect(offset).toBe(0);
});
it("in start of a second non-editable part, with another one before it", function () {
const pc = createPartCreator();
const model = new EditorModel(
[pc.userPill("Alice", "@alice:hs.tld"), pc.userPill("Bob", "@bob:hs.tld")],
Unit test typescriptification - editor utils (#8226) * editor/mock ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/serialize-test.js -> test/editor/serialize-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix ts in serialize-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/range-test.js -> test/editor/range-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/position-test.js -> test/editor/position-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix position-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/operations-test.js -> test/editor/operations-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/model-test.js -> test/editor/model-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix ts in model-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/history-test.js -> test/editor/history-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix ts in history-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/diff-test.js -> test/editor/diff-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/deserialize-test.js -> test/editor/deserialize-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * ts in deserialize-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/caret-test.js -> test/editor/caret-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * ts caret-test Signed-off-by: Kerry Archibald <kerrya@element.io>
2022-04-06 14:40:13 +03:00
pc,
);
2021-06-29 15:11:58 +03:00
const { offset, lineIndex, nodeIndex } = getLineAndNodePosition(model, { index: 1, offset: 0 });
expect(lineIndex).toBe(0);
//presumed nodes on line are (caret, pill, caret, pill, caret)
expect(nodeIndex).toBe(2);
expect(offset).toBe(0);
});
it("in middle of a second non-editable part, with another one before it", function () {
const pc = createPartCreator();
const model = new EditorModel(
[pc.userPill("Alice", "@alice:hs.tld"), pc.userPill("Bob", "@bob:hs.tld")],
Unit test typescriptification - editor utils (#8226) * editor/mock ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/serialize-test.js -> test/editor/serialize-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix ts in serialize-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/range-test.js -> test/editor/range-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/position-test.js -> test/editor/position-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix position-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/operations-test.js -> test/editor/operations-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/model-test.js -> test/editor/model-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix ts in model-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/history-test.js -> test/editor/history-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * fix ts in history-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/diff-test.js -> test/editor/diff-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/deserialize-test.js -> test/editor/deserialize-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * ts in deserialize-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/editor/caret-test.js -> test/editor/caret-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * ts caret-test Signed-off-by: Kerry Archibald <kerrya@element.io>
2022-04-06 14:40:13 +03:00
pc,
);
2021-06-29 15:11:58 +03:00
const { offset, lineIndex, nodeIndex } = getLineAndNodePosition(model, { index: 1, offset: 1 });
expect(lineIndex).toBe(0);
//presumed nodes on line are (caret, pill, caret, pill, caret)
expect(nodeIndex).toBe(4);
expect(offset).toBe(0);
});
});
});