Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: centralize exports to svgo #2071

Merged
merged 2 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import js from '@eslint/js';
import globals from 'globals';

/**
* @typedef {import('eslint').Linter.FlatConfig} FlatConfig
* @typedef {import('eslint').Linter.Config} Config
*/

/** @type {FlatConfig[]} */
/** @type {Config[]} */
export default [
{
ignores: [
Expand Down
14 changes: 13 additions & 1 deletion lib/svgo-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
VERSION,
optimize as optimizeAgnostic,
builtinPlugins,
querySelector,
querySelectorAll,
_collections,
} from './svgo.js';

const importConfig = async (configFile) => {
Expand All @@ -29,7 +32,13 @@ const isFile = async (file) => {
}
};

export { VERSION, builtinPlugins };
export {
VERSION,
builtinPlugins,
querySelector,
querySelectorAll,
_collections,
};

export const loadConfig = async (configFile, cwd = process.cwd()) => {
if (configFile != null) {
Expand Down Expand Up @@ -84,4 +93,7 @@ export default {
builtinPlugins,
loadConfig,
optimize,
querySelector,
querySelectorAll,
_collections,
};
103 changes: 101 additions & 2 deletions lib/svgo.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type { StringifyOptions, DataUri, Plugin } from './types.js';
import type {
StringifyOptions,
DataUri,
Plugin,
XastChild,
XastNode,
} from './types.js';
import type {
BuiltinsWithOptionalParams,
BuiltinsWithRequiredParams,
Expand Down Expand Up @@ -34,7 +40,10 @@ type BuiltinPlugin<Name, Params> = {
fn: Plugin<Params>;
};

type BuiltinPluginOrPreset<Name, Params> = BuiltinPlugin<Name, Params> & {
export type BuiltinPluginOrPreset<Name, Params> = BuiltinPlugin<
Name,
Params
> & {
/** If the plugin is itself a preset that invokes other plugins. */
isPreset: true | undefined;
/**
Expand Down Expand Up @@ -83,8 +92,98 @@ type Output = {
data: string;
};

export declare const _collections: {
elemsGroups: Readonly<Record<string, Set<string>>>;
/**
* Elements where adding or removing whitespace may effect rendering, metadata,
* or semantic meaning.
*
* @see https://developer.mozilla.org/docs/Web/HTML/Element/pre
*/
textElems: Readonly<Set<string>>;
pathElems: Readonly<Set<string>>;
/**
* @see https://www.w3.org/TR/SVG11/intro.html#Definitions
*/
attrsGroups: Readonly<Record<string, Set<string>>>;
attrsGroupsDefaults: Readonly<Record<string, Record<string, string>>>;
/**
* @see https://www.w3.org/TR/SVG11/intro.html#Definitions
*/
attrsGroupsDeprecated: Readonly<
Record<string, { safe?: Set<string>; unsafe?: Set<string> }>
>;
/**
* @see https://www.w3.org/TR/SVG11/eltindex.html
*/
elems: Readonly<
Record<
string,
{
attrsGroups: Set<string>;
attrs?: Set<string>;
defaults?: Record<string, string>;
deprecated?: {
safe?: Set<string>;
unsafe?: Set<string>;
};
contentGroups?: Set<string>;
content?: Set<string>;
}
>
>;
/**
* @see https://wiki.inkscape.org/wiki/index.php/Inkscape-specific_XML_attributes
*/
editorNamespaces: Readonly<Set<string>>;
/**
* @see https://www.w3.org/TR/SVG11/linking.html#processingIRI
*/
referencesProps: Readonly<Set<string>>;
/**
* @see https://www.w3.org/TR/SVG11/propidx.html
*/
inheritableAttrs: Readonly<Set<string>>;
presentationNonInheritableGroupAttrs: Readonly<Set<string>>;
/**
* @see https://www.w3.org/TR/SVG11/single-page.html#types-ColorKeywords
*/
colorsNames: Readonly<Record<string, string>>;
colorsShortNames: Readonly<Record<string, string>>;
/**
* @see https://www.w3.org/TR/SVG11/single-page.html#types-DataTypeColor
*/
colorsProps: Readonly<Set<string>>;
/**
* @see https://developer.mozilla.org/docs/Web/CSS/Pseudo-classes
*/
pseudoClasses: Readonly<Record<string, Set<string>>>;
};

export type * from './types.d.ts';

/** Installed version of SVGO. */
export declare const VERSION: string;

/** The core of SVGO */
export declare function optimize(input: string, config?: Config): Output;

/**
* @param node Element to query the children of.
* @param selector CSS selector string.
* @returns First match, or null if there was no match.
*/
export declare function querySelector(
node: XastNode,
selector: string,
): XastChild | null;

/**
* @param node Element to query the children of.
* @param selector CSS selector string.
* @returns All matching elements.
*/
export declare function querySelectorAll(
node: XastNode,
selector: string,
): XastChild[];
13 changes: 12 additions & 1 deletion lib/svgo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { builtin } from './builtin.js';
import { invokePlugins } from './svgo/plugins.js';
import { encodeSVGDatauri } from './svgo/tools.js';
import { VERSION } from './version.js';
import { querySelector, querySelectorAll } from './xast.js';
import _collections from '../plugins/_collections.js';

/**
* @typedef {import('./svgo.js').BuiltinPluginOrPreset<?, ?>} BuiltinPluginOrPreset
Expand Down Expand Up @@ -65,7 +67,13 @@ const resolvePluginConfig = (plugin) => {
return null;
};

export { VERSION, builtin as builtinPlugins };
export {
VERSION,
builtin as builtinPlugins,
querySelector,
querySelectorAll,
_collections,
};

export const optimize = (input, config) => {
if (config == null) {
Expand Down Expand Up @@ -124,4 +132,7 @@ export default {
VERSION,
optimize,
builtinPlugins: builtin,
querySelector,
querySelectorAll,
_collections,
};
6 changes: 4 additions & 2 deletions lib/xast.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import xastAdaptor from './svgo/css-select-adapter.js';
* @typedef {import('./types.js').XastChild} XastChild
* @typedef {import('./types.js').XastParent} XastParent
* @typedef {import('./types.js').Visitor} Visitor
* @typedef {import('./svgo.ts').querySelector} querySelector
* @typedef {import('./svgo.ts').querySelectorAll} querySelectorAll
*/

const cssSelectOptions = {
Expand All @@ -14,14 +16,14 @@ const cssSelectOptions = {
};

/**
* @type {(node: XastNode, selector: string) => XastChild[]}
* @type {querySelectorAll}
*/
export const querySelectorAll = (node, selector) => {
return selectAll(selector, node, cssSelectOptions);
};

/**
* @type {(node: XastNode, selector: string) => ?XastChild}
* @type {querySelector}
*/
export const querySelector = (node, selector) => {
return selectOne(selector, node, cssSelectOptions);
Expand Down
55 changes: 41 additions & 14 deletions plugins/_collections.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// https://www.w3.org/TR/SVG11/intro.html#Definitions

/**
* @typedef {import('../lib/svgo.ts')} svgo
*/

/**
* @type {Record<string, Set<string>>}
* @see svgo#_collections
*/
export const elemsGroups = {
animation: new Set([
Expand Down Expand Up @@ -101,18 +106,18 @@ export const elemsGroups = {
};

/**
* Elements where adding or removing whitespace may effect rendering, metadata,
* or semantic meaning.
*
* @see https://developer.mozilla.org/docs/Web/HTML/Element/pre
* @see svgo#_collections
*/
export const textElems = new Set([...elemsGroups.textContent, 'pre', 'title']);

/**
* @see svgo#_collections
*/
export const pathElems = new Set(['glyph', 'missing-glyph', 'path']);

/**
* @type {Record<string, Set<string>>}
* @see https://www.w3.org/TR/SVG11/intro.html#Definitions
* @see svgo#_collections
*/
export const attrsGroups = {
animationAddition: new Set(['additive', 'accumulate']),
Expand Down Expand Up @@ -311,6 +316,7 @@ export const attrsGroups = {

/**
* @type {Record<string, Record<string, string>>}
* @see svgo#_collections
*/
export const attrsGroupsDefaults = {
core: { 'xml:space': 'default' },
Expand Down Expand Up @@ -378,7 +384,7 @@ export const attrsGroupsDefaults = {

/**
* @type {Record<string, { safe?: Set<string>, unsafe?: Set<string> }>}
* @see https://www.w3.org/TR/SVG11/intro.html#Definitions
* @see svgo#_collections
*/
export const attrsGroupsDeprecated = {
animationAttributeTarget: { unsafe: new Set(['attributeType']) },
Expand Down Expand Up @@ -408,7 +414,7 @@ export const attrsGroupsDeprecated = {
* contentGroups?: Set<string>,
* content?: Set<string>,
* }>}
* @see https://www.w3.org/TR/SVG11/eltindex.html
* @see svgo#_collections
*/
export const elems = {
a: {
Expand Down Expand Up @@ -2067,7 +2073,9 @@ export const elems = {
},
};

// https://wiki.inkscape.org/wiki/index.php/Inkscape-specific_XML_attributes
/**
* @see svgo#_collections
*/
export const editorNamespaces = new Set([
'http://creativecommons.org/ns#',
'http://inkscape.sourceforge.net/DTD/sodipodi-0.dtd',
Expand All @@ -2094,7 +2102,7 @@ export const editorNamespaces = new Set([
]);

/**
* @see https://www.w3.org/TR/SVG11/linking.html#processingIRI
* @see svgo#_collections
*/
export const referencesProps = new Set([
'clip-path',
Expand All @@ -2110,7 +2118,7 @@ export const referencesProps = new Set([
]);

/**
* @see https://www.w3.org/TR/SVG11/propidx.html
* @see svgo#_collections
*/
export const inheritableAttrs = new Set([
'clip-rule',
Expand Down Expand Up @@ -2172,9 +2180,8 @@ export const presentationNonInheritableGroupAttrs = new Set([
]);

/**
* https://www.w3.org/TR/SVG11/single-page.html#types-ColorKeywords
*
* @type {Record<string, string>}
* @see svgo#_collections
*/
export const colorsNames = {
aliceblue: '#f0f8ff',
Expand Down Expand Up @@ -2366,7 +2373,7 @@ export const colorsShortNames = {
};

/**
* @see https://www.w3.org/TR/SVG11/single-page.html#types-DataTypeColor
* @see svgo#_collections
*/
export const colorsProps = new Set([
'color',
Expand All @@ -2377,7 +2384,9 @@ export const colorsProps = new Set([
'stroke',
]);

/** @see https://developer.mozilla.org/docs/Web/CSS/Pseudo-classes */
/**
* @see svgo#_collections
*/
export const pseudoClasses = {
displayState: new Set(['fullscreen', 'modal', 'picture-in-picture']),
input: new Set([
Expand Down Expand Up @@ -2434,3 +2443,21 @@ export const pseudoClasses = {
]),
functional: new Set(['is', 'not', 'where', 'has']),
};

export default {
elemsGroups,
textElems,
pathElems,
attrsGroups,
attrsGroupsDefaults,
attrsGroupsDeprecated,
elems,
editorNamespaces,
referencesProps,
inheritableAttrs,
presentationNonInheritableGroupAttrs,
colorsNames,
colorsShortNames,
colorsProps,
pseudoClasses,
};
Loading