Skip to content

Add support for :read-only and :read-write pseudo-selectors #1496

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

Closed
jed opened this issue Feb 28, 2025 · 2 comments
Closed

Add support for :read-only and :read-write pseudo-selectors #1496

jed opened this issue Feb 28, 2025 · 2 comments

Comments

@jed
Copy link

jed commented Feb 28, 2025

I'd love to help add support for :read-only and :read-write pseudo-selectors. I've currently got this in the pseudos export of pseudos.ts:

    "read-only"(elem, { adapter }) {
        const readonly = adapter.hasAttrib(elem, "readonly");
        if (!readonly) return false;

        const name = adapter.getName(elem);
        if (name === "TEXTAREA") return true;
        if (name !== "INPUT") return false;

        const type = adapter.getAttributeValue(elem, "type");
        return type && readonlyTypePattern.test(type);
    },
    "read-write"(elem, { adapter }) {
        const readonly = adapter.hasAttrib(elem, "readonly");
        if (readonly) return false;

        const name = adapter.getName(elem);
        if (name === "TEXTAREA") return true;
        if (name !== "INPUT") return false;

        const type = adapter.getAttributeValue(elem, "type");
        return type && readonlyTypePattern.test(type);
    },

but I'm not too well-versed with TypeScript and am getting this error:

ts: Type '<Node, ElementNode extends Node>(elem: ElementNode, { adapter }: InternalOptions<Node, ElementNode>) => boolean | "" | undefined' is not assignable to type 'Pseudo'.
Type 'string | boolean | undefined' is not assignable to type 'boolean'.
Type 'undefined' is not assignable to type 'boolean'.

If I can clear this error I'd love to start adding tests and putting together a PR. Thanks for your help, and for a great library!

@fb55
Copy link
Owner

fb55 commented Feb 28, 2025

Hi @jed, you'll have to make sure to return a Boolean. Converting the string to a Boolean (return !!type && …) should do the trick.

@jed
Copy link
Author

jed commented Feb 28, 2025

Thanks! I just submitted a PR here, let me know if it needs any tweaking.

@jed jed closed this as completed Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants