aboutsummaryrefslogtreecommitdiff
path: root/ext/js/accessibility/google-docs-util.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2023-11-27 12:48:14 -0500
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2023-11-27 12:48:14 -0500
commit4da4827bcbcdd1ef163f635d9b29416ff272b0bb (patch)
treea8a0f1a8befdb78a554e1be91f2c6059ca3ad5f9 /ext/js/accessibility/google-docs-util.js
parentfd6bba8a2a869eaf2b2c1fa49001f933fce3c618 (diff)
Add JSDoc type annotations to project (rebased)
Diffstat (limited to 'ext/js/accessibility/google-docs-util.js')
-rw-r--r--ext/js/accessibility/google-docs-util.js46
1 files changed, 38 insertions, 8 deletions
diff --git a/ext/js/accessibility/google-docs-util.js b/ext/js/accessibility/google-docs-util.js
index e170f334..9db45cc1 100644
--- a/ext/js/accessibility/google-docs-util.js
+++ b/ext/js/accessibility/google-docs-util.js
@@ -29,8 +29,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 +46,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 +67,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 +109,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 +124,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 +140,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;