diff --git a/src/element-categories.js b/src/element-categories.js deleted file mode 100644 index f556682a..00000000 --- a/src/element-categories.js +++ /dev/null @@ -1,30 +0,0 @@ -// Elements that can have paint attributes (e.g. fill and stroke) -// Per the SVG spec, things like fill and stroke apply to shapes and text content elements -// https://www.w3.org/TR/SVG11/painting.html#FillProperty -// https://www.w3.org/TR/SVG11/painting.html#StrokeProperty -const PAINTABLE_ELEMENTS = new Set([ - // Shape elements (https://www.w3.org/TR/SVG11/intro.html#TermShape) - 'path', 'rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', - // Text content elements (https://www.w3.org/TR/SVG11/intro.html#TermTextContentElement) - // The actual tag names are `altGlyph` and `textPath`, but we're lowercasing the tag name in isContainerElement, - // so they should be lowercased here too. - 'altglyph', 'textpath', 'text', 'tref', 'tspan' -]); - -// "An element which can have graphics elements and other container elements as child elements." -// https://www.w3.org/TR/SVG11/intro.html#TermContainerElement -const CONTAINER_ELEMENTS = new Set([ - 'a', 'defs', 'g', 'marker', 'glyph', 'missing-glyph', 'pattern', 'svg', 'switch', 'symbol' -]); - -const isPaintableElement = function (element) { - return element.tagName && PAINTABLE_ELEMENTS.has(element.tagName.toLowerCase()); -}; -const isContainerElement = function (element) { - return element.tagName && CONTAINER_ELEMENTS.has(element.tagName.toLowerCase()); -}; - -module.exports = { - isPaintableElement, - isContainerElement -}; diff --git a/src/transform-applier.js b/src/transform-applier.js index 591cb0b5..0dde3446 100644 --- a/src/transform-applier.js +++ b/src/transform-applier.js @@ -1,6 +1,5 @@ const Matrix = require('transformation-matrix'); const SvgElement = require('./svg-element'); -const {isPaintableElement, isContainerElement} = require('./element-categories'); const log = require('./util/log'); /** @@ -289,6 +288,14 @@ const _transformPath = function (pathString, transform) { return result; }; +const GRAPHICS_ELEMENTS = ['circle', 'ellipse', 'image', 'line', 'path', 'polygon', 'polyline', 'rect', 'text', 'use']; +const CONTAINER_ELEMENTS = ['a', 'defs', 'g', 'marker', 'glyph', 'missing-glyph', 'pattern', 'svg', 'switch', 'symbol']; +const _isContainerElement = function (element) { + return element.tagName && CONTAINER_ELEMENTS.includes(element.tagName.toLowerCase()); +}; +const _isGraphicsElement = function (element) { + return element.tagName && GRAPHICS_ELEMENTS.includes(element.tagName.toLowerCase()); +}; const _isPathWithTransformAndStroke = function (element, strokeWidth) { if (!element.attributes) return false; strokeWidth = element.attributes['stroke-width'] ? @@ -507,7 +514,7 @@ const transformStrokeWidths = function (svgTag, windowRef, bboxForTesting) { const inherited = Matrix.identity(); const applyTransforms = (element, matrix, strokeWidth, fill, stroke) => { - if (isContainerElement(element)) { + if (_isContainerElement(element)) { // Push fills and stroke width down to leaves if (element.attributes['stroke-width']) { strokeWidth = element.attributes['stroke-width'].value; @@ -594,7 +601,7 @@ const transformStrokeWidths = function (svgTag, windowRef, bboxForTesting) { element.setAttribute('stroke-width', _quadraticMean(matrixScale.x, matrixScale.y) * strokeWidth); if (fill) element.setAttribute('fill', fill); if (stroke) element.setAttribute('stroke', stroke); - } else if (isPaintableElement(element)) { + } else if (_isGraphicsElement(element)) { // Push stroke width, fill, and stroke down to leaves if (strokeWidth && !element.attributes['stroke-width']) { element.setAttribute('stroke-width', strokeWidth);