@posthog/rrweb-snapshot 0.0.29 → 0.0.30

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.
@@ -515,6 +515,36 @@ function absolutifyURLs(cssText, href) {
515
515
  }
516
516
  );
517
517
  }
518
+ const STRIPED_PLACEHOLDER_SVG = "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTAwJSIgaGVpZ2h0PSIxMDAlIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgogIDxkZWZzPgogICAgPHBhdHRlcm4gaWQ9InN0cmlwZXMiIHBhdHRlcm5Vbml0cz0idXNlclNwYWNlT25Vc2UiIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiI+CiAgICAgIDxyZWN0IHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiIgZmlsbD0iYmxhY2siLz4KICAgICAgPHBhdGggZD0iTTggMEgxNkwwIDE2VjhMOCAwWiIgZmlsbD0iIzJEMkQyRCIvPgogICAgICA8cGF0aCBkPSJNMTYgOFYxNkg4TDE2IDhaIiBmaWxsPSIjMkQyRDJEIi8+CiAgICA8L3BhdHRlcm4+CiAgPC9kZWZzPgogIDxyZWN0IHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InVybCgjc3RyaXBlcykiLz4KPC9zdmc+Cg==";
519
+ const MAX_IMAGE_DIMENSION_FOR_RECOMPRESSION = 4096;
520
+ function recompressBase64Image(img, dataURL, type, quality) {
521
+ if (!img.complete || img.naturalWidth === 0) {
522
+ return dataURL;
523
+ }
524
+ if (img.naturalWidth > MAX_IMAGE_DIMENSION_FOR_RECOMPRESSION || img.naturalHeight > MAX_IMAGE_DIMENSION_FOR_RECOMPRESSION) {
525
+ return dataURL;
526
+ }
527
+ try {
528
+ const canvas = document.createElement("canvas");
529
+ canvas.width = img.naturalWidth;
530
+ canvas.height = img.naturalHeight;
531
+ const ctx = canvas.getContext("2d");
532
+ if (!ctx) {
533
+ return dataURL;
534
+ }
535
+ ctx.drawImage(img, 0, 0);
536
+ const recompressed = canvas.toDataURL(type || "image/webp", quality != null ? quality : 0.4);
537
+ return recompressed;
538
+ } catch (err) {
539
+ return dataURL;
540
+ }
541
+ }
542
+ function checkDataURLSize(dataURL, maxLength) {
543
+ if (!maxLength || dataURL.length <= maxLength) {
544
+ return dataURL;
545
+ }
546
+ return STRIPED_PLACEHOLDER_SVG;
547
+ }
518
548
  let _id = 1;
519
549
  const tagNameRegex = new RegExp("[^a-z0-9-_:]");
520
550
  const IGNORED_NODE = -2;
@@ -613,12 +643,32 @@ function getHref(doc, customHref) {
613
643
  a.setAttribute("href", customHref);
614
644
  return a.href;
615
645
  }
616
- function transformAttribute(doc, tagName, name, value) {
646
+ function transformAttribute(doc, tagName, name, value, element, dataURLOptions) {
617
647
  if (!value) {
618
648
  return value;
619
649
  }
620
650
  if (name === "src" || name === "href" && !(tagName === "use" && value[0] === "#")) {
621
- return absoluteToDoc(doc, value);
651
+ const transformedValue = absoluteToDoc(doc, value);
652
+ if (tagName === "img" && transformedValue.startsWith("data:") && element) {
653
+ const img = element;
654
+ let processedDataURL = transformedValue;
655
+ if ((dataURLOptions == null ? void 0 : dataURLOptions.type) || (dataURLOptions == null ? void 0 : dataURLOptions.quality) !== void 0) {
656
+ processedDataURL = recompressBase64Image(
657
+ img,
658
+ transformedValue,
659
+ dataURLOptions.type,
660
+ dataURLOptions.quality
661
+ );
662
+ }
663
+ if (dataURLOptions == null ? void 0 : dataURLOptions.maxBase64ImageLength) {
664
+ processedDataURL = checkDataURLSize(
665
+ processedDataURL,
666
+ dataURLOptions.maxBase64ImageLength
667
+ );
668
+ }
669
+ return processedDataURL;
670
+ }
671
+ return transformedValue;
622
672
  } else if (name === "xlink:href" && value[0] !== "#") {
623
673
  return absoluteToDoc(doc, value);
624
674
  } else if (name === "background" && (tagName === "table" || tagName === "td" || tagName === "th")) {
@@ -909,7 +959,9 @@ function serializeElementNode(n, options) {
909
959
  doc,
910
960
  tagName,
911
961
  toLowerCase(attr.name),
912
- attr.value
962
+ attr.value,
963
+ n,
964
+ dataURLOptions
913
965
  );
914
966
  }
915
967
  }
@@ -5779,6 +5831,7 @@ exports.NodeType = NodeType;
5779
5831
  exports.absolutifyURLs = absolutifyURLs;
5780
5832
  exports.adaptCssForReplay = adaptCssForReplay;
5781
5833
  exports.buildNodeWithSN = buildNodeWithSN;
5834
+ exports.checkDataURLSize = checkDataURLSize;
5782
5835
  exports.classMatchesRegex = classMatchesRegex;
5783
5836
  exports.cleanupSnapshot = cleanupSnapshot;
5784
5837
  exports.createCache = createCache;
@@ -5799,6 +5852,7 @@ exports.isShadowRoot = isShadowRoot;
5799
5852
  exports.maskInputValue = maskInputValue;
5800
5853
  exports.needMaskingText = needMaskingText;
5801
5854
  exports.rebuild = rebuild;
5855
+ exports.recompressBase64Image = recompressBase64Image;
5802
5856
  exports.serializeNodeWithId = serializeNodeWithId;
5803
5857
  exports.snapshot = snapshot;
5804
5858
  exports.stringifyRule = stringifyRule;