@jsenv/ast 6.2.3 → 6.2.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/ast",
3
- "version": "6.2.3",
3
+ "version": "6.2.4",
4
4
  "description": "Parse and transform HTML, CSS and JS",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -3,6 +3,18 @@ export const getHtmlNodeAttribute = (htmlNode, attributeName) => {
3
3
  return attribute ? attribute.value || "" : undefined;
4
4
  };
5
5
 
6
+ export const getHtmlNodeAttributes = (htmlNode) => {
7
+ const { attrs } = htmlNode;
8
+ if (!attrs) {
9
+ return {};
10
+ }
11
+ const attributes = {};
12
+ for (const attr of attrs) {
13
+ attributes[attr.name] = attr.value;
14
+ }
15
+ return attributes;
16
+ };
17
+
6
18
  export const setHtmlNodeAttributes = (htmlNode, attributesToAssign) => {
7
19
  if (typeof attributesToAssign !== "object") {
8
20
  throw new TypeError(`attributesToAssign must be an object`);
@@ -35,9 +47,10 @@ export const setHtmlNodeAttributes = (htmlNode, attributesToAssign) => {
35
47
  };
36
48
 
37
49
  const getHtmlAttributeByName = (htmlNode, attributeName) => {
38
- const attrs = htmlNode.attrs;
39
- const attribute = attrs
40
- ? attrs.find((attr) => attr.name === attributeName)
41
- : null;
50
+ const { attrs } = htmlNode;
51
+ if (!attrs) {
52
+ return null;
53
+ }
54
+ const attribute = attrs.find((attr) => attr.name === attributeName);
42
55
  return attribute;
43
56
  };
@@ -13,11 +13,7 @@ import {
13
13
  import { findHtmlChildNode, visitHtmlNodes } from "./html_search.js";
14
14
  import { getHtmlNodeText } from "./html_node_text.js";
15
15
 
16
- export const parseHtml = ({
17
- html,
18
- url,
19
- storeOriginalPositions = true,
20
- } = {}) => {
16
+ export const parseHtml = ({ html, url, storeOriginalPositions = true }) => {
21
17
  const htmlAst = parse(html, {
22
18
  sourceCodeLocationInfo: true,
23
19
  onParseError: (parse5Error) => {
package/src/main.js CHANGED
@@ -15,6 +15,7 @@ export {
15
15
  export { parseSrcSet, stringifySrcSet } from "./html/html_src_set.js";
16
16
  export {
17
17
  getHtmlNodeAttribute,
18
+ getHtmlNodeAttributes,
18
19
  setHtmlNodeAttributes,
19
20
  } from "./html/html_node_attributes.js";
20
21
  export {