From 38b1117647541a98e32371e4be53667c0bfd6929 Mon Sep 17 00:00:00 2001 From: "Mingde (Matthew) Zeng" Date: Sat, 12 Dec 2020 21:21:48 -0500 Subject: [PATCH] browser: fix axs already defined error Signed-off-by: Mingde (Matthew) Zeng --- core/js/caret_browsing.js | 621 +++++++++++++++++++------------------- 1 file changed, 312 insertions(+), 309 deletions(-) diff --git a/core/js/caret_browsing.js b/core/js/caret_browsing.js index 67f46bb03..c0589f81a 100644 --- a/core/js/caret_browsing.js +++ b/core/js/caret_browsing.js @@ -2,12 +2,12 @@ /** * This file is derived from qutebrowser caret.js * https://github.com/qutebrowser/qutebrowser/blob/b44e3ba657e622cf813d0c072d6fe538fca9bf2a/qutebrowser/javascript/caret.js - * - * Copyright 2018-2020 Florian Bruhin (The Compiler) - * + * + * Copyright 2018-2020 Florian Bruhin (The Compiler) + * * Ported chrome-caretbrowsing extension. * https://cs.chromium.org/chromium/src/ui/accessibility/extensions/caretbrowsing/ - * + * */ // Copyright 2014 The Chromium Authors. All rights reserved. @@ -41,180 +41,183 @@ "use strict"; -const axs = {}; +try { + const axs = {}; -axs.dom = {}; + axs.dom = {}; -axs.color = {}; + axs.color = {}; -axs.utils = {}; + axs.utils = {}; -axs.dom.parentElement = function(node) { - if (!node) { - return null; - } - const composedNode = axs.dom.composedParentNode(node); - if (!composedNode) { - return null; - } - switch (composedNode.nodeType) { - case Node.ELEMENT_NODE: - return composedNode; - default: - return axs.dom.parentElement(composedNode); - } -}; - -axs.dom.shadowHost = function(node) { - if ("host" in node) { - return node.host; - } - return null; -}; + axs.dom.parentElement = function(node) { + if (!node) { + return null; + } + const composedNode = axs.dom.composedParentNode(node); + if (!composedNode) { + return null; + } + switch (composedNode.nodeType) { + case Node.ELEMENT_NODE: + return composedNode; + default: + return axs.dom.parentElement(composedNode); + } + }; -axs.dom.composedParentNode = function(node) { - if (!node) { + axs.dom.shadowHost = function(node) { + if ("host" in node) { + return node.host; + } return null; - } - if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) { - return axs.dom.shadowHost(node); - } - const parentNode = node.parentNode; - if (!parentNode) { + }; + + axs.dom.composedParentNode = function(node) { + if (!node) { + return null; + } + if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) { + return axs.dom.shadowHost(node); + } + const parentNode = node.parentNode; + if (!parentNode) { + return null; + } + if (parentNode.nodeType === Node.DOCUMENT_FRAGMENT_NODE) { + return axs.dom.shadowHost(parentNode); + } + if (!parentNode.shadowRoot) { + return parentNode; + } + const points = node.getDestinationInsertionPoints(); + if (points.length > 0) { + return axs.dom.composedParentNode(points[points.length - 1]); + } return null; - } - if (parentNode.nodeType === Node.DOCUMENT_FRAGMENT_NODE) { - return axs.dom.shadowHost(parentNode); - } - if (!parentNode.shadowRoot) { - return parentNode; - } - const points = node.getDestinationInsertionPoints(); - if (points.length > 0) { - return axs.dom.composedParentNode(points[points.length - 1]); - } - return null; -}; + }; -axs.color.Color = function(red, green, blue, alpha) { // eslint-disable-line max-params,max-len - this.red = red; - this.green = green; - this.blue = blue; - this.alpha = alpha; -}; + axs.color.Color = function(red, green, blue, alpha) { // eslint-disable-line max-params,max-len + this.red = red; + this.green = green; + this.blue = blue; + this.alpha = alpha; + }; -axs.color.parseColor = function(colorText) { - if (colorText === "transparent") { - return new axs.color.Color(0, 0, 0, 0); - } - let match = colorText.match(/^rgb\((\d+), (\d+), (\d+)\)$/); - if (match) { - const blue = parseInt(match[3], 10); - const green = parseInt(match[2], 10); - const red = parseInt(match[1], 10); - return new axs.color.Color(red, green, blue, 1); - } - match = colorText.match(/^rgba\((\d+), (\d+), (\d+), (\d*(\.\d+)?)\)/); - if (match) { - const red = parseInt(match[1], 10); - const green = parseInt(match[2], 10); - const blue = parseInt(match[3], 10); - const alpha = parseFloat(match[4]); - return new axs.color.Color(red, green, blue, alpha); - } - return null; -}; + axs.color.parseColor = function(colorText) { + if (colorText === "transparent") { + return new axs.color.Color(0, 0, 0, 0); + } + let match = colorText.match(/^rgb\((\d+), (\d+), (\d+)\)$/); + if (match) { + const blue = parseInt(match[3], 10); + const green = parseInt(match[2], 10); + const red = parseInt(match[1], 10); + return new axs.color.Color(red, green, blue, 1); + } + match = colorText.match(/^rgba\((\d+), (\d+), (\d+), (\d*(\.\d+)?)\)/); + if (match) { + const red = parseInt(match[1], 10); + const green = parseInt(match[2], 10); + const blue = parseInt(match[3], 10); + const alpha = parseFloat(match[4]); + return new axs.color.Color(red, green, blue, alpha); + } + return null; + }; -axs.color.flattenColors = function(color1, color2) { - const colorAlpha = color1.alpha; - return new axs.color.Color( - ((1 - colorAlpha) * color2.red) + (colorAlpha * color1.red), - ((1 - colorAlpha) * color2.green) + (colorAlpha * color1.green), - ((1 - colorAlpha) * color2.blue) + (colorAlpha * color2.blue), - color1.alpha + (color2.alpha * (1 - color1.alpha))); -}; + axs.color.flattenColors = function(color1, color2) { + const colorAlpha = color1.alpha; + return new axs.color.Color( + ((1 - colorAlpha) * color2.red) + (colorAlpha * color1.red), + ((1 - colorAlpha) * color2.green) + (colorAlpha * color1.green), + ((1 - colorAlpha) * color2.blue) + (colorAlpha * color2.blue), + color1.alpha + (color2.alpha * (1 - color1.alpha))); + }; -axs.utils.getParentBgColor = function(_el) { - let el = _el; - let el2 = el; - let iter = null; - el = []; - for (iter = null; (el2 = axs.dom.parentElement(el2));) { - const style = window.getComputedStyle(el2, null); - if (style) { - const color = axs.color.parseColor(style.backgroundColor); - if (color && - (style.opacity < 1 && - (color.alpha *= style.opacity), - color.alpha !== 0 && - (el.push(color), color.alpha === 1))) { - iter = !0; - break; + axs.utils.getParentBgColor = function(_el) { + let el = _el; + let el2 = el; + let iter = null; + el = []; + for (iter = null; (el2 = axs.dom.parentElement(el2));) { + const style = window.getComputedStyle(el2, null); + if (style) { + const color = axs.color.parseColor(style.backgroundColor); + if (color && + (style.opacity < 1 && + (color.alpha *= style.opacity), + color.alpha !== 0 && + (el.push(color), color.alpha === 1))) { + iter = !0; + break; + } } } - } - if (!iter) { - el.push(new axs.color.Color(255, 255, 255, 1)); - } - for (el2 = el.pop(); el.length;) { - iter = el.pop(); - el2 = axs.color.flattenColors(iter, el2); - } - return el2; -}; + if (!iter) { + el.push(new axs.color.Color(255, 255, 255, 1)); + } + for (el2 = el.pop(); el.length;) { + iter = el.pop(); + el2 = axs.color.flattenColors(iter, el2); + } + return el2; + }; -axs.utils.getFgColor = function(el, el2, color) { - let color2 = axs.color.parseColor(el.color); - if (!color2) { - return null; - } - if (color2.alpha < 1) { - color2 = axs.color.flattenColors(color2, color); - } - if (el.opacity < 1) { - const el3 = axs.utils.getParentBgColor(el2); - color2.alpha *= el.opacity; - color2 = axs.color.flattenColors(color2, el3); - } - return color2; -}; + axs.utils.getFgColor = function(el, el2, color) { + let color2 = axs.color.parseColor(el.color); + if (!color2) { + return null; + } + if (color2.alpha < 1) { + color2 = axs.color.flattenColors(color2, color); + } + if (el.opacity < 1) { + const el3 = axs.utils.getParentBgColor(el2); + color2.alpha *= el.opacity; + color2 = axs.color.flattenColors(color2, el3); + } + return color2; + }; -axs.utils.getBgColor = function(el, elParent) { - let color = axs.color.parseColor(el.backgroundColor); - if (!color) { - return null; - } - if (el.opacity < 1) { - color.alpha *= el.opacity; - } - if (color.alpha < 1) { - const bgColor = axs.utils.getParentBgColor(elParent); - if (bgColor === null) { + axs.utils.getBgColor = function(el, elParent) { + let color = axs.color.parseColor(el.backgroundColor); + if (!color) { return null; } - color = axs.color.flattenColors(color, bgColor); - } - return color; -}; + if (el.opacity < 1) { + color.alpha *= el.opacity; + } + if (color.alpha < 1) { + const bgColor = axs.utils.getParentBgColor(elParent); + if (bgColor === null) { + return null; + } + color = axs.color.flattenColors(color, bgColor); + } + return color; + }; -axs.color.colorChannelToString = function(_color) { - const color = Math.round(_color); - if (color < 15) { - return `0${color.toString(16)}`; - } - return color.toString(16); -}; + axs.color.colorChannelToString = function(_color) { + const color = Math.round(_color); + if (color < 15) { + return `0${color.toString(16)}`; + } + return color.toString(16); + }; -axs.color.colorToString = function(color) { - if (color.alpha === 1) { - const red = axs.color.colorChannelToString(color.red); - const green = axs.color.colorChannelToString(color.green); - const blue = axs.color.colorChannelToString(color.blue); - return `#${red}${green}${blue}`; - } - const arr = [color.red, color.green, color.blue, color.alpha].join(); - return `rgba(${arr})`; -}; + axs.color.colorToString = function(color) { + if (color.alpha === 1) { + const red = axs.color.colorChannelToString(color.red); + const green = axs.color.colorChannelToString(color.green); + const blue = axs.color.colorChannelToString(color.blue); + return `#${red}${green}${blue}`; + } + const arr = [color.red, color.green, color.blue, color.alpha].join(); + return `rgba(${arr})`; + }; +} catch (e) { +} const Cursor = function(node, index, text) { // eslint-disable-line func-style,max-len this.node = node; @@ -278,8 +281,8 @@ TraverseUtil.forwardsChar = function(cursor, nodesCrossed) { // eslint-disable-l let childNode = null; if (!TraverseUtil.treatAsLeafNode(cursor.node)) { for (let i = cursor.index; - i < cursor.node.childNodes.length; - i++) { + i < cursor.node.childNodes.length; + i++) { const node = cursor.node.childNodes[i]; if (TraverseUtil.isSkipped(node)) { nodesCrossed.push(node); @@ -304,8 +307,8 @@ TraverseUtil.forwardsChar = function(cursor, nodesCrossed) { // eslint-disable-l while (cursor.node !== null) { let siblingNode = null; for (let node = cursor.node.nextSibling; - node !== null; - node = node.nextSibling) { + node !== null; + node = node.nextSibling) { if (TraverseUtil.isSkipped(node)) { nodesCrossed.push(node); } else if (TraverseUtil.isVisible(node)) { @@ -350,7 +353,7 @@ TraverseUtil.getNextChar = function( // eslint-disable-line max-params const initialWhitespace = TraverseUtil.isWhitespace(fChar); while ((TraverseUtil.isWhitespace(fChar)) || - (TraverseUtil.isSkipped(endCursor.node))) { + (TraverseUtil.isSkipped(endCursor.node))) { fChar = TraverseUtil.forwardsChar(endCursor, nodesCrossed); if (fChar === null) { return null; @@ -413,8 +416,8 @@ TraverseUtil.backwardsChar = function(cursor, nodesCrossed) { // Try to move to the previous sibling. var siblingNode = null; for (var node = cursor.node.previousSibling; - node != null; - node = node.previousSibling) { + node != null; + node = node.previousSibling) { if (TraverseUtil.isSkipped(node)) { nodesCrossed.push(node); continue; @@ -450,88 +453,88 @@ TraverseUtil.backwardsChar = function(cursor, nodesCrossed) { }; TraverseUtil.getNextWord = function(startCursor, endCursor, - nodesCrossed) { - -// Find the first non-whitespace or non-skipped character. -var cursor = endCursor.clone(); -var c = TraverseUtil.forwardsChar(cursor, nodesCrossed); -if (c == null) - return null; -while ((TraverseUtil.isWhitespace(c)) || - (TraverseUtil.isSkipped(cursor.node))) { - c = TraverseUtil.forwardsChar(cursor, nodesCrossed); + nodesCrossed) { + + // Find the first non-whitespace or non-skipped character. + var cursor = endCursor.clone(); + var c = TraverseUtil.forwardsChar(cursor, nodesCrossed); if (c == null) - return null; -} + return null; + while ((TraverseUtil.isWhitespace(c)) || + (TraverseUtil.isSkipped(cursor.node))) { + c = TraverseUtil.forwardsChar(cursor, nodesCrossed); + if (c == null) + return null; + } -// Set startCursor to the position immediately before the first -// character in our word. It's safe to decrement |index| because -// forwardsChar guarantees that the cursor will be immediately to the -// right of the returned character on exit. -startCursor.copyFrom(cursor); -startCursor.index--; - -// Keep building up our word until we reach a whitespace character or -// would cross a tag. Don't actually return any tags crossed, because this -// word goes up until the tag boundary but not past it. -endCursor.copyFrom(cursor); -var word = c; -var newNodesCrossed = []; -c = TraverseUtil.forwardsChar(cursor, newNodesCrossed); -if (c == null) { - return word; -} -while (!TraverseUtil.isWhitespace(c) && - newNodesCrossed.length == 0) { - word += c; + // Set startCursor to the position immediately before the first + // character in our word. It's safe to decrement |index| because + // forwardsChar guarantees that the cursor will be immediately to the + // right of the returned character on exit. + startCursor.copyFrom(cursor); + startCursor.index--; + + // Keep building up our word until we reach a whitespace character or + // would cross a tag. Don't actually return any tags crossed, because this + // word goes up until the tag boundary but not past it. endCursor.copyFrom(cursor); + var word = c; + var newNodesCrossed = []; c = TraverseUtil.forwardsChar(cursor, newNodesCrossed); if (c == null) { - return word; + return word; } -} -return word; + while (!TraverseUtil.isWhitespace(c) && + newNodesCrossed.length == 0) { + word += c; + endCursor.copyFrom(cursor); + c = TraverseUtil.forwardsChar(cursor, newNodesCrossed); + if (c == null) { + return word; + } + } + return word; }; TraverseUtil.getPreviousWord = function(startCursor, endCursor, - nodesCrossed) { -// Find the first non-whitespace or non-skipped character. -var cursor = startCursor.clone(); -var c = TraverseUtil.backwardsChar(cursor, nodesCrossed); -if (c == null) - return null; -while ((TraverseUtil.isWhitespace(c) || - (TraverseUtil.isSkipped(cursor.node)))) { - c = TraverseUtil.backwardsChar(cursor, nodesCrossed); + nodesCrossed) { + // Find the first non-whitespace or non-skipped character. + var cursor = startCursor.clone(); + var c = TraverseUtil.backwardsChar(cursor, nodesCrossed); if (c == null) - return null; -} + return null; + while ((TraverseUtil.isWhitespace(c) || + (TraverseUtil.isSkipped(cursor.node)))) { + c = TraverseUtil.backwardsChar(cursor, nodesCrossed); + if (c == null) + return null; + } -// Set endCursor to the position immediately after the first -// character we've found (the last character of the word, since we're -// searching backwards). -endCursor.copyFrom(cursor); -endCursor.index++; - -// Keep building up our word until we reach a whitespace character or -// would cross a tag. Don't actually return any tags crossed, because this -// word goes up until the tag boundary but not past it. -startCursor.copyFrom(cursor); -var word = c; -var newNodesCrossed = []; -c = TraverseUtil.backwardsChar(cursor, newNodesCrossed); -if (c == null) - return word; -while (!TraverseUtil.isWhitespace(c) && - newNodesCrossed.length == 0) { - word = c + word; + // Set endCursor to the position immediately after the first + // character we've found (the last character of the word, since we're + // searching backwards). + endCursor.copyFrom(cursor); + endCursor.index++; + + // Keep building up our word until we reach a whitespace character or + // would cross a tag. Don't actually return any tags crossed, because this + // word goes up until the tag boundary but not past it. startCursor.copyFrom(cursor); + var word = c; + var newNodesCrossed = []; c = TraverseUtil.backwardsChar(cursor, newNodesCrossed); if (c == null) - return word; -} + return word; + while (!TraverseUtil.isWhitespace(c) && + newNodesCrossed.length == 0) { + word = c + word; + startCursor.copyFrom(cursor); + c = TraverseUtil.backwardsChar(cursor, newNodesCrossed); + if (c == null) + return word; + } -return word; + return word; }; const CaretBrowsing = {}; @@ -614,50 +617,50 @@ CaretBrowsing.isControlThatNeedsArrowKeys = function(node) { // eslint-disable-l if (node.constructor === HTMLInputElement) { switch (node.type) { // eslint-disable-line default-case - case "email": - case "number": - case "password": - case "search": - case "text": - case "tel": - case "url": - case "": - return true; - case "datetime": - case "datetime-local": - case "date": - case "month": - case "radio": - case "range": - case "week": - return true; + case "email": + case "number": + case "password": + case "search": + case "text": + case "tel": + case "url": + case "": + return true; + case "datetime": + case "datetime-local": + case "date": + case "month": + case "radio": + case "range": + case "week": + return true; } } if (node.getAttribute && CaretBrowsing.isFocusable(node)) { const role = node.getAttribute("role"); switch (role) { // eslint-disable-line default-case - case "combobox": - case "grid": - case "gridcell": - case "listbox": - case "menu": - case "menubar": - case "menuitem": - case "menuitemcheckbox": - case "menuitemradio": - case "option": - case "radiogroup": - case "scrollbar": - case "slider": - case "spinbutton": - case "tab": - case "tablist": - case "textbox": - case "tree": - case "treegrid": - case "treeitem": - return true; + case "combobox": + case "grid": + case "gridcell": + case "listbox": + case "menu": + case "menubar": + case "menuitem": + case "menuitemcheckbox": + case "menuitemradio": + case "option": + case "radiogroup": + case "scrollbar": + case "slider": + case "spinbutton": + case "tab": + case "tablist": + case "textbox": + case "tree": + case "treegrid": + case "treeitem": + return true; } } @@ -666,27 +669,27 @@ CaretBrowsing.isControlThatNeedsArrowKeys = function(node) { // eslint-disable-l CaretBrowsing.injectCaretStyles = function() { const style = ".CaretBrowsing_Caret {" + - " position: absolute;" + - " z-index: 2147483647;" + - " min-height: 10px;" + - " background-color: %1;" + - "}" + - ".CaretBrowsing_AnimateCaret {" + - " position: absolute;" + - " z-index: 2147483647;" + - " min-height: 10px;" + - "}" + - ".CaretBrowsing_FlashVert {" + - " position: absolute;" + - " z-index: 2147483647;" + - " background: linear-gradient(" + - " 270deg," + - " rgba(128, 128, 255, 0) 0%," + - " rgba(128, 128, 255, 0.3) 45%," + - " rgba(128, 128, 255, 0.8) 50%," + - " rgba(128, 128, 255, 0.3) 65%," + - " rgba(128, 128, 255, 0) 100%);" + - "}"; + " position: absolute;" + + " z-index: 2147483647;" + + " min-height: 10px;" + + " background-color: %1;" + + "}" + + ".CaretBrowsing_AnimateCaret {" + + " position: absolute;" + + " z-index: 2147483647;" + + " min-height: 10px;" + + "}" + + ".CaretBrowsing_FlashVert {" + + " position: absolute;" + + " z-index: 2147483647;" + + " background: linear-gradient(" + + " 270deg," + + " rgba(128, 128, 255, 0) 0%," + + " rgba(128, 128, 255, 0.3) 45%," + + " rgba(128, 128, 255, 0.8) 50%," + + " rgba(128, 128, 255, 0.3) 65%," + + " rgba(128, 128, 255, 0) 100%);" + + "}"; const node = document.createElement("style"); node.innerHTML = style; document.body.appendChild(node); @@ -875,7 +878,7 @@ CaretBrowsing.getCursorRect = function(cursor) { // eslint-disable-line max-stat CaretBrowsing.updateCaretOrSelection = function(scrollToSelection) { // eslint-disable-line max-statements - const previousX = CaretBrowsing.caretX; + const previousX = CaretBrowsing.caretX; const previousY = CaretBrowsing.caretY; const sel = window.getSelection(); @@ -910,7 +913,7 @@ CaretBrowsing.updateCaretOrSelection = } CaretBrowsing.isSelectionCollapsed = false; } else if (range.startOffset !== range.endOffset || - range.startContainer !== range.endContainer) { + range.startContainer !== range.endContainer) { const rect = range.getBoundingClientRect(); if (!rect) { return; @@ -923,8 +926,8 @@ CaretBrowsing.updateCaretOrSelection = } else { const rect = CaretBrowsing.getCursorRect( new Cursor(range.startContainer, - range.startOffset, - TraverseUtil.getNodeText(range.startContainer))); + range.startOffset, + TraverseUtil.getNodeText(range.startContainer))); CaretBrowsing.caretX = rect.left; CaretBrowsing.caretY = rect.top; CaretBrowsing.caretWidth = rect.width; @@ -957,7 +960,7 @@ CaretBrowsing.updateCaretOrSelection = if (scrollToSelection) { const rect = CaretBrowsing.getCursorRect( new Cursor(sel.focusNode, sel.focusOffset, - TraverseUtil.getNodeText(sel.focusNode))); + TraverseUtil.getNodeText(sel.focusNode))); const yscroll = window.pageYOffset; const pageHeight = window.innerHeight; @@ -1031,7 +1034,7 @@ CaretBrowsing.updateIsCaretVisible = function() { CaretBrowsing.caretBlinkFunction, 500); } } else if (!CaretBrowsing.isCaretVisible && - CaretBrowsing.caretElement) { + CaretBrowsing.caretElement) { window.clearInterval(CaretBrowsing.blinkFunctionId); if (CaretBrowsing.caretElement) { CaretBrowsing.isSelectionCollapsed = false; @@ -1097,13 +1100,13 @@ CaretBrowsing.toggleMark = function() { }; CaretBrowsing.rotateSelection = function() { - var selection = window.getSelection(); - var pos = [selection.anchorNode, selection.anchorOffset]; - selection.collapse(selection.focusNode, selection.focusOffset); - selection.extend(pos[0], pos[1]); - window.setTimeout(() => { - CaretBrowsing.updateCaretOrSelection(true); - }, 0); + var selection = window.getSelection(); + var pos = [selection.anchorNode, selection.anchorOffset]; + selection.collapse(selection.focusNode, selection.focusOffset); + selection.extend(pos[0], pos[1]); + window.setTimeout(() => { + CaretBrowsing.updateCaretOrSelection(true); + }, 0); } CaretBrowsing.cutSelection = function() { @@ -1124,14 +1127,14 @@ CaretBrowsing.post_message_down = function(message_name, arg) { if (self !== top) { register_message_handler("CaretBrowsing.setInitialCursor", - CaretBrowsing.setInitialCursor); + CaretBrowsing.setInitialCursor); register_message_handler("CaretBrowsing.shutdown", - CaretBrowsing.shutdown); + CaretBrowsing.shutdown); register_message_handler("CaretBrowsing.move", function (args) { CaretBrowsing.move(args[0], args[1]); }); register_message_handler("CaretBrowsing.toggleMark", - CaretBrowsing.toggleMark); + CaretBrowsing.toggleMark); register_message_handler("CaretBrowsing.cutSelection", - CaretBrowsing.cutSelection); + CaretBrowsing.cutSelection); }