diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2022-09-25 09:37:33 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-25 09:37:33 -0400 |
commit | 75d30594511a6593044565829ad55369fecaf4cd (patch) | |
tree | 7908a9ffc211f9d8b9342e98bd143a7dc09b500d /ext/js/dom/document-util.js | |
parent | 8240482e9d0b6da7a996bc581c9b5304ebcf22aa (diff) |
TextSource* API updates (#2236)
* Move TextSourceRange static functions to DocumentUtil
getWritingMode is also simplified
* Update Google Docs range to be empty to match other range sources
* Rename imposterContainer to imposterElement
* Add static creation functions
* Add static creation function
* Remove unused collapse function
* Don't select imposter elements
* Refactor setEndOffset
* Adjust argument order for setEndOffset
* Update TextSourceRange constructor
* Remove unused isConnected
* Cache rects
* Fix test
* Remove unused getRect
* Revert "Fix test"
* Remove cachedRect
* Use the source element rect to handle scroll differences
* Writing mode update
* Remove _cachedRects update
This shouldn't be necessary as the imposter is usually detached
almost immediately after scanning, giving no time for the window
to be resized or scrolled.
Diffstat (limited to 'ext/js/dom/document-util.js')
-rw-r--r-- | ext/js/dom/document-util.js | 59 |
1 files changed, 56 insertions, 3 deletions
diff --git a/ext/js/dom/document-util.js b/ext/js/dom/document-util.js index 76f551c7..73525ff5 100644 --- a/ext/js/dom/document-util.js +++ b/ext/js/dom/document-util.js @@ -66,7 +66,7 @@ class DocumentUtil { case 'IMG': case 'BUTTON': case 'SELECT': - return new TextSourceElement(element); + return TextSourceElement.create(element); case 'INPUT': if (element.type === 'text') { imposterSourceElement = element; @@ -85,8 +85,9 @@ class DocumentUtil { if (imposter !== null) { this._setImposterStyle(imposterContainer.style, 'z-index', '-2147483646'); this._setImposterStyle(imposter.style, 'pointer-events', 'none'); + return TextSourceRange.createFromImposter(range, imposterContainer, imposterSourceElement); } - return new TextSourceRange(range, '', imposterContainer, imposterSourceElement); + return TextSourceRange.create(range); } else { if (imposterContainer !== null) { imposterContainer.parentNode.removeChild(imposterContainer); @@ -131,7 +132,7 @@ class DocumentUtil { // Scan text source = source.clone(); const startLength = source.setStartOffset(extent, layoutAwareScan); - const endLength = source.setEndOffset(extent * 2 - startLength, layoutAwareScan, true); + const endLength = source.setEndOffset(extent * 2 - startLength, true, layoutAwareScan); const text = source.text(); const textLength = text.length; const textEndAnchor = textLength - endLength; @@ -418,6 +419,58 @@ class DocumentUtil { } } + /** + * Offsets an array of DOMRects by a given amount. + * @param {DOMRect[]} rects The DOMRects to offset. + * @param {number} x The horizontal offset amount. + * @param {number} y The vertical offset amount. + * @returns {DOMRect} The DOMRects with the offset applied. + */ + static offsetDOMRects(rects, x, y) { + const results = []; + for (const rect of rects) { + results.push(new DOMRect(rect.left + x, rect.top + y, rect.width, rect.height)); + } + return results; + } + + /** + * Gets the parent writing mode of an element. + * See: https://developer.mozilla.org/en-US/docs/Web/CSS/writing-mode. + * @param {Element} element The HTML element to check. + * @returns {string} The writing mode. + */ + static getElementWritingMode(element) { + if (element !== null) { + const {writingMode} = getComputedStyle(element); + if (typeof writingMode === 'string') { + return this.normalizeWritingMode(writingMode); + } + } + return 'horizontal-tb'; + } + + /** + * Normalizes a CSS writing mode value by converting non-standard and deprecated values + * into their corresponding standard vaules. + * @param {string} writingMode The writing mode to normalize. + * @returns {string} The normalized writing mode. + */ + static normalizeWritingMode(writingMode) { + switch (writingMode) { + case 'lr': + case 'lr-tb': + case 'rl': + return 'horizontal-tb'; + case 'tb': + return 'vertical-lr'; + case 'tb-rl': + return 'vertical-rl'; + default: + return writingMode; + } + } + // Private static _getActiveButtons(event, array) { |