Skip to content

Commit 0470106

Browse files
author
Anatoly Ostrovsky
committed
Additional noderef optimizations
1 parent cb988ad commit 0470106

File tree

4 files changed

+51
-48
lines changed

4 files changed

+51
-48
lines changed

@types/shared/noderef.d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,24 @@ export class NodeRef {
3636
/** @returns {number} */
3737
get size(): number;
3838
/** @returns {Element | Node | ChildNode} */
39-
getAny(): Element | Node | ChildNode;
39+
_getAny(): Element | Node | ChildNode;
4040
/** @returns {Element | Array<Node> | Node | ChildNode} */
41-
getAll(): Element | Array<Node> | Node | ChildNode;
41+
_getAll(): Element | Array<Node> | Node | ChildNode;
4242
/** @returns {Array<Element> | Array<Node>} */
43-
collection(): Array<Element> | Array<Node>;
43+
_collection(): Array<Element> | Array<Node>;
4444
/**
4545
* @param {number} index
4646
* @returns {Element | Node | ChildNode}
4747
*/
48-
getIndex(index: number): Element | Node | ChildNode;
48+
_getIndex(index: number): Element | Node | ChildNode;
4949
/**
5050
* @param {number} index
5151
* @param {Element | Node | ChildNode} node
5252
*/
53-
setIndex(index: number, node: Element | Node | ChildNode): void;
53+
_setIndex(index: number, node: Element | Node | ChildNode): void;
5454
/**
5555
* @returns {NodeRef}
5656
*/
57-
clone(): NodeRef;
58-
isElement(): boolean;
57+
_clone(): NodeRef;
58+
_isElement(): boolean;
5959
}

