1.0.6 ā€¢ Published 1 year ago

attribute-analyzer v1.0.6

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago
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

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago