Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Make attributes.length optional #107

Merged
merged 2 commits into from Nov 29, 2022

Conversation

SantosGuillamot
Copy link
Contributor

We have found a couple of sites where our hydration script is failing because attributes.length is undefined. It seems that adding ? to it to make it optional solves the problem. These are the sites where it is failing:

@SantosGuillamot SantosGuillamot marked this pull request as draft November 29, 2022 11:43
@SantosGuillamot SantosGuillamot changed the base branch from main-custom-elements-hydration to main-wp-directives-plugin November 29, 2022 11:44
@SantosGuillamot SantosGuillamot marked this pull request as ready for review November 29, 2022 11:44
@@ -9,7 +9,7 @@ export default function toVdom(node) {

if (node.nodeType === 3) return node.data;

for (let i = 0; i < attributes.length; i++) {
for (let i = 0; i < attributes?.length; i++) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attributes[i].name will also crash if attributes is undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I thought that if attributes is undefined, it wasn't ever going to enter the for loop. What should we do then? Wrapping the whole for loop in a conditional like if(attributes){ for (...){} }?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a curious case:

As you first in the file use destructure to get attributes (you could also define let attributes; which also defaults to undefined), in this case: attributes?.length is 'undefined'.

So the comparison is 0 < undefined which indeed is false 😆, so your thought is true, and it should not enter the loop.

So the code should work, but you may have unexpected comparisons 😅 . So I guess in this case preventing the loop with the conditional would be a good approach.

In case you try in a console without initializing attributes, it should crash 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the explanation! It totally makes sense 🙂

I've just pushed a new commit with your suggestion. Hope that's correct 😁

Copy link
Collaborator

@cbravobernal cbravobernal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@SantosGuillamot SantosGuillamot merged commit b822772 into main-wp-directives-plugin Nov 29, 2022
@SantosGuillamot SantosGuillamot deleted the fix/attributes-length branch November 29, 2022 15:17
@luisherranz
Copy link
Member

I've inspected why those nodes don't have attributes, and it's because they are CDATA (nodeType = 4). I'd prefer to make a conscious decision about what to do with CDATA, instead of just checking if attributes exist or not.

I've opened an issue to discuss it.

@SantosGuillamot
Copy link
Contributor Author

Great! That totally makes sense. Should I revert the PR to ensure we are not skipping other use cases like CDATA?

@luisherranz
Copy link
Member

Let's see what comes up from #109 first.

SantosGuillamot added a commit that referenced this pull request Dec 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants