-
Notifications
You must be signed in to change notification settings - Fork 402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: convert nodelist to array before passing to native Array methods #1548
fix: convert nodelist to array before passing to native Array methods #1548
Conversation
collection: NodeListOf<T> | HTMLCollectionOf<K> | ||
): Array<T | K> { | ||
const size = collection.length; | ||
const cloned: T[] | K[] = new ArrayConstructor(size); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method performs better than instantiating an empty array and calling ArrayPush.
https://jsperf.com/new-array-vs-push-lwc
* This is to aviod conflict with some legacy third party libraries like prototype.js | ||
* that patch Array.prototype and are incapable of handling NodeList and HTMLCollection. | ||
*/ | ||
export function arrayFromCollection<T extends Node, K extends Element>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we have two methods, one for htmlcollection and one for nodelist? even if the underlying implementation is the same... I feel that this PR is messing with the types.
@@ -83,7 +84,7 @@ export function getAllMatches(owner: Element, nodeList: NodeList | Node[]): Arra | |||
return filteredAndPatched; | |||
} | |||
|
|||
export function getFirstMatch(owner: Element, nodeList: NodeList): Element | null { | |||
export function getFirstMatch(owner: Element, nodeList: NodeList | Element[]): Element | null { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this type extension seems wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is called in two places. One with a NodeList and the other with an Element[]. That is the reason for this change.
BTW, the same pattern is in L73
export function getAllMatches(owner: Element, nodeList: NodeList | Node[]):
@@ -0,0 +1,24 @@ | |||
/* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should move this utility function to ./utils.ts
* Issue #1545 - Writing a custom util to convert NodeList and HTMLCollection instances to array. | ||
* This is to aviod conflict with some legacy third party libraries like prototype.js | ||
* that patch Array.prototype and are incapable of handling NodeList and HTMLCollection. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This utility should be used to convert NodeList and HTMLCollection into an array before we perform array operations on them. See issue #1545 for more details.
packages/@lwc/synthetic-shadow/src/polyfills/document-shadow/polyfill.ts
Show resolved
Hide resolved
packages/@lwc/synthetic-shadow/src/polyfills/document-shadow/polyfill.ts
Show resolved
Hide resolved
packages/@lwc/synthetic-shadow/src/polyfills/document-shadow/polyfill.ts
Show resolved
Hide resolved
packages/@lwc/synthetic-shadow/src/polyfills/document-shadow/polyfill.ts
Show resolved
Hide resolved
packages/@lwc/synthetic-shadow/src/polyfills/document-shadow/polyfill.ts
Show resolved
Hide resolved
5682736
to
2661bcf
Compare
Details
Alternative to: #1546
Does this PR introduce breaking changes?
No, it does not introduce breaking changes.
If yes, please describe the impact and migration path for existing applications.
The PR fulfills these requirements: