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

[LiveComponent] Morph error trying to apply invalid classes #1819

Open
aleho opened this issue May 2, 2024 · 7 comments · May be fixed by #1828
Open

[LiveComponent] Morph error trying to apply invalid classes #1819

aleho opened this issue May 2, 2024 · 7 comments · May be fixed by #1828
Labels
LiveComponent Status: Waiting Feedback Needs feedback from the author

Comments

@aleho
Copy link

aleho commented May 2, 2024

When live components try to apply changes to elements for some reason the different classes are picked up with whitespaces or even empty strings. Browser console then throws Uncaught (in promise) DOMException: DOMTokenList.remove: The empty string is not a valid token..

This happened here when an element was morphed that probably shouldn't have been in the first place (and data-skip-morph doesn't solve the problem).


For example when an inline menu (with ID and data-skip-morph) is displayed after clicking an element only the class "hidden" changes. Then after updating the live component the element and its menu are still there (but the menu should be invisible, as backend renders context menus hidden of course):

The changes that are picked up:

  • addedClasses: Set [ "w-full" ].
  • removedClasses: Set(3) [ "", "hidden\n", "w-full\n" ]

In my case, simply filtering class changes at least prevents the error and didn't have any side effects here:

className = className.trim();

if (className === '') {
    return;
}

addClass(className: string) {
if (!this.removedClasses.delete(className)) {
this.addedClasses.add(className);
}
}
removeClass(className: string) {
if (!this.addedClasses.delete(className)) {
this.removedClasses.add(className);
}
}

Am I doing something wrong?

@smnandre
Copy link
Collaborator

smnandre commented May 2, 2024

Where and how is the w-full set ? In template or JS ?

@aleho
Copy link
Author

aleho commented May 3, 2024

w-full is always on the Element. The only class that actually changes is hidden.

@smnandre
Copy link
Collaborator

smnandre commented May 3, 2024

Could you create a minimal reproducer ? It would be easier to investigate, thank you :)

@WebMamba
Copy link
Collaborator

WebMamba commented May 4, 2024

Hey I tried to reproduce your issue on the UX website but couldn't! What I did was take a live component where the class changes and add space at the beginning of the class attribute on this component:

class="form-control form-control-lg{{ error ? ' is-invalid' }}"

There may maybe something I don't understand about your issue, could you try to reproduce your issue on this component ?

@WebMamba WebMamba added the Status: Waiting Feedback Needs feedback from the author label May 4, 2024
aleho added a commit to aleho/symfony-ux that referenced this issue May 6, 2024
The class attribute can be tokenized by any white space as defined by
the HTML spec. A class string containing new lines was not correctly
cleaned / trimmed, resulting in incorrectly detected class changes and a
browser error if empty classes or classes with newlines were applied.

Fixes symfony#1819
@aleho
Copy link
Author

aleho commented May 6, 2024

After investigating further I finally found the root of the problem:

handleClassAttributeMutation doesn't consider the spec correctly.

According to the HTML standard the class attribute can contain "a set of space-separated tokens". Splitting on spaces only will tokenize newlines too resulting in an array containing newlines and empty spaces.

const previousValues = previousValue ? previousValue.split(' ') : [];
previousValues.forEach((value, index) => {
const trimmedValue = value.trim();
if (trimmedValue !== '') {
previousValues[index] = trimmedValue;
} else {
previousValues.splice(index, 1);
}
});

You can easily reproduce this with a simple markup for a live component:

<div {{ attributes }}>
    <p
        class="
            a
            b
            c
        "
    >
        <button type="button" data-action="live#$render" onclick="this.parentElement.classList.remove('a')">
            Remove class and render
        </button>
    </p>
</div>

@smnandre
Copy link
Collaborator

smnandre commented May 6, 2024

But i think your markup is not valid.. class should not contain new lines

Ok i'm pretty sure we already had a problem with that but great if you have a fix :)

aleho added a commit to aleho/symfony-ux that referenced this issue May 6, 2024
The class attribute can be tokenized by any white space as defined by
the HTML spec. A class string containing new lines was not correctly
cleaned / trimmed, resulting in incorrectly detected class changes and a
browser error if empty classes or classes with newlines were applied.

Fixes symfony#1819
@MsLady45
Copy link

MsLady45 commented May 8, 2024

What class

aleho added a commit to aleho/symfony-ux that referenced this issue May 8, 2024
The class attribute can be tokenized by any white space as defined by
the HTML spec. A class string containing new lines was not correctly
cleaned / trimmed, resulting in incorrectly detected class changes and a
browser error if empty classes or classes with newlines were applied.

Fixes symfony#1819
aleho added a commit to aleho/symfony-ux that referenced this issue May 13, 2024
The class attribute can be tokenized by any white space as defined by
the HTML spec. A class string containing new lines was not correctly
cleaned / trimmed, resulting in incorrectly detected class changes and a
browser error if empty classes or classes with newlines were applied.

Fixes symfony#1819
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LiveComponent Status: Waiting Feedback Needs feedback from the author
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants