scratch-paint 4.1.52 → 4.1.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "scratch-paint",
3
- "version": "4.1.52",
3
+ "version": "4.1.54",
4
4
  "description": "Graphical User Interface for the Scratch 3.0 paint editor, which is used to make and edit sprites for use in projects.",
5
5
  "main": "./dist/scratch-paint.js",
6
6
  "browser": "./src/index.js",
@@ -24,7 +24,7 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "@scratch/paper": "^0.11.20221201200345",
27
- "@scratch/scratch-svg-renderer": "13.7.2",
27
+ "@scratch/scratch-svg-renderer": "13.7.3",
28
28
  "classnames": "^2.2.5",
29
29
  "keymirror": "^0.1.1",
30
30
  "lodash.bindall": "^4.4.0",
@@ -7,6 +7,7 @@ import {sanitizeSvg} from '@scratch/scratch-svg-renderer';
7
7
  import Formats from '../lib/format';
8
8
  import log from '../log/log';
9
9
 
10
+ import {stripInvalidPaperData} from '../helper/strip-invalid-paper-data';
10
11
  import {performSnapshot} from '../helper/undo';
11
12
  import {undoSnapshot, clearUndoState} from '../reducers/undo';
12
13
  import {isGroup, ungroupItems} from '../helper/group';
@@ -215,11 +216,12 @@ class PaperCanvas extends React.Component {
215
216
  // well-formed document.
216
217
  svg = sanitizeSvg.sanitizeSvgText(svg);
217
218
 
218
- // Get the origin which the viewBox is defined relative to. During import, Paper will translate
219
- // the viewBox to start at (0, 0), and we need to translate it back for some costumes to render
220
- // correctly.
221
- const parser = new DOMParser();
222
- const svgDom = parser.parseFromString(svg, 'text/xml');
219
+ // 4. Parse once: read viewBox (translated back for some costumes
220
+ // to render correctly paper translates it to (0, 0) on import)
221
+ // and strip data-paper-data values that fail JSON.parse (paper.js
222
+ // synchronously throws on these and aborts the whole import).
223
+ const svgDom = new DOMParser().parseFromString(svg, 'text/xml');
224
+ const modified = stripInvalidPaperData(svgDom);
223
225
  const viewBox = svgDom.documentElement.attributes.viewBox ?
224
226
  svgDom.documentElement.attributes.viewBox.value.match(/\S+/g) : null;
225
227
  if (viewBox) {
@@ -227,6 +229,7 @@ class PaperCanvas extends React.Component {
227
229
  viewBox[i] = parseFloat(viewBox[i]);
228
230
  }
229
231
  }
232
+ if (modified) svg = new XMLSerializer().serializeToString(svgDom);
230
233
 
231
234
  paper.project.importSVG(svg, {
232
235
  expandShapes: true,
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Drop `data-paper-data` attributes whose value isn't valid JSON. Paper.js
3
+ * synchronously calls JSON.parse on this attribute during importSVG and
4
+ * throws on malformed values, taking down the whole import. The attribute
5
+ * is paper's own serialization metadata; if it can't parse, paper wouldn't
6
+ * have been able to use it.
7
+ *
8
+ * Operates on a parsed Document in place so callers that already have one
9
+ * (e.g. for viewBox extraction) don't pay for a second parse-and-serialize.
10
+ * @param {Document} svgDoc - parsed SVG document; mutated in place.
11
+ * @returns {boolean} true if any attribute was removed (caller should
12
+ * re-serialize); false if the document was untouched.
13
+ */
14
+ const stripInvalidPaperData = function (svgDoc) {
15
+ let modified = false;
16
+ const els = svgDoc.querySelectorAll('[data-paper-data]');
17
+ for (let i = 0; i < els.length; i++) {
18
+ try {
19
+ JSON.parse(els[i].getAttribute('data-paper-data'));
20
+ } catch {
21
+ els[i].removeAttribute('data-paper-data');
22
+ modified = true;
23
+ }
24
+ }
25
+ return modified;
26
+ };
27
+
28
+ export {stripInvalidPaperData};