@lvce-editor/preview-sandbox-worker 3.0.0 → 3.2.0

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.
@@ -1238,117 +1238,63 @@ const Del = 'del';
1238
1238
  const H6 = 'h6';
1239
1239
  const Canvas = 'canvas';
1240
1240
 
1241
+ const tagMap = {
1242
+ [A]: A$1,
1243
+ [Abbr]: Abbr$1,
1244
+ [Article]: Article$1,
1245
+ [Aside]: Aside$1,
1246
+ [Br]: Br$1,
1247
+ [Button]: Button$1,
1248
+ [Canvas]: Canvas$1,
1249
+ [Cite]: Cite$1,
1250
+ [Code]: Code$1,
1251
+ [Data]: Data$1,
1252
+ [Dd]: Dd$1,
1253
+ [Del]: Del$1,
1254
+ [Div]: Div$1,
1255
+ [Dl]: Dl$1,
1256
+ [Dt]: Dt$1,
1257
+ [Figcaption]: Figcaption$1,
1258
+ [Figure]: Figure$1,
1259
+ [Footer]: Footer$1,
1260
+ [Form]: Div$1,
1261
+ [H1]: H1$1,
1262
+ [H2]: H2$1,
1263
+ [H3]: H3$1,
1264
+ [H4]: H4$1,
1265
+ [H5]: H5$1,
1266
+ [H6]: H6$1,
1267
+ [Header]: Header$1,
1268
+ [Hr]: Hr$1,
1269
+ [Iframe]: Iframe$1,
1270
+ [Img]: Img$1,
1271
+ [Input]: Input$1,
1272
+ [Label]: Label$1,
1273
+ [Li]: Li$1,
1274
+ [Nav]: Nav$1,
1275
+ [Ol]: Ol$1,
1276
+ [Option]: Option$1,
1277
+ [P]: P$1,
1278
+ [Pre]: Pre$1,
1279
+ [Search]: Search$1,
1280
+ [Section]: Section$1,
1281
+ [Select]: Select$1,
1282
+ [Span]: Span$1,
1283
+ [Table]: Table$1,
1284
+ [TBody]: TBody$1,
1285
+ [Td]: Td$1,
1286
+ [TextArea]: TextArea$1,
1287
+ [Tfoot]: Tfoot$1,
1288
+ [Th]: Th$1,
1289
+ [THead]: THead$1,
1290
+ [Time]: Time$1,
1291
+ [Tr]: Tr$1,
1292
+ [Ul]: Ul$1,
1293
+ html: Html,
1294
+ style: Style
1295
+ };
1241
1296
  const getVirtualDomTag = text => {
1242
- switch (text) {
1243
- case A:
1244
- return A$1;
1245
- case Abbr:
1246
- return Abbr$1;
1247
- case Article:
1248
- return Article$1;
1249
- case Aside:
1250
- return Aside$1;
1251
- case Br:
1252
- return Br$1;
1253
- case Button:
1254
- return Button$1;
1255
- case Canvas:
1256
- return Canvas$1;
1257
- case Cite:
1258
- return Cite$1;
1259
- case Code:
1260
- return Code$1;
1261
- case Data:
1262
- return Data$1;
1263
- case Dd:
1264
- return Dd$1;
1265
- case Del:
1266
- return Del$1;
1267
- case Div:
1268
- return Div$1;
1269
- case Dl:
1270
- return Dl$1;
1271
- case Dt:
1272
- return Dt$1;
1273
- case Figcaption:
1274
- return Figcaption$1;
1275
- case Figure:
1276
- return Figure$1;
1277
- case Footer:
1278
- return Footer$1;
1279
- case Form:
1280
- return Div$1;
1281
- case H1:
1282
- return H1$1;
1283
- case H2:
1284
- return H2$1;
1285
- case H3:
1286
- return H3$1;
1287
- case H4:
1288
- return H4$1;
1289
- case H5:
1290
- return H5$1;
1291
- case H6:
1292
- return H6$1;
1293
- case Header:
1294
- return Header$1;
1295
- case Hr:
1296
- return Hr$1;
1297
- case Iframe:
1298
- return Iframe$1;
1299
- case Img:
1300
- return Img$1;
1301
- case Input:
1302
- return Input$1;
1303
- case Label:
1304
- return Label$1;
1305
- case Li:
1306
- return Li$1;
1307
- case Nav:
1308
- return Nav$1;
1309
- case Ol:
1310
- return Ol$1;
1311
- case Option:
1312
- return Option$1;
1313
- case P:
1314
- return P$1;
1315
- case Pre:
1316
- return Pre$1;
1317
- case Search:
1318
- return Search$1;
1319
- case Section:
1320
- return Section$1;
1321
- case Select:
1322
- return Select$1;
1323
- case Span:
1324
- return Span$1;
1325
- case Table:
1326
- return Table$1;
1327
- case TBody:
1328
- return TBody$1;
1329
- case Td:
1330
- return Td$1;
1331
- case TextArea:
1332
- return TextArea$1;
1333
- case Tfoot:
1334
- return Tfoot$1;
1335
- case Th:
1336
- return Th$1;
1337
- case THead:
1338
- return THead$1;
1339
- case Time:
1340
- return Time$1;
1341
- case Tr:
1342
- return Tr$1;
1343
- case Ul:
1344
- return Ul$1;
1345
- case 'html':
1346
- return Html;
1347
- case 'style':
1348
- return Style;
1349
- default:
1350
- return Div$1;
1351
- }
1297
+ return tagMap[text] || Div$1;
1352
1298
  };
1353
1299
 
1354
1300
  /* eslint-disable @cspell/spellchecker */
@@ -1397,6 +1343,32 @@ const TAGS_TO_SKIP_TAG_ONLY = new Set(['html', 'body', 'head']);
1397
1343
 
1398
1344
  // Tags where we extract content as CSS
1399
1345
  const CSS_TAGS = new Set(['style']);
1346
+ const serializeAttributeName = attrName => {
1347
+ if (attrName === 'class') {
1348
+ return 'className';
1349
+ }
1350
+ if (attrName === 'type') {
1351
+ return 'inputType';
1352
+ }
1353
+ return attrName;
1354
+ };
1355
+ const applyAllowedAttributes = (targetNode, sourceNode) => {
1356
+ const {
1357
+ attributes
1358
+ } = sourceNode;
1359
+ if (!attributes) {
1360
+ return;
1361
+ }
1362
+ for (let i = 0; i < attributes.length; i++) {
1363
+ const attr = attributes[i];
1364
+ const attrName = attr.name;
1365
+ if (isDefaultAllowedAttribute(attrName, [])) {
1366
+ const finalName = serializeAttributeName(attrName);
1367
+ targetNode[finalName] = attr.value;
1368
+ }
1369
+ }
1370
+ };
1371
+
1400
1372
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
1401
1373
  const serializeNode = (node, dom, css, context) => {
1402
1374
  const {
@@ -1452,6 +1424,7 @@ const serializeNode = (node, dom, css, context) => {
1452
1424
  type: Reference,
1453
1425
  uid: node.__canvasId
1454
1426
  };
1427
+ applyAllowedAttributes(refNode, node);
1455
1428
  if (context.elementMap) {
1456
1429
  context.elementMap[node.__canvasId + ''] = node;
1457
1430
  }
@@ -1464,26 +1437,7 @@ const serializeNode = (node, dom, css, context) => {
1464
1437
  childCount: 0,
1465
1438
  type: getVirtualDomTag(tagName)
1466
1439
  };
1467
-
1468
- // Copy allowed attributes
1469
- const {
1470
- attributes
1471
- } = node;
1472
- if (attributes) {
1473
- for (let i = 0; i < attributes.length; i++) {
1474
- const attr = attributes[i];
1475
- const attrName = attr.name;
1476
- if (isDefaultAllowedAttribute(attrName, [])) {
1477
- let finalName = attrName;
1478
- if (attrName === 'class') {
1479
- finalName = 'className';
1480
- } else if (attrName === 'type') {
1481
- finalName = 'inputType';
1482
- }
1483
- newNode[finalName] = attr.value;
1484
- }
1485
- }
1486
- }
1440
+ applyAllowedAttributes(newNode, node);
1487
1441
 
1488
1442
  // Assign element tracking ID for interactivity
1489
1443
  const hdId = String(context.nextId++);
@@ -89603,6 +89557,28 @@ const createWindow = (rawHtml, url = 'http://localhost:3000') => {
89603
89557
  };
89604
89558
  };
89605
89559
 
89560
+ /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
89561
+
89562
+ const exposeCanvasGlobals = (window, document) => {
89563
+ const existingCanvasRenderingContext2D = window.CanvasRenderingContext2D || globalThis.CanvasRenderingContext2D;
89564
+ if (existingCanvasRenderingContext2D) {
89565
+ window.CanvasRenderingContext2D = existingCanvasRenderingContext2D;
89566
+ globalThis.CanvasRenderingContext2D = existingCanvasRenderingContext2D;
89567
+ return;
89568
+ }
89569
+ const canvas = document.querySelector('canvas');
89570
+ if (!canvas || typeof canvas.getContext !== 'function') {
89571
+ return;
89572
+ }
89573
+ const context = canvas.getContext('2d');
89574
+ const CanvasRenderingContext2D = context?.constructor;
89575
+ if (typeof CanvasRenderingContext2D !== 'function') {
89576
+ return;
89577
+ }
89578
+ window.CanvasRenderingContext2D = CanvasRenderingContext2D;
89579
+ globalThis.CanvasRenderingContext2D = CanvasRenderingContext2D;
89580
+ };
89581
+
89606
89582
  /**
89607
89583
  * Extracts line and column information from an error stack trace
89608
89584
  * Looks for patterns like "<anonymous>:114:26" in the stack trace
@@ -89713,17 +89689,19 @@ const createLocalStorage = () => {
89713
89689
  return localStorage;
89714
89690
  };
89715
89691
 
89716
- const getGlobals = (width, height) => {
89692
+ const getGlobals = (width, height, devicePixelRatio = 1) => {
89717
89693
  const localStorage = createLocalStorage();
89718
89694
  return {
89719
89695
  globalGlobals: {
89720
89696
  alert: alert,
89697
+ devicePixelRatio,
89721
89698
  innerHeight: height,
89722
89699
  innerWidth: width,
89723
89700
  localStorage
89724
89701
  },
89725
89702
  windowGlobals: {
89726
89703
  alert: alert,
89704
+ devicePixelRatio,
89727
89705
  innerHeight: height,
89728
89706
  innerWidth: width
89729
89707
  }
@@ -89833,11 +89811,12 @@ const setGlobals = (window, globalGlobals, windowGlobals) => {
89833
89811
  /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
89834
89812
  /* eslint-disable @typescript-eslint/no-implied-eval */
89835
89813
 
89836
- const executeScripts = (window, document, scripts, width = 0, height = 0) => {
89814
+ const executeScripts = (window, document, scripts, width = 0, height = 0, devicePixelRatio = 1) => {
89815
+ exposeCanvasGlobals(window, document);
89837
89816
  const {
89838
89817
  globalGlobals,
89839
89818
  windowGlobals
89840
- } = getGlobals(width, height);
89819
+ } = getGlobals(width, height, devicePixelRatio);
89841
89820
  setGlobals(window, globalGlobals, windowGlobals);
89842
89821
  let firstError = null;
89843
89822
  let firstCodeFrame = '';
@@ -89904,9 +89883,7 @@ const handleMutations = async uid => {
89904
89883
  elementMap,
89905
89884
  window: happyDomInstance.window
89906
89885
  });
89907
- const parsedDom = serialized.dom;
89908
- // @ts-ignore
89909
- getParsedNodesChildNodeCount(parsedDom);
89886
+ getParsedNodesChildNodeCount(serialized.dom);
89910
89887
  try {
89911
89888
  await invoke$1('Preview.handleMutation', uid);
89912
89889
  } catch (error) {
@@ -89951,6 +89928,9 @@ const toNumber = value => {
89951
89928
 
89952
89929
  /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
89953
89930
 
89931
+ const reflectCanvasDimensionAttribute = (element, name, value) => {
89932
+ element.setAttribute(name, String(value));
89933
+ };
89954
89934
  const patchCanvasElements = async (document, uid) => {
89955
89935
  const canvasElements = document.querySelectorAll('canvas');
89956
89936
  if (canvasElements.length === 0) {
@@ -89998,6 +89978,7 @@ const patchCanvasElements = async (document, uid) => {
89998
89978
  get: () => widthValue,
89999
89979
  set: newWidth => {
90000
89980
  widthValue = toNumber(newWidth);
89981
+ reflectCanvasDimensionAttribute(element, 'width', widthValue);
90001
89982
  // @ts-ignore
90002
89983
  element.__offscreenCanvas.width = widthValue;
90003
89984
  }
@@ -90011,6 +89992,7 @@ const patchCanvasElements = async (document, uid) => {
90011
89992
  get: () => heightValue,
90012
89993
  set: newHeight => {
90013
89994
  heightValue = toNumber(newHeight);
89995
+ reflectCanvasDimensionAttribute(element, 'height', heightValue);
90014
89996
  // @ts-ignore
90015
89997
  element.__offscreenCanvas.height = heightValue;
90016
89998
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/preview-sandbox-worker",
3
- "version": "3.0.0",
3
+ "version": "3.2.0",
4
4
  "description": "Preview SandBox Worker",
5
5
  "repository": {
6
6
  "type": "git",