ace2_inner: Delete unnecessary `doc` and `root` variables

pull/5153/head
Richard Hansen 2021-06-22 02:30:24 -04:00
parent ec63c15a40
commit 4b4584c264
1 changed files with 46 additions and 49 deletions

View File

@ -110,7 +110,6 @@ function Ace2Inner(editorInfo, cssManagers) {
undoModule.apool = rep.apool; undoModule.apool = rep.apool;
} }
let root, doc; // set in init()
let isEditable = true; let isEditable = true;
let doesWrap = true; let doesWrap = true;
let hasLineNumbers = true; let hasLineNumbers = true;
@ -403,7 +402,7 @@ function Ace2Inner(editorInfo, cssManagers) {
const setWraps = (newVal) => { const setWraps = (newVal) => {
doesWrap = newVal; doesWrap = newVal;
root.classList.toggle('doesWrap', doesWrap); document.body.classList.toggle('doesWrap', doesWrap);
scheduler.setTimeout(() => { scheduler.setTimeout(() => {
inCallStackIfNecessary('setWraps', () => { inCallStackIfNecessary('setWraps', () => {
fastIncorp(7); fastIncorp(7);
@ -433,7 +432,7 @@ function Ace2Inner(editorInfo, cssManagers) {
}; };
const setTextFace = (face) => { const setTextFace = (face) => {
root.style.fontFamily = face; document.body.style.fontFamily = face;
lineMetricsDiv.style.fontFamily = face; lineMetricsDiv.style.fontFamily = face;
}; };
@ -444,8 +443,8 @@ function Ace2Inner(editorInfo, cssManagers) {
const setEditable = (newVal) => { const setEditable = (newVal) => {
isEditable = newVal; isEditable = newVal;
root.contentEditable = isEditable ? 'true' : 'false'; document.body.contentEditable = isEditable ? 'true' : 'false';
root.classList.toggle('static', !isEditable); document.body.classList.toggle('static', !isEditable);
}; };
const enforceEditability = () => setEditable(isEditable); const enforceEditability = () => setEditable(isEditable);
@ -628,8 +627,8 @@ function Ace2Inner(editorInfo, cssManagers) {
// These properties are exposed // These properties are exposed
const setters = { const setters = {
wraps: setWraps, wraps: setWraps,
showsauthorcolors: (val) => root.classList.toggle('authorColors', !!val), showsauthorcolors: (val) => document.body.classList.toggle('authorColors', !!val),
showsuserselections: (val) => root.classList.toggle('userSelections', !!val), showsuserselections: (val) => document.body.classList.toggle('userSelections', !!val),
showslinenumbers: (value) => { showslinenumbers: (value) => {
hasLineNumbers = !!value; hasLineNumbers = !!value;
sideDiv.parentNode.classList.toggle('line-numbers-hidden', !hasLineNumbers); sideDiv.parentNode.classList.toggle('line-numbers-hidden', !hasLineNumbers);
@ -642,8 +641,8 @@ function Ace2Inner(editorInfo, cssManagers) {
styled: setStyled, styled: setStyled,
textface: setTextFace, textface: setTextFace,
rtlistrue: (value) => { rtlistrue: (value) => {
root.classList.toggle('rtl', value); document.body.classList.toggle('rtl', value);
root.classList.toggle('ltr', !value); document.body.classList.toggle('ltr', !value);
document.documentElement.dir = value ? 'rtl' : 'ltr'; document.documentElement.dir = value ? 'rtl' : 'ltr';
}, },
}; };
@ -681,7 +680,7 @@ function Ace2Inner(editorInfo, cssManagers) {
editorInfo.ace_getUnhandledErrors = () => caughtErrors.slice(); editorInfo.ace_getUnhandledErrors = () => caughtErrors.slice();
editorInfo.ace_getDocument = () => doc; editorInfo.ace_getDocument = () => document;
editorInfo.ace_getDebugProperty = (prop) => { editorInfo.ace_getDebugProperty = (prop) => {
if (prop === 'debugger') { if (prop === 'debugger') {
@ -901,11 +900,11 @@ function Ace2Inner(editorInfo, cssManagers) {
clearObservedChanges(); clearObservedChanges();
const getCleanNodeByKey = (key) => { const getCleanNodeByKey = (key) => {
let n = doc.getElementById(key); let n = document.getElementById(key);
// copying and pasting can lead to duplicate ids // copying and pasting can lead to duplicate ids
while (n && isNodeDirty(n)) { while (n && isNodeDirty(n)) {
n.id = ''; n.id = '';
n = doc.getElementById(key); n = document.getElementById(key);
} }
return n; return n;
}; };
@ -987,11 +986,11 @@ function Ace2Inner(editorInfo, cssManagers) {
const observeSuspiciousNodes = () => { const observeSuspiciousNodes = () => {
// inspired by Firefox bug #473255, where pasting formatted text // inspired by Firefox bug #473255, where pasting formatted text
// causes the cursor to jump away, making the new HTML never found. // causes the cursor to jump away, making the new HTML never found.
if (root.getElementsByTagName) { if (document.body.getElementsByTagName) {
const nds = root.getElementsByTagName('style'); const nds = document.body.getElementsByTagName('style');
for (let i = 0; i < nds.length; i++) { for (let i = 0; i < nds.length; i++) {
const n = topLevel(nds[i]); const n = topLevel(nds[i]);
if (n && n.parentNode === root) { if (n && n.parentNode === document.body) {
observeChangesAroundNode(n); observeChangesAroundNode(n);
} }
} }
@ -1006,8 +1005,8 @@ function Ace2Inner(editorInfo, cssManagers) {
if (DEBUG && window.DONT_INCORP || window.DEBUG_DONT_INCORP) return false; if (DEBUG && window.DONT_INCORP || window.DEBUG_DONT_INCORP) return false;
// returns true if dom changes were made // returns true if dom changes were made
if (!root.firstChild) { if (!document.body.firstChild) {
root.innerHTML = '<div><!-- --></div>'; document.body.innerHTML = '<div><!-- --></div>';
} }
observeChangesAroundSelection(); observeChangesAroundSelection();
@ -1029,9 +1028,9 @@ function Ace2Inner(editorInfo, cssManagers) {
j++; j++;
} }
if (!dirtyRangesCheckOut) { if (!dirtyRangesCheckOut) {
const numBodyNodes = root.childNodes.length; const numBodyNodes = document.body.childNodes.length;
for (let k = 0; k < numBodyNodes; k++) { for (let k = 0; k < numBodyNodes; k++) {
const bodyNode = root.childNodes.item(k); const bodyNode = document.body.childNodes.item(k);
if ((bodyNode.tagName) && ((!bodyNode.id) || (!rep.lines.containsKey(bodyNode.id)))) { if ((bodyNode.tagName) && ((!bodyNode.id) || (!rep.lines.containsKey(bodyNode.id)))) {
observeChangesAroundNode(bodyNode); observeChangesAroundNode(bodyNode);
} }
@ -1053,11 +1052,11 @@ function Ace2Inner(editorInfo, cssManagers) {
const range = dirtyRanges[i]; const range = dirtyRanges[i];
a = range[0]; a = range[0];
b = range[1]; b = range[1];
let firstDirtyNode = (((a === 0) && root.firstChild) || let firstDirtyNode = (((a === 0) && document.body.firstChild) ||
getCleanNodeByKey(rep.lines.atIndex(a - 1).key).nextSibling); getCleanNodeByKey(rep.lines.atIndex(a - 1).key).nextSibling);
firstDirtyNode = (firstDirtyNode && isNodeDirty(firstDirtyNode) && firstDirtyNode); firstDirtyNode = (firstDirtyNode && isNodeDirty(firstDirtyNode) && firstDirtyNode);
let lastDirtyNode = (((b === rep.lines.length()) && root.lastChild) || let lastDirtyNode = (((b === rep.lines.length()) && document.body.lastChild) ||
getCleanNodeByKey(rep.lines.atIndex(b).key).previousSibling); getCleanNodeByKey(rep.lines.atIndex(b).key).previousSibling);
lastDirtyNode = (lastDirtyNode && isNodeDirty(lastDirtyNode) && lastDirtyNode); lastDirtyNode = (lastDirtyNode && isNodeDirty(lastDirtyNode) && lastDirtyNode);
@ -1159,7 +1158,7 @@ function Ace2Inner(editorInfo, cssManagers) {
callstack: currentCallStack, callstack: currentCallStack,
editorInfo, editorInfo,
rep, rep,
root, root: document.body,
point: selection.startPoint, point: selection.startPoint,
documentAttributeManager, documentAttributeManager,
}); });
@ -1171,7 +1170,7 @@ function Ace2Inner(editorInfo, cssManagers) {
callstack: currentCallStack, callstack: currentCallStack,
editorInfo, editorInfo,
rep, rep,
root, root: document.body,
point: selection.endPoint, point: selection.endPoint,
documentAttributeManager, documentAttributeManager,
}); });
@ -1253,9 +1252,9 @@ function Ace2Inner(editorInfo, cssManagers) {
info.prepareForAdd(); info.prepareForAdd();
entry.lineMarker = info.lineMarker; entry.lineMarker = info.lineMarker;
if (!nodeToAddAfter) { if (!nodeToAddAfter) {
root.insertBefore(node, root.firstChild); document.body.insertBefore(node, document.body.firstChild);
} else { } else {
root.insertBefore(node, nodeToAddAfter.nextSibling); document.body.insertBefore(node, nodeToAddAfter.nextSibling);
} }
nodeToAddAfter = node; nodeToAddAfter = node;
info.notifyAdded(); info.notifyAdded();
@ -1352,7 +1351,7 @@ function Ace2Inner(editorInfo, cssManagers) {
// Turn DOM node selection into [line,char] selection. // Turn DOM node selection into [line,char] selection.
// This method has to work when the DOM is not pristine, // This method has to work when the DOM is not pristine,
// assuming the point is not in a dirty node. // assuming the point is not in a dirty node.
if (point.node === root) { if (point.node === document.body) {
if (point.index === 0) { if (point.index === 0) {
return [0, 0]; return [0, 0];
} else { } else {
@ -1371,7 +1370,7 @@ function Ace2Inner(editorInfo, cssManagers) {
col = nodeText(n).length; col = nodeText(n).length;
} }
let parNode, prevSib; let parNode, prevSib;
while ((parNode = n.parentNode) !== root) { while ((parNode = n.parentNode) !== document.body) {
if ((prevSib = n.previousSibling)) { if ((prevSib = n.previousSibling)) {
n = prevSib; n = prevSib;
col += nodeText(n).length; col += nodeText(n).length;
@ -1424,7 +1423,7 @@ function Ace2Inner(editorInfo, cssManagers) {
insertDomLines(nodeToAddAfter, lineEntries.map((entry) => entry.domInfo)); insertDomLines(nodeToAddAfter, lineEntries.map((entry) => entry.domInfo));
keysToDelete.forEach((k) => { keysToDelete.forEach((k) => {
const n = doc.getElementById(k); const n = document.getElementById(k);
n.parentNode.removeChild(n); n.parentNode.removeChild(n);
}); });
@ -2082,7 +2081,8 @@ function Ace2Inner(editorInfo, cssManagers) {
}); });
}; };
const doCreateDomLine = (nonEmpty) => domline.createDomLine(nonEmpty, doesWrap, browser, doc); const doCreateDomLine =
(nonEmpty) => domline.createDomLine(nonEmpty, doesWrap, browser, document);
const textify = const textify =
(str) => str.replace(/[\n\r ]/g, ' ').replace(/\xa0/g, ' ').replace(/\t/g, ' '); (str) => str.replace(/[\n\r ]/g, ' ').replace(/\xa0/g, ' ').replace(/\t/g, ' ');
@ -2141,7 +2141,7 @@ function Ace2Inner(editorInfo, cssManagers) {
const a = cleanNodeForIndex(i - 1); const a = cleanNodeForIndex(i - 1);
const b = cleanNodeForIndex(i); const b = cleanNodeForIndex(i);
if ((!a) || (!b)) return false; // violates precondition if ((!a) || (!b)) return false; // violates precondition
if ((a === true) && (b === true)) return !root.firstChild; if ((a === true) && (b === true)) return !document.body.firstChild;
if ((a === true) && b.previousSibling) return false; if ((a === true) && b.previousSibling) return false;
if ((b === true) && a.nextSibling) return false; if ((b === true) && a.nextSibling) return false;
if ((a === true) || (b === true)) return true; if ((a === true) || (b === true)) return true;
@ -2294,7 +2294,7 @@ function Ace2Inner(editorInfo, cssManagers) {
}; };
const isNodeDirty = (n) => { const isNodeDirty = (n) => {
if (n.parentNode !== root) return true; if (n.parentNode !== document.body) return true;
const data = getAssoc(n, 'dirtiness'); const data = getAssoc(n, 'dirtiness');
if (!data) return true; if (!data) return true;
if (n.id !== data.nodeId) return true; if (n.id !== data.nodeId) return true;
@ -3073,13 +3073,13 @@ function Ace2Inner(editorInfo, cssManagers) {
// with background doesn't seem to show up... // with background doesn't seem to show up...
if (isNodeText(p.node) && p.index === p.maxIndex) { if (isNodeText(p.node) && p.index === p.maxIndex) {
let n = p.node; let n = p.node;
while (!n.nextSibling && n !== root && n.parentNode !== root) { while (!n.nextSibling && n !== document.body && n.parentNode !== document.body) {
n = n.parentNode; n = n.parentNode;
} }
if (n.nextSibling && if (n.nextSibling &&
!(typeof n.nextSibling.tagName === 'string' && !(typeof n.nextSibling.tagName === 'string' &&
n.nextSibling.tagName.toLowerCase() === 'br') && n.nextSibling.tagName.toLowerCase() === 'br') &&
n !== p.node && n !== root && n.parentNode !== root) { n !== p.node && n !== document.body && n.parentNode !== document.body) {
// found a parent, go to next node and dive in // found a parent, go to next node and dive in
p.node = n.nextSibling; p.node = n.nextSibling;
p.maxIndex = nodeMaxIndex(p.node); p.maxIndex = nodeMaxIndex(p.node);
@ -3122,7 +3122,7 @@ function Ace2Inner(editorInfo, cssManagers) {
browserSelection.collapse(end.container, end.offset); browserSelection.collapse(end.container, end.offset);
browserSelection.extend(start.container, start.offset); browserSelection.extend(start.container, start.offset);
} else { } else {
const range = doc.createRange(); const range = document.createRange();
range.setStart(start.container, start.offset); range.setStart(start.container, start.offset);
range.setEnd(end.container, end.offset); range.setEnd(end.container, end.offset);
browserSelection.removeAllRanges(); browserSelection.removeAllRanges();
@ -3199,7 +3199,7 @@ function Ace2Inner(editorInfo, cssManagers) {
if (!isInBody(container)) { if (!isInBody(container)) {
// command-click in Firefox selects whole document, HEAD and BODY! // command-click in Firefox selects whole document, HEAD and BODY!
return { return {
node: root, node: document.body,
index: 0, index: 0,
maxIndex: 1, maxIndex: 1,
}; };
@ -3294,7 +3294,7 @@ function Ace2Inner(editorInfo, cssManagers) {
// If non-nullish, pasting on a link should be suppressed. // If non-nullish, pasting on a link should be suppressed.
let suppressPasteOnLink = null; let suppressPasteOnLink = null;
$(root).on('auxclick', (e) => { $(document.body).on('auxclick', (e) => {
if (e.originalEvent.button === 1 && (e.target.a || e.target.localName === 'a')) { if (e.originalEvent.button === 1 && (e.target.a || e.target.localName === 'a')) {
// The user middle-clicked on a link. Usually users do this to open a link in a new tab, but // The user middle-clicked on a link. Usually users do this to open a link in a new tab, but
// in X11 (Linux) this will instead paste the contents of the primary selection at the mouse // in X11 (Linux) this will instead paste the contents of the primary selection at the mouse
@ -3316,7 +3316,7 @@ function Ace2Inner(editorInfo, cssManagers) {
} }
}); });
$(root).on('paste', (e) => { $(document.body).on('paste', (e) => {
if (suppressPasteOnLink != null && (e.target.a || e.target.localName === 'a')) { if (suppressPasteOnLink != null && (e.target.a || e.target.localName === 'a')) {
scheduler.clearTimeout(suppressPasteOnLink); scheduler.clearTimeout(suppressPasteOnLink);
suppressPasteOnLink = null; suppressPasteOnLink = null;
@ -3378,8 +3378,8 @@ function Ace2Inner(editorInfo, cssManagers) {
}; };
const topLevel = (n) => { const topLevel = (n) => {
if ((!n) || n === root) return null; if ((!n) || n === document.body) return null;
while (n.parentNode !== root) { while (n.parentNode !== document.body) {
n = n.parentNode; n = n.parentNode;
} }
return n; return n;
@ -3547,7 +3547,7 @@ function Ace2Inner(editorInfo, cssManagers) {
const innerdocbodyStyles = getComputedStyle(innerdocbody); const innerdocbodyStyles = getComputedStyle(innerdocbody);
const defaultLineHeight = parseInt(innerdocbodyStyles['line-height']); const defaultLineHeight = parseInt(innerdocbodyStyles['line-height']);
let docLine = doc.body.firstChild; let docLine = document.body.firstChild;
let currentLine = 0; let currentLine = 0;
let h = null; let h = null;
@ -3561,7 +3561,7 @@ function Ace2Inner(editorInfo, cssManagers) {
// included on the first line. The default stylesheet doesn't add // included on the first line. The default stylesheet doesn't add
// extra margins/padding, but plugins might. // extra margins/padding, but plugins might.
h = docLine.nextSibling.offsetTop - parseInt( h = docLine.nextSibling.offsetTop - parseInt(
window.getComputedStyle(doc.body) window.getComputedStyle(document.body)
.getPropertyValue('padding-top').split('px')[0]); .getPropertyValue('padding-top').split('px')[0]);
} else { } else {
h = docLine.nextSibling.offsetTop - docLine.offsetTop; h = docLine.nextSibling.offsetTop - docLine.offsetTop;
@ -3639,19 +3639,16 @@ function Ace2Inner(editorInfo, cssManagers) {
this.init = async () => { this.init = async () => {
await $.ready; await $.ready;
doc = document; // defined as a var in scope outside
inCallStack('setup', () => { inCallStack('setup', () => {
const body = doc.getElementById('innerdocbody'); if (browser.firefox) $(document.body).addClass('mozilla');
root = body; // defined as a var in scope outside if (browser.safari) $(document.body).addClass('safari');
if (browser.firefox) $(root).addClass('mozilla'); document.body.classList.toggle('authorColors', true);
if (browser.safari) $(root).addClass('safari'); document.body.classList.toggle('doesWrap', doesWrap);
root.classList.toggle('authorColors', true);
root.classList.toggle('doesWrap', doesWrap);
enforceEditability(); enforceEditability();
// set up dom and rep // set up dom and rep
while (root.firstChild) root.removeChild(root.firstChild); while (document.body.firstChild) document.body.removeChild(document.body.firstChild);
const oneEntry = createDomLineEntry(''); const oneEntry = createDomLineEntry('');
doRepLineSplice(0, rep.lines.length(), [oneEntry]); doRepLineSplice(0, rep.lines.length(), [oneEntry]);
insertDomLines(null, [oneEntry.domInfo]); insertDomLines(null, [oneEntry.domInfo]);