@posthog/rrweb-record 0.0.10 → 0.0.12

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.
@@ -5,7 +5,7 @@ var _a;
5
5
  var __defProp$1 = Object.defineProperty;
6
6
  var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
7
  var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
8
- var NodeType$3 = /* @__PURE__ */ ((NodeType2) => {
8
+ var NodeType$1$1 = /* @__PURE__ */ ((NodeType2) => {
9
9
  NodeType2[NodeType2["Document"] = 0] = "Document";
10
10
  NodeType2[NodeType2["DocumentType"] = 1] = "DocumentType";
11
11
  NodeType2[NodeType2["Element"] = 2] = "Element";
@@ -13,7 +13,7 @@ var NodeType$3 = /* @__PURE__ */ ((NodeType2) => {
13
13
  NodeType2[NodeType2["CDATA"] = 4] = "CDATA";
14
14
  NodeType2[NodeType2["Comment"] = 5] = "Comment";
15
15
  return NodeType2;
16
- })(NodeType$3 || {});
16
+ })(NodeType$1$1 || {});
17
17
  const testableAccessors$1 = {
18
18
  Node: ["childNodes", "parentNode", "parentElement", "textContent"],
19
19
  ShadowRoot: ["host", "styleSheets"],
@@ -237,13 +237,9 @@ function stringifyStylesheet(s2) {
237
237
  if (!rules2) {
238
238
  return null;
239
239
  }
240
- let sheetHref = s2.href;
241
- if (!sheetHref && s2.ownerNode && s2.ownerNode.ownerDocument) {
242
- sheetHref = s2.ownerNode.ownerDocument.location.href;
243
- }
244
240
  const stringifiedRules = Array.from(
245
241
  rules2,
246
- (rule2) => stringifyRule(rule2, sheetHref)
242
+ (rule2) => stringifyRule(rule2, s2.href)
247
243
  ).join("");
248
244
  return fixBrowserCompatibilityIssuesInCSS(stringifiedRules);
249
245
  } catch (error) {
@@ -456,90 +452,6 @@ function absolutifyURLs(cssText, href) {
456
452
  }
457
453
  );
458
454
  }
459
- const normalizationCache = /* @__PURE__ */ new Map();
460
- const splitCache = /* @__PURE__ */ new Map();
461
- function normalizeCssString(cssText) {
462
- if (!normalizationCache.has(cssText)) {
463
- const normalized = cssText.replace(/(\/\*[^*]*\*\/)|[\s;]/g, "");
464
- normalizationCache.set(cssText, normalized);
465
- }
466
- return normalizationCache.get(cssText);
467
- }
468
- function splitCssText(cssText, style) {
469
- const og = cssText;
470
- if (splitCache.has(cssText)) {
471
- return splitCache.get(cssText);
472
- }
473
- const childNodes2 = Array.from(style.childNodes);
474
- const splits = [];
475
- let iterLimit = 0;
476
- if (childNodes2.length > 1 && cssText && typeof cssText === "string") {
477
- let cssTextNorm = normalizeCssString(cssText);
478
- const normFactor = cssTextNorm.length / cssText.length;
479
- for (let i2 = 1; i2 < childNodes2.length; i2++) {
480
- if (childNodes2[i2].textContent && typeof childNodes2[i2].textContent === "string") {
481
- const textContentNorm = normalizeCssString(childNodes2[i2].textContent);
482
- let j = 3;
483
- for (; j < textContentNorm.length; j++) {
484
- if (
485
- // keep consuming css identifiers (to get a decent chunk more quickly)
486
- textContentNorm[j].match(/[a-zA-Z0-9]/) || // substring needs to be unique to this section
487
- textContentNorm.indexOf(textContentNorm.substring(0, j), 1) !== -1
488
- ) {
489
- continue;
490
- }
491
- break;
492
- }
493
- for (; j < textContentNorm.length; j++) {
494
- const bit = textContentNorm.substring(0, j);
495
- const bits = cssTextNorm.split(bit);
496
- let splitNorm = -1;
497
- if (bits.length === 2) {
498
- splitNorm = cssTextNorm.indexOf(bit);
499
- } else if (bits.length > 2 && bits[0] === "" && childNodes2[i2 - 1].textContent !== "") {
500
- splitNorm = cssTextNorm.indexOf(bit, 1);
501
- }
502
- if (splitNorm !== -1) {
503
- let k = Math.floor(splitNorm / normFactor);
504
- for (; k > 0 && k < cssText.length; ) {
505
- iterLimit += 1;
506
- if (iterLimit > 50 * childNodes2.length) {
507
- splits.push(cssText);
508
- return splits;
509
- }
510
- const normPart = normalizeCssString(cssText.substring(0, k));
511
- if (normPart.length === splitNorm) {
512
- splits.push(cssText.substring(0, k));
513
- cssText = cssText.substring(k);
514
- cssTextNorm = cssTextNorm.substring(splitNorm);
515
- break;
516
- } else if (normPart.length < splitNorm) {
517
- k += Math.max(
518
- 1,
519
- Math.floor((splitNorm - normPart.length) / normFactor)
520
- );
521
- } else {
522
- k -= Math.max(
523
- 1,
524
- Math.floor((normPart.length - splitNorm) * normFactor)
525
- );
526
- }
527
- }
528
- break;
529
- }
530
- }
531
- }
532
- }
533
- }
534
- splits.push(cssText);
535
- if (og) {
536
- splitCache.set(og, splits);
537
- }
538
- return splits;
539
- }
540
- function markCssSplits(cssText, style) {
541
- return splitCssText(cssText, style).join("/* rr_split */");
542
- }
543
455
  let _id = 1;
544
456
  const tagNameRegex = new RegExp("[^a-z0-9-_:]");
545
457
  const IGNORED_NODE = -2;
@@ -798,28 +710,27 @@ function serializeNode(n2, options) {
798
710
  inlineImages,
799
711
  recordCanvas,
800
712
  keepIframeSrcFn,
801
- newlyAddedElement = false,
802
- cssCaptured = false
713
+ newlyAddedElement = false
803
714
  } = options;
804
715
  const rootId = getRootId(doc, mirror2);
805
716
  switch (n2.nodeType) {
806
717
  case n2.DOCUMENT_NODE:
807
718
  if (n2.compatMode !== "CSS1Compat") {
808
719
  return {
809
- type: NodeType$3.Document,
720
+ type: NodeType$1$1.Document,
810
721
  childNodes: [],
811
722
  compatMode: n2.compatMode
812
723
  // probably "BackCompat"
813
724
  };
814
725
  } else {
815
726
  return {
816
- type: NodeType$3.Document,
727
+ type: NodeType$1$1.Document,
817
728
  childNodes: []
818
729
  };
819
730
  }
820
731
  case n2.DOCUMENT_TYPE_NODE:
821
732
  return {
822
- type: NodeType$3.DocumentType,
733
+ type: NodeType$1$1.DocumentType,
823
734
  name: n2.name,
824
735
  publicId: n2.publicId,
825
736
  systemId: n2.systemId,
@@ -845,18 +756,17 @@ function serializeNode(n2, options) {
845
756
  doc,
846
757
  needsMask,
847
758
  maskTextFn,
848
- rootId,
849
- cssCaptured
759
+ rootId
850
760
  });
851
761
  case n2.CDATA_SECTION_NODE:
852
762
  return {
853
- type: NodeType$3.CDATA,
763
+ type: NodeType$1$1.CDATA,
854
764
  textContent: "",
855
765
  rootId
856
766
  };
857
767
  case n2.COMMENT_NODE:
858
768
  return {
859
- type: NodeType$3.Comment,
769
+ type: NodeType$1$1.Comment,
860
770
  textContent: index$1.textContent(n2) || "",
861
771
  rootId
862
772
  };
@@ -870,26 +780,37 @@ function getRootId(doc, mirror2) {
870
780
  return docId === 1 ? void 0 : docId;
871
781
  }
872
782
  function serializeTextNode(n2, options) {
873
- const { needsMask, maskTextFn, rootId, cssCaptured } = options;
783
+ var _a2;
784
+ const { needsMask, maskTextFn, rootId } = options;
874
785
  const parent = index$1.parentNode(n2);
875
786
  const parentTagName = parent && parent.tagName;
876
- let textContent2 = "";
787
+ let text = index$1.textContent(n2);
877
788
  const isStyle = parentTagName === "STYLE" ? true : void 0;
878
789
  const isScript = parentTagName === "SCRIPT" ? true : void 0;
879
- if (isScript) {
880
- textContent2 = "SCRIPT_PLACEHOLDER";
881
- } else if (!cssCaptured) {
882
- textContent2 = index$1.textContent(n2);
883
- if (isStyle && textContent2) {
884
- textContent2 = absolutifyURLs(textContent2, getHref(options.doc));
790
+ if (isStyle && text) {
791
+ try {
792
+ if (n2.nextSibling || n2.previousSibling) {
793
+ } else if ((_a2 = parent.sheet) == null ? void 0 : _a2.cssRules) {
794
+ text = stringifyStylesheet(parent.sheet);
795
+ }
796
+ } catch (err) {
797
+ console.warn(
798
+ `Cannot get CSS styles from text's parentNode. Error: ${err}`,
799
+ n2
800
+ );
885
801
  }
802
+ text = absolutifyURLs(text, getHref(options.doc));
886
803
  }
887
- if (!isStyle && !isScript && textContent2 && needsMask) {
888
- textContent2 = maskTextFn ? maskTextFn(textContent2, index$1.parentElement(n2)) : textContent2.replace(/[\S]/g, "*");
804
+ if (isScript) {
805
+ text = "SCRIPT_PLACEHOLDER";
806
+ }
807
+ if (!isStyle && !isScript && text && needsMask) {
808
+ text = maskTextFn ? maskTextFn(text, index$1.parentElement(n2)) : text.replace(/[\S]/g, "*");
889
809
  }
890
810
  return {
891
- type: NodeType$3.Text,
892
- textContent: textContent2 || "",
811
+ type: NodeType$1$1.Text,
812
+ textContent: text || "",
813
+ isStyle,
893
814
  rootId
894
815
  };
895
816
  }
@@ -950,14 +871,12 @@ function serializeElementNode(n2, options) {
950
871
  }
951
872
  }
952
873
  }
953
- if (tagName === "style" && n2.sheet) {
954
- let cssText = stringifyStylesheet(
874
+ if (tagName === "style" && n2.sheet && // TODO: Currently we only try to get dynamic stylesheet when it is an empty style element
875
+ !(n2.innerText || index$1.textContent(n2) || "").trim().length) {
876
+ const cssText = stringifyStylesheet(
955
877
  n2.sheet
956
878
  );
957
879
  if (cssText) {
958
- if (n2.childNodes.length > 1) {
959
- cssText = markCssSplits(cssText, n2);
960
- }
961
880
  attributes._cssText = cssText;
962
881
  }
963
882
  }
@@ -1092,7 +1011,7 @@ function serializeElementNode(n2, options) {
1092
1011
  } catch (e2) {
1093
1012
  }
1094
1013
  return {
1095
- type: NodeType$3.Element,
1014
+ type: NodeType$1$1.Element,
1096
1015
  tagName,
1097
1016
  attributes,
1098
1017
  childNodes: [],
@@ -1110,9 +1029,9 @@ function lowerIfExists(maybeAttr) {
1110
1029
  }
1111
1030
  }
1112
1031
  function slimDOMExcluded(sn, slimDOMOptions) {
1113
- if (slimDOMOptions.comment && sn.type === NodeType$3.Comment) {
1032
+ if (slimDOMOptions.comment && sn.type === NodeType$1$1.Comment) {
1114
1033
  return true;
1115
- } else if (sn.type === NodeType$3.Element) {
1034
+ } else if (sn.type === NodeType$1$1.Element) {
1116
1035
  if (slimDOMOptions.script && // script tag
1117
1036
  (sn.tagName === "script" || // (module)preload link
1118
1037
  sn.tagName === "link" && (sn.attributes.rel === "preload" && sn.attributes.as === "script" || sn.attributes.rel === "modulepreload") || // prefetch link
@@ -1164,8 +1083,7 @@ function serializeNodeWithId(n2, options) {
1164
1083
  onStylesheetLoad,
1165
1084
  stylesheetLoadTimeout = 5e3,
1166
1085
  keepIframeSrcFn = () => false,
1167
- newlyAddedElement = false,
1168
- cssCaptured = false
1086
+ newlyAddedElement = false
1169
1087
  } = options;
1170
1088
  let { needsMask } = options;
1171
1089
  let { preserveWhiteSpace = true } = options;
@@ -1192,8 +1110,7 @@ function serializeNodeWithId(n2, options) {
1192
1110
  inlineImages,
1193
1111
  recordCanvas,
1194
1112
  keepIframeSrcFn,
1195
- newlyAddedElement,
1196
- cssCaptured
1113
+ newlyAddedElement
1197
1114
  });
1198
1115
  if (!_serializedNode) {
1199
1116
  console.warn(n2, "not serialized");
@@ -1202,7 +1119,7 @@ function serializeNodeWithId(n2, options) {
1202
1119
  let id;
1203
1120
  if (mirror2.hasNode(n2)) {
1204
1121
  id = mirror2.getId(n2);
1205
- } else if (slimDOMExcluded(_serializedNode, slimDOMOptions) || !preserveWhiteSpace && _serializedNode.type === NodeType$3.Text && !_serializedNode.textContent.replace(/^\s+|\s+$/gm, "").length) {
1122
+ } else if (slimDOMExcluded(_serializedNode, slimDOMOptions) || !preserveWhiteSpace && _serializedNode.type === NodeType$1$1.Text && !_serializedNode.isStyle && !_serializedNode.textContent.replace(/^\s+|\s+$/gm, "").length) {
1206
1123
  id = IGNORED_NODE;
1207
1124
  } else {
1208
1125
  id = genId();
@@ -1216,15 +1133,15 @@ function serializeNodeWithId(n2, options) {
1216
1133
  onSerialize(n2);
1217
1134
  }
1218
1135
  let recordChild = !skipChild;
1219
- if (serializedNode.type === NodeType$3.Element) {
1136
+ if (serializedNode.type === NodeType$1$1.Element) {
1220
1137
  recordChild = recordChild && !serializedNode.needBlock;
1221
1138
  delete serializedNode.needBlock;
1222
1139
  const shadowRootEl = index$1.shadowRoot(n2);
1223
1140
  if (shadowRootEl && isNativeShadowDom(shadowRootEl))
1224
1141
  serializedNode.isShadowHost = true;
1225
1142
  }
1226
- if ((serializedNode.type === NodeType$3.Document || serializedNode.type === NodeType$3.Element) && recordChild) {
1227
- if (slimDOMOptions.headWhitespace && serializedNode.type === NodeType$3.Element && serializedNode.tagName === "head") {
1143
+ if ((serializedNode.type === NodeType$1$1.Document || serializedNode.type === NodeType$1$1.Element) && recordChild) {
1144
+ if (slimDOMOptions.headWhitespace && serializedNode.type === NodeType$1$1.Element && serializedNode.tagName === "head") {
1228
1145
  preserveWhiteSpace = false;
1229
1146
  }
1230
1147
  const bypassOptions = {
@@ -1250,14 +1167,10 @@ function serializeNodeWithId(n2, options) {
1250
1167
  iframeLoadTimeout,
1251
1168
  onStylesheetLoad,
1252
1169
  stylesheetLoadTimeout,
1253
- keepIframeSrcFn,
1254
- cssCaptured: false
1170
+ keepIframeSrcFn
1255
1171
  };
1256
- if (serializedNode.type === NodeType$3.Element && serializedNode.tagName === "textarea" && serializedNode.attributes.value !== void 0) ;
1172
+ if (serializedNode.type === NodeType$1$1.Element && serializedNode.tagName === "textarea" && serializedNode.attributes.value !== void 0) ;
1257
1173
  else {
1258
- if (serializedNode.type === NodeType$3.Element && serializedNode.attributes._cssText !== void 0 && typeof serializedNode.attributes._cssText === "string") {
1259
- bypassOptions.cssCaptured = true;
1260
- }
1261
1174
  for (const childN of Array.from(index$1.childNodes(n2))) {
1262
1175
  const serializedChildNode = serializeNodeWithId(childN, bypassOptions);
1263
1176
  if (serializedChildNode) {
@@ -1280,7 +1193,7 @@ function serializeNodeWithId(n2, options) {
1280
1193
  if (parent && isShadowRoot(parent) && isNativeShadowDom(parent)) {
1281
1194
  serializedNode.isShadow = true;
1282
1195
  }
1283
- if (serializedNode.type === NodeType$3.Element && serializedNode.tagName === "iframe") {
1196
+ if (serializedNode.type === NodeType$1$1.Element && serializedNode.tagName === "iframe") {
1284
1197
  onceIframeLoaded(
1285
1198
  n2,
1286
1199
  () => {
@@ -1322,7 +1235,7 @@ function serializeNodeWithId(n2, options) {
1322
1235
  iframeLoadTimeout
1323
1236
  );
1324
1237
  }
1325
- if (serializedNode.type === NodeType$3.Element && serializedNode.tagName === "link" && typeof serializedNode.attributes.rel === "string" && (serializedNode.attributes.rel === "stylesheet" || serializedNode.attributes.rel === "preload" && typeof serializedNode.attributes.href === "string" && extractFileExtension(serializedNode.attributes.href) === "css")) {
1238
+ if (serializedNode.type === NodeType$1$1.Element && serializedNode.tagName === "link" && typeof serializedNode.attributes.rel === "string" && (serializedNode.attributes.rel === "stylesheet" || serializedNode.attributes.rel === "preload" && typeof serializedNode.attributes.href === "string" && extractFileExtension(serializedNode.attributes.href) === "css")) {
1326
1239
  onceStylesheetLoaded(
1327
1240
  n2,
1328
1241
  () => {
@@ -9964,18 +9877,9 @@ class MutationBuffer {
9964
9877
  };
9965
9878
  const pushAdd = (n2) => {
9966
9879
  const parent = index.parentNode(n2);
9967
- if (!parent || !inDom(n2)) {
9880
+ if (!parent || !inDom(n2) || parent.tagName === "TEXTAREA") {
9968
9881
  return;
9969
9882
  }
9970
- let cssCaptured = false;
9971
- if (n2.nodeType === Node.TEXT_NODE) {
9972
- const parentTag = parent.tagName;
9973
- if (parentTag === "TEXTAREA") {
9974
- return;
9975
- } else if (parentTag === "STYLE" && this.addedSet.has(parent)) {
9976
- cssCaptured = true;
9977
- }
9978
- }
9979
9883
  const parentId = isShadowRoot(parent) ? this.mirror.getId(getShadowHost(n2)) : this.mirror.getId(parent);
9980
9884
  const nextId = getNextId(n2);
9981
9885
  if (parentId === -1 || nextId === -1) {
@@ -10017,8 +9921,7 @@ class MutationBuffer {
10017
9921
  },
10018
9922
  onStylesheetLoad: (link, childSn) => {
10019
9923
  this.stylesheetManager.attachLinkElement(link, childSn);
10020
- },
10021
- cssCaptured
9924
+ }
10022
9925
  });
10023
9926
  if (sn) {
10024
9927
  adds.push({