Git Product home page Git Product logo

smart-dom's Introduction

SmartDOM

SmartDOM is a JavaScript library for efficient DOM element searching.

Installation

To use SmartDOM in your project, you can install it via npm.

npm install smart-dom

Then, import it into your JavaScript/TypeScript file:

import SmartDOM from 'smart-dom';

Finding Elements

You can use SmartDOM to find elements in the DOM using various criteria:

const smartDOM = new SmartDOM( window );

Find by text

const elementsByText = smartDOM.findElement( { 
  text: 'Example Text' 
} );

Find by tag name

const elementsByTagName = smartDOM.findElement( { 
  tagName: 'div'
} );

Find by attributes

const elementsByAttributes = smartDOM.findElement( {
  attributes: {
    class: 'example-class',
    text: 'value'
  }
} );

Find by parent

const elementsByAttributes = smartDOM.findElement( {
  parent: {
    class: 'parent-class',
    tagName: 'div'
  }
} );

Find by children

const elementsByAttributes = smartDOM.findElement( {
  children: [ {
    class: 'children-class',
    tagName: 'div'
  } ]
} );

API

SmartDOM(window: Window)

The constructor accepts a window object, which is used for DOM manipulation. It initializes the SmartDOM instance.

find(options: OptionType): Array<Element> Finds elements in the DOM based on the provided options.

  • options (OptionType): An object specifying the criteria for finding elements.

OptionType

An object with optional properties for specifying the criteria to find elements:

  • text (string): Text content to search for within elements.
  • tagName (string): The tag name of elements to search for.
  • attributes (AttributeType): An object specifying attribute-value pairs to match elements.

AttributeType

An object where keys represent attribute names, and values represent the expected attribute values. Values can be strings or true for attribute existence checks.

smart-dom's People

Contributors

illia-stv avatar

Watchers

 avatar

smart-dom's Issues

Finding by text doesn't work in case with "new line"

If you have in you html document long text with enters like:

<p>
        This is a sample paragraph. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nemo, rerum!
        This is a sample paragraph. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nemo, rerum!
        This is a sample paragraph. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nemo, rerum!
        This is a sample paragraph. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nemo, rerum!
        This is a sample paragraph. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nemo, rerum!
        This is a sample paragraph. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nemo, rerum!
</p>

and you copy it from browser, and will use as an argument in:

const elements = smartDOM.findElement( {
    text: '
        This is a sample paragraph. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nemo, rerum!
        This is a sample paragraph. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nemo, rerum!
        This is a sample paragraph. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nemo, rerum!
        This is a sample paragraph. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nemo, rerum!
        This is a sample paragraph. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nemo, rerum!
        This is a sample paragraph. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nemo, rerum!
   '
} )

It will fail, thats because in document instead of enter you have "\n ".

In string as argument there is no "\n " then it will doesn't match.

Maybe we need consider idea with flag in config, kind of "removeNewLines".

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.