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.
Files changed (3) hide show
  1. package/index.d.ts +1 -1
  2. package/index.js +21 -16
  3. package/package.json +8 -8
package/index.d.ts CHANGED
@@ -114,4 +114,4 @@ createHtmlElement({
114
114
  */
115
115
  export default function createHtmlElement(options?: Options): string;
116
116
 
117
- export {HTMLAttributes} from 'stringify-attributes';
117
+ export type {HTMLAttributes} from 'stringify-attributes';
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
- export default function createHtmlElement(
8
- {
9
- name = 'div',
10
- attributes = {},
11
- html,
12
- text,
13
- children,
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
- let result = `<${name}${stringifyAttributes(attributes)}>`;
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": "5.1.0",
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": "^18.20.0 || >=20.10.0"
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": "^4.0.0",
41
- "stringify-attributes": "^4.0.0",
42
- "type-fest": "^4.27.0"
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": "^6.2.0",
46
- "tsd": "^0.31.2",
47
- "xo": "^0.59.3"
45
+ "ava": "^8.0.1",
46
+ "tsd": "^0.33.0",
47
+ "xo": "^3.0.2"
48
48
  }
49
49
  }