Skip to content
Snippets Groups Projects
Unverified Commit ccd1f972 authored by Tom Robinson's avatar Tom Robinson
Browse files

Fix the expression editor in recent versions of Chrome and Firefox. Resolves #4906

parent 9d49adaf
Branches
Tags
No related merge requests found
......@@ -90,7 +90,8 @@ export function getSelectionPosition(element) {
else {
try {
const selection = window.getSelection();
const range = selection.getRangeAt(0);
// Clone the Range otherwise setStart/setEnd will mutate the actual selection in Chrome 58+ and Firefox!
const range = selection.getRangeAt(0).cloneRange();
const { startContainer, startOffset } = range;
range.setStart(element, 0);
const end = range.toString().length;
......
......@@ -26,4 +26,14 @@ describe("getSelectionPosition/setSelectionPosition", () => {
const position = getSelectionPosition(contenteditable);
expect(position).toEqual([3, 6]);
});
it("should not mutate the actual selection", () => {
let contenteditable = document.createElement("div");
container.appendChild(contenteditable);
contenteditable.textContent = "<div>hello world</div>"
setSelectionPosition(contenteditable, [3, 6]);
const position = getSelectionPosition(contenteditable);
expect(position).toEqual([3, 6]);
const position2 = getSelectionPosition(contenteditable);
expect(position2).toEqual([3, 6]);
})
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment