Skip to content

Commit 414c7d7

Browse files
committed
Move element categories into own file+correct them
Instead of checking if an element is a graphics element, check if it's "paintable" (fill/stroke applies to it). This includes several text-related elements, and excludes `use` and `image` elements.
1 parent 06c0e4a commit 414c7d7

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/element-categories.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Elements that can have paint attributes (e.g. fill and stroke)
2+
// Per the SVG spec, things like fill and stroke apply to shapes and text content elements
3+
// https://www.w3.org/TR/SVG11/painting.html#FillProperty
4+
// https://www.w3.org/TR/SVG11/painting.html#StrokeProperty
5+
const PAINTABLE_ELEMENTS = new Set([
6+
// Shape elements (https://www.w3.org/TR/SVG11/intro.html#TermShape)
7+
'path', 'rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon',
8+
// Text content elements (https://www.w3.org/TR/SVG11/intro.html#TermTextContentElement)
9+
// The actual tag names are `altGlyph` and `textPath`, but we're lowercasing the tag name in isContainerElement,
10+
// so they should be lowercased here too.
11+
'altglyph', 'textpath', 'text', 'tref', 'tspan'
12+
]);
13+
14+
// "An element which can have graphics elements and other container elements as child elements."
15+
// https://www.w3.org/TR/SVG11/intro.html#TermContainerElement
16+
const CONTAINER_ELEMENTS = new Set([
17+
'a', 'defs', 'g', 'marker', 'glyph', 'missing-glyph', 'pattern', 'svg', 'switch', 'symbol'
18+
]);
19+
20+
const isPaintableElement = function (element) {
21+
return element.tagName && PAINTABLE_ELEMENTS.has(element.tagName.toLowerCase());
22+
};
23+
const isContainerElement = function (element) {
24+
return element.tagName && CONTAINER_ELEMENTS.has(element.tagName.toLowerCase());
25+
};
26+
27+
module.exports = {
28+
isPaintableElement,
29+
isContainerElement
30+
};

src/transform-applier.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const Matrix = require('transformation-matrix');
22
const SvgElement = require('./svg-element');
3+
const {isPaintableElement, isContainerElement} = require('./element-categories');
34
const log = require('./util/log');
45

56
/**
@@ -288,14 +289,6 @@ const _transformPath = function (pathString, transform) {
288289
return result;
289290
};
290291

291-
const GRAPHICS_ELEMENTS = ['circle', 'ellipse', 'image', 'line', 'path', 'polygon', 'polyline', 'rect', 'text', 'use'];
292-
const CONTAINER_ELEMENTS = ['a', 'defs', 'g', 'marker', 'glyph', 'missing-glyph', 'pattern', 'svg', 'switch', 'symbol'];
293-
const _isContainerElement = function (element) {
294-
return element.tagName && CONTAINER_ELEMENTS.includes(element.tagName.toLowerCase());
295-
};
296-
const _isGraphicsElement = function (element) {
297-
return element.tagName && GRAPHICS_ELEMENTS.includes(element.tagName.toLowerCase());
298-
};
299292
const _isPathWithTransformAndStroke = function (element, strokeWidth) {
300293
if (!element.attributes) return false;
301294
strokeWidth = element.attributes['stroke-width'] ?
@@ -514,7 +507,7 @@ const transformStrokeWidths = function (svgTag, windowRef, bboxForTesting) {
514507
const inherited = Matrix.identity();
515508

516509
const applyTransforms = (element, matrix, strokeWidth, fill, stroke) => {
517-
if (_isContainerElement(element)) {
510+
if (isContainerElement(element)) {
518511
// Push fills and stroke width down to leaves
519512
if (element.attributes['stroke-width']) {
520513
strokeWidth = element.attributes['stroke-width'].value;
@@ -601,7 +594,7 @@ const transformStrokeWidths = function (svgTag, windowRef, bboxForTesting) {
601594
element.setAttribute('stroke-width', _quadraticMean(matrixScale.x, matrixScale.y) * strokeWidth);
602595
if (fill) element.setAttribute('fill', fill);
603596
if (stroke) element.setAttribute('stroke', stroke);
604-
} else if (_isGraphicsElement(element)) {
597+
} else if (isPaintableElement(element)) {
605598
// Push stroke width, fill, and stroke down to leaves
606599
if (strokeWidth && !element.attributes['stroke-width']) {
607600
element.setAttribute('stroke-width', strokeWidth);

0 commit comments

Comments
 (0)