@oscarpalmer/toretto 0.28.0 → 0.30.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/attribute/index.js +13 -1
- package/dist/html/index.js +63 -0
- package/dist/html/sanitize.js +40 -0
- package/dist/index.js +3 -6
- package/dist/internal/attribute.js +52 -25
- package/dist/toretto.full.js +760 -1026
- package/package.json +12 -12
- package/src/attribute/index.ts +87 -6
- package/src/{html.ts → html/index.ts} +52 -37
- package/src/html/sanitize.ts +83 -0
- package/src/index.ts +7 -2
- package/src/internal/attribute.ts +111 -121
- package/types/attribute/index.d.ts +57 -1
- package/types/{html.d.ts → html/index.d.ts} +4 -5
- package/types/html/sanitize.d.ts +2 -0
- package/types/index.d.ts +2 -2
- package/types/internal/attribute.d.ts +4 -55
- package/dist/html.js +0 -55
- package/dist/internal/sanitize.js +0 -30
- package/src/internal/sanitize.ts +0 -67
- package/types/internal/sanitize.d.ts +0 -13
|
@@ -1,3 +1,59 @@
|
|
|
1
|
-
|
|
1
|
+
import type { Attribute } from '../models';
|
|
2
|
+
/**
|
|
3
|
+
* Is the attribute considered bad and potentially harmful?
|
|
4
|
+
* @param attribute Attribute to check
|
|
5
|
+
* @returns `true` if attribute is considered bad
|
|
6
|
+
*/
|
|
7
|
+
export declare function isBadAttribute(attribute: Attr | Attribute): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Is the attribute considered bad and potentially harmful?
|
|
10
|
+
* @param name Attribute name
|
|
11
|
+
* @param value Attribute value
|
|
12
|
+
* @returns `true` if attribute is considered bad
|
|
13
|
+
*/
|
|
14
|
+
export declare function isBadAttribute(name: string, value: string): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Is the attribute a boolean attribute?
|
|
17
|
+
* @param name Attribute to check
|
|
18
|
+
* @returns `true` if attribute is a boolean attribute
|
|
19
|
+
*/
|
|
20
|
+
export declare function isBooleanAttribute(attribute: Attr | Attribute): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Is the attribute a boolean attribute?
|
|
23
|
+
* @param name Attribute name
|
|
24
|
+
* @returns `true` if attribute is a boolean attribute
|
|
25
|
+
*/
|
|
26
|
+
export declare function isBooleanAttribute(name: string): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Is the attribute empty and not a boolean attribute?
|
|
29
|
+
* @param attribute Attribute to check
|
|
30
|
+
* @returns `true` if attribute is empty and not a boolean attribute
|
|
31
|
+
*/
|
|
32
|
+
export declare function isEmptyNonBooleanAttribute(attribute: Attr | Attribute): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Is the attribute empty and not a boolean attribute?
|
|
35
|
+
* @param name Attribute name
|
|
36
|
+
* @param value Attribute value
|
|
37
|
+
* @returns `true` if attribute is empty and not a boolean attribute
|
|
38
|
+
*/
|
|
39
|
+
export declare function isEmptyNonBooleanAttribute(name: string, value: string): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Is the attribute an invalid boolean attribute?
|
|
42
|
+
*
|
|
43
|
+
* _(I.e., its value is not empty or the same as its name)_
|
|
44
|
+
* @param attribute Attribute to check
|
|
45
|
+
* @returns `true` if attribute is an invalid boolean attribute
|
|
46
|
+
*/
|
|
47
|
+
export declare function isInvalidBooleanAttribute(attribute: Attr | Attribute): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Is the attribute an invalid boolean attribute?
|
|
50
|
+
*
|
|
51
|
+
* _(I.e., its value is not empty or the same as its name)_
|
|
52
|
+
* @param name Attribute name
|
|
53
|
+
* @param value Attribute value
|
|
54
|
+
* @returns `true` if attribute is an invalid boolean attribute
|
|
55
|
+
*/
|
|
56
|
+
export declare function isInvalidBooleanAttribute(name: string, value: string): boolean;
|
|
57
|
+
export { booleanAttributes } from '../internal/attribute';
|
|
2
58
|
export * from './get';
|
|
3
59
|
export * from './set';
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { type SanitizeOptions } from './internal/sanitize';
|
|
2
1
|
type Html = {
|
|
3
2
|
/**
|
|
4
3
|
* Create nodes from an HTML string or a template element
|
|
@@ -26,10 +25,10 @@ type Html = {
|
|
|
26
25
|
};
|
|
27
26
|
type HtmlOptions = {
|
|
28
27
|
/**
|
|
29
|
-
*
|
|
28
|
+
* Cache template element for the HTML string? _(defaults to `true`)_
|
|
30
29
|
*/
|
|
31
|
-
|
|
32
|
-
}
|
|
30
|
+
cache?: boolean;
|
|
31
|
+
};
|
|
33
32
|
declare const html: Html;
|
|
34
33
|
/**
|
|
35
34
|
* Sanitize one or more nodes, recursively
|
|
@@ -37,5 +36,5 @@ declare const html: Html;
|
|
|
37
36
|
* @param options Sanitization options
|
|
38
37
|
* @returns Sanitized nodes
|
|
39
38
|
*/
|
|
40
|
-
export declare function sanitize(value: Node | Node[]
|
|
39
|
+
export declare function sanitize(value: Node | Node[]): Node[];
|
|
41
40
|
export { html };
|
package/types/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import supportsTouch from './touch';
|
|
2
|
-
export
|
|
2
|
+
export { isBadAttribute, isBooleanAttribute, isEmptyNonBooleanAttribute, isInvalidBooleanAttribute, } from './attribute';
|
|
3
3
|
export * from './data';
|
|
4
4
|
export * from './event/index';
|
|
5
5
|
export * from './find/index';
|
|
6
6
|
export * from './focusable';
|
|
7
|
-
export * from './html';
|
|
7
|
+
export * from './html/index';
|
|
8
8
|
export * from './is';
|
|
9
9
|
export * from './models';
|
|
10
10
|
export * from './style';
|
|
@@ -1,59 +1,8 @@
|
|
|
1
1
|
import type { Attribute, HTMLOrSVGElement, Property } from '../models';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*/
|
|
7
|
-
export declare function isBadAttribute(attribute: Attr | Attribute): boolean;
|
|
8
|
-
/**
|
|
9
|
-
* Is the attribute considered bad and potentially harmful?
|
|
10
|
-
* @param name Attribute name
|
|
11
|
-
* @param value Attribute value
|
|
12
|
-
* @returns `true` if attribute is considered bad
|
|
13
|
-
*/
|
|
14
|
-
export declare function isBadAttribute(name: string, value: string): boolean;
|
|
15
|
-
/**
|
|
16
|
-
* Is the attribute a boolean attribute?
|
|
17
|
-
* @param name Attribute to check
|
|
18
|
-
* @returns `true` if attribute is a boolean attribute
|
|
19
|
-
*/
|
|
20
|
-
export declare function isBooleanAttribute(attribute: Attr | Attribute): boolean;
|
|
21
|
-
/**
|
|
22
|
-
* Is the attribute a boolean attribute?
|
|
23
|
-
* @param name Attribute name
|
|
24
|
-
* @returns `true` if attribute is a boolean attribute
|
|
25
|
-
*/
|
|
26
|
-
export declare function isBooleanAttribute(name: string): boolean;
|
|
27
|
-
/**
|
|
28
|
-
* Is the attribute empty and not a boolean attribute?
|
|
29
|
-
* @param attribute Attribute to check
|
|
30
|
-
* @returns `true` if attribute is empty and not a boolean attribute
|
|
31
|
-
*/
|
|
32
|
-
export declare function isEmptyNonBooleanAttribute(attribute: Attr | Attribute): boolean;
|
|
33
|
-
/**
|
|
34
|
-
* Is the attribute empty and not a boolean attribute?
|
|
35
|
-
* @param name Attribute name
|
|
36
|
-
* @param value Attribute value
|
|
37
|
-
* @returns `true` if attribute is empty and not a boolean attribute
|
|
38
|
-
*/
|
|
39
|
-
export declare function isEmptyNonBooleanAttribute(name: string, value: string): boolean;
|
|
40
|
-
/**
|
|
41
|
-
* Is the attribute an invalid boolean attribute?
|
|
42
|
-
*
|
|
43
|
-
* _(I.e., its value is not empty or the same as its name)_
|
|
44
|
-
* @param attribute Attribute to check
|
|
45
|
-
* @returns `true` if attribute is an invalid boolean attribute
|
|
46
|
-
*/
|
|
47
|
-
export declare function isInvalidBooleanAttribute(attribute: Attr | Attribute): boolean;
|
|
48
|
-
/**
|
|
49
|
-
* Is the attribute an invalid boolean attribute?
|
|
50
|
-
*
|
|
51
|
-
* _(I.e., its value is not empty or the same as its name)_
|
|
52
|
-
* @param name Attribute name
|
|
53
|
-
* @param value Attribute value
|
|
54
|
-
* @returns `true` if attribute is an invalid boolean attribute
|
|
55
|
-
*/
|
|
56
|
-
export declare function isInvalidBooleanAttribute(name: string, value: string): boolean;
|
|
2
|
+
export declare function isBadAttribute(first: unknown, second: unknown, decode: boolean): boolean;
|
|
3
|
+
export declare function isBooleanAttribute(first: unknown, decode: boolean): boolean;
|
|
4
|
+
export declare function isEmptyNonBooleanAttribute(first: unknown, second: unknown, decode: boolean): boolean;
|
|
5
|
+
export declare function isInvalidBooleanAttribute(first: unknown, second: unknown, decode: boolean): boolean;
|
|
57
6
|
export declare function isProperty(value: unknown): value is Property;
|
|
58
7
|
export declare function updateValue(element: HTMLOrSVGElement, first: unknown, second: unknown): void;
|
|
59
8
|
export declare function updateValues(element: HTMLOrSVGElement, values: Attribute<unknown>[] | Record<string, unknown>): void;
|
package/dist/html.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { getSanitizeOptions, sanitizeNodes } from "./internal/sanitize.js";
|
|
2
|
-
import { isPlainObject } from "@oscarpalmer/atoms/is";
|
|
3
|
-
function createTemplate(html$1, ignore) {
|
|
4
|
-
const template = document.createElement("template");
|
|
5
|
-
template.innerHTML = html$1;
|
|
6
|
-
if (!ignore) templates[html$1] = template;
|
|
7
|
-
return template;
|
|
8
|
-
}
|
|
9
|
-
function getHtml(value, options) {
|
|
10
|
-
if (typeof value !== "string" && !(value instanceof HTMLTemplateElement)) return [];
|
|
11
|
-
const template = value instanceof HTMLTemplateElement ? value : getTemplate(value, options.ignoreCache);
|
|
12
|
-
if (template == null) return [];
|
|
13
|
-
const cloned = template.content.cloneNode(true);
|
|
14
|
-
const scripts = cloned.querySelectorAll("script");
|
|
15
|
-
for (const script of scripts) script.remove();
|
|
16
|
-
cloned.normalize();
|
|
17
|
-
return sanitizeNodes([...cloned.childNodes], options);
|
|
18
|
-
}
|
|
19
|
-
function getOptions(input) {
|
|
20
|
-
const options = isPlainObject(input) ? input : {};
|
|
21
|
-
options.ignoreCache = typeof options.ignoreCache === "boolean" ? options.ignoreCache : false;
|
|
22
|
-
options.sanitizeBooleanAttributes = typeof options.sanitizeBooleanAttributes === "boolean" ? options.sanitizeBooleanAttributes : true;
|
|
23
|
-
return options;
|
|
24
|
-
}
|
|
25
|
-
function getTemplate(value, ignore) {
|
|
26
|
-
if (typeof value !== "string" || value.trim().length === 0) return;
|
|
27
|
-
let template = templates[value];
|
|
28
|
-
if (template != null) return template;
|
|
29
|
-
const element = EXPRESSION_ID.test(value) ? document.querySelector(`#${value}`) : null;
|
|
30
|
-
template = element instanceof HTMLTemplateElement ? element : createTemplate(value, ignore);
|
|
31
|
-
return template;
|
|
32
|
-
}
|
|
33
|
-
var html = ((value, options) => {
|
|
34
|
-
return getHtml(value, getOptions(options));
|
|
35
|
-
});
|
|
36
|
-
html.clear = () => {
|
|
37
|
-
templates = {};
|
|
38
|
-
};
|
|
39
|
-
html.remove = (template) => {
|
|
40
|
-
if (typeof template !== "string" || templates[template] == null) return;
|
|
41
|
-
const keys = Object.keys(templates);
|
|
42
|
-
const { length } = keys;
|
|
43
|
-
const updated = {};
|
|
44
|
-
for (let index = 0; index < length; index += 1) {
|
|
45
|
-
const key = keys[index];
|
|
46
|
-
if (key !== template) updated[key] = templates[key];
|
|
47
|
-
}
|
|
48
|
-
templates = updated;
|
|
49
|
-
};
|
|
50
|
-
function sanitize(value, options) {
|
|
51
|
-
return sanitizeNodes(Array.isArray(value) ? value : [value], getSanitizeOptions(options));
|
|
52
|
-
}
|
|
53
|
-
var EXPRESSION_ID = /^[a-z][\w-]*$/i;
|
|
54
|
-
var templates = {};
|
|
55
|
-
export { html, sanitize };
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { isBadAttribute, isEmptyNonBooleanAttribute, isInvalidBooleanAttribute } from "./attribute.js";
|
|
2
|
-
import { isPlainObject } from "@oscarpalmer/atoms/is";
|
|
3
|
-
function getSanitizeOptions(input) {
|
|
4
|
-
const options = isPlainObject(input) ? input : {};
|
|
5
|
-
options.sanitizeBooleanAttributes = typeof options.sanitizeBooleanAttributes === "boolean" ? options.sanitizeBooleanAttributes : true;
|
|
6
|
-
return options;
|
|
7
|
-
}
|
|
8
|
-
function sanitizeAttributes(element, attributes, options) {
|
|
9
|
-
const { length } = attributes;
|
|
10
|
-
for (let index = 0; index < length; index += 1) {
|
|
11
|
-
const attribute = attributes[index];
|
|
12
|
-
if (isBadAttribute(attribute) || isEmptyNonBooleanAttribute(attribute)) element.removeAttribute(attribute.name);
|
|
13
|
-
else if (options.sanitizeBooleanAttributes && isInvalidBooleanAttribute(attribute)) element.setAttribute(attribute.name, "");
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
function sanitizeNodes(nodes, options) {
|
|
17
|
-
const actual = nodes.filter((node) => node instanceof Node);
|
|
18
|
-
const { length } = nodes;
|
|
19
|
-
for (let index = 0; index < length; index += 1) {
|
|
20
|
-
const node = actual[index];
|
|
21
|
-
if (node instanceof Element) {
|
|
22
|
-
const scripts = node.querySelectorAll("script");
|
|
23
|
-
for (const script of scripts) script.remove();
|
|
24
|
-
sanitizeAttributes(node, [...node.attributes], options);
|
|
25
|
-
}
|
|
26
|
-
if (node.hasChildNodes()) sanitizeNodes([...node.childNodes], options);
|
|
27
|
-
}
|
|
28
|
-
return nodes;
|
|
29
|
-
}
|
|
30
|
-
export { getSanitizeOptions, sanitizeAttributes, sanitizeNodes };
|
package/src/internal/sanitize.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import {isPlainObject} from '@oscarpalmer/atoms/is';
|
|
2
|
-
import {isBadAttribute, isEmptyNonBooleanAttribute, isInvalidBooleanAttribute} from './attribute';
|
|
3
|
-
|
|
4
|
-
//
|
|
5
|
-
|
|
6
|
-
type Options = Required<SanitizeOptions>;
|
|
7
|
-
|
|
8
|
-
export type SanitizeOptions = {
|
|
9
|
-
/**
|
|
10
|
-
* Sanitize boolean attributes? _(Defaults to `true`)_
|
|
11
|
-
*
|
|
12
|
-
* E.g. `checked="abc"` => `checked=""`
|
|
13
|
-
*/
|
|
14
|
-
sanitizeBooleanAttributes?: boolean;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
//
|
|
18
|
-
|
|
19
|
-
export function getSanitizeOptions(input?: SanitizeOptions): Options {
|
|
20
|
-
const options = isPlainObject(input) ? input : {};
|
|
21
|
-
|
|
22
|
-
options.sanitizeBooleanAttributes =
|
|
23
|
-
typeof options.sanitizeBooleanAttributes === 'boolean'
|
|
24
|
-
? options.sanitizeBooleanAttributes
|
|
25
|
-
: true;
|
|
26
|
-
|
|
27
|
-
return options as Options;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function sanitizeAttributes(element: Element, attributes: Attr[], options: Options): void {
|
|
31
|
-
const {length} = attributes;
|
|
32
|
-
|
|
33
|
-
for (let index = 0; index < length; index += 1) {
|
|
34
|
-
const attribute = attributes[index];
|
|
35
|
-
|
|
36
|
-
if (isBadAttribute(attribute) || isEmptyNonBooleanAttribute(attribute)) {
|
|
37
|
-
element.removeAttribute(attribute.name);
|
|
38
|
-
} else if (options.sanitizeBooleanAttributes && isInvalidBooleanAttribute(attribute)) {
|
|
39
|
-
element.setAttribute(attribute.name, '');
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function sanitizeNodes(nodes: Node[], options: Options): Node[] {
|
|
45
|
-
const actual = nodes.filter(node => node instanceof Node);
|
|
46
|
-
const {length} = nodes;
|
|
47
|
-
|
|
48
|
-
for (let index = 0; index < length; index += 1) {
|
|
49
|
-
const node = actual[index];
|
|
50
|
-
|
|
51
|
-
if (node instanceof Element) {
|
|
52
|
-
const scripts = node.querySelectorAll('script');
|
|
53
|
-
|
|
54
|
-
for (const script of scripts) {
|
|
55
|
-
script.remove();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
sanitizeAttributes(node, [...node.attributes], options);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (node.hasChildNodes()) {
|
|
62
|
-
sanitizeNodes([...node.childNodes], options);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return nodes;
|
|
67
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
type Options = Required<SanitizeOptions>;
|
|
2
|
-
export type SanitizeOptions = {
|
|
3
|
-
/**
|
|
4
|
-
* Sanitize boolean attributes? _(Defaults to `true`)_
|
|
5
|
-
*
|
|
6
|
-
* E.g. `checked="abc"` => `checked=""`
|
|
7
|
-
*/
|
|
8
|
-
sanitizeBooleanAttributes?: boolean;
|
|
9
|
-
};
|
|
10
|
-
export declare function getSanitizeOptions(input?: SanitizeOptions): Options;
|
|
11
|
-
export declare function sanitizeAttributes(element: Element, attributes: Attr[], options: Options): void;
|
|
12
|
-
export declare function sanitizeNodes(nodes: Node[], options: Options): Node[];
|
|
13
|
-
export {};
|