vadivam 0.0.80 → 0.0.82

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.
@@ -1,16 +1,12 @@
1
1
  import createElement from "./createElement.js";
2
2
  import defaultAttributes from "./defaultAttributes.js";
3
+ import { sanitizeRootAttributes } from "../runtimePolicy.js";
3
4
 
4
5
  function attributes(element) {
5
6
  return Object.fromEntries(Array.from(element.attributes, ({ name, value }) => [name, value]));
6
7
  }
7
- function classes(value) {
8
- if (Array.isArray(value)) return value;
9
- return typeof value === "string" ? value.split(/\s+/).filter(Boolean) : [];
10
- }
11
- function hasAccessibleName(attrs) {
12
- return Object.keys(attrs).some((name) => name.startsWith("aria-") || name === "role" || name === "title");
13
- }
8
+ function mergeClasses(...values) { return values.flatMap((value) => typeof value === "string" ? value.split(/\s+/) : []).filter((value, index, all) => value && all.indexOf(value) === index).join(" "); }
9
+ function isLabelled(title, attributeNames) { return Boolean(title) || attributeNames.some((name) => name.startsWith("aria-") || name === "role" || name === "title"); }
14
10
  function replaceElement(element, { nameAttr, icons, attrs }) {
15
11
  const name = element.getAttribute(nameAttr);
16
12
  if (!name) return;
@@ -21,8 +17,8 @@ function replaceElement(element, { nameAttr, icons, attrs }) {
21
17
  return;
22
18
  }
23
19
  const elementAttrs = attributes(element);
24
- const iconAttrs = { ...defaultAttributes, [nameAttr]: name, ...(hasAccessibleName(elementAttrs) ? {} : { "aria-hidden": "true" }), ...attrs, ...elementAttrs };
25
- iconAttrs.class = ["vadivam", `vadivam-${name}`, ...classes(elementAttrs.class), ...classes(attrs.class)].filter((value, index, all) => value && all.indexOf(value) === index).join(" ");
20
+ const iconAttrs = { ...defaultAttributes, [nameAttr]: name, ...(isLabelled(null, Object.keys(sanitizeRootAttributes({ ...attrs, ...elementAttrs }))) ? {} : { "aria-hidden": "true" }), ...attrs, ...elementAttrs };
21
+ iconAttrs.class = mergeClasses("vadivam", `vadivam-${name}`, elementAttrs.class, ...[attrs.class].flat());
26
22
  element.parentNode?.replaceChild(createElement(iconNode, iconAttrs), element);
27
23
  }
28
24
 
@@ -30,7 +26,8 @@ export function createIcons({ icons = {}, nameAttr = "data-vadivam", attrs = {},
30
26
  if (!Object.keys(icons).length) throw new Error("Please provide an icons object to createIcons().");
31
27
  if (!root) throw new Error("createIcons() only works in a browser environment.");
32
28
  if (!/^[A-Za-z_:][A-Za-z0-9_.:-]*$/.test(nameAttr)) throw new Error("createIcons() received an invalid nameAttr.");
33
- for (const element of root.querySelectorAll(`[${nameAttr}]`)) replaceElement(element, { nameAttr, icons, attrs });
29
+ // A valid attribute name may hold "." or ":", which a selector must escape.
30
+ for (const element of root.querySelectorAll(`[${nameAttr.replace(/[.:]/g, "\\$&")}]`)) replaceElement(element, { nameAttr, icons, attrs });
34
31
  if (inTemplates) {
35
32
  for (const template of root.querySelectorAll("template")) createIcons({ icons, nameAttr, attrs, root: template.content, inTemplates });
36
33
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vadivam",
3
- "version": "0.0.80",
3
+ "version": "0.0.82",
4
4
  "description": "Pixel-perfect 24px outline and solid SVG icons with tree-shakeable ESM imports and metadata.",
5
5
  "license": "MIT",
6
6
  "author": "Praveen Juge",