Split out SVG serialization+quirks-mode conversion

I may be going a bit overboard with the exports here.
This commit is contained in:
adroitwhiz 2021-01-07 20:04:37 -05:00
parent 57eb4a8274
commit 8473294384
4 changed files with 36 additions and 8 deletions

View file

@ -2,7 +2,9 @@ const SVGRenderer = require('./svg-renderer');
const BitmapAdapter = require('./bitmap-adapter'); const BitmapAdapter = require('./bitmap-adapter');
const inlineSvgFonts = require('./font-inliner'); const inlineSvgFonts = require('./font-inliner');
const loadSvgString = require('./load-svg-string'); const loadSvgString = require('./load-svg-string');
const serializeSvgToString = require('./serialize-svg-to-string');
const SvgElement = require('./svg-element'); const SvgElement = require('./svg-element');
const V2SVGAdapter = require('./v2-svg-adapter');
const convertFonts = require('./font-converter'); const convertFonts = require('./font-converter');
// /** // /**
// * Export for NPM & Node.js // * Export for NPM & Node.js
@ -13,6 +15,8 @@ module.exports = {
convertFonts: convertFonts, convertFonts: convertFonts,
inlineSvgFonts: inlineSvgFonts, inlineSvgFonts: inlineSvgFonts,
loadSvgString: loadSvgString, loadSvgString: loadSvgString,
serializeSvgToString: serializeSvgToString,
SvgElement: SvgElement, SvgElement: SvgElement,
SVGRenderer: SVGRenderer SVGRenderer: SVGRenderer,
V2SVGAdapter: V2SVGAdapter
}; };

View file

@ -0,0 +1,19 @@
const inlineSvgFonts = require('./font-inliner');
/**
* Serialize a given SVG DOM to a string.
* @param {SVGSVGElement} svgTag The SVG element to serialize.
* @param {?boolean} shouldInjectFonts True if fonts should be included in the SVG as
* base64 data.
* @returns {string} String representing current SVG data.
*/
const serializeSvgToString = (svgTag, shouldInjectFonts) => {
const serializer = new XMLSerializer();
let string = serializer.serializeToString(svgTag);
if (shouldInjectFonts) {
string = inlineSvgFonts(string);
}
return string;
};
module.exports = serializeSvgToString;

View file

@ -1,5 +1,5 @@
const inlineSvgFonts = require('./font-inliner');
const loadSvgString = require('./load-svg-string'); const loadSvgString = require('./load-svg-string');
const serializeSvgToString = require('./serialize-svg-to-string');
/** /**
* Main quirks-mode SVG rendering code. * Main quirks-mode SVG rendering code.
@ -124,14 +124,10 @@ class SvgRenderer {
* @param {?boolean} shouldInjectFonts True if fonts should be included in the SVG as * @param {?boolean} shouldInjectFonts True if fonts should be included in the SVG as
* base64 data. * base64 data.
* @returns {string} String representing current SVG data. * @returns {string} String representing current SVG data.
* @deprecated Use the standalone `serializeSvgToString` export instead.
*/ */
toString (shouldInjectFonts) { toString (shouldInjectFonts) {
const serializer = new XMLSerializer(); return serializeSvgToString(this._svgTag, shouldInjectFonts);
let string = serializer.serializeToString(this._svgTag);
if (shouldInjectFonts) {
string = inlineSvgFonts(string);
}
return string;
} }
/** /**

9
src/v2-svg-adapter.js Normal file
View file

@ -0,0 +1,9 @@
const loadSvgString = require('./load-svg-string');
const serializeSvgToString = require('./serialize-svg-to-string');
/**
* Convert a version 2 SVG (Scratch 2.0 "quirks mode") to a version 3 svg (complies with normal SVG standards).
* @param {string} svgString the SVG string to convert from version 2 SVG.
* @returns {string} the converted SVG string.
*/
module.exports = svgString => serializeSvgToString(loadSvgString(svgString, true /* fromVersion2 */));