@oscarpalmer/abydon 0.2.0 → 0.3.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/bun.lockb +0 -0
- package/dist/index.js +53 -3
- package/package.json +4 -1
- package/src/helpers/dom.ts +27 -0
- package/src/html.ts +27 -0
- package/src/index.ts +1 -0
- package/types/helpers/dom.d.ts +2 -0
- package/types/html.d.ts +1 -0
- package/types/index.d.ts +1 -1
- package/dist/index.mjs +0 -5
package/bun.lockb
CHANGED
|
Binary file
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,55 @@
|
|
|
1
|
-
//
|
|
2
|
-
var
|
|
1
|
+
// node_modules/@oscarpalmer/atoms/dist/js/string/index.mjs
|
|
2
|
+
var getString = function(value) {
|
|
3
|
+
if (typeof value === "string") {
|
|
4
|
+
return value;
|
|
5
|
+
}
|
|
6
|
+
if (typeof value !== "object" || value == null) {
|
|
7
|
+
return String(value);
|
|
8
|
+
}
|
|
9
|
+
const valueOff = value.valueOf?.() ?? value;
|
|
10
|
+
const asString = valueOff?.toString?.() ?? String(valueOff);
|
|
11
|
+
return asString.startsWith("[object ") ? JSON.stringify(value) : asString;
|
|
12
|
+
};
|
|
13
|
+
// node_modules/@oscarpalmer/atoms/dist/js/is.mjs
|
|
14
|
+
var isNullableOrWhitespace = function(value) {
|
|
15
|
+
return value == null || /^\s*$/.test(getString(value));
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// src/helpers/dom.ts
|
|
19
|
+
function createTemplate(html) {
|
|
20
|
+
const template = document.createElement("template");
|
|
21
|
+
template.innerHTML = html;
|
|
22
|
+
const cloned = template.content.cloneNode(true);
|
|
23
|
+
const scripts = [
|
|
24
|
+
...cloned instanceof Element ? cloned.querySelectorAll("script") : []
|
|
25
|
+
];
|
|
26
|
+
const { length } = scripts;
|
|
27
|
+
for (let index = 0;index < length; index += 1) {
|
|
28
|
+
scripts[index].remove();
|
|
29
|
+
}
|
|
30
|
+
cloned.normalize();
|
|
31
|
+
return cloned;
|
|
32
|
+
}
|
|
33
|
+
function getNodes(node) {
|
|
34
|
+
return /^documentfragment$/i.test(node.constructor.name) ? [...node.childNodes] : [node];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/html.ts
|
|
38
|
+
function html(strings, ...values) {
|
|
39
|
+
const parsed = parse(strings, values);
|
|
40
|
+
const templated = createTemplate(parsed);
|
|
41
|
+
return getNodes(templated);
|
|
42
|
+
}
|
|
43
|
+
var parse = function(parts, values) {
|
|
44
|
+
const { length } = parts;
|
|
45
|
+
let template = "";
|
|
46
|
+
for (let index = 0;index < length; index += 1) {
|
|
47
|
+
const part = parts[index];
|
|
48
|
+
const value = values[index];
|
|
49
|
+
template += isNullableOrWhitespace(value) ? part : `${part}${value}`;
|
|
50
|
+
}
|
|
51
|
+
return template;
|
|
52
|
+
};
|
|
3
53
|
export {
|
|
4
|
-
|
|
54
|
+
html
|
|
5
55
|
};
|
package/package.json
CHANGED
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
"name": "Oscar Palmér",
|
|
4
4
|
"url": "https://oscarpalmer.se"
|
|
5
5
|
},
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@oscarpalmer/atoms": "^0.61.0"
|
|
8
|
+
},
|
|
6
9
|
"devDependencies": {
|
|
7
10
|
"@biomejs/biome": "^1.8.3",
|
|
8
11
|
"@types/bun": "^1.1.6",
|
|
@@ -32,5 +35,5 @@
|
|
|
32
35
|
"watch": "bun build ./src/index.ts --outfile ./dist/index.js --watch"
|
|
33
36
|
},
|
|
34
37
|
"types": "src/types.d.ts",
|
|
35
|
-
"version": "0.
|
|
38
|
+
"version": "0.3.0"
|
|
36
39
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export function createTemplate(html: string): Node {
|
|
2
|
+
const template = document.createElement('template');
|
|
3
|
+
|
|
4
|
+
template.innerHTML = html;
|
|
5
|
+
|
|
6
|
+
const cloned = template.content.cloneNode(true);
|
|
7
|
+
|
|
8
|
+
const scripts = [
|
|
9
|
+
...(cloned instanceof Element ? cloned.querySelectorAll('script') : []),
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
const {length} = scripts;
|
|
13
|
+
|
|
14
|
+
for (let index = 0; index < length; index += 1) {
|
|
15
|
+
scripts[index].remove();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
cloned.normalize();
|
|
19
|
+
|
|
20
|
+
return cloned;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function getNodes(node: Node): Node[] {
|
|
24
|
+
return /^documentfragment$/i.test(node.constructor.name)
|
|
25
|
+
? [...node.childNodes]
|
|
26
|
+
: [node];
|
|
27
|
+
}
|
package/src/html.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {isNullableOrWhitespace} from '@oscarpalmer/atoms/is';
|
|
2
|
+
import {createTemplate, getNodes} from './helpers/dom';
|
|
3
|
+
|
|
4
|
+
export function html(
|
|
5
|
+
strings: TemplateStringsArray,
|
|
6
|
+
...values: unknown[]
|
|
7
|
+
): Node[] {
|
|
8
|
+
const parsed = parse(strings, values);
|
|
9
|
+
const templated = createTemplate(parsed);
|
|
10
|
+
|
|
11
|
+
return getNodes(templated);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function parse(parts: TemplateStringsArray, values: unknown[]): string {
|
|
15
|
+
const {length} = parts;
|
|
16
|
+
|
|
17
|
+
let template = '';
|
|
18
|
+
|
|
19
|
+
for (let index = 0; index < length; index += 1) {
|
|
20
|
+
const part = parts[index];
|
|
21
|
+
const value = values[index];
|
|
22
|
+
|
|
23
|
+
template += isNullableOrWhitespace(value) ? part : `${part}${value}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return template;
|
|
27
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './html';
|
package/types/html.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function html(strings: TemplateStringsArray, ...values: unknown[]): Node[];
|
package/types/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './html';
|