@oscarpalmer/toretto 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -437,6 +437,34 @@ var tabbableFilters = [
437
437
  isNotTabbableRadio,
438
438
  ...focusableFilters
439
439
  ];
440
+ // src/sanitise.ts
441
+ function sanitise(value2, options) {
442
+ return sanitiseNodes(Array.isArray(value2) ? value2 : [value2], {
443
+ sanitiseBooleanAttributes: options?.sanitiseBooleanAttributes ?? true
444
+ });
445
+ }
446
+ function sanitiseAttributes(element, attributes, options) {
447
+ const { length } = attributes;
448
+ for (let index = 0;index < length; index += 1) {
449
+ const attribute2 = attributes[index];
450
+ if (isBadAttribute(attribute2) || isEmptyNonBooleanAttribute(attribute2)) {
451
+ element.removeAttribute(attribute2.name);
452
+ } else if (options.sanitiseBooleanAttributes && isInvalidBooleanAttribute(attribute2)) {
453
+ element.setAttribute(attribute2.name, "");
454
+ }
455
+ }
456
+ }
457
+ function sanitiseNodes(nodes, options) {
458
+ const { length } = nodes;
459
+ for (let index = 0;index < length; index += 1) {
460
+ const node = nodes[index];
461
+ if (node instanceof Element) {
462
+ sanitiseAttributes(node, [...node.attributes], options);
463
+ }
464
+ sanitiseNodes([...node.childNodes], options);
465
+ }
466
+ return nodes;
467
+ }
440
468
  // src/style.ts
