GithubHelp home page GithubHelp logo

gerhobbelt / optimal-select Goto Github PK

View Code? Open in Web Editor NEW

This project forked from autarc/optimal-select

0.0 1.0 0.0 386 KB

Get efficient & robust CSS selectors for HTML elements

License: MIT License

JavaScript 77.48% HTML 22.52%

optimal-select's Introduction

js-standard-style

optimal select

A library which creates efficient and robust CSS selectors for HTML elements.

Features

  • provide UMD integration (usage with Browser + Node)
  • supports the browser environment and the htmlparser2 DOM
  • allow single and multiple element inputs
  • configurations allow to define custom ignore patterns
  • micro library (~ 11kb + no external dependency)
  • shortest path and fastest selection in comparison

How To Use

Aside of the prebundled versions the library is also available via npm:

npm install --save optimal-select

import { select } from 'optimal-select' // global: 'OptimalSelect'

document.addEventListener('click', (e) => {
  var selector = select(e.target)
  console.log(selector)  
})

By default following attributes are ignored for robustness towards changes:

  • style (inline styles often temporary and used for dynamic visualizations)
  • data-reactid (reacts element identifier which depends on the current DOM structure)
  • data-react-checksum (react string rendered markup which depends on the current DOM structure)

To define custom filters you can pass the 'ignore' property as a secondary optional parameter. You can then specify a validation function for the different types (id, class, attribute, tag).

var selector = select(element, {
  root: document, // default reference
  skip: (traverseNode) {
    // ignore select information of the direct parent
    return traverseNode === element.parentNode
  },
  ignore: {
    class (className) {
      // disregard short classnames
      return className.length < 3
    },
    attribute (name, value, defaultPredicate) {
      // exclude HTML5 data attributes
      return (/data-*/).test(name) || defaultPredicate(name, value)
    },
    // define simplified ignore patterns as a string/number/regex
    tag: 'div'
  }
})

Furthermore the root option allows to define the container element (default: document). The skip option allows to define a function, a single node or an array of nodes which should be ignored as the selector is created (default: null).

Client & Server

The latest version optimal-select allows the generation and optimization of selectors on virtual environments. It uses the basic structure the htmlparser2 DOM provides and adds some utilities to create the same results as the browser (note: the withDOMLv1 option has to be enabled). Other libraries like cheerio are built on top of these and therefore compatible.

In contrast to the browser does server environments not have a global context which defines their scope. Therefore one can either be specified explicit as a node using the context options field or automatically extracted from the provided input element. Checkout the example for more details.

TODO

Development

To build your own version run npm run dev for development (incl. watch) or npm run build for production (minified).

optimal-select's People

Contributors

autarc avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.