create-html-element 5.1.0 → 6.0.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/index.d.ts +1 -1
- package/index.js +21 -16
- package/package.json +8 -8
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -4,15 +4,20 @@ import {htmlEscape} from 'escape-goat';
|
|
|
4
4
|
|
|
5
5
|
const voidHtmlTags = new Set(voidHtmlTagsArray);
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
7
|
+
// Matches a valid HTML tag name (a letter followed by letters, digits, hyphens, underscores, or periods).
|
|
8
|
+
const tagNamePattern = /^[a-z][\w\-.]*$/iv;
|
|
9
|
+
|
|
10
|
+
export default function createHtmlElement({
|
|
11
|
+
name = 'div',
|
|
12
|
+
attributes = {},
|
|
13
|
+
html,
|
|
14
|
+
text,
|
|
15
|
+
children,
|
|
16
|
+
} = {}) {
|
|
17
|
+
if (!tagNamePattern.test(name)) {
|
|
18
|
+
throw new Error(`Invalid tag name: ${JSON.stringify(name)}`);
|
|
19
|
+
}
|
|
20
|
+
|
|
16
21
|
const hasHtml = html !== undefined;
|
|
17
22
|
const hasText = text !== undefined;
|
|
18
23
|
const hasChildren = children !== undefined;
|
|
@@ -25,6 +30,12 @@ export default function createHtmlElement(
|
|
|
25
30
|
throw new TypeError('The `children` option must be an array');
|
|
26
31
|
}
|
|
27
32
|
|
|
33
|
+
const openingTag = `<${name}${stringifyAttributes(attributes)}>`;
|
|
34
|
+
|
|
35
|
+
if (voidHtmlTags.has(name)) {
|
|
36
|
+
return openingTag;
|
|
37
|
+
}
|
|
38
|
+
|
|
28
39
|
let content = '';
|
|
29
40
|
|
|
30
41
|
if (hasChildren) {
|
|
@@ -45,11 +56,5 @@ export default function createHtmlElement(
|
|
|
45
56
|
content = html;
|
|
46
57
|
}
|
|
47
58
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (!voidHtmlTags.has(name)) {
|
|
51
|
-
result += `${content}</${name}>`;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return result;
|
|
59
|
+
return `${openingTag}${content}</${name}>`;
|
|
55
60
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-html-element",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "Create a HTML element string",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/create-html-element",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
},
|
|
18
18
|
"sideEffects": false,
|
|
19
19
|
"engines": {
|
|
20
|
-
"node": "
|
|
20
|
+
"node": ">=22"
|
|
21
21
|
},
|
|
22
22
|
"scripts": {
|
|
23
23
|
"test": "xo && ava && tsd"
|
|
@@ -37,13 +37,13 @@
|
|
|
37
37
|
],
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"escape-goat": "^4.0.0",
|
|
40
|
-
"html-tags": "^
|
|
41
|
-
"stringify-attributes": "^
|
|
42
|
-
"type-fest": "^
|
|
40
|
+
"html-tags": "^5.1.0",
|
|
41
|
+
"stringify-attributes": "^5.0.0",
|
|
42
|
+
"type-fest": "^5.7.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"ava": "^
|
|
46
|
-
"tsd": "^0.
|
|
47
|
-
"xo": "^0.
|
|
45
|
+
"ava": "^8.0.1",
|
|
46
|
+
"tsd": "^0.33.0",
|
|
47
|
+
"xo": "^3.0.2"
|
|
48
48
|
}
|
|
49
49
|
}
|