JSPM

get-element-selectors

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3
  • Score
    100M100P100Q21918F
  • License MIT

Get element CSS selectors

Package Exports

  • get-element-selectors

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (get-element-selectors) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

get-element-selectors

Returns an array of CSS selector strings for a given DOM element.

Usage

getElementSelectors(element, option)

Example

import { getElementSelectors } from 'get-element-selectors';

const option =  { attributes: ['name', 'type', 'data-foo'], maxResults: 10 }

const ele = document.querySelectorAll("div")[3];

const selectors = getElementSelectors(element, option);

console.log(selectors); // Output: ['.target', ...]

Parameters

element

This is the HTML Element.

option

Option is an Object with the following fields:

  1. attributes : This is an array containing attribute names can be used as selectors. Default is ['type', 'name'] .
  2. maxResults : This sets the maximum number of selector strings will return

Types

export type GetElementSelectorsOption = {
  attributes: string[];
  maxResults: number;
};

export const getElementSelectors = (
  element: Element,
  option: GetElementSelectorsOption = { attributes: ['type', 'name'], maxResults: 10 },
): string[]  

Notes

  1. If an element has an ID attribute, the id selector will be the only selector in the returned array.
  2. Only valid class names will be used in CSS selectors.