Skip to content

Commit 9b8dc25

Browse files
Zyntondmo-odoo
authored andcommitted
[FIX] remove all unnecessary accesses to document.defaultView
`document.defaultView` can be null, resulting in errors if we depend on it for calling `getSelection`. This is easily fixed given that `document` has its own `getSelection` method which does exactly what we were expecting from that of the `window` object. This removes all these unnecessary `defaultView` accesses.
1 parent 4c813a0 commit 9b8dc25

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

src/commands.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const TEXT_CLASSES_REGEX = /\btext-[^\s]*\b/g;
3232
const BG_CLASSES_REGEX = /\bbg-[^\s]*\b/g;
3333

3434
function insert(editor, data, isText = true) {
35-
const selection = editor.document.defaultView.getSelection();
35+
const selection = editor.document.getSelection();
3636
const range = selection.getRangeAt(0);
3737
let startNode;
3838
let insertBefore = false;
@@ -45,7 +45,7 @@ function insert(editor, data, isText = true) {
4545
} else {
4646
editor.deleteRange(selection);
4747
}
48-
startNode = startNode || editor.document.defaultView.getSelection().anchorNode;
48+
startNode = startNode || editor.document.getSelection().anchorNode;
4949
if (startNode.nodeType === Node.ELEMENT_NODE) {
5050
if (selection.anchorOffset === 0) {
5151
startNode.prepend(editor.document.createTextNode(''));
@@ -82,7 +82,7 @@ function insert(editor, data, isText = true) {
8282
return insertedNodes;
8383
}
8484
function align(editor, mode) {
85-
const sel = editor.document.defaultView.getSelection();
85+
const sel = editor.document.getSelection();
8686
const visitedBlocks = new Set();
8787
const traversedNode = getTraversedNodes(editor.editable);
8888
for (const node of traversedNode) {
@@ -147,7 +147,7 @@ function hasColor(element, mode) {
147147
* which the wanted style should be applied
148148
*/
149149
function applyInlineStyle(editor, applyStyle) {
150-
const sel = editor.document.defaultView.getSelection();
150+
const sel = editor.document.getSelection();
151151
const { startContainer, startOffset, endContainer, endOffset } = sel.getRangeAt(0);
152152
const { anchorNode, anchorOffset, focusNode, focusOffset } = sel;
153153
const direction = getCursorDirection(anchorNode, anchorOffset, focusNode, focusOffset);
@@ -311,7 +311,7 @@ export const editorCommands = {
311311

312312
// Link
313313
createLink: (editor, link, content) => {
314-
const sel = editor.document.defaultView.getSelection();
314+
const sel = editor.document.getSelection();
315315
if (content && !sel.isCollapsed) {
316316
editor.deleteRange(sel);
317317
}
@@ -329,7 +329,7 @@ export const editorCommands = {
329329
}
330330
},
331331
unlink: editor => {
332-
const sel = editor.document.defaultView.getSelection();
332+
const sel = editor.document.getSelection();
333333
// we need to remove the contentEditable isolation of links
334334
// before we apply the unlink, otherwise the command is not performed
335335
// because the content editable root is the link
@@ -469,7 +469,7 @@ export const editorCommands = {
469469
const tdsHtml = new Array(colCount).fill('<td><br></td>').join('');
470470
const trsHtml = new Array(rowCount).fill(`<tr>${tdsHtml}</tr>`).join('');
471471
const tableHtml = `<table class="table table-bordered"><tbody>${trsHtml}</tbody></table>`;
472-
const sel = editor.document.defaultView.getSelection();
472+
const sel = editor.document.getSelection();
473473
if (!sel.isCollapsed) {
474474
editor.deleteRange(sel);
475475
}

src/editor.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ export class OdooEditor extends EventTarget {
890890

891891
let hiliteColor = params.hiliteColor;
892892
if (!hiliteColor) {
893-
const sel = this.document.defaultView.getSelection();
893+
const sel = this.document.getSelection();
894894
if (sel.rangeCount) {
895895
const endContainer = closestElement(sel.getRangeAt(0).endContainer);
896896
const hiliteColorRgb = getComputedStyle(endContainer).backgroundColor;
@@ -921,7 +921,7 @@ export class OdooEditor extends EventTarget {
921921
* @returns {?}
922922
*/
923923
_applyRawCommand(method, ...args) {
924-
const sel = this.document.defaultView.getSelection();
924+
const sel = this.document.getSelection();
925925
if (!sel.isCollapsed && BACKSPACE_FIRST_COMMANDS.includes(method)) {
926926
this.deleteRange(sel);
927927
if (BACKSPACE_ONLY_COMMANDS.includes(method)) {
@@ -1008,7 +1008,7 @@ export class OdooEditor extends EventTarget {
10081008
* @returns {Object}
10091009
*/
10101010
_computeHistoryCursor() {
1011-
const sel = this.document.defaultView.getSelection();
1011+
const sel = this.document.getSelection();
10121012
if (!sel.anchorNode) {
10131013
return this._latestComputedCursor;
10141014
}
@@ -1072,7 +1072,7 @@ export class OdooEditor extends EventTarget {
10721072
this.toolbar.style.visibility = 'visible';
10731073
}
10741074

1075-
const sel = this.document.defaultView.getSelection();
1075+
const sel = this.document.getSelection();
10761076
if (!sel.anchorNode) {
10771077
show = false;
10781078
}
@@ -1168,7 +1168,7 @@ export class OdooEditor extends EventTarget {
11681168
let isBottom = false;
11691169
this.toolbar.classList.toggle('toolbar-bottom', false);
11701170
this.toolbar.style.maxWidth = this.editable.offsetWidth - OFFSET * 2 + 'px';
1171-
const sel = this.document.defaultView.getSelection();
1171+
const sel = this.document.getSelection();
11721172
const range = sel.getRangeAt(0);
11731173
const isSelForward =
11741174
sel.anchorNode === range.startContainer && sel.anchorOffset === range.startOffset;
@@ -1255,7 +1255,7 @@ export class OdooEditor extends EventTarget {
12551255
}
12561256
} else if (['insertText', 'insertCompositionText'].includes(ev.inputType)) {
12571257
// insertCompositionText, courtesy of Samsung keyboard.
1258-
const selection = this.document.defaultView.getSelection();
1258+
const selection = this.document.getSelection();
12591259
// Detect that text was selected and change behavior only if it is the case,
12601260
// since it is the only text insertion case that may cause problems.
12611261
if (anchorNode !== focusNode || anchorOffset !== focusOffset) {
@@ -1289,7 +1289,7 @@ export class OdooEditor extends EventTarget {
12891289
// representation of the key. In this case, call `deleteRange` before
12901290
// inserting the printed representation of the character.
12911291
if (/^.$/u.test(ev.key) && !ev.ctrlKey && !ev.metaKey) {
1292-
const selection = this.document.defaultView.getSelection();
1292+
const selection = this.document.getSelection();
12931293
if (selection && !selection.isCollapsed) {
12941294
this.deleteRange(selection);
12951295
}
@@ -1336,7 +1336,7 @@ export class OdooEditor extends EventTarget {
13361336
// that to the command execution or the 'input' event handler.
13371337
this._computeHistoryCursor();
13381338

1339-
const selection = this.document.defaultView.getSelection();
1339+
const selection = this.document.getSelection();
13401340
const isSelectionInEditable =
13411341
!selection.isCollapsed &&
13421342
this.editable.contains(selection.anchorNode) &&
@@ -1464,7 +1464,7 @@ export class OdooEditor extends EventTarget {
14641464
*/
14651465
_onDrop(ev) {
14661466
ev.preventDefault();
1467-
const sel = this.document.defaultView.getSelection();
1467+
const sel = this.document.getSelection();
14681468
let isInEditor = false;
14691469
let ancestor = sel.anchorNode;
14701470
while (ancestor && !isInEditor) {
@@ -1488,7 +1488,7 @@ export class OdooEditor extends EventTarget {
14881488
const range = this.document.caretRangeFromPoint(ev.clientX, ev.clientY);
14891489
setCursor(range.startContainer, range.startOffset);
14901490
}
1491-
insertText(this.document.defaultView.getSelection(), pastedText);
1491+
insertText(this.document.getSelection(), pastedText);
14921492
});
14931493
}
14941494
this.historyStep();
@@ -1606,7 +1606,7 @@ export class OdooEditor extends EventTarget {
16061606
* Fix the current selection range in case the range start or end inside a fontAwesome node
16071607
*/
16081608
_fixFontAwesomeSelection() {
1609-
const selection = this.document.defaultView.getSelection();
1609+
const selection = this.document.getSelection();
16101610
if (
16111611
selection.isCollapsed ||
16121612
(selection.anchorNode &&

src/tests/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ export async function createLink(editor, content) {
369369
}
370370

371371
export async function insertText(editor, text) {
372-
const sel = document.defaultView.getSelection();
372+
const sel = document.getSelection();
373373
insertTextSel(sel, text);
374374
sel.collapseToEnd();
375375
editor.historyStep();

src/utils/utils.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ export function setCursor(
481481
: getNormalizedCursorPosition(focusNode, focusOffset, normalize);
482482

483483
const direction = getCursorDirection(anchorNode, anchorOffset, focusNode, focusOffset);
484-
const sel = document.defaultView.getSelection();
484+
const sel = document.getSelection();
485485
const range = new Range();
486486
if (direction === DIRECTIONS.RIGHT) {
487487
range.setStart(anchorNode, anchorOffset);
@@ -563,7 +563,7 @@ export function getTraversedNodes(editable) {
563563
*/
564564
export function getSelectedNodes(editable) {
565565
const document = editable.ownerDocument;
566-
const sel = document.defaultView.getSelection();
566+
const sel = document.getSelection();
567567
if (!sel.rangeCount) {
568568
return [];
569569
}
@@ -587,7 +587,7 @@ export function getSelectedNodes(editable) {
587587
* @returns {Range}
588588
*/
589589
export function getDeepRange(editable, { range, sel, splitText, select, correctTripleClick } = {}) {
590-
sel = sel || editable.ownerDocument.defaultView.getSelection();
590+
sel = sel || editable.ownerDocument.getSelection();
591591
range = range ? range.cloneRange() : sel.rangeCount && sel.getRangeAt(0).cloneRange();
592592
if (!range) return;
593593
let start = range.startContainer;
@@ -706,7 +706,7 @@ export function getDeepestPosition(node, offset) {
706706
}
707707

708708
export function getCursors(document) {
709-
const sel = document.defaultView.getSelection();
709+
const sel = document.getSelection();
710710
if (
711711
getCursorDirection(sel.anchorNode, sel.anchorOffset, sel.focusNode, sel.focusOffset) ===
712712
DIRECTIONS.LEFT
@@ -722,7 +722,7 @@ export function getCursors(document) {
722722
}
723723

724724
export function preserveCursor(document) {
725-
const sel = document.defaultView.getSelection();
725+
const sel = document.getSelection();
726726
const cursorPos = [sel.anchorNode, sel.anchorOffset, sel.focusNode, sel.focusOffset];
727727
return replace => {
728728
replace = replace || new Map();
@@ -881,7 +881,7 @@ export function containsUnremovable(node) {
881881
}
882882

883883
export function getInSelection(document, selector) {
884-
const selection = document.defaultView.getSelection();
884+
const selection = document.getSelection();
885885
const range = !!selection.rangeCount && selection.getRangeAt(0);
886886
return (
887887
range &&

0 commit comments

Comments
 (0)