Socket
Socket
Sign inDemoInstall

attribute-analyzer

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

attribute-analyzer

Parse your complex element attributes with ease


Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

attribute-analyzer

Parse complex attributes on the server with ease

import { EmptyAttribute,analyze,getLocalName } from "attribute-analyzer"

const input = `<button on:click={() => {value++}} test="yes" :data>testing</button>`;

//Getting the local name
const name = getLocalName(input); // => button

//Analyzing the element
const analyzedElement = analyze(input); /* => 
{
  element: {
    attributeOffsets: { 'on:click': [Object], test: [Object], ':data': [Object] },
    attributes: {
      ':data': EmptyAttribute {},
      'on:click': '{() => {value++}}',
      test: '"yes"'
    },
    rawElement: '<button on:click={() => {value++}} test="yes" :data>testing</button>',
    rawAttributes: 'on:click={() => {value++}} test="yes" :data',
    localName: 'button',
    innerHTML: { content: 'testing', startOffset: 52, endOffset: 59 },
    selfClosing: false
  },
  addAttribute: [Function: bound addAttribute],
  removeAttribute: [Function: bound removeAttribute]
}
*/

//Validating an attribute
const isEmpty = analyzedElement.element.attributes[":data"] instanceof EmptyAttribute // => true

//Adding an attribute to the element
attributes.addAttribute({"name": "on:mouseover", "value": `{() => alert("hey")}`})
console.log(attributes.element.rawElement) /* => 
<button on:click={() => {value++}} test="yes" :data on:mouseover={() => alert("hey")}>testing</button>
*/

//Removing an attribute from the element
attributes.removeAttribute("on:mouseover")
console.log(attributes.element.rawElement) /* => 
<button on:click={() => {value++}} test="yes" :data >testing</button>
*/

Installation

npm install attribute-analyzer

Features

👻 0 dependencies

😎 Fast

🐱‍👤 Ability to parse complex attributes

License

MIT

Keywords

FAQs

Package last updated on 24 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc