attributes-parser
Advanced tools
Comparing version 2.1.3 to 2.2.0
@@ -9,2 +9,15 @@ /** | ||
/** | ||
* Ensure that the string doesn't start with any of the prohibited characters, | ||
* including space characters, U+0000 NULL, U+0022 QUOTATION MARK | ||
* (""), U+0027 APOSTROPHE ("'"), U+003E GREATER-THAN SIGN (>), | ||
* U+002F SOLIDUS (/), U+003D EQUALS SIGN (=), control characters | ||
* (U+0000 to U+001F, U+007F to U+009F), and characters that are not | ||
* defined by Unicode. Following by one or more characters that are not in | ||
* the prohibited set of characters. | ||
* | ||
* @see [HTML syntax attributes](https://www.w3.org/TR/2011/WD-html5-20110525/syntax.html#syntax-attributes) | ||
*/ | ||
export declare const AttributeName: RegExp; | ||
export declare const AttributeShorthand: RegExp; | ||
/** | ||
* Matches boolean literals, allowing for optional single or double quotes. | ||
@@ -46,13 +59,1 @@ */ | ||
export declare const UnquotedLiteral: RegExp; | ||
/** | ||
* Ensure that the string doesn't start with any of the prohibited characters, | ||
* including space characters, U+0000 NULL, U+0022 QUOTATION MARK | ||
* (""), U+0027 APOSTROPHE ("'"), U+003E GREATER-THAN SIGN (>), | ||
* U+002F SOLIDUS (/), U+003D EQUALS SIGN (=), control characters | ||
* (U+0000 to U+001F, U+007F to U+009F), and characters that are not | ||
* defined by Unicode. Following by one or more characters that are not in | ||
* the prohibited set of characters. | ||
* | ||
* @see [HTML syntax attributes](https://www.w3.org/TR/2011/WD-html5-20110525/syntax.html#syntax-attributes) | ||
*/ | ||
export declare const AttributeName: RegExp; |
@@ -1,1 +0,1 @@ | ||
"use strict";const o=require("moo"),u=require("json-loose"),c=/[ \t\v\f\ufeff]+/,f=new RegExp("(?<==)(?:true|false)"),b=new RegExp("(?<==)-?(?:(?:0[xX][\\da-fA-F](?:_?[\\da-fA-F])*|0[oO][0-7](?:_?[0-7])*|0[bB][01](?:_?[01])*)n?|-?0n|-?[1-9](?:_?\\d)*n|(?:(?:0(?!\\d)|0\\d*[89]\\d*|[1-9](?:_?\\d)*)(?:\\.(?:\\d(?:_?\\d)*)?)?|\\.\\d(?:_?\\d)*)(?:[eE][+-]?\\d(?:_?\\d)*)?|-?0[0-7]+)"),d=new RegExp(`(?<==)'(?!.*&[0-9a-zA-Z]+;)[^'\\\\]*(?:\\\\.|\\\\n[^"\\\\]*|&[^0-9a-zA-Z;]*)*'`),m=new RegExp('(?<==)"(?!.*&[0-9a-zA-Z]+;)[^"\\\\]*(?:\\\\.|\\\\n[^"\\\\]*|&[^0-9a-zA-Z;]*)*"'),x=new RegExp("(?<==)[^\"\\s'`=<>\\x00]+"),L=/(?:(?![\s\x00\x22\x27\x3E\x2F\x3D\x00-\x1F\x7F-\x9F])[^\s\x00-\x1F\x7F-\x9F\x22\x27\x3E\x2F\x3D])+/;function n(t){const e=typeof t=="string"&&/^(['"]).*?\1$/.test(t)?t.slice(1,-1):t;return e.startsWith("[")&&e.endsWith("]")||e.startsWith("{")&&e.endsWith("}")?JSON.parse(u(e)):e}function g(t){let e="";for(const a in t){const r=t[a];switch(typeof r){case"object":e+=` ${a}='${JSON.stringify(r)}'`;break;case"string":e+=` ${a}="${r}"`;break;case"number":case"boolean":e+=` ${a}=${r}`;break}}return e.slice(1)}const i=o.states({main:{WhiteSpace:c,BooleanLiteral:{match:f,value(t){return t==="true"}},NumericLiteral:{match:b,value(t){const e=Number(t);return Number.isNaN(e)?Number(t.replace(/_|n$/g,"")):Number(t)}},SingleQuotedValue:{match:d,value:n,type:()=>"StringLiteral"},DoubleQuotedLiteral:{match:m,value:n,type:()=>"StringLiteral"},UnquotedLiteral:{match:x,value:n,type:()=>"StringLiteral"},AttributeName:L,Separator:"="}});function N(t){let e=null;const a=i.reset(t),r={};Object.defineProperties(r,{toString:{writable:!1,enumerable:!1,configurable:!1,value:()=>g(r)},getTokens:{writable:!1,enumerable:!1,configurable:!1,value:()=>Array.from(i.reset(t))}});for(const{type:l,value:s}of a)switch(l){case"AttributeName":e=s,r[e]=e;break;case"BooleanLiteral":case"NumericLiteral":case"StringLiteral":e&&(r[e]=s,e=null);break}return r}module.exports=N; | ||
"use strict";const u=require("moo"),f=require("json-loose"),b=/[ \t\v\f\ufeff]+/,d=/(?:(?![\s\x00\x22\x27\x3E\x2F\x3D\x00-\x1F\x7F-\x9F])[^\s\x00-\x1F\x7F-\x9F\x22\x27\x3E\x2F\x3D])+/,m=/[\.#](?:(?!-?\d)(?:[a-zA-Z0-9\xA0-\uFFFF_-])+)/,h=new RegExp("(?<==)(?:true|false)"),x=new RegExp("(?<==)-?(?:(?:0[xX][\\da-fA-F](?:_?[\\da-fA-F])*|0[oO][0-7](?:_?[0-7])*|0[bB][01](?:_?[01])*)n?|-?0n|-?[1-9](?:_?\\d)*n|(?:(?:0(?!\\d)|0\\d*[89]\\d*|[1-9](?:_?\\d)*)(?:\\.(?:\\d(?:_?\\d)*)?)?|\\.\\d(?:_?\\d)*)(?:[eE][+-]?\\d(?:_?\\d)*)?|-?0[0-7]+)"),g=new RegExp(`(?<==)'(?!.*&[0-9a-zA-Z]+;)[^'\\\\]*(?:\\\\.|\\\\n[^"\\\\]*|&[^0-9a-zA-Z;]*)*'`),L=new RegExp('(?<==)"(?!.*&[0-9a-zA-Z]+;)[^"\\\\]*(?:\\\\.|\\\\n[^"\\\\]*|&[^0-9a-zA-Z;]*)*"'),p=new RegExp("(?<==)[^\"\\s'`=<>\\x00]+");function i(t){const e=typeof t=="string"&&/^(['"]).*?\1$/.test(t)?t.slice(1,-1):t;return e.startsWith("[")&&e.endsWith("]")||e.startsWith("{")&&e.endsWith("}")?JSON.parse(f(e)):e}function A(t){let e="";for(const a in t){const r=t[a];switch(typeof r){case"object":e+=` ${a}='${JSON.stringify(r)}'`;break;case"string":e+=` ${a}="${r}"`;break;case"number":case"boolean":e+=` ${a}=${r}`;break}}return e.slice(1)}const o=u.states({main:{WhiteSpace:b,AttributeShorthand:m,BooleanLiteral:{match:h,value(t){return t==="true"}},NumericLiteral:{match:x,value(t){const e=Number(t);return Number.isNaN(e)?Number(t.replace(/_|n$/g,"")):Number(t)}},SingleQuotedValue:{match:g,value:i,type:()=>"StringLiteral"},DoubleQuotedLiteral:{match:L,value:i,type:()=>"StringLiteral"},UnquotedLiteral:{match:p,value:i,type:()=>"StringLiteral"},AttributeName:d,Separator:"="}});function F(t){let e=null;const a=o.reset(t),r={};Object.defineProperties(r,{toString:{writable:!1,enumerable:!1,configurable:!1,value:()=>A(r)},getTokens:{writable:!1,enumerable:!1,configurable:!1,value:()=>Array.from(o.reset(t))}});const n=[];for(const{type:c,value:s}of a)switch(c){case"AttributeName":e=s,r[e]=e;break;case"AttributeShorthand":const l=s[0];l==="."?n.push(s.slice(1)):l==="#"&&(r.id=s.slice(1));break;case"BooleanLiteral":case"NumericLiteral":case"StringLiteral":e&&(e==="class"&&n.push(s),r[e]=s,e=null);break}return n.length&&(r.class=n.join(" ")),r}module.exports=F; |
@@ -1,5 +0,5 @@ | ||
import i from "moo"; | ||
import u from "json-loose"; | ||
const c = /[ \t\v\f\ufeff]+/, f = new RegExp("(?<==)(?:true|false)"), m = new RegExp("(?<==)-?(?:(?:0[xX][\\da-fA-F](?:_?[\\da-fA-F])*|0[oO][0-7](?:_?[0-7])*|0[bB][01](?:_?[01])*)n?|-?0n|-?[1-9](?:_?\\d)*n|(?:(?:0(?!\\d)|0\\d*[89]\\d*|[1-9](?:_?\\d)*)(?:\\.(?:\\d(?:_?\\d)*)?)?|\\.\\d(?:_?\\d)*)(?:[eE][+-]?\\d(?:_?\\d)*)?|-?0[0-7]+)"), b = new RegExp(`(?<==)'(?!.*&[0-9a-zA-Z]+;)[^'\\\\]*(?:\\\\.|\\\\n[^"\\\\]*|&[^0-9a-zA-Z;]*)*'`), d = new RegExp('(?<==)"(?!.*&[0-9a-zA-Z]+;)[^"\\\\]*(?:\\\\.|\\\\n[^"\\\\]*|&[^0-9a-zA-Z;]*)*"'), x = new RegExp("(?<==)[^\"\\s'`=<>\\x00]+"), L = /(?:(?![\s\x00\x22\x27\x3E\x2F\x3D\x00-\x1F\x7F-\x9F])[^\s\x00-\x1F\x7F-\x9F\x22\x27\x3E\x2F\x3D])+/; | ||
function n(t) { | ||
import u from "moo"; | ||
import f from "json-loose"; | ||
const b = /[ \t\v\f\ufeff]+/, d = /(?:(?![\s\x00\x22\x27\x3E\x2F\x3D\x00-\x1F\x7F-\x9F])[^\s\x00-\x1F\x7F-\x9F\x22\x27\x3E\x2F\x3D])+/, m = /[\.#](?:(?!-?\d)(?:[a-zA-Z0-9\xA0-\uFFFF_-])+)/, h = new RegExp("(?<==)(?:true|false)"), x = new RegExp("(?<==)-?(?:(?:0[xX][\\da-fA-F](?:_?[\\da-fA-F])*|0[oO][0-7](?:_?[0-7])*|0[bB][01](?:_?[01])*)n?|-?0n|-?[1-9](?:_?\\d)*n|(?:(?:0(?!\\d)|0\\d*[89]\\d*|[1-9](?:_?\\d)*)(?:\\.(?:\\d(?:_?\\d)*)?)?|\\.\\d(?:_?\\d)*)(?:[eE][+-]?\\d(?:_?\\d)*)?|-?0[0-7]+)"), g = new RegExp(`(?<==)'(?!.*&[0-9a-zA-Z]+;)[^'\\\\]*(?:\\\\.|\\\\n[^"\\\\]*|&[^0-9a-zA-Z;]*)*'`), p = new RegExp('(?<==)"(?!.*&[0-9a-zA-Z]+;)[^"\\\\]*(?:\\\\.|\\\\n[^"\\\\]*|&[^0-9a-zA-Z;]*)*"'), L = new RegExp("(?<==)[^\"\\s'`=<>\\x00]+"); | ||
function i(t) { | ||
const e = typeof t == "string" && /^(['"]).*?\1$/.test(t) ? ( | ||
@@ -9,5 +9,5 @@ // omit quotes | ||
) : t; | ||
return e.startsWith("[") && e.endsWith("]") || e.startsWith("{") && e.endsWith("}") ? JSON.parse(u(e)) : e; | ||
return e.startsWith("[") && e.endsWith("]") || e.startsWith("{") && e.endsWith("}") ? JSON.parse(f(e)) : e; | ||
} | ||
function g(t) { | ||
function A(t) { | ||
let e = ""; | ||
@@ -31,7 +31,8 @@ for (const r in t) { | ||
} | ||
const l = i.states({ | ||
const o = u.states({ | ||
main: { | ||
WhiteSpace: c, | ||
WhiteSpace: b, | ||
AttributeShorthand: m, | ||
BooleanLiteral: { | ||
match: f, | ||
match: h, | ||
value(t) { | ||
@@ -42,3 +43,3 @@ return t === "true"; | ||
NumericLiteral: { | ||
match: m, | ||
match: x, | ||
value(t) { | ||
@@ -50,23 +51,23 @@ const e = Number(t); | ||
SingleQuotedValue: { | ||
match: b, | ||
value: n, | ||
match: g, | ||
value: i, | ||
type: () => "StringLiteral" | ||
}, | ||
DoubleQuotedLiteral: { | ||
match: d, | ||
value: n, | ||
match: p, | ||
value: i, | ||
type: () => "StringLiteral" | ||
}, | ||
UnquotedLiteral: { | ||
match: x, | ||
value: n, | ||
match: L, | ||
value: i, | ||
type: () => "StringLiteral" | ||
}, | ||
AttributeName: L, | ||
AttributeName: d, | ||
Separator: "=" | ||
} | ||
}); | ||
function h(t) { | ||
function N(t) { | ||
let e = null; | ||
const r = l.reset(t), a = {}; | ||
const r = o.reset(t), a = {}; | ||
Object.defineProperties(a, { | ||
@@ -77,3 +78,3 @@ toString: { | ||
configurable: !1, | ||
value: () => g(a) | ||
value: () => A(a) | ||
}, | ||
@@ -84,20 +85,25 @@ getTokens: { | ||
configurable: !1, | ||
value: () => Array.from(l.reset(t)) | ||
value: () => Array.from(o.reset(t)) | ||
} | ||
}); | ||
for (const { type: o, value: s } of r) | ||
switch (o) { | ||
const n = []; | ||
for (const { type: c, value: s } of r) | ||
switch (c) { | ||
case "AttributeName": | ||
e = s, a[e] = e; | ||
break; | ||
case "AttributeShorthand": | ||
const l = s[0]; | ||
l === "." ? n.push(s.slice(1)) : l === "#" && (a.id = s.slice(1)); | ||
break; | ||
case "BooleanLiteral": | ||
case "NumericLiteral": | ||
case "StringLiteral": | ||
e && (a[e] = s, e = null); | ||
e && (e === "class" && n.push(s), a[e] = s, e = null); | ||
break; | ||
} | ||
return a; | ||
return n.length && (a.class = n.join(" ")), a; | ||
} | ||
export { | ||
h as default | ||
N as default | ||
}; |
@@ -1,1 +0,1 @@ | ||
(function(a,s){typeof exports=="object"&&typeof module<"u"?module.exports=s(require("moo"),require("json-loose")):typeof define=="function"&&define.amd?define(["moo","json-loose"],s):(a=typeof globalThis<"u"?globalThis:a||self,a.parseAttrs=s(a.moo,a.jsonLoose))})(this,function(a,s){"use strict";const u=/[ \t\v\f\ufeff]+/,c=new RegExp("(?<==)(?:true|false)"),f=new RegExp("(?<==)-?(?:(?:0[xX][\\da-fA-F](?:_?[\\da-fA-F])*|0[oO][0-7](?:_?[0-7])*|0[bB][01](?:_?[01])*)n?|-?0n|-?[1-9](?:_?\\d)*n|(?:(?:0(?!\\d)|0\\d*[89]\\d*|[1-9](?:_?\\d)*)(?:\\.(?:\\d(?:_?\\d)*)?)?|\\.\\d(?:_?\\d)*)(?:[eE][+-]?\\d(?:_?\\d)*)?|-?0[0-7]+)"),d=new RegExp(`(?<==)'(?!.*&[0-9a-zA-Z]+;)[^'\\\\]*(?:\\\\.|\\\\n[^"\\\\]*|&[^0-9a-zA-Z;]*)*'`),m=new RegExp('(?<==)"(?!.*&[0-9a-zA-Z]+;)[^"\\\\]*(?:\\\\.|\\\\n[^"\\\\]*|&[^0-9a-zA-Z;]*)*"'),b=new RegExp("(?<==)[^\"\\s'`=<>\\x00]+"),x=/(?:(?![\s\x00\x22\x27\x3E\x2F\x3D\x00-\x1F\x7F-\x9F])[^\s\x00-\x1F\x7F-\x9F\x22\x27\x3E\x2F\x3D])+/;function o(t){const e=typeof t=="string"&&/^(['"]).*?\1$/.test(t)?t.slice(1,-1):t;return e.startsWith("[")&&e.endsWith("]")||e.startsWith("{")&&e.endsWith("}")?JSON.parse(s(e)):e}function p(t){let e="";for(const n in t){const r=t[n];switch(typeof r){case"object":e+=` ${n}='${JSON.stringify(r)}'`;break;case"string":e+=` ${n}="${r}"`;break;case"number":case"boolean":e+=` ${n}=${r}`;break}}return e.slice(1)}const i=a.states({main:{WhiteSpace:u,BooleanLiteral:{match:c,value(t){return t==="true"}},NumericLiteral:{match:f,value(t){const e=Number(t);return Number.isNaN(e)?Number(t.replace(/_|n$/g,"")):Number(t)}},SingleQuotedValue:{match:d,value:o,type:()=>"StringLiteral"},DoubleQuotedLiteral:{match:m,value:o,type:()=>"StringLiteral"},UnquotedLiteral:{match:b,value:o,type:()=>"StringLiteral"},AttributeName:x,Separator:"="}});function L(t){let e=null;const n=i.reset(t),r={};Object.defineProperties(r,{toString:{writable:!1,enumerable:!1,configurable:!1,value:()=>p(r)},getTokens:{writable:!1,enumerable:!1,configurable:!1,value:()=>Array.from(i.reset(t))}});for(const{type:h,value:l}of n)switch(h){case"AttributeName":e=l,r[e]=e;break;case"BooleanLiteral":case"NumericLiteral":case"StringLiteral":e&&(r[e]=l,e=null);break}return r}return L}); | ||
(function(s,i){typeof exports=="object"&&typeof module<"u"?module.exports=i(require("moo"),require("json-loose")):typeof define=="function"&&define.amd?define(["moo","json-loose"],i):(s=typeof globalThis<"u"?globalThis:s||self,s.parseAttrs=i(s.moo,s.jsonLoose))})(this,function(s,i){"use strict";const f=/[ \t\v\f\ufeff]+/,d=/(?:(?![\s\x00\x22\x27\x3E\x2F\x3D\x00-\x1F\x7F-\x9F])[^\s\x00-\x1F\x7F-\x9F\x22\x27\x3E\x2F\x3D])+/,b=/[\.#](?:(?!-?\d)(?:[a-zA-Z0-9\xA0-\uFFFF_-])+)/,m=new RegExp("(?<==)(?:true|false)"),h=new RegExp("(?<==)-?(?:(?:0[xX][\\da-fA-F](?:_?[\\da-fA-F])*|0[oO][0-7](?:_?[0-7])*|0[bB][01](?:_?[01])*)n?|-?0n|-?[1-9](?:_?\\d)*n|(?:(?:0(?!\\d)|0\\d*[89]\\d*|[1-9](?:_?\\d)*)(?:\\.(?:\\d(?:_?\\d)*)?)?|\\.\\d(?:_?\\d)*)(?:[eE][+-]?\\d(?:_?\\d)*)?|-?0[0-7]+)"),p=new RegExp(`(?<==)'(?!.*&[0-9a-zA-Z]+;)[^'\\\\]*(?:\\\\.|\\\\n[^"\\\\]*|&[^0-9a-zA-Z;]*)*'`),x=new RegExp('(?<==)"(?!.*&[0-9a-zA-Z]+;)[^"\\\\]*(?:\\\\.|\\\\n[^"\\\\]*|&[^0-9a-zA-Z;]*)*"'),L=new RegExp("(?<==)[^\"\\s'`=<>\\x00]+");function l(t){const e=typeof t=="string"&&/^(['"]).*?\1$/.test(t)?t.slice(1,-1):t;return e.startsWith("[")&&e.endsWith("]")||e.startsWith("{")&&e.endsWith("}")?JSON.parse(i(e)):e}function A(t){let e="";for(const a in t){const r=t[a];switch(typeof r){case"object":e+=` ${a}='${JSON.stringify(r)}'`;break;case"string":e+=` ${a}="${r}"`;break;case"number":case"boolean":e+=` ${a}=${r}`;break}}return e.slice(1)}const u=s.states({main:{WhiteSpace:f,AttributeShorthand:b,BooleanLiteral:{match:m,value(t){return t==="true"}},NumericLiteral:{match:h,value(t){const e=Number(t);return Number.isNaN(e)?Number(t.replace(/_|n$/g,"")):Number(t)}},SingleQuotedValue:{match:p,value:l,type:()=>"StringLiteral"},DoubleQuotedLiteral:{match:x,value:l,type:()=>"StringLiteral"},UnquotedLiteral:{match:L,value:l,type:()=>"StringLiteral"},AttributeName:d,Separator:"="}});function g(t){let e=null;const a=u.reset(t),r={};Object.defineProperties(r,{toString:{writable:!1,enumerable:!1,configurable:!1,value:()=>A(r)},getTokens:{writable:!1,enumerable:!1,configurable:!1,value:()=>Array.from(u.reset(t))}});const o=[];for(const{type:F,value:n}of a)switch(F){case"AttributeName":e=n,r[e]=e;break;case"AttributeShorthand":const c=n[0];c==="."?o.push(n.slice(1)):c==="#"&&(r.id=n.slice(1));break;case"BooleanLiteral":case"NumericLiteral":case"StringLiteral":e&&(e==="class"&&o.push(n),r[e]=n,e=null);break}return o.length&&(r.class=o.join(" ")),r}return g}); |
{ | ||
"name": "attributes-parser", | ||
"description": "Parsing and tokenizing attributes string", | ||
"version": "2.1.3", | ||
"version": "2.2.0", | ||
"publishConfig": { | ||
@@ -6,0 +6,0 @@ "access": "public" |
@@ -28,3 +28,3 @@ # Attributes Parser | ||
const attr = `id="my-id" class='my-class' num=3.14 numNeg=-3.14 data-num="3.14" data-value="123" data-value=1_000_000 options=\'{"key": "value", "array": [1, 2, 3]}\' data-list="[1, 2, 3]" punc="a=b,c,d,e" checked=false checked=false data-checked="false" disabled` | ||
const attr = `#my-id.foo.bar class="baz" num=3.14 numNeg=-3.14 data-num="3.14" data-value="123" data-value=1_000_000 options=\'{"key": "value", "array": [1, 2, 3]}\' data-list="[1, 2, 3]" punc="a=b,c,d,e" checked=false checked=false data-checked="false" disabled` | ||
const parsedAttr = parseAttrs(attr) | ||
@@ -41,8 +41,8 @@ | ||
{ | ||
id: 'my-id', | ||
class: 'my-class', | ||
id: 'my-id', // from shorthand attr #my-id | ||
class: 'foo bar baz', // from shorthand attr .foo.bar and class="baz" | ||
num: 3.14, // number | ||
numNeg: -3.14, // negative number | ||
'data-num': '3.14', // preserve string | ||
'data-value': 1000000, // duplicate key, second value is kept | ||
'data-value': 1000000, // any duplicate key but `class`, last value is kept | ||
options: { key: 'value', array: [ 1, 2, 3 ] }, | ||
@@ -101,2 +101,7 @@ 'data-list': [ 1, 2, 3 ], | ||
### `AttributeShorthand` | ||
- `#bar` (Shorthand for attribute `id="bar"`) | ||
- `.foo` (Shorthand for attribute `class="foo"`) | ||
### `BooleanLiteral` | ||
@@ -103,0 +108,0 @@ |
21993
266
231