441
469
  function getStyle(element, property) {
442
470
  return element.style[property];
@@ -476,6 +504,7 @@ export {
476
504
  setData,
477
505
  setAttributes,
478
506
  setAttribute,
507
+ sanitise,
479
508
  isTabbable,
480
509
  isInvalidBooleanAttribute,
481
510
  isFocusable,
package/dist/index.mjs CHANGED
@@ -4,4 +4,5 @@ export * from "./data";
4
4
  export * from "./find";
5
5
  export * from "./focusable";
6
6
  export * from "./models";
7
+ export * from "./sanitise";
7
8
  export * from "./style";
@@ -0,0 +1,75 @@
1
+ // src/attribute.ts
2
+ function isBadAttribute(attribute) {
3
+ return onPrefix.test(attribute.name) || sourcePrefix.test(attribute.name) && valuePrefix.test(attribute.value);
4
+ }
5
+ function isEmptyNonBooleanAttribute(attribute) {
6
+ return !booleanAttributes.includes(attribute.name) && attribute.value.trim().length === 0;
7
+ }
8
+ function isInvalidBooleanAttribute(attribute) {
9
+ if (!booleanAttributes.includes(attribute.name)) {
10
+ return true;
11
+ }
12
+ const normalised = attribute.value.toLowerCase().trim();
13
+ return !(normalised.length === 0 || normalised === attribute.name || attribute.name === "hidden" && normalised === "until-found");
14
+ }
15
+ var booleanAttributes = Object.freeze([
16
+ "async",
17
+ "autofocus",
18
+ "autoplay",
19
+ "checked",
20
+ "controls",
21
+ "default",
22
+ "defer",
23
+ "disabled",
24
+ "formnovalidate",
25
+ "hidden",
26
+ "inert",
27
+ "ismap",
28
+ "itemscope",
29
+ "loop",
30
+ "multiple",
31
+ "muted",
32
+ "nomodule",
33
+ "novalidate",
34
+ "open",
35
+ "playsinline",
36
+ "readonly",
37
+ "required",
38
+ "reversed",
39
+ "selected"
40
+ ]);
41
+ var onPrefix = /^on/i;
42
+ var sourcePrefix = /^(href|src|xlink:href)$/i;
43
+ var valuePrefix = /(data:text\/html|javascript:)/i;
44
+
45
+ // src/sanitise.ts
46
+ function sanitise(value2, options) {
47
+ return sanitiseNodes(Array.isArray(value2) ? value2 : [value2], {
48
+ sanitiseBooleanAttributes: options?.sanitiseBooleanAttributes ?? true
49
+ });
50
+ }
51
+ function sanitiseAttributes(element, attributes, options) {
52
+ const { length } = attributes;
53
+ for (let index = 0;index < length; index += 1) {
54
+ const attribute2 = attributes[index];
55
+ if (isBadAttribute(attribute2) || isEmptyNonBooleanAttribute(attribute2)) {
56
+ element.removeAttribute(attribute2.name);
57
+ } else if (options.sanitiseBooleanAttributes && isInvalidBooleanAttribute(attribute2)) {
58
+ element.setAttribute(attribute2.name, "");
59
+ }
60
+ }
61
+ }
62
+ function sanitiseNodes(nodes, options) {
63
+ const { length } = nodes;
64
+ for (let index = 0;index < length; index += 1) {
65
+ const node = nodes[index];
66
+ if (node instanceof Element) {
67
+ sanitiseAttributes(node, [...node.attributes], options);
68
+ }
69
+ sanitiseNodes([...node.childNodes], options);
70
+ }
71
+ return nodes;
72
+ }
73
+ export {
74
+ sanitise
75
+ };
@@ -0,0 +1,36 @@
1
+ // src/sanitise.ts
2
+ import {
3
+ isBadAttribute,
4
+ isEmptyNonBooleanAttribute,
5
+ isInvalidBooleanAttribute
6
+ } from "./attribute";
7
+ function sanitise(value, options) {
8
+ return sanitiseNodes(Array.isArray(value) ? value : [value], {
9
+ sanitiseBooleanAttributes: options?.sanitiseBooleanAttributes ?? true
10
+ });
11
+ }
12
+ function sanitiseAttributes(element, attributes, options) {
13
+ const { length } = attributes;
14
+ for (let index = 0;index < length; index += 1) {
15
+ const attribute2 = attributes[index];
16
+ if (isBadAttribute(attribute2) || isEmptyNonBooleanAttribute(attribute2)) {
17
+ element.removeAttribute(attribute2.name);
18
+ } else if (options.sanitiseBooleanAttributes && isInvalidBooleanAttribute(attribute2)) {
19
+ element.setAttribute(attribute2.name, "");
20
+ }
21
+ }
22
+ }
23
+ function sanitiseNodes(nodes, options) {
24
+ const { length } = nodes;
25
+ for (let index = 0;index < length; index += 1) {
26
+ const node = nodes[index];
27
+ if (node instanceof Element) {
28
+ sanitiseAttributes(node, [...node.attributes], options);
29
+ }
30
+ sanitiseNodes([...node.childNodes], options);
31
+ }
32
+ return nodes;
33
+ }
34
+ export {
35
+ sanitise
36
+ };
package/package.json CHANGED
@@ -55,6 +55,12 @@
55
55
  "types": "./types/models.d.ts",
56
56
  "bun": "./src/models.ts"
57
57
  },
58
+ "./sanitise": {
59
+ "types": "./types/sanitise.d.ts",
60
+ "bun": "./src/sanitise.ts",
61
+ "import": "./dist/sanitise.mjs",
62
+ "require": "./dist/sanitise.js"
63
+ },
58
64
  "./style": {
59
65
  "types": "./types/style.d.ts",
60
66
  "bun": "./src/style.ts",
@@ -91,5 +97,5 @@
91
97
  },
92
98
  "type": "module",
93
99
  "types": "types/index.d.cts",
94
- "version": "0.8.0"
100
+ "version": "0.9.0"
95
101
  }
package/src/index.ts CHANGED
@@ -3,4 +3,5 @@ export * from './data';
3
3
  export * from './find';
4
4
  export * from './focusable';
5
5
  export * from './models';
6
+ export * from './sanitise';
6
7
  export * from './style';
