Skip to content

Commit 08a7659

Browse files
committed
support escaping in class literals for great tailwinds justice
1 parent 68a0f5f commit 08a7659

4 files changed

Lines changed: 42 additions & 4 deletions

File tree

src/_hyperscript.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,10 @@
356356
value += consumeChar(); // consume final curly
357357
}
358358
} else {
359-
while (Lexer.isValidCSSClassChar(currentChar())) {
359+
while (Lexer.isValidCSSClassChar(currentChar()) || currentChar() === "\\") {
360+
if (currentChar() === "\\") {
361+
consumeChar();
362+
}
360363
value += consumeChar();
361364
}
362365
}
@@ -2322,7 +2325,7 @@
23222325
* @returns {string}
23232326
*/
23242327
escapeSelector(str) {
2325-
return str.replace(/:/g, function (str) {
2328+
return str.replace(/[:&()\[\]\/]/g, function (str) {
23262329
return "\\" + str;
23272330
});
23282331
}

test/commands/toggle.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,14 @@ describe("the toggle command", function () {
186186
div.click();
187187
getComputedStyle(div2).visibility.should.equal("visible");
188188
});
189+
190+
it("can toggle crazy tailwinds class ref on a single form", function () {
191+
var form = make("<form _='on click toggle .group-\\[:nth-of-type\\(3\\)_\\&\\]:block'></form>");
192+
form.classList.contains("group-[:nth-of-type(3)_&]:block").should.equal(false);
193+
form.click();
194+
console.log(form, form.classList);
195+
form.classList.contains("group-[:nth-of-type(3)_&]:block").should.equal(true);
196+
form.click();
197+
form.classList.contains("group-[:nth-of-type(3)_&]:block").should.equal(false);
198+
});
189199
});

test/expressions/classRef.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,16 @@ describe("the classRef expression", function () {
4747
Array.from(value)[0].should.equal(div);
4848
});
4949

50+
it("slashes in class references work", function () {
51+
var div = make("<div class='-c1/22'></div>");
52+
var value = evalHyperScript(".-c1\\/22");
53+
Array.from(value)[0].should.equal(div);
54+
});
55+
56+
it("tailwind insanity in class references work", function () {
57+
var div = make("<div class='group-[:nth-of-type(3)_&]:block'></div>");
58+
var value = evalHyperScript(".group-\\[:nth-of-type\\(3\\)_\\&\\]:block");
59+
Array.from(value)[0].should.equal(div);
60+
});
61+
5062
});

www/expressions.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ main expression types:
4747
You can refer to an element by ID directly in hyperscript as follows:
4848

4949
```html
50-
<div _="on click put 'Clicked!' into #example.innerHTML">Click Me</div>
50+
<button _="on click put 'Clicked!' into #example.innerHTML">Click Me</button>
5151
<div id="example"></div>
5252
```
5353

@@ -59,7 +59,7 @@ The `#example` is an ID literal and will evaluate to the element with the given
5959
You can refer to a group of elements by class directly in hyperscript as follows:
6060

6161
```html
62-
<div _="on click put 'Clicked!' into .example.innerHTML">Click Me</div>
62+
<button _="on click put 'Clicked!' into .example.innerHTML">Click Me</button>
6363
<div class="example"></div>
6464
<div class="example"></div>
6565
```
@@ -68,6 +68,19 @@ The `#example` is an ID literal and will evaluate all the elements with the clas
6868
text into their `innerHTML` when the top div is clicked. Note that the [put command](/commands/put) can work with
6969
collections as well as single values, so it can put the given value into all the returned elements.
7070

71+
If you want to use non-standard characters in a class name you can escape them using the backslash `\` character. For
72+
example, consider the complex tailwinds class (`group-[:nth-of-type(3)_&]:block`)[https://tailwindcss.com/docs/hover-focus-and-other-states#arbitrary-groups].
73+
In this class the following characters are non-standard: `[]()&`. Another common caracter in tailwinds classes is the
74+
forward slash `/`.
75+
76+
Therefore, to express this class in hyperscript you would write the following:
77+
78+
```html
79+
<button _="on click toggle .group-\[:nth-of-type\(3\)_\&\]:block">
80+
Toggle Tailwinds Class
81+
</button>
82+
```
83+
7184
### Query Literals
7285

7386
You can refer to a group of elements by an arbitrary [CSS selector](https://www.w3schools.com/cssref/css_selectors.asp)

0 commit comments

Comments
 (0)