Skip to content

Commit 6fa8622

Browse files
thePunderWomanAndrewKushnir
authored andcommitted
refactor(core): replace regexp in incremental hydration (#59158)
This swaps the regular expression for a string split instead. PR Close #59158
1 parent c9b8319 commit 6fa8622

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

packages/core/src/hydration/node_lookup_utils.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {getFirstNativeNode} from '../render3/node_manipulation';
1919
import {ɵɵresolveBody} from '../render3/util/misc_utils';
2020
import {renderStringify} from '../render3/util/stringify_utils';
2121
import {getNativeByTNode, unwrapRNode} from '../render3/util/view_utils';
22-
import {assertDefined} from '../util/assert';
22+
import {assertDefined, assertEqual} from '../util/assert';
2323

2424
import {compressNodeLocation, decompressNodeLocation} from './compression';
2525
import {
@@ -408,11 +408,19 @@ export function gatherDeferBlocksCommentNodes(
408408

409409
const nodesByBlockId = new Map<string, Comment>();
410410
while ((currentNode = commentNodesIterator.nextNode() as Comment)) {
411-
// TODO(incremental-hydration: convert this to use string parsing rather than regex
412-
const regex = new RegExp(/^\s*ngh=(d[0-9]+)/g);
413-
const result = regex.exec(currentNode?.textContent ?? '');
414-
if (result && result?.length > 0) {
415-
nodesByBlockId.set(result[1], currentNode);
411+
const nghPattern = 'ngh=';
412+
const content = currentNode?.textContent;
413+
const nghIdx = content?.indexOf(nghPattern) ?? -1;
414+
if (nghIdx > -1) {
415+
const nghValue = content!.substring(nghIdx + nghPattern.length).trim();
416+
// Make sure the value has an expected format.
417+
ngDevMode &&
418+
assertEqual(
419+
nghValue.startsWith('d'),
420+
true,
421+
'Invalid defer block id found in a comment node.',
422+
);
423+
nodesByBlockId.set(nghValue, currentNode);
416424
}
417425
}
418426
return nodesByBlockId;

0 commit comments

Comments
 (0)