src/core/compile/compile.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,8 @@ export class CompileProvider {
678678
assertArg(scope, "scope");
679679

680680
// could be empty nodelist
681-
if (nodeRef.getAny()) {
682-
setScope(nodeRef.getAny(), scope);
681+
if (nodeRef._getAny()) {
682+
setScope(nodeRef._getAny(), scope);
683683
}
684684

685685
if (
@@ -732,7 +732,7 @@ export class CompileProvider {
732732

733733
$linkNode = new NodeRef(wrappedTemplate[0]);
734734
} else if (cloneConnectFn) {
735-
$linkNode = nodeRef.clone();
735+
$linkNode = nodeRef._clone();
736736
} else {
737737
$linkNode = nodeRef;
738738
}
@@ -760,7 +760,7 @@ export class CompileProvider {
760760
nodeRef = compositeLinkFn = null;
761761
}
762762

763-
return $linkNode.getAll();
763+
return $linkNode._getAll();
764764
}
765765
}
766766

@@ -815,7 +815,7 @@ export class CompileProvider {
815815
const attrs = new Attributes($animate, $exceptionHandler, $sce);
816816

817817
const directives = collectDirectives(
818-
/** @type Element */ (nodeRefList.getIndex(i)),
818+
/** @type Element */ (nodeRefList._getIndex(i)),
819819
attrs,
820820
i === 0 ? maxPriority : undefined,
821821
ignoreDirective,
@@ -827,7 +827,7 @@ export class CompileProvider {
827827
if (directives.length) {
828828
nodeLinkFnCtx = applyDirectivesToNode(
829829
directives,
830-
nodeRefList.getIndex(i),
830+
nodeRefList._getIndex(i),
831831
attrs,
832832
transcludeFn,
833833
null,
@@ -845,7 +845,7 @@ export class CompileProvider {
845845

846846
const nodeLinkFn = nodeLinkFnCtx?.nodeLinkFn;
847847

848-
const { childNodes } = nodeRefList.getIndex(i);
848+
const { childNodes } = nodeRefList._getIndex(i);
849849

850850
if (
851851
(nodeLinkFn && nodeLinkFnCtx.terminal) ||
@@ -908,7 +908,7 @@ export class CompileProvider {
908908
? nodeRef.nodes[idx]
909909
: nodeRef.node;
910910
} else {
911-
if (nodeRefList.getIndex(idx)) {
911+
if (nodeRefList._getIndex(idx)) {
912912
stableNodeList[idx] = nodeRef.nodes[idx];
913913
}
914914
}
@@ -1771,15 +1771,15 @@ export class CompileProvider {
17711771
compileNode = compileNodeRef.node;
17721772
ctxNodeRef.node = compileNode;
17731773
replaceWith(
1774-
new NodeRef($template.getAny()),
1774+
new NodeRef($template._getAny()),
17751775
compileNode,
17761776
index,
17771777
);
17781778

17791779
// @ts-ignore
17801780
childTranscludeFn = compilationGenerator(
17811781
mightHaveMultipleTransclusionError,
1782-
$template.getAny(),
1782+
$template._getAny(),
17831783
transcludeFn,
17841784
terminalPriority,
17851785
replaceDirective && replaceDirective.name,
@@ -1950,7 +1950,10 @@ export class CompileProvider {
19501950
replaceWith(compileNodeRef, compileNode);
19511951

19521952
if (parentNodeRef) {
1953-
parentNodeRef.setIndex(index, compileNode);
1953+
/** @type {NodeRef} */ (parentNodeRef)._setIndex(
1954+
index,
1955+
compileNode,
1956+
);
19541957
}
19551958

19561959
const newTemplateAttrs = { $attr: {} };
@@ -1988,7 +1991,7 @@ export class CompileProvider {
19881991

19891992
ii = directives.length;
19901993
} else {
1991-
if (compileNodeRef.isElement()) {
1994+
if (compileNodeRef._isElement()) {
19921995
compileNodeRef.element.innerHTML = directiveValue;
19931996
}
19941997
}
@@ -2033,7 +2036,7 @@ export class CompileProvider {
20332036
try {
20342037
/** @type {ng.PublicLinkFn} */
20352038
const linkFn = directive.compile(
2036-
compileNodeRef.getAny(),
2039+
compileNodeRef._getAny(),
20372040
templateAttrs,
20382041
childTranscludeFn,
20392042
);
@@ -2249,7 +2252,7 @@ export class CompileProvider {
22492252
// later, once we have the actual element.
22502253
elementControllers[directive.name] = controllerInstance;
22512254

2252-
if ($element.isElement()) {
2255+
if ($element._isElement()) {
22532256
setCacheData(
22542257
$element.element,
22552258
`$${directive.name}Controller`,
@@ -2398,7 +2401,7 @@ export class CompileProvider {
23982401

23992402
let afterTemplateNodeLinkFnCtx;
24002403

2401-
const beforeTemplateCompileNode = $compileNode.getAny();
2404+
const beforeTemplateCompileNode = $compileNode._getAny();
24022405

24032406
const origAsyncDirective = directives.shift();
24042407

@@ -2520,7 +2523,7 @@ export class CompileProvider {
25202523
});
25212524
}
25222525
afterTemplateChildLinkFn = compileNodes(
2523-
new NodeRef($compileNode.getAny().childNodes),
2526+
new NodeRef($compileNode._getAny().childNodes),
25242527
childTranscludeFn,
25252528
);
25262529

@@ -2531,7 +2534,7 @@ export class CompileProvider {
25312534

25322535
const boundTranscludeFn = linkQueue.shift();
25332536

2534-
let linkNode = $compileNode.getAny();
2537+
let linkNode = $compileNode._getAny();
25352538

25362539
if (scope.$$destroyed) {
25372540
continue;
@@ -2655,7 +2658,7 @@ export class CompileProvider {
26552658
previousDirective.name,
26562659
directive.name,
26572660
what,
2658-
startingTag(element.getAny()),
2661+
startingTag(/** @tupe {NodeRef} */ element._getAny()),
26592662
);
26602663
}
26612664
}
@@ -3015,7 +3018,7 @@ export class CompileProvider {
30153018
* @param {number} [index] Parent node index.
30163019
*/
30173020
function replaceWith(elementsToRemove, newNode, index) {
3018-
const firstElementToRemove = elementsToRemove.getAny();
3021+
const firstElementToRemove = elementsToRemove._getAny();
30193022

30203023
// const removeCount = elementsToRemove.length;
30213024
const parent = firstElementToRemove.parentNode;
@@ -3039,7 +3042,7 @@ export class CompileProvider {
30393042
// - allow a single fragment.qSA to fetch all elements being removed
30403043
const fragment = document.createDocumentFragment();
30413044

3042-
elementsToRemove.collection().forEach((element) => {
3045+
elementsToRemove._collection().forEach((element) => {
30433046
fragment.appendChild(element);
30443047
});
30453048

src/shared/noderef.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export class NodeRef {
134134
}
135135

136136
/** @returns {Element | Node | ChildNode} */
137-
getAny() {
137+
_getAny() {
138138
if (this._isList) {
139139
return this._nodes[0];
140140
} else {
@@ -143,7 +143,7 @@ export class NodeRef {
143143
}
144144

145145
/** @returns {Element | Array<Node> | Node | ChildNode} */
146-
getAll() {
146+
_getAll() {
147147
if (this._isList) {
148148
return this._nodes;
149149
} else {
@@ -152,7 +152,7 @@ export class NodeRef {
152152
}
153153

154154
/** @returns {Array<Element> | Array<Node>} */
155-
collection() {
155+
_collection() {
156156
if (this._isList) {
157157
return Array.from(this._nodes);
158158
} else {
@@ -164,7 +164,7 @@ export class NodeRef {
164164
* @param {number} index
165165
* @returns {Element | Node | ChildNode}
166166
*/
167-
getIndex(index) {
167+
_getIndex(index) {
168168
if (this._isList) {
169169
return this._nodes[index];
170170
} else {
@@ -176,7 +176,7 @@ export class NodeRef {
176176
* @param {number} index
177177
* @param {Element | Node | ChildNode} node
178178
*/
179-
setIndex(index, node) {
179+
_setIndex(index, node) {
180180
if (this._isList) {
181181
this._nodes[index] = node;
182182
} else {
@@ -187,15 +187,15 @@ export class NodeRef {
187187
/**
188188
* @returns {NodeRef}
189189
*/
190-
clone() {
190+
_clone() {
191191
const cloned = this._isList
192192
? this.nodes.map((el) => el.cloneNode(true))
193193
: this.node.cloneNode(true);
194194

195195
return new NodeRef(cloned);
196196
}
197197

198-
isElement() {
198+
_isElement() {
199199
return this._element !== undefined;
200200
}
201201
}

src/shared/noderef.spec.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ describe("NodeRef", () => {
2020
expect(ref.element).toBe(div);
2121
expect(ref.size).toBe(1);
2222
expect(ref._isList).toBeFalse();
23-
expect(ref.isElement()).toBeTrue();
23+
expect(ref._isElement()).toBeTrue();
2424
});
2525

2626
it("wraps a single Node", () => {
2727
const ref = new NodeRef(textNode);
2828
expect(ref.node).toBe(textNode);
29-
expect(ref.isElement()).toBeFalse();
29+
expect(ref._isElement()).toBeFalse();
3030
});
3131

3232
it("wraps an array of Nodes", () => {
@@ -61,7 +61,7 @@ describe("NodeRef", () => {
6161
it("handles HTML string with multiple nodes", () => {
6262
const html = "<div>A</div><span>B</span>";
6363
const ref = new NodeRef(html);
64-
expect(ref.getAny().outerHTML).toContain("div");
64+
expect(ref._getAny().outerHTML).toContain("div");
6565
});
6666

6767
it("throws on invalid input", () => {
@@ -106,27 +106,27 @@ describe("NodeRef", () => {
106106
describe("list and collection methods", () => {
107107
it("getAny() returns first node of list", () => {
108108
const ref = new NodeRef([div, span]);
109-
expect(ref.getAny()).toBe(div);
109+
expect(ref._getAny()).toBe(div);
110110
});
111111

112112
it("getAll() returns all nodes", () => {
113113
const ref = new NodeRef([div, span]);
114-
expect(ref.getAll()).toEqual([div, span]);
114+
expect(ref._getAll()).toEqual([div, span]);
115115
});
116116

117117
it("collection() always returns array", () => {
118118
const ref1 = new NodeRef(div);
119119
const ref2 = new NodeRef([div, span]);
120-
expect(ref1.collection()).toEqual([div]);
121-
expect(ref2.collection()).toEqual([div, span]);
120+
expect(ref1._collection()).toEqual([div]);
121+
expect(ref2._collection()).toEqual([div, span]);
122122
});
123123

124124
it("getIndex() and setIndex() work properly", () => {
125125
const ref = new NodeRef([div, span]);
126-
expect(ref.getIndex(1)).toBe(span);
126+
expect(ref._getIndex(1)).toBe(span);
127127
const newNode = document.createElement("p");
128-
ref.setIndex(1, newNode);
129-
expect(ref.getIndex(1)).toBe(newNode);
128+
ref._setIndex(1, newNode);
129+
expect(ref._getIndex(1)).toBe(newNode);
130130
});
131131
});
132132

@@ -149,14 +149,14 @@ describe("NodeRef", () => {
149149

150150
it("clone() creates deep copy", () => {
151151
const ref = new NodeRef([div, span]);
152-
const clone = ref.clone();
152+
const clone = ref._clone();
153153
expect(clone.nodes[0].isEqualNode(div)).toBeTrue();
154154
expect(clone.nodes[0]).not.toBe(div);
155155
});
156156

157157
it("clone() works on single element", () => {
158158
const ref = new NodeRef(div);
159-
const clone = ref.clone();
159+
const clone = ref._clone();
160160
expect(clone.node.isEqualNode(div)).toBeTrue();
161161
expect(clone.node).not.toBe(div);
162162
});
@@ -166,7 +166,7 @@ describe("NodeRef", () => {
166166
it("handles text node only", () => {
167167
const ref = new NodeRef(textNode);
168168
expect(ref.node.textContent).toBe("plain text");
169-
expect(ref.isElement()).toBeFalse();
169+
expect(ref._isElement()).toBeFalse();
170170
});
171171

172172
it("handles DocumentFragment", () => {
@@ -181,7 +181,7 @@ describe("NodeRef", () => {
181181
const html = "<div><span>Missing close";
182182
const ref = new NodeRef(html);
183183
expect(ref.node).toBeTruthy();
184-
expect(ref.getAny().nodeType).toBe(Node.ELEMENT_NODE);
184+
expect(ref._getAny().nodeType).toBe(Node.ELEMENT_NODE);
185185
});
186186
});
187187
});

0 commit comments

Comments
 (0)