• Stars
    star
    158
  • Rank 237,131 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 11 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A pure Javascript reverse CSS selector engine which calculates a DOM element's unique CSS selector on the current page.

Simmer JS Build Status Code Climate

Docs

A pure Javascript reverse CSS selector engine which calculates a DOM element's unique CSS selector on the current page.

Installation

Simmer is now meant to be consumed as a module via npm and is no longer meant as a drop in script into the browser. That said, we still build a version meant to be dropped into the browser which resides on window.Simmer as before.

We highly recommend you consume Simmer in the following manner, but in the interest of supporting exosting users we've retained the previous API.

npm i --save simmerjs

And then in your code:

import Simmer from 'simmerjs'

const simmer = new Simmer()

const el =  document.getElementById('#SomeElement')

expect(
    simmer(el)
).to.equal(
    '#SomeElement'
)

When using the distribution located in the repo under dist/simmer.js a global Simmer function will be exposed on the window. This is not the constructor, but rather a default instance which has exposed itself on the window wit ha default configuration. This is not an idea API and is meant to maintain the original API dating back to 2011 when this library was originally written.

Basic usage

To use Simmer to analyze an element and produce a unique CSS selector for it, all you have to do is instansiate a simmer query engine and pass it an element on the page. Usually you'd create a single instance which you would then use multiple times.

<div id="myUniqueElement">
    <span></span>
    <br/>
    <span></span>
</div>
import Simmer from 'simmerjs'

const simmer = new Simmer(window, { /* some custom configuration */ })
var myElement = document.getElementById("#myUniqueElement");

console.log(simmer(myElement)); // prints "[id='myUniqueElement']"

API

Simmer

    Simmer([Scope], [Options], [query])

Scope

When you create an instance of Simmer its first argument is the context in which Simmer should query for elements. Generally speaking this would be the window, which is the default value, but it would be overriden in a situation where you might be using Simmer against a Virtual Dom implementation. If you are using a Virtual DOM, you should provide the Window object of the Virtual DOM

Options

The second argument is an Options object allowing you to override the default configuration for Simmer's behaviour.

The options are:

Option Default
specificityThreshold - A minimum specificty level. Once the parser reaches this level it starts verifying the selector after every method is called. This can cut down our execution time by avoiding needless parsing but can also hurt execution times by performing many verifications. Specificity is calculated based on the W3C spec: http://www.w3.org/TR/css3-selectors/#specificity 100
depth - How deep into the DOM hierarchy should Simmer go in order to reach a unique selector. This is a delicate game because the higher the number the more likely you are to reach a unique selector, but it also means a longer and more breakable one. Assuming you want to store this selector to use later, making it longer also means it is more likely to change and loose it's validity. 3
errorHandling - How to handle errors which occur during the analysis

Valid Options
  • false: errors are ignored by Simmer
  • true: errors rethrown and expected to be caught by the user
  • _a function callback will be called with two parameters: the exception and the element being analyzed
false
selectorMaxLength - A maximum length for the CSS selector can be specified - if no specific selector can be found which is shorter than this length then it is treated as if no selector could be found. 520

query

The third argument is a query engine you wish Simmer to use when evaluating generated selectors. By default Simmer uses the window.document.querySelectorAll function and if you provide a window to the scope, Simmer will assume that you want it to use the document.querySelectorAll on that window. But if you wish Simmer to use another custom function, such as your own tweaked version of jQuery, you can do so by passing the third argument to the Simmer constructor.

The signature the query function should provide is this:

query(selector: String, onError: Error => {}) : Array(DomElements)

What this means is that the function you provide should expect to receive a string CSS selector and a function. It should them query for elements matching the selector and return an array of the results (even if there is only one result, it should be returned in an Array.).

The second argument is a function which should be called if any error is encountered when querying for the elements. If an error occurs or a problem is encountered, instead of throwing, you should call the function and pass the error object to it. Simmer will then handle the error as per its configuration.

Reconfiguring

If you have an existing instance of Simmer, you can use its configure method to instanciate a new Simmer which has the same scope and configuration as the existing one, with any new configuration you wish to apply.

So, for example, in the browser, you can replace the global Simmer wit ha newly configured version:

window.Simmer = window.Simmer.configure({
    depth: 10
})

And in Node:

import Simmer from 'simmerjs'

const virtualWindow = new JSDom()

const simmer = new Simmer(virtualWindow)

const reconfiguredSimmer = simmer.configure({ /* some custom configuration */ })

Conflict

When the Simmer browser dist located at dist/simmer.js is injected in to the browser it adds the noConflict function on itself. This is not relevant in a Node environment and isn't available there.

Just in case you also had the brilliant idea of using a variable called "Simmer", or you wish to move it off of the global object then you can use the noConflict method to receive a reference to the object and remove it from the window. Calling it will also revert the original value of window.Simmer which was there before loading the Simmer.js script (if there was one)

    var mySimmer = Simmer.noConflict();
    mySimmer(myElm);

More Repositories

1

jg

Jeff Goldblum (jg) is a command-line JSON processor. jg searches for structural patterns in json input and prints each json object that matches the pattern.
Rust
35
star
2

safeobj

Safe object property access using Proxies as described in my article: https://medium.com/@chekofif/using-es6-s-proxy-for-safe-object-property-access-f42fa4380b2c
JavaScript
11
star
3

higher-order-classes

Code examples for my How To Grok Higher Order Classes article, exploring how to approach applying higher order function paradigms against class definitions.
JavaScript
6
star
4

meze

A Component Composition library for Declaratively Composable Operations
JavaScript
5
star
5

bs-aws-sdk

BuckleScript bindings for the JavaScript AWS SDK
OCaml
4
star
6

user_manual_for_me

A User Manual for working with me.
3
star
7

birdwatcherjs

A covert error handling utility for Javascript. A small utility for implementing the NCZ JavaScript error handling pattern (his anti anti-pattern pattern).
JavaScript
3
star
8

bs-rewire

BuckleScript bindings to the Rewire unit test monkey-patching utility
OCaml
2
star
9

cv

My online CV
HTML
1
star
10

naturebound.ai

Landing Page for NatureBound pre-funding
HTML
1
star
11

gmmorris.github.io

SCSS
1
star
12

TheOCamlsystem4.06InReason

A copy of the TheOCamlsystem4.06 with all code examples in ReasonML
HTML
1
star
13

get-caller-path

Get the file name of the code which called you
JavaScript
1
star
14

unfuckthe.earth

SCSS
1
star
15

conference-talks

Conference proposals I have submitted
1
star
16

npm-dependency-tree

NPM Module Dependency Tree Viewer (App & Service)
JavaScript
1
star
17

gio

Unit Test Precompilation
JavaScript
1
star
18

json-highlight-writer

A writer for json-rust object which supports highlighting slices when printing JSON objects
Rust
1
star