attribute-analyzer
Advanced tools
Comparing version 1.0.4 to 1.0.5
42
index.js
export class EmptyAttribute {} | ||
export function getAttributes(element) { | ||
const localName = getLocalName(element); | ||
element = element.replace("<"+localName, ''); | ||
if(element.endsWith(`</${localName}>`)) element = element.slice(0, -(`</${localName}>`.length)); | ||
if(element.endsWith(`/>`)) element = element.slice(0, -(`/>`.length)); | ||
if(element.endsWith(`>`)) element = element.slice(0, -(`>`.length)); | ||
const attributes = parseAttributes(element); | ||
return correctify(attributes); | ||
element = element.trim() | ||
const localName = getLocalName(element) | ||
//determine it is a self closing tag | ||
if (element.endsWith(`/>`) && element.startsWith(`<${localName}`)) { | ||
const lastIndexOf = element.lastIndexOf(`/>`) | ||
element = element.slice(localName.length + 1, lastIndexOf).trim() | ||
} else if (element.endsWith(`</${localName}>`) && element.startsWith(`<${localName}`)) { | ||
element = element.slice(localName.length + 1, -localName.length - 3).trim() | ||
const lastIndexOf = element.lastIndexOf(`>`) | ||
element = element.slice(0, lastIndexOf).trim() | ||
} else { | ||
throw new Error("Parsing Error: Element does not have a closing tag") | ||
} | ||
const attributes = parseAttributes(element); | ||
return correctify(attributes); | ||
} | ||
@@ -16,3 +30,3 @@ | ||
Object.keys(attributes).forEach(key => { | ||
if(key == "") { | ||
if (key == "") { | ||
newobj[attributes[key]] = new EmptyAttribute(); | ||
@@ -66,8 +80,8 @@ } else { | ||
export function getLocalName(input) { | ||
const openingTagEndIndex = input.indexOf('>'); | ||
const tagStringWithoutClosingSlash = input.slice(0, openingTagEndIndex).trimEnd(); | ||
const tagName = tagStringWithoutClosingSlash.split(' ')[0].replace("<", ''); | ||
return tagName; | ||
const openingTagEndIndex = input.indexOf('>'); | ||
const tagStringWithoutClosingSlash = input.slice(0, openingTagEndIndex).trimEnd(); | ||
const tagName = tagStringWithoutClosingSlash.split(' ')[0].replace("<", ''); | ||
return tagName; | ||
} |
{ | ||
"name": "attribute-analyzer", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Parse your complex element attributes with ease", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -12,3 +12,3 @@ <h1 align="center">attribute-analyzer</h1> | ||
const input = `<a style="background-color: {red}" :data={"sa" + hello} test="yes" selected on:click='{() => alert("{}")}'></a>`; | ||
const input = `<a style="background-color: {red}" :data={"sa" + hello} test="yes" selected on:click='{() => alert("{}")}'>hello</a>`; | ||
@@ -15,0 +15,0 @@ //Getting the local name |
import {EmptyAttribute, getAttributes, getLocalName } from "./index.js"; | ||
const input = `<a style="background-color: {red}" :data={"sa" + hello} test="yes" selected on:click='{() => alert("{}")}'></a>`; | ||
const input = `<a style="background-color: {red};" :data={"sa" + hello} test="yes" selected on:click='{() => alert("{}")}'/>`; | ||
const attributes = getAttributes(input); | ||
console.log(getLocalName(input)) | ||
console.log(attributes) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5324
75