{"id":18547962,"url":"https://github.com/jvanbruegge/tree-selector","last_synced_at":"2025-04-09T21:32:00.046Z","repository":{"id":57379530,"uuid":"130387442","full_name":"jvanbruegge/tree-selector","owner":"jvanbruegge","description":"Use CSS selectors to match nodes in a custom object tree","archived":false,"fork":false,"pushed_at":"2020-02-09T18:22:38.000Z","size":130,"stargazers_count":6,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T15:06:51.421Z","etag":null,"topics":["css-selector","query","tree"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jvanbruegge.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-20T16:25:56.000Z","updated_at":"2024-12-07T01:05:28.000Z","dependencies_parsed_at":"2022-09-02T21:22:08.441Z","dependency_job_id":null,"html_url":"https://github.com/jvanbruegge/tree-selector","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jvanbruegge%2Ftree-selector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jvanbruegge%2Ftree-selector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jvanbruegge%2Ftree-selector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jvanbruegge%2Ftree-selector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jvanbruegge","download_url":"https://codeload.github.com/jvanbruegge/tree-selector/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248114765,"owners_count":21050109,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["css-selector","query","tree"],"created_at":"2024-11-06T20:32:32.537Z","updated_at":"2025-04-09T21:31:59.602Z","avatar_url":"https://github.com/jvanbruegge.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tree-selector\n[![Build Status](https://travis-ci.org/jvanbruegge/tree-selector.svg?branch=master)](https://travis-ci.org/jvanbruegge/tree-selector) [![codecov](https://codecov.io/gh/jvanbruegge/tree-selector/branch/master/graph/badge.svg)](https://codecov.io/gh/jvanbruegge/tree-selector) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)\n\nbuild a matching or query function for CSS selectors for any nested object structure!\n\n```js\nimport { createQuerySelector, createMatches } from 'tree-selector';\n\nconst options = {\n    tag: n =\u003e n.tagName,\n    contents: n =\u003e n.innerText,\n    id: n =\u003e n.id,\n    class: n =\u003e n.className,\n    parent: n =\u003e n.parentElement,\n    children: n =\u003e n.childNodes,\n    attr: (n, attr) =\u003e n.getAttribute(attr)\n};\n\nconst querySelector = createQuerySelector(options);\nconst matches = createMatches(options);\n\nconst selector = 'span.mySpan';\nconst element = document.getElementsByClassName('span')[0]\n\nif(matches(selector, element)) {\n  // there are elements matching the selector\n} else {\n  // no elements found\n}\n\n//Also possible, but less efficient\nif(querySelector(selector, element).length \u003e 0) {\n    // there are elements found\n}\n```\n\n# API\n\n### createQuerySelector(options) -\u003e querySelector\n\nConfigure `tree-selector` for the nested object structure you'll want to match against.\n\n### createMatches(options) -\u003e matches\n\nConfigure a `matches` function for a node in your tree structure. (This is used internally by `createQuerySelector`)\n\n#### options\n\n`options` are an object of lookup functions for queried nodes. You only need to provide the configuration necessary for the selectors you're planning on creating.\n(If you're not going to use `#id` lookups, there's no need to provide the `id` lookup in your options.)\n\n* `tag`: Extract tag information from a node for `div` style selectors.\n* `contents`: Extract text information from a node, for `:contains(xxx)` selectors.\n* `id`: Extract id for `#my_sweet_id` selectors.\n* `class`: `.class_name`\n* `parent`: Used for sibling selectors\n* `children`: Used to traverse from a parent to its children for sibling selectors `div + span`, `a ~ p`.\n* `attr`: Used to extract attribute information, for `[attr=thing]` style selectors.\n\n## Supported pseudoclasses \n\n - `:first-child`\n - `:last-child`\n - `:nth-child`\n - `:empty`\n - `:root`\n - `:contains(text)`\n\n## Supported attribute lookups\n\n - `[attr=value]`: Exact match\n - `[attr]`: Attribute exists and is not false-y.\n - `[attr$=value]`: Attribute ends with value\n - `[attr^=value]`: Attribute starts with value\n - `[attr*=value]`: Attribute contains value\n - `[attr~=value]`: Attribute, split by whitespace, contains value.\n - `[attr|=value]`: Attribute, split by `-`, contains value.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjvanbruegge%2Ftree-selector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjvanbruegge%2Ftree-selector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjvanbruegge%2Ftree-selector/lists"}