@@ -0,0 +1,63 @@
1
+ import {
2
+ isBadAttribute,
3
+ isEmptyNonBooleanAttribute,
4
+ isInvalidBooleanAttribute,
5
+ } from './attribute';
6
+
7
+ export type SanitiseOptions = {
8
+ /**
9
+ * - Sanitise boolean attributes? _(Defaults to `true`)_
10
+ * - E.g. `checked="abc"` => `checked=""`
11
+ */
12
+ sanitiseBooleanAttributes?: boolean;
13
+ };
14
+
15
+ /**
16
+ * - Sanitise one or more nodes _(as well as all their children)_:
17
+ * - Removes or sanitises bad attributes
18
+ */
19
+ export function sanitise(
20
+ value: Node | Node[],
21
+ options?: Partial<SanitiseOptions>,
22
+ ): Node[] {
23
+ return sanitiseNodes(Array.isArray(value) ? value : [value], {
24
+ sanitiseBooleanAttributes: options?.sanitiseBooleanAttributes ?? true,
25
+ });
26
+ }
27
+
28
+ function sanitiseAttributes(
29
+ element: Element,
30
+ attributes: Attr[],
31
+ options: SanitiseOptions,
32
+ ): void {
33
+ const {length} = attributes;
34
+
35
+ for (let index = 0; index < length; index += 1) {
36
+ const attribute = attributes[index];
37
+
38
+ if (isBadAttribute(attribute) || isEmptyNonBooleanAttribute(attribute)) {
39
+ element.removeAttribute(attribute.name);
40
+ } else if (
41
+ options.sanitiseBooleanAttributes &&
42
+ isInvalidBooleanAttribute(attribute)
43
+ ) {
44
+ element.setAttribute(attribute.name, '');
45
+ }
46
+ }
47
+ }
48
+
49
+ function sanitiseNodes(nodes: Node[], options: SanitiseOptions): Node[] {
50
+ const {length} = nodes;
51
+
52
+ for (let index = 0; index < length; index += 1) {
53
+ const node = nodes[index];
54
+
55
+ if (node instanceof Element) {
56
+ sanitiseAttributes(node, [...node.attributes], options);
57
+ }
58
+
59
+ sanitiseNodes([...node.childNodes], options);
60
+ }
61
+
62
+ return nodes;
63
+ }
package/types/index.d.cts CHANGED
@@ -137,6 +137,18 @@ export declare function isFocusable(element: Element): boolean;
137
137
  * Is the element tabbable?
138
138
  */
139
139
  export declare function isTabbable(element: Element): boolean;
140
+ export type SanitiseOptions = {
141
+ /**
142
+ * - Sanitise boolean attributes? _(Defaults to `true`)_
143
+ * - E.g. `checked="abc"` => `checked=""`
144
+ */
145
+ sanitiseBooleanAttributes?: boolean;
146
+ };
147
+ /**
148
+ * - Sanitise one or more nodes _(as well as all their children)_:
149
+ * - Removes or sanitises bad attributes
150
+ */
151
+ export declare function sanitise(value: Node | Node[], options?: Partial<SanitiseOptions>): Node[];
140
152
  /**
141
153
  * Get a style from an element
142
154
  */
package/types/index.d.ts CHANGED
@@ -3,4 +3,5 @@ export * from './data';
3
3
  export * from './find';
4
4
  export * from './focusable';
5
5
  export * from './models';
6
+ export * from './sanitise';
6
7
  export * from './style';
@@ -0,0 +1,12 @@
1
+ export type SanitiseOptions = {
2
+ /**
3
+ * - Sanitise boolean attributes? _(Defaults to `true`)_
4
+ * - E.g. `checked="abc"` => `checked=""`
5
+ */
6
+ sanitiseBooleanAttributes?: boolean;
7
+ };
8
+ /**
9
+ * - Sanitise one or more nodes _(as well as all their children)_:
10
+ * - Removes or sanitises bad attributes
11
+ */
12
+ export declare function sanitise(value: Node | Node[], options?: Partial<SanitiseOptions>): Node[];