npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

css-optimum-selector

v2.0.9

Published

Module for finding relative css-selector

Downloads

11

Readme

CSS Optimum Selector

A module to identify an unique css selector for a jQuery element in a most efficient way. An input of two jQuery elements return a common CSS selector matching a pattern of both the input elements.

Installation

npm i css-optimum-selector --save

or

<script src="https://unpkg.com/css-optimum-selector/lib/dist.js"></script>

What CSS Optimum Selector do?

The module solves two use-cases:

  1. Given an element, extract the unique css selector.
  2. Given a couple of elements, extract a multi-element css selector uniquely identifying the patten in the input element list.

What is multi-element css selector?

Imagine an ordered list with n number of elements. To select all the elements in the list, one would skim through each of the element and find a common pattern of CSS selector among them. "CSS Optimum Selector" do it more intelligently. A input of just 2 elements from the list is enough to pull-out the common pattern and the module extracts a single CSS selector that can select all the list elements.

What if there is a different pattern, for instance odd, even type? Can the module uniquely identify all the elements or can it get only certain pattern of elements?

Yes, it can still identify all the elements. The relative-depth option allows to select a either only a single matching pattern or all the patterns.

What if there is no pattern in selection ?

In no patterns case, CSS Optimum Selector will return css selector for each of the element passed as input.

How to use ?

import CSSOptimumSelector from 'css-optimum-selector'

Optionally, The module accepts a list of options. The options help to identify the selector in more optimum way by applying filter operations the on input element's tag, id, class or any other attributes.

Below are definition of options:

  • root : The root element to begin unique CSS selector extraction. Default - html

  • startWith: The selector can start with the specified rule. This is optional, if not specified the selector of shortest will be returned. The value is an array which means that the startwith value can be more than one (i.e) specifying ['id', 'class', 'tag'] means the selector can start with any one of the above value.

  • priority : The ordered list of identifiers to be used as priority in extractiong selector. Default - priority: ['tag', 'id', 'class']

  • relativeDepth - Used only for multi-element selector. A value of 1 is to select all matching list of elements. A value of 2 or above is to select only the patterns which match its occurance. For example: Consider a (odd, even, odd, even..) pattern of elements. An input of two odd elements with relativeDepth=2 will return CSS selector to identify all the odd elements. An input of two odd elements with relativeDepth=1 will return CSS selector to identify all listed elements. Default - 1

  • ignore : For each of class, id and tag, the option accepts an array of classes, ids or tags to ignored in extracting a CSS selector.

  • ignoreFunc : Same logic as ignore parameter, but ignoreFunc accepts a function returning a boolean indicating whether the class, id or tag can be ignored or not.

  • Option Structure

    const option = {
      root: 'html',                            //type - string
      startWith: ['id', 'class']               //type - Array of string
      priority: ['tag', 'id', 'class'],        //type - Array of string
      relativeDepth: 2,                        //type - number
      ignore: {                                //type - Object of array of string
        class: ['class1', 'class2', 'class3'],
        id: ['id1', 'id2', 'id3'],             //not mandatory to have all keys
        tag: ['tag1', 'tag2', 'tag3'],         //include only the needed one
      },
      ignoreFunc: {
        class: args => args.filter(arg =>      //type - Object of functions
          arg.index('sample') > -1)            //type args - Array of string
      }
    }

### Usage

There are two member-functions availabe to use. One for unique-css-selector and one for multi-selector.

The module can be used in ES6 or ES5. Below is an example in ES6 format.

* Unique CSS Selector

=> getUniqueCssSelector ---> argument is jQuery node element; return type string


* Multi-element CSS Selector

=> getMultiSelector ---> argument is jQuery node element; return type array of string


**getUniqueCssSelector** accepts one argument the target element.

**getMultiSelector** accepts three arguments, the third being optional. The first two arguments are the target elements and the third optional argument is a number indicating the relative depth.

* Snippet

import CSSOptimumSelector from 'css-optimum-selector'

const option = { root: 'body', relativeDepth: 3, }

class SomeRandomClass { constructor() { this.cssOptimumSelector = new cssOptimumSelector(option) }

someEventHandler(event) { const selector = this.cssOptimumSelector.getUniqueCssSelector($(event.target)) }

multiSelector(target1, target2) { const multiSelector = this.cssOptimumSelector.getMultiSelector(target1, target2, 2) }

}


### Status
This project is being actively developed and should be considered alpha quality.


### Further Enhancement

What if the module suggests the maximum relative depth available and the same can be set at runtime, instead of hard-coding relativeDepth within options? Star or watch the project for updates on features.

### Contribute

For any issues or queries, do raise a Github Issue. If you're interested to contribute, please feel free to fork and send in a Pull Request.