@cyberalien/svg-utils 1.0.9 → 1.0.10
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/lib/index.d.ts +2 -2
- package/lib/svg-css/animations/tags.d.ts +13 -1
- package/lib/svg-css/animations/tags.js +16 -4
- package/lib/svg-css/content.js +1 -1
- package/lib/svg-css/props/prop.d.ts +1 -1
- package/lib/svg-css/props/prop.js +2 -2
- package/lib/svg-css/props/props.d.ts +1 -1
- package/lib/svg-css/props/props.js +2 -2
- package/lib/svg-css/root.d.ts +3 -2
- package/lib/svg-css/root.js +4 -2
- package/lib/svg-css/types.d.ts +8 -3
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { ClassProp, classProps, defaultClassProp } from "./classname/const.js";
|
|
|
2
2
|
import { splitClassName, toggleClassName } from "./classname/toggle.js";
|
|
3
3
|
import { UniqueHashOptions } from "./helpers/hash/types.js";
|
|
4
4
|
import { ParsedXMLNode, ParsedXMLTagElement, ParsedXMLTextElement, StringifyXMLOptions } from "./xml/types.js";
|
|
5
|
-
import { ConvertSVGContentOptions, ConvertedSVGContent } from "./svg-css/types.js";
|
|
5
|
+
import { BaseConvertSVGContentOptions, ConvertSVGContentOptions, ConvertedSVGContent } from "./svg-css/types.js";
|
|
6
6
|
import { createCSSClassName } from "./css/hash.js";
|
|
7
7
|
import { stringifyCSSKeyframes, stringifyCSSRules, stringifyCSSSelector } from "./css/stringify.js";
|
|
8
8
|
import { hashString } from "./helpers/hash/hash.js";
|
|
@@ -24,4 +24,4 @@ import { changeSVGIDs } from "./svg/ids/change.js";
|
|
|
24
24
|
import { createUniqueIDs } from "./svg/ids/unique.js";
|
|
25
25
|
import { convertSVGRootToCSS } from "./svg-css/root.js";
|
|
26
26
|
import { convertSVGContentToCSSRules } from "./svg-css/content.js";
|
|
27
|
-
export { ChangeIDResult, ClassProp, ComparisonKey, ConvertSVGContentOptions, ConvertedSVGContent, ParsedXMLNode, ParsedXMLTagElement, ParsedXMLTextElement, StringifyXMLOptions, UniqueHashOptions, changeIDInString, changeSVGIDs, classProps, cloneObject, compareKeys, compareSets, compareValues, convertSVGContentToCSSRules, convertSVGRootToCSS, createCSSClassName, createUniqueIDs, defaultClassProp, getUniqueHash, hashString, hashToString, iterateXMLContent, parseXMLContent, removeDuplicateIDs, removeUnusedIDs, sortObject, splitClassName, stringifyCSSKeyframes, stringifyCSSRules, stringifyCSSSelector, stringifyXMLContent, toggleClassName, uniquePromise };
|
|
27
|
+
export { BaseConvertSVGContentOptions, ChangeIDResult, ClassProp, ComparisonKey, ConvertSVGContentOptions, ConvertedSVGContent, ParsedXMLNode, ParsedXMLTagElement, ParsedXMLTextElement, StringifyXMLOptions, UniqueHashOptions, changeIDInString, changeSVGIDs, classProps, cloneObject, compareKeys, compareSets, compareValues, convertSVGContentToCSSRules, convertSVGRootToCSS, createCSSClassName, createUniqueIDs, defaultClassProp, getUniqueHash, hashString, hashToString, iterateXMLContent, parseXMLContent, removeDuplicateIDs, removeUnusedIDs, sortObject, splitClassName, stringifyCSSKeyframes, stringifyCSSRules, stringifyCSSSelector, stringifyXMLContent, toggleClassName, uniquePromise };
|
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tag used to set attribute value without animation duration
|
|
3
|
+
*/
|
|
4
|
+
declare const svgSetTag = "set";
|
|
5
|
+
/**
|
|
6
|
+
* Tag to discard animation
|
|
7
|
+
*/
|
|
8
|
+
declare const svgDiscardTag = "discard";
|
|
9
|
+
/**
|
|
10
|
+
* Attribute animation tag
|
|
11
|
+
*/
|
|
12
|
+
declare const svgAnimationTag = "animate";
|
|
1
13
|
/**
|
|
2
14
|
* Tags for animating one attribute in SVG
|
|
3
15
|
*/
|
|
@@ -14,4 +26,4 @@ declare const svgAnimateMotionTag = "animateMotion";
|
|
|
14
26
|
* All tags for animating SVG
|
|
15
27
|
*/
|
|
16
28
|
declare const svgAnimationTags: string[];
|
|
17
|
-
export { svgAnimateMotionTag, svgAnimateTransformTag, svgAnimationTags, svgSimpleAnimationTags };
|
|
29
|
+
export { svgAnimateMotionTag, svgAnimateTransformTag, svgAnimationTag, svgAnimationTags, svgDiscardTag, svgSetTag, svgSimpleAnimationTags };
|
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* Tag used to set attribute value without animation duration
|
|
3
|
+
*/
|
|
4
|
+
const svgSetTag = "set";
|
|
5
|
+
/**
|
|
6
|
+
* Tag to discard animation
|
|
7
|
+
*/
|
|
8
|
+
const svgDiscardTag = "discard";
|
|
9
|
+
/**
|
|
10
|
+
* Attribute animation tag
|
|
11
|
+
*/
|
|
12
|
+
const svgAnimationTag = "animate";
|
|
13
|
+
/**
|
|
2
14
|
* Tags for animating one attribute in SVG
|
|
3
15
|
*/
|
|
4
16
|
const svgSimpleAnimationTags = [
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
17
|
+
svgSetTag,
|
|
18
|
+
svgDiscardTag,
|
|
19
|
+
svgAnimationTag
|
|
8
20
|
];
|
|
9
21
|
/**
|
|
10
22
|
* Tag for animating transformations in SVG
|
|
@@ -23,4 +35,4 @@ const svgAnimationTags = [
|
|
|
23
35
|
svgAnimateMotionTag
|
|
24
36
|
];
|
|
25
37
|
|
|
26
|
-
export { svgAnimateMotionTag, svgAnimateTransformTag, svgAnimationTags, svgSimpleAnimationTags };
|
|
38
|
+
export { svgAnimateMotionTag, svgAnimateTransformTag, svgAnimationTag, svgAnimationTags, svgDiscardTag, svgSetTag, svgSimpleAnimationTags };
|
package/lib/svg-css/content.js
CHANGED
|
@@ -8,7 +8,7 @@ import { convertSVGRootToCSS } from "./root.js";
|
|
|
8
8
|
function convertSVGContentToCSSRules(content, options) {
|
|
9
9
|
const root = parseXMLContent(content);
|
|
10
10
|
if (!root) return { content };
|
|
11
|
-
const classes = convertSVGRootToCSS(root, options
|
|
11
|
+
const classes = convertSVGRootToCSS(root, options);
|
|
12
12
|
if (classes) {
|
|
13
13
|
const newContent = stringifyXMLContent(root, options);
|
|
14
14
|
if (newContent) return {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Convert property to CSS
|
|
3
3
|
*/
|
|
4
|
-
declare function convertSVGPropertyToCSS(tag: string, prop: string, value: string | number): [string, string] | undefined;
|
|
4
|
+
declare function convertSVGPropertyToCSS(tag: string, prop: string, value: string | number, supportLegacyBrowsers?: boolean): [string, string] | undefined;
|
|
5
5
|
export { convertSVGPropertyToCSS };
|
|
@@ -57,10 +57,10 @@ function getSVGPropertyType(tag, prop) {
|
|
|
57
57
|
/**
|
|
58
58
|
* Convert property to CSS
|
|
59
59
|
*/
|
|
60
|
-
function convertSVGPropertyToCSS(tag, prop, value) {
|
|
60
|
+
function convertSVGPropertyToCSS(tag, prop, value, supportLegacyBrowsers = false) {
|
|
61
61
|
switch (getSVGPropertyType(tag, prop)) {
|
|
62
62
|
case "path":
|
|
63
|
-
if (typeof value !== "string") return;
|
|
63
|
+
if (supportLegacyBrowsers || typeof value !== "string") return;
|
|
64
64
|
return [prop, `path("${value.replace(/\s+/g, " ")}")`];
|
|
65
65
|
case "px":
|
|
66
66
|
if (typeof value === "string" && !value.match(/^[0-9.-]+$/)) return [prop, value];
|
|
@@ -5,5 +5,5 @@ import { SVGConvertedToCSSProperties } from "./types.js";
|
|
|
5
5
|
*
|
|
6
6
|
* Returns CSS rules, does not add a class to tag, but does remove properties from tag
|
|
7
7
|
*/
|
|
8
|
-
declare function extractSVGTagPropertiesForCSS(tag: ParsedXMLTagElement): SVGConvertedToCSSProperties | undefined;
|
|
8
|
+
declare function extractSVGTagPropertiesForCSS(tag: ParsedXMLTagElement, supportLegacyBrowsers?: boolean): SVGConvertedToCSSProperties | undefined;
|
|
9
9
|
export { extractSVGTagPropertiesForCSS };
|
|
@@ -7,7 +7,7 @@ import { svgAnimateTransformTag, svgSimpleAnimationTags } from "../animations/ta
|
|
|
7
7
|
*
|
|
8
8
|
* Returns CSS rules, does not add a class to tag, but does remove properties from tag
|
|
9
9
|
*/
|
|
10
|
-
function extractSVGTagPropertiesForCSS(tag) {
|
|
10
|
+
function extractSVGTagPropertiesForCSS(tag, supportLegacyBrowsers = false) {
|
|
11
11
|
const result = {
|
|
12
12
|
props: [],
|
|
13
13
|
rules: Object.create(null)
|
|
@@ -24,7 +24,7 @@ function extractSVGTagPropertiesForCSS(tag) {
|
|
|
24
24
|
});
|
|
25
25
|
for (const prop in tag.attribs) if (!animatedProps.has(prop)) {
|
|
26
26
|
const value = tag.attribs[prop];
|
|
27
|
-
const converted = convertSVGPropertyToCSS(tag.tag, prop, value);
|
|
27
|
+
const converted = convertSVGPropertyToCSS(tag.tag, prop, value, supportLegacyBrowsers);
|
|
28
28
|
if (converted) {
|
|
29
29
|
const [propName, propValue] = converted;
|
|
30
30
|
result.props.push(propName);
|
package/lib/svg-css/root.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CSSRules } from "../css/types.js";
|
|
2
2
|
import { ParsedXMLTagElement } from "../xml/types.js";
|
|
3
|
+
import { BaseConvertSVGContentOptions } from "./types.js";
|
|
3
4
|
/**
|
|
4
5
|
* Convert SVG tags tree to SVG+CSS
|
|
5
6
|
*
|
|
6
7
|
* Returns used CSS class names with rules
|
|
7
8
|
*/
|
|
8
|
-
declare function convertSVGRootToCSS(root: ParsedXMLTagElement[],
|
|
9
|
+
declare function convertSVGRootToCSS(root: ParsedXMLTagElement[], options?: BaseConvertSVGContentOptions): Record<string, CSSRules>;
|
|
9
10
|
export { convertSVGRootToCSS };
|
package/lib/svg-css/root.js
CHANGED
|
@@ -8,11 +8,13 @@ import { extractSVGTagPropertiesForCSS } from "./props/props.js";
|
|
|
8
8
|
*
|
|
9
9
|
* Returns used CSS class names with rules
|
|
10
10
|
*/
|
|
11
|
-
function convertSVGRootToCSS(root,
|
|
11
|
+
function convertSVGRootToCSS(root, options = {}) {
|
|
12
12
|
const rules = Object.create(null);
|
|
13
|
+
const hashOptions = options.hashOptions || {};
|
|
14
|
+
const classNamePrefix = options.classNamePrefix || "";
|
|
13
15
|
iterateXMLContent(root, (node) => {
|
|
14
16
|
if (node.type === "tag") {
|
|
15
|
-
const props = extractSVGTagPropertiesForCSS(node);
|
|
17
|
+
const props = extractSVGTagPropertiesForCSS(node, options.legacy);
|
|
16
18
|
if (props) {
|
|
17
19
|
const className = createCSSClassName(props.rules, classNamePrefix, hashOptions);
|
|
18
20
|
toggleClassName(node.attribs, className, true);
|
package/lib/svg-css/types.d.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
import { CSSHashOptions, CSSKeyframes, CSSRules } from "../css/types.js";
|
|
2
2
|
import { StringifyXMLOptions } from "../xml/types.js";
|
|
3
3
|
/**
|
|
4
|
-
* Options for converting SVG
|
|
4
|
+
* Options for converting SVG tags to SVG+CSS
|
|
5
5
|
*/
|
|
6
|
-
interface
|
|
6
|
+
interface BaseConvertSVGContentOptions {
|
|
7
7
|
classNamePrefix?: string;
|
|
8
8
|
hashOptions?: CSSHashOptions;
|
|
9
|
+
legacy?: boolean;
|
|
9
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* Options for converting SVG content to SVG+CSS
|
|
13
|
+
*/
|
|
14
|
+
interface ConvertSVGContentOptions extends StringifyXMLOptions, BaseConvertSVGContentOptions {}
|
|
10
15
|
/**
|
|
11
16
|
* Result of converting SVG content to SVG+CSS
|
|
12
17
|
*/
|
|
@@ -15,4 +20,4 @@ interface ConvertedSVGContent {
|
|
|
15
20
|
classes?: Record<string, CSSRules>;
|
|
16
21
|
keyframes?: Record<string, CSSKeyframes>;
|
|
17
22
|
}
|
|
18
|
-
export { ConvertSVGContentOptions, ConvertedSVGContent };
|
|
23
|
+
export { BaseConvertSVGContentOptions, ConvertSVGContentOptions, ConvertedSVGContent };
|
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.0.
|
|
6
|
+
"version": "1.0.10",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"bugs": "https://github.com/cyberalien/svg-utils/issues",
|
|
9
9
|
"homepage": "https://cyberalien.dev/",
|