@cyberalien/svg-utils 1.1.1 → 1.1.3

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.
@@ -0,0 +1,16 @@
1
+ import { CSSGeneratedStylesheet, CSSRules } from "./types.js";
2
+ /**
3
+ * Create empty stylesheet
4
+ */
5
+ declare function createEmptyStylesheet(): CSSGeneratedStylesheet;
6
+ /**
7
+ * Add generated selector to stylesheet
8
+ *
9
+ * If item exists, it will be overwritten. Class names should be hashed to avoid conflicts, so this should not cause issues.
10
+ */
11
+ declare function addGeneratedSelector(stylesheet: CSSGeneratedStylesheet, tree: string[], rules: CSSRules | string): void;
12
+ /**
13
+ * Stringify generated stylesheet to CSS string
14
+ */
15
+ declare function stringifyStylesheet(stylesheet: CSSGeneratedStylesheet): string;
16
+ export { addGeneratedSelector, createEmptyStylesheet, stringifyStylesheet };
@@ -0,0 +1,67 @@
1
+ import { stringifyCSSKeyframes, stringifyCSSSelector } from "./stringify.js";
2
+
3
+ /**
4
+ * Create empty stylesheet
5
+ */
6
+ function createEmptyStylesheet() {
7
+ return {
8
+ selectors: Object.create(null),
9
+ keyframes: Object.create(null)
10
+ };
11
+ }
12
+ /**
13
+ * Add generated selector to stylesheet
14
+ *
15
+ * If item exists, it will be overwritten. Class names should be hashed to avoid conflicts, so this should not cause issues.
16
+ */
17
+ function addGeneratedSelector(stylesheet, tree, rules) {
18
+ let parent = stylesheet.selectors;
19
+ for (let i = 0; i < tree.length; i++) {
20
+ const selector = tree[i];
21
+ if (!parent[selector]) parent[selector] = {};
22
+ const parentItem = parent[selector];
23
+ if (i === tree.length - 1) {
24
+ parentItem.rules = rules;
25
+ return;
26
+ }
27
+ if (!parentItem.nested) parentItem.nested = Object.create(null);
28
+ parent = parentItem.nested;
29
+ }
30
+ }
31
+ function indent(depth) {
32
+ return " ".repeat(depth);
33
+ }
34
+ /**
35
+ * Stringify generated selectors to CSS string
36
+ */
37
+ function stringifySelectors(data, depth = 0) {
38
+ const lines = [];
39
+ for (const selector in data) {
40
+ const item = data[selector];
41
+ if (item.rules) {
42
+ const value = stringifyCSSSelector(selector, item.rules, depth);
43
+ if (value.length) lines.push(value);
44
+ }
45
+ if (item.nested) {
46
+ const nestedContent = stringifySelectors(item.nested, depth + 1);
47
+ if (nestedContent.length) lines.push(`${indent(depth)}${selector} {\n${nestedContent}${indent(depth)}}\n`);
48
+ }
49
+ }
50
+ return lines.join("\n");
51
+ }
52
+ /**
53
+ * Stringify generated stylesheet to CSS string
54
+ */
55
+ function stringifyStylesheet(stylesheet) {
56
+ const lines = [];
57
+ const selectors = stringifySelectors(stylesheet.selectors);
58
+ if (selectors.length) lines.push(selectors);
59
+ for (const animationName in stylesheet.keyframes) {
60
+ const keyframes = stylesheet.keyframes[animationName];
61
+ const keyframesContent = stringifyCSSKeyframes(animationName, keyframes);
62
+ if (keyframesContent.length) lines.push(keyframesContent);
63
+ }
64
+ return lines.join("\n");
65
+ }
66
+
67
+ export { addGeneratedSelector, createEmptyStylesheet, stringifyStylesheet };
@@ -18,4 +18,16 @@ interface CSSKeyframes {
18
18
  * Hash options
19
19
  */
20
20
  type CSSHashOptions = UniqueHashPartialOptions;
21
- export { CSSHashOptions, CSSKeyframe, CSSKeyframes, CSSRules };
21
+ /**
22
+ * Generated stylesheet
23
+ */
24
+ interface CSSGeneratedSelector {
25
+ rules?: CSSRules | string;
26
+ nested?: CSSGeneratedSelectors;
27
+ }
28
+ type CSSGeneratedSelectors = Record<string, CSSGeneratedSelector>;
29
+ interface CSSGeneratedStylesheet {
30
+ selectors: CSSGeneratedSelectors;
31
+ keyframes: Record<string, CSSKeyframes>;
32
+ }
33
+ export { CSSGeneratedSelector, CSSGeneratedSelectors, CSSGeneratedStylesheet, CSSHashOptions, CSSKeyframe, CSSKeyframes, CSSRules };
package/lib/index.d.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  import { ClassProp, classProps, defaultClassProp } from "./classname/const.js";
2
2
  import { splitClassName, toggleClassName } from "./classname/toggle.js";
3
3
  import { HashContext, UniqueHashOptions, UniqueHashPartialOptions } from "./helpers/hash/types.js";
4
+ import { CSSGeneratedSelector, CSSGeneratedSelectors, CSSGeneratedStylesheet, CSSHashOptions, CSSKeyframe, CSSKeyframes, CSSRules } from "./css/types.js";
4
5
  import { ParsedXMLNode, ParsedXMLTagElement, ParsedXMLTextElement, StringifyXMLOptions } from "./xml/types.js";
5
6
  import { BaseConvertSVGContentOptions, ConvertSVGContentOptions, ConvertedSVGContent } from "./svg-css/types.js";
6
7
  import { createCSSClassName } from "./css/hash.js";
7
8
  import { stringifyCSSKeyframes, stringifyCSSRules, stringifyCSSSelector } from "./css/stringify.js";
9
+ import { addGeneratedSelector, createEmptyStylesheet, stringifyStylesheet } from "./css/stylesheet.js";
8
10
  import { createUniqueHashContext } from "./helpers/hash/context.js";
9
11
  import { hashString } from "./helpers/hash/hash.js";
10
12
  import { hashToString } from "./helpers/hash/stringify.js";
@@ -25,4 +27,4 @@ import { changeSVGIDs } from "./svg/ids/change.js";
25
27
  import { createUniqueIDs } from "./svg/ids/unique.js";
26
28
  import { convertSVGRootToCSS } from "./svg-css/root.js";
27
29
  import { convertSVGContentToCSSRules } from "./svg-css/content.js";
28
- export { BaseConvertSVGContentOptions, ChangeIDResult, ClassProp, ComparisonKey, ConvertSVGContentOptions, ConvertedSVGContent, HashContext, ParsedXMLNode, ParsedXMLTagElement, ParsedXMLTextElement, StringifyXMLOptions, UniqueHashOptions, UniqueHashPartialOptions, UniqueIDOptions, changeIDInString, changeSVGIDs, classProps, cloneObject, compareKeys, compareSets, compareValues, convertSVGContentToCSSRules, convertSVGRootToCSS, createCSSClassName, createUniqueHashContext, createUniqueIDs, defaultClassProp, getUniqueHash, hashString, hashToString, iterateXMLContent, parseXMLContent, removeDuplicateIDs, removeUnusedIDs, sortObject, splitClassName, stringifyCSSKeyframes, stringifyCSSRules, stringifyCSSSelector, stringifyXMLContent, toggleClassName, uniquePromise };
30
+ export { BaseConvertSVGContentOptions, CSSGeneratedSelector, CSSGeneratedSelectors, CSSGeneratedStylesheet, CSSHashOptions, CSSKeyframe, CSSKeyframes, CSSRules, ChangeIDResult, ClassProp, ComparisonKey, ConvertSVGContentOptions, ConvertedSVGContent, HashContext, ParsedXMLNode, ParsedXMLTagElement, ParsedXMLTextElement, StringifyXMLOptions, UniqueHashOptions, UniqueHashPartialOptions, UniqueIDOptions, addGeneratedSelector, changeIDInString, changeSVGIDs, classProps, cloneObject, compareKeys, compareSets, compareValues, convertSVGContentToCSSRules, convertSVGRootToCSS, createCSSClassName, createEmptyStylesheet, createUniqueHashContext, createUniqueIDs, defaultClassProp, getUniqueHash, hashString, hashToString, iterateXMLContent, parseXMLContent, removeDuplicateIDs, removeUnusedIDs, sortObject, splitClassName, stringifyCSSKeyframes, stringifyCSSRules, stringifyCSSSelector, stringifyStylesheet, stringifyXMLContent, toggleClassName, uniquePromise };
package/lib/index.js CHANGED
@@ -12,6 +12,7 @@ import { parseXMLContent } from "./xml/parse.js";
12
12
  import { stringifyXMLContent } from "./xml/stringify.js";
13
13
  import { createCSSClassName } from "./css/hash.js";
14
14
  import { stringifyCSSKeyframes, stringifyCSSRules, stringifyCSSSelector } from "./css/stringify.js";
15
+ import { addGeneratedSelector, createEmptyStylesheet, stringifyStylesheet } from "./css/stylesheet.js";
15
16
  import { classProps, defaultClassProp } from "./classname/const.js";
16
17
  import { splitClassName, toggleClassName } from "./classname/toggle.js";
17
18
  import { changeIDInString } from "./svg/ids/string.js";
@@ -22,4 +23,4 @@ import { createUniqueIDs } from "./svg/ids/unique.js";
22
23
  import { convertSVGRootToCSS } from "./svg-css/root.js";
23
24
  import { convertSVGContentToCSSRules } from "./svg-css/content.js";
24
25
 
25
- export { changeIDInString, changeSVGIDs, classProps, cloneObject, compareKeys, compareSets, compareValues, convertSVGContentToCSSRules, convertSVGRootToCSS, createCSSClassName, createUniqueHashContext, createUniqueIDs, defaultClassProp, getUniqueHash, hashString, hashToString, iterateXMLContent, parseXMLContent, removeDuplicateIDs, removeUnusedIDs, sortObject, splitClassName, stringifyCSSKeyframes, stringifyCSSRules, stringifyCSSSelector, stringifyXMLContent, toggleClassName, uniquePromise };
26
+ export { addGeneratedSelector, changeIDInString, changeSVGIDs, classProps, cloneObject, compareKeys, compareSets, compareValues, convertSVGContentToCSSRules, convertSVGRootToCSS, createCSSClassName, createEmptyStylesheet, createUniqueHashContext, createUniqueIDs, defaultClassProp, getUniqueHash, hashString, hashToString, iterateXMLContent, parseXMLContent, removeDuplicateIDs, removeUnusedIDs, sortObject, splitClassName, stringifyCSSKeyframes, stringifyCSSRules, stringifyCSSSelector, stringifyStylesheet, stringifyXMLContent, toggleClassName, uniquePromise };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "type": "module",
4
4
  "description": "Common functions for working with SVG used by various packages.",
5
5
  "author": "Vjacheslav Trushkin",
6
- "version": "1.1.1",
6
+ "version": "1.1.3",
7
7
  "license": "MIT",
8
8
  "bugs": "https://github.com/cyberalien/svg-utils/issues",
9
9
  "homepage": "https://cyberalien.dev/",
@@ -25,14 +25,14 @@
25
25
  "devDependencies": {
26
26
  "@eslint/eslintrc": "^3.3.3",
27
27
  "@eslint/js": "^9.39.2",
28
- "@iconify-json/ri": "^1.2.8",
28
+ "@iconify-json/ri": "^1.2.10",
29
29
  "@types/jest": "^30.0.0",
30
- "@types/node": "^25.1.0",
31
- "@typescript-eslint/eslint-plugin": "^8.54.0",
32
- "@typescript-eslint/parser": "^8.54.0",
30
+ "@types/node": "^25.2.3",
31
+ "@typescript-eslint/eslint-plugin": "^8.55.0",
32
+ "@typescript-eslint/parser": "^8.55.0",
33
33
  "eslint": "^9.39.2",
34
- "globals": "^17.2.0",
35
- "tsdown": "^0.20.1",
34
+ "globals": "^17.3.0",
35
+ "tsdown": "^0.20.3",
36
36
  "typescript": "^5.9.3",
37
37
  "vitest": "^4.0.18"
38
38
  },