Skip to content

DOMException for trailing whitespace in x-bind:class #436

@felixsc

Description

@felixsc

Hi,
I'm not sure if this is an issue of Alpine itself or just something JS specific.

Alpine version: latest 2.x.x (CDN)

The following error appears when a whitespace is added after the last class declaration:

Uncaught (in promise) DOMException: Failed to execute 'add' on 'DOMTokenList': The token provided must not be empty. at https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js:7:8076 at Array.forEach (<anonymous>) at https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js:7:8053 at Array.forEach (<anonymous>) at x (https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js:7:8023) at https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js:7:18631 at Array.forEach (<anonymous>) at ie.resolveBoundAttributes (https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js:7:18456) at ie.initializeElement (https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js:7:17197) at https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js:7:16908

Code to reproduce

html (error):

<div x-data="{color: 'green'}">
    <h1 x-bind:class="{'green-text ': color === 'green'}">Does my color change</h1>
</div>

html (working)

<div x-data="{color: 'green'}">
    <h1 x-bind:class="{'green-text': color === 'green'}">Does my color change</h1>
</div>

styles:

.green-text {
  color: green;
}

Notice the whitespace in 'green-text '. Removing the whitespace fixes it.

I ran into this today and it wasn't immediately obvious to me why it broke. Especially using tailwind makes it hard to spot this, when the class declarations get longer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions