Skip to content

Commit

Permalink
Remove result.info.width and result.info.height
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed Oct 9, 2022
1 parent 420ae9d commit 5857816
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 47 deletions.
31 changes: 2 additions & 29 deletions lib/stringifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ const { textElems } = require('../plugins/_collections.js');

/**
* @typedef {{
* width: void | string,
* height: void | string,
* indent: string,
* textContext: null | XastElement,
* indentLevel: number,
Expand Down Expand Up @@ -81,13 +79,7 @@ const entities = {
/**
* convert XAST to SVG string
*
* @type {(data: XastRoot, config: StringifyOptions) => {
* data: string,
* info: {
* width: void | string,
* height: void | string
* }
* }}
* @type {(data: XastRoot, config: StringifyOptions) => string}
*/
const stringifySvg = (data, userOptions = {}) => {
/**
Expand All @@ -105,9 +97,6 @@ const stringifySvg = (data, userOptions = {}) => {
* @type {State}
*/
const state = {
// TODO remove width and height in v3
width: undefined,
height: undefined,
indent: newIndent,
textContext: null,
indentLevel: 0,
Expand All @@ -127,13 +116,7 @@ const stringifySvg = (data, userOptions = {}) => {
if (config.finalNewline && svg.length > 0 && svg[svg.length - 1] !== '\n') {
svg += eol;
}
return {
data: svg,
info: {
width: state.width,
height: state.height,
},
};
return svg;
};
exports.stringifySvg = stringifySvg;

Expand Down Expand Up @@ -219,16 +202,6 @@ const stringifyCdata = (node, config, state) => {
* @type {(node: XastElement, config: Options, state: State) => string}
*/
const stringifyElement = (node, config, state) => {
// beautiful injection for obtaining SVG information :)
if (
node.name === 'svg' &&
node.attributes.width != null &&
node.attributes.height != null
) {
state.width = node.attributes.width;
state.height = node.attributes.height;
}

// empty element and short tag
if (node.children.length === 0) {
if (config.useShortTags) {
Expand Down
29 changes: 11 additions & 18 deletions lib/svgo.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,14 @@ const optimize = (input, config) => {
}
const maxPassCount = config.multipass ? 10 : 1;
let prevResultSize = Number.POSITIVE_INFINITY;
let svgjs = null;
let output = '';
const info = {};
if (config.path != null) {
info.path = config.path;
}
for (let i = 0; i < maxPassCount; i += 1) {
info.multipassCount = i;
svgjs = parseSvg(input, config.path);
if (svgjs.error != null) {
if (config.path != null) {
svgjs.path = config.path;
}
return svgjs;
}
const ast = parseSvg(input, config.path);
const plugins = config.plugins || ['preset-default'];
if (Array.isArray(plugins) === false) {
throw Error(
Expand All @@ -81,21 +75,20 @@ const optimize = (input, config) => {
if (config.floatPrecision != null) {
globalOverrides.floatPrecision = config.floatPrecision;
}
invokePlugins(svgjs, info, resolvedPlugins, null, globalOverrides);
svgjs = stringifySvg(svgjs, config.js2svg);
if (svgjs.data.length < prevResultSize) {
input = svgjs.data;
prevResultSize = svgjs.data.length;
invokePlugins(ast, info, resolvedPlugins, null, globalOverrides);
output = stringifySvg(ast, config.js2svg);
if (output.length < prevResultSize) {
input = output;
prevResultSize = output.length;
} else {
break;
}
}
if (config.datauri) {
svgjs.data = encodeSVGDatauri(svgjs.data, config.datauri);
}
if (config.path != null) {
svgjs.path = config.path;
output = encodeSVGDatauri(output, config.datauri);
}
return svgjs;
return {
data: output,
};
};
exports.optimize = optimize;

0 comments on commit 5857816

Please sign in to comment.