0.5.0 • Published 4 years ago

selector-to-element v0.5.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

selector-to-element

Transforms a selector into a matching HTML element.

Installation

Install via npm:

npm install selector-to-element --save-dev

Usage

Quickly mock a specific DOM branch to assist in unit testing. Helpful for testing event listeners that rely on a specific DOM structure. Be sure to run tests in the jsdom environment or something comparable if running in Node. Supports type, ID, class, attribute, and sibling selectors. The :has, :nth-child, :nth-of-type are supported, in addition to input-based pseudo-classes such as :checked and :invalid on the appropriate element type. Unless specified, each descending node will not contain siblings so pseudo-classes such as :first-child and :last-child will be supported by default. The final node in the selector will be returned. Nodes without a specified element type will default to div.

import selectorToElement from 'selector-to-element';

const imgEl = selectorToElement('article#myId a.linkClass[target="_blank"] img');

Params

ParamTypeDescription
selectorstringA selector.
appendToElement(Optional) A root element to append the DOM branch specified by the selector. Defaults to a new div.
0.3.0

4 years ago

0.5.0

4 years ago

0.4.0

4 years ago

0.3.1

4 years ago

0.2.0

4 years ago

0.1.1

4 years ago

0.1.0-alpha

4 years ago

0.1.0

4 years ago