Skip to content
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

Merged
merged 4 commits into from
Oct 2, 2019

Conversation

ravijayaramappa
Copy link
Contributor

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:

  • Have tests for the proposed changes been added? ✅
  • Have you followed these instructions to clearly describe the issue being fixed or feature enhanced? ✅

collection: NodeListOf<T> | HTMLCollectionOf<K>
): Array<T | K> {
const size = collection.length;
const cloned: T[] | K[] = new ArrayConstructor(size);
Copy link
Contributor Author

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
Screen Shot 2019-10-01 at 1 51 29 PM

* 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>(
Copy link
Contributor

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 {
Copy link
Contributor

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.

Copy link
Contributor Author

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 @@
/*
Copy link
Member

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

Comment on lines 9 to 11
* 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.
Copy link
Member

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.

@ravijayaramappa ravijayaramappa force-pushed the ravi/master/collection-to-array/W-6684382 branch from 5682736 to 2661bcf Compare October 2, 2019 17:50
@ravijayaramappa ravijayaramappa merged commit c3fd522 into master Oct 2, 2019
@ravijayaramappa ravijayaramappa deleted the ravi/master/collection-to-array/W-6684382 branch October 2, 2019 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants