Skip to content

Commit 06f51ef

Browse files
authored
Add support for :read-only and :read-write pseudos (#1497)
1 parent 1605ff6 commit 06f51ef

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

src/pseudo-selectors/aliases.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* Only text controls can be made read-only, since for other controls (such
3+
* as checkboxes and buttons) there is no useful distinction between being
4+
* read-only and being disabled.
5+
*
6+
* @see {@link https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly}
7+
*/
8+
const textControl =
9+
"input:is([type=text i],[type=search i],[type=url i],[type=tel i],[type=email i],[type=password i],[type=date i],[type=month i],[type=week i],[type=time i],[type=datetime-local i],[type=number i])";
10+
111
/**
212
* Aliases are pseudos that are expressed as selectors.
313
*/
@@ -21,6 +31,9 @@ export const aliases: Record<string, string> = {
2131
required: ":is(input, select, textarea)[required]",
2232
optional: ":is(input, select, textarea):not([required])",
2333

34+
"read-only": `[readonly]:is(textarea, ${textControl})`,
35+
"read-write": `:not([readonly]):is(textarea, ${textControl})`,
36+
2437
// JQuery extensions
2538

2639
/**

test/pseudo-classes.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,26 @@ describe(":has", () => {
200200
).toHaveLength(2);
201201
});
202202
});
203+
204+
describe(":read-only and :read-write", () => {
205+
it("should match", () => {
206+
const dom = parseDocument(`
207+
<div>
208+
<input type="text" readonly>
209+
<input type="text">
210+
<input type="color" readonly>
211+
<input type="color">
212+
<textarea readonly></textarea>
213+
<textarea></textarea>
214+
</div>
215+
`);
216+
217+
expect(
218+
CSSselect.selectAll<AnyNode, Element>(":read-only", dom),
219+
).toHaveLength(2);
220+
221+
expect(
222+
CSSselect.selectAll<AnyNode, Element>(":read-write", dom),
223+
).toHaveLength(2);
224+
});
225+
});

0 commit comments

Comments
 (0)