|
1 | | -const DOMPurify = require('isomorphic-dompurify'); |
2 | 1 | const SvgElement = require('./svg-element'); |
3 | 2 | const convertFonts = require('./font-converter'); |
4 | | -const fixupSvgString = require('./fixup-svg-string'); |
5 | 3 | const transformStrokeWidths = require('./transform-applier'); |
| 4 | +const {sanitizeSvgText} = require('./sanitize-svg'); |
6 | 5 |
|
7 | 6 | /** |
8 | 7 | * @param {SVGElement} svgTag the tag to search within |
@@ -208,24 +207,10 @@ const transformMeasurements = svgTag => { |
208 | 207 | // which returns the full bounding-box of all drawn SVG |
209 | 208 | // elements, similar to how Scratch 2.0 did measurement. |
210 | 209 | const svgSpot = document.createElement('span'); |
211 | | - // Since we're adding user-provided SVG to document.body, |
212 | | - // sanitizing is required. This should not affect bounding box calculation. |
213 | | - // outerHTML is attribute of Element (and not HTMLElement), so use it instead of |
214 | | - // calling serializer or toString() |
215 | | - // NOTE: svgTag remains untouched! |
216 | | - const rawValue = svgTag.outerHTML; |
217 | | - const sanitizedValue = DOMPurify.sanitize(rawValue, { |
218 | | - // Use SVG profile (no HTML elements) |
219 | | - USE_PROFILES: {svg: true}, |
220 | | - // Remove some tags that Scratch does not use. |
221 | | - FORBID_TAGS: ['a', 'audio', 'canvas', 'video'], |
222 | | - // Allow data URI in image tags (e.g. SVGs converted from bitmap) |
223 | | - ADD_DATA_URI_TAGS: ['image'] |
224 | | - }); |
225 | 210 | let bbox; |
226 | 211 | try { |
227 | 212 | // Insert sanitized value. |
228 | | - svgSpot.innerHTML = sanitizedValue; |
| 213 | + svgSpot.innerHTML = svgTag.outerHTML; |
229 | 214 | document.body.appendChild(svgSpot); |
230 | 215 | // Take the bounding box. We have to get elements via svgSpot |
231 | 216 | // because we added it via innerHTML. |
@@ -320,8 +305,11 @@ const normalizeSvg = (svgTag, fromVersion2) => { |
320 | 305 | const loadSvgString = (svgString, fromVersion2) => { |
321 | 306 | // Parse string into SVG XML. |
322 | 307 | const parser = new DOMParser(); |
323 | | - svgString = fixupSvgString(svgString); |
324 | | - const svgDom = parser.parseFromString(svgString, 'text/xml'); |
| 308 | + |
| 309 | + // Since we're adding user-provided SVG to document.body as part of normalization, |
| 310 | + // sanitization is required. This should not affect bounding box calculation. |
| 311 | + const sanitizedSvgString = sanitizeSvgText(svgString); |
| 312 | + const svgDom = parser.parseFromString(sanitizedSvgString, 'text/xml'); |
325 | 313 | if (svgDom.childNodes.length < 1 || |
326 | 314 | svgDom.documentElement.localName !== 'svg') { |
327 | 315 | throw new Error('Document does not appear to be SVG.'); |
|
0 commit comments