diff options
Diffstat (limited to 'ext/js/accessibility/google-docs-util.js')
-rw-r--r-- | ext/js/accessibility/google-docs-util.js | 47 |
1 files changed, 38 insertions, 9 deletions
diff --git a/ext/js/accessibility/google-docs-util.js b/ext/js/accessibility/google-docs-util.js index e170f334..4321f082 100644 --- a/ext/js/accessibility/google-docs-util.js +++ b/ext/js/accessibility/google-docs-util.js @@ -17,7 +17,6 @@ */ import {DocumentUtil} from '../dom/document-util.js'; -import {TextSourceElement} from '../dom/text-source-element.js'; import {TextSourceRange} from '../dom/text-source-range.js'; /** @@ -29,8 +28,8 @@ export class GoogleDocsUtil { * Coordinates are provided in [client space](https://developer.mozilla.org/en-US/docs/Web/CSS/CSSOM_View/Coordinate_systems). * @param {number} x The x coordinate to search at. * @param {number} y The y coordinate to search at. - * @param {GetRangeFromPointOptions} options Options to configure how element detection is performed. - * @returns {?TextSourceRange|TextSourceElement} A range for the hovered text or element, or `null` if no applicable content was found. + * @param {import('document-util').GetRangeFromPointOptions} options Options to configure how element detection is performed. + * @returns {?TextSourceRange} A range for the hovered text or element, or `null` if no applicable content was found. */ static getRangeFromPoint(x, y, {normalizeCssZoom}) { const styleNode = this._getStyleNode(); @@ -46,10 +45,13 @@ export class GoogleDocsUtil { return null; } + /** + * @returns {HTMLStyleElement} + */ static _getStyleNode() { // This <style> node is necessary to force the SVG <rect> elements to have a fill, // which allows them to be included in document.elementsFromPoint's return value. - if (this._styleNode === null) { + if (typeof this._styleNode === 'undefined') { const style = document.createElement('style'); style.textContent = [ '.kix-canvas-tile-content{pointer-events:none!important;}', @@ -64,21 +66,34 @@ export class GoogleDocsUtil { return this._styleNode; } + /** + * @param {Element} element + * @param {string} text + * @param {number} x + * @param {number} y + * @param {boolean} normalizeCssZoom + * @returns {TextSourceRange} + */ static _createRange(element, text, x, y, normalizeCssZoom) { // Create imposter const content = document.createTextNode(text); const svgText = document.createElementNS('http://www.w3.org/2000/svg', 'text'); const transform = element.getAttribute('transform') || ''; const font = element.getAttribute('data-font-css') || ''; - svgText.setAttribute('x', element.getAttribute('x')); - svgText.setAttribute('y', element.getAttribute('y')); + const elementX = element.getAttribute('x'); + const elementY = element.getAttribute('y'); + if (typeof elementX === 'string') { svgText.setAttribute('x', elementX); } + if (typeof elementY === 'string') { svgText.setAttribute('y', elementY); } svgText.appendChild(content); const textStyle = svgText.style; this._setImportantStyle(textStyle, 'all', 'initial'); this._setImportantStyle(textStyle, 'transform', transform); this._setImportantStyle(textStyle, 'font', font); this._setImportantStyle(textStyle, 'text-anchor', 'start'); - element.parentNode.appendChild(svgText); + const {parentNode} = element; + if (parentNode !== null) { + parentNode.appendChild(svgText); + } // Adjust offset const elementRect = element.getBoundingClientRect(); @@ -93,6 +108,13 @@ export class GoogleDocsUtil { return TextSourceRange.createFromImposter(range, svgText, element); } + /** + * @param {Text} textNode + * @param {number} x + * @param {number} y + * @param {boolean} normalizeCssZoom + * @returns {Range} + */ static _getRangeWithPoint(textNode, x, y, normalizeCssZoom) { if (normalizeCssZoom) { const scale = DocumentUtil.computeZoomScale(textNode); @@ -101,7 +123,7 @@ export class GoogleDocsUtil { } const range = document.createRange(); let start = 0; - let end = textNode.nodeValue.length; + let end = /** @type {string} */ (textNode.nodeValue).length; while (end - start > 1) { const mid = Math.floor((start + end) / 2); range.setStart(textNode, mid); @@ -117,9 +139,16 @@ export class GoogleDocsUtil { return range; } + /** + * @param {CSSStyleDeclaration} style + * @param {string} propertyName + * @param {string} value + */ static _setImportantStyle(style, propertyName, value) { style.setProperty(propertyName, value, 'important'); } } + +/** @type {HTMLStyleElement|undefined} */ // eslint-disable-next-line no-underscore-dangle -GoogleDocsUtil._styleNode = null; +GoogleDocsUtil._styleNode = void 0; |