Skip to content

Commit

Permalink
Fix setStartOffset stopAtWordBoundary behavior to terminate at the be…
Browse files Browse the repository at this point in the history
…ginning of current div (#1782)

* Fix setStartOffset to terminate at the beginning of word, not right before it

* lint

* Fix tests

* Fix tests
  • Loading branch information
jamesmaa authored Jan 29, 2025
1 parent 0e63359 commit 65c7b5f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
34 changes: 16 additions & 18 deletions ext/js/dom/dom-text-scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export class DOMTextScanner {
let lastNode = /** @type {Node} */ (node);
let resetOffset = this._resetOffset;
let newlines = 0;
seekLoop:
while (node !== null) {
let enterable = false;
const nodeType = node.nodeType;
Expand All @@ -142,7 +143,7 @@ export class DOMTextScanner {
this._seekTextNodeBackward(/** @type {Text} */ (node), resetOffset);

if (!shouldContinueScanning) {
// Length reached
// Length reached or reached a word boundary
break;
}
} else if (nodeType === ELEMENT_NODE) {
Expand All @@ -154,8 +155,9 @@ export class DOMTextScanner {
const initialNodeAtBeginningOfNodeGoingBackwards = node === this._initialNode && this._offset === 0 && !forward;
const initialNodeAtEndOfNodeGoingForwards = node === this._initialNode && this._offset === node.childNodes.length && forward;
this._offset = 0;
const isInitialNode = node === this._initialNode;
({enterable, newlines} = DOMTextScanner.getElementSeekInfo(/** @type {Element} */ (node)));
if (newlines > this._newlines && generateLayoutContent) {
if (!isInitialNode && newlines > this._newlines && generateLayoutContent) {
this._newlines = newlines;
}
if (initialNodeAtBeginningOfNodeGoingBackwards || initialNodeAtEndOfNodeGoingForwards) {
Expand All @@ -173,6 +175,10 @@ export class DOMTextScanner {
if (newlines > this._newlines && generateLayoutContent) {
this._newlines = newlines;
}
if (newlines > 0 && this._stopAtWordBoundary && !forward) {
// Element nodes are considered word boundaries when scanning backwards
break seekLoop;
}
}

resetOffset = true;
Expand Down Expand Up @@ -285,14 +291,10 @@ export class DOMTextScanner {
case 2:
case 3:
if (this._newlines > 0) {
if (this._content.length > 0) {
const useNewlineCount = Math.min(this._remainder, this._newlines);
this._content += '\n'.repeat(useNewlineCount);
this._remainder -= useNewlineCount;
this._newlines -= useNewlineCount;
} else {
this._newlines = 0;
}
const useNewlineCount = Math.min(this._remainder, this._newlines);
this._content += '\n'.repeat(useNewlineCount);
this._remainder -= useNewlineCount;
this._newlines -= useNewlineCount;
this._lineHasContent = false;
this._lineHasWhitespace = false;
if (this._remainder <= 0) {
Expand Down Expand Up @@ -341,14 +343,10 @@ export class DOMTextScanner {
case 2:
case 3:
if (this._newlines > 0) {
if (this._content.length > 0) {
const useNewlineCount = Math.min(this._remainder, this._newlines);
this._content = '\n'.repeat(useNewlineCount) + this._content;
this._remainder -= useNewlineCount;
this._newlines -= useNewlineCount;
} else {
this._newlines = 0;
}
const useNewlineCount = Math.min(this._remainder, this._newlines);
this._content = '\n'.repeat(useNewlineCount) + this._content;
this._remainder -= useNewlineCount;
this._newlines -= useNewlineCount;
this._lineHasContent = false;
this._lineHasWhitespace = false;
if (this._remainder <= 0) {
Expand Down
4 changes: 2 additions & 2 deletions test/data/html/dom-text-scanner.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ <h1>DOMTextScanner Tests</h1>
data-test-data='{
"node": "div:nth-of-type(1)",
"offset": 0,
"length": 15,
"length": 16,
"expected": {
"node": "div:nth-of-type(2)>div::text",
"offset": 3,
"content": "小ぢん\nまり1\n小ぢん\nまり2"
"content": "\n小ぢん\nまり1\n小ぢん\nまり2"
}
}'
>
Expand Down

0 comments on commit 65c7b5f

Please sign in to comment.