I'm experiencing Lightning Web Components Range proxy behavior that is not expected, on API 55.
Creating a new Javascript Range object in a Locker Component:
// <div class="container" lwc:dom="manual"></div>
renderedCallback() {
const container = this.template.querySelector('.container');
container.innerHTML = '<div>Hello World</div>';
const div = container.querySelector('div');
const range1 = document.createRange();
range1.setStart(div, 0);
range1.setEnd(div, 1);
console.log('div: ', div); // div: Proxy {}
console.log('range1: ', range1); // range1: n {}
console.log('range1: ', range1.toString()); // range1: [object Object]
console.log('range1: ', JSON.stringify(range1)); // range1: {}
}
Instead of returning the string of the text range, it inherits the default Object.toString method and returns [object Object]
Another piece of range functionality that I want to use is checking if 2 ranges intersect.
export function firstRangeIntersect(rangeOne, rangeTwo) {
const {
startContainer,
startOffset,
endContainer,
endOffset,
} = rangeTwo;
return rangeOne.isPointInRange(startContainer, startOffset) ||
rangeOne.isPointInRange(endContainer, endOffset);
}
TypeError: Failed to execute 'isPointInRange' on 'Range': parameter 1 is not of type 'Node'.
at Proxy.c (aura_prod.js:65:119975)
I believe the error here is that range.startContainer and range.endContainer return proxy objects instead of DOM Nodes, but isPointInRange handler for the range proxy still expects a Node.
[1] https://developer.salesforce.com/docs/component-library/tools/locker-service-viewer
[2] https://developer.mozilla.org/en-US/docs/Web/API/Range
If there is a suggested way to use ranges or a workaround please let me know!
Update: There seems to be 2 representations for range objects:
Type 1.
- prints to console as
Proxy {}
- does not implement
range.isPointInRange
- implements
range.toString
Type 2.
- prints to console as
n {}
- implements
range.isPointInRange
- does not implement
range.toString
(returns[object Object]
) document.createRange
mostly creates this kind of range for me, after a while they seem to turn into Type 1 ranges somehow.
Theres a simple path forward for me to recreate a range with document.createRange to use isPointInRange
, I just need to figure out what deterministic operation turns a freshly create range into a "Proxy".