@ecency/render-helper 2.5.39 → 2.5.41

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.
@@ -66,7 +66,7 @@ var YOUTUBE_REGEX = /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|shorts\
66
66
  var YOUTUBE_EMBED_REGEX = /^(https?:)?\/\/(?:www\.|m\.)?youtube\.com\/(embed|shorts)\/.*/i;
67
67
  var VIMEO_REGEX = /(https?:\/\/)?(www\.)?(?:vimeo)\.com.*(?:videos|video|channels|)\/([\d]+)/i;
68
68
  var VIMEO_EMBED_REGEX = /https:\/\/player\.vimeo\.com\/video\/([0-9]+)(?:$|[?#])/;
69
- var BITCHUTE_REGEX = /^(?:https?:\/\/)?(?:www\.)?bitchute\.com\/(?:video|embed)\/([a-z0-9]+)/i;
69
+ var BITCHUTE_REGEX = /^(?:https?:\/\/)?(?:www\.)?bitchute\.com\/(?:video|embed)\/([a-z0-9_-]+)/i;
70
70
  var D_TUBE_REGEX = /(https?:\/\/d\.tube\/#!\/v\/)(\w+)\/(\w+)/g;
71
71
  var D_TUBE_REGEX2 = /(https?:\/\/d\.tube\/v\/)(\w+)\/(\w+)/g;
72
72
  var D_TUBE_EMBED_REGEX = /^https:\/\/emb\.d\.tube\/#!\/[^/?#]+\/[^/?#]+(?:$|[?#])/i;
@@ -771,6 +771,27 @@ function buildPictureSources(rawUrl) {
771
771
  return { avif, webp };
772
772
  }
773
773
 
774
+ // src/methods/image-dimensions.ts
775
+ var MAX_IMAGE_DIMENSION_PX = 8192;
776
+ var MAX_IMAGE_DIMENSION_RATIO = 40;
777
+ function parsePixelDimension(value) {
778
+ if (value == null) return null;
779
+ const trimmed = value.trim();
780
+ if (!/^\d{1,4}$/.test(trimmed)) return null;
781
+ const n = Number(trimmed);
782
+ if (n < 1 || n > MAX_IMAGE_DIMENSION_PX) return null;
783
+ return n;
784
+ }
785
+ function authorPixelSize(width, height) {
786
+ const w = parsePixelDimension(width);
787
+ const h = parsePixelDimension(height);
788
+ if (w == null || h == null) return null;
789
+ const larger = Math.max(w, h);
790
+ const smaller = Math.min(w, h);
791
+ if (larger / smaller > MAX_IMAGE_DIMENSION_RATIO) return null;
792
+ return { width: String(w), height: String(h) };
793
+ }
794
+
774
795
  // src/methods/sanitize-html.method.ts
775
796
  var EMBED_SRC_DATA_ATTRS = /* @__PURE__ */ new Set(["data-embed-src", "data-video-href"]);
776
797
  var isSafeNavValue = (value) => {
@@ -785,9 +806,34 @@ var isProxyPSrcset = (srcset) => {
785
806
  const candidates = srcset.split(",").map((c) => c.trim().split(/\s+/)[0]).filter(Boolean);
786
807
  return candidates.length > 0 && candidates.every((url) => url.startsWith(`${base}/p/`));
787
808
  };
788
- function sanitizeHtml(html) {
809
+ var QUOTED_ATTR = /\s([^\s"'>\/=]+)="([^"]*)"/g;
810
+ function enforceImageDimensionPair(html) {
811
+ return html.replace(/<img\b(?:[^>"']|"[^"]*"|'[^']*')*>/gi, (tag) => {
812
+ QUOTED_ATTR.lastIndex = 0;
813
+ const attrs = [];
814
+ let match;
815
+ while (match = QUOTED_ATTR.exec(tag)) {
816
+ attrs.push({ name: match[1].toLowerCase(), value: decodeEntities(match[2]) });
817
+ }
818
+ const width = attrs.find((attr) => attr.name === "width")?.value;
819
+ const height = attrs.find((attr) => attr.name === "height")?.value;
820
+ const src = (attrs.find((attr) => attr.name === "src")?.value ?? "").trim();
821
+ if (src && authorPixelSize(width, height)) return tag;
822
+ QUOTED_ATTR.lastIndex = 0;
823
+ return tag.replace(
824
+ QUOTED_ATTR,
825
+ (full, name) => name.toLowerCase() === "width" || name.toLowerCase() === "height" ? "" : full
826
+ );
827
+ });
828
+ }
829
+ function sanitizeHtml(html, options) {
830
+ const preserveImageDimensions = !!options?.preserveImageDimensions;
831
+ const whiteList = preserveImageDimensions ? {
832
+ ...ALLOWED_ATTRIBUTES,
833
+ img: [...ALLOWED_ATTRIBUTES.img ?? [], "width", "height"]
834
+ } : ALLOWED_ATTRIBUTES;
789
835
  const cleaned = xss(html, {
790
- whiteList: ALLOWED_ATTRIBUTES,
836
+ whiteList,
791
837
  stripIgnoreTag: true,
792
838
  stripIgnoreTagBody: ["style"],
793
839
  css: false,
@@ -806,6 +852,9 @@ function sanitizeHtml(html) {
806
852
  if ((tag === "video" || tag === "audio") && ["src", "poster"].includes(name) && !/^https?:\/\//.test(decodedLower)) return "";
807
853
  if (tag === "audio" && name === "preload" && decodedLower !== "metadata" && decodedLower !== "none") return "";
808
854
  if (tag === "img" && ["dynsrc", "lowsrc"].includes(name)) return "";
855
+ if (tag === "img" && (name === "width" || name === "height")) {
856
+ if (!preserveImageDimensions || parsePixelDimension(decoded) == null) return "";
857
+ }
809
858
  if (tag === "span" && name === "class" && decoded.toLowerCase().trim() === "wr") return "";
810
859
  if (EMBED_SRC_DATA_ATTRS.has(name) && !isAllowedEmbedSrc(decoded)) return "";
811
860
  if (name === "data-href" && !isSafeNavValue(decoded)) return "";
@@ -815,10 +864,11 @@ function sanitizeHtml(html) {
815
864
  return void 0;
816
865
  }
817
866
  });
818
- return cleaned.replace(
867
+ const withoutBareSource = cleaned.replace(
819
868
  /<source\b(?:[^>"']|"[^"]*"|'[^']*')*>/gi,
820
869
  (t) => /\btype\s*=\s*["'](?:image\/avif|image\/webp)["']/i.test(t) ? t : ""
821
870
  );
871
+ return preserveImageDimensions ? enforceImageDimensionPair(withoutBareSource) : withoutBareSource;
822
872
  }
823
873
 
824
874
  // src/methods/img.method.ts
@@ -846,9 +896,10 @@ function wrapInPicture(el, rawUrl) {
846
896
  picture.appendChild(webp);
847
897
  picture.appendChild(el);
848
898
  }
849
- function img(el, state, forApp = true) {
899
+ function img(el, state, forApp = true, renderOptions) {
850
900
  const src = el.getAttribute("src") || "";
851
901
  const decodedSrc = decodeImageSrc(src);
902
+ const reserved = renderOptions?.preserveImageDimensions ? authorPixelSize(el.getAttribute("width"), el.getAttribute("height")) : null;
852
903
  ["onerror", "dynsrc", "lowsrc", "width", "height"].forEach((attr) => el.removeAttribute(attr));
853
904
  const isInvalid = !src || decodedSrc.startsWith("javascript") || decodedSrc.startsWith("vbscript") || decodedSrc === "x";
854
905
  if (isInvalid) {
@@ -864,6 +915,10 @@ function img(el, state, forApp = true) {
864
915
  el.removeAttribute("sizes");
865
916
  return;
866
917
  }
918
+ if (reserved) {
919
+ el.setAttribute("width", reserved.width);
920
+ el.setAttribute("height", reserved.height);
921
+ }
867
922
  el.setAttribute("itemprop", "image");
868
923
  const avatarRoute = new RegExp(`^${trimTrailingSlash(getProxyBase()).replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}/u/[^/]+/avatar(?:/[a-z]+)?$`);
869
924
  const classTokens = (el.getAttribute("class") || "").split(/\s+/);
@@ -2032,7 +2087,7 @@ function traverse(node, forApp, depth = 0, state = { firstImageFound: false }, p
2032
2087
  text(child, forApp, renderOptions);
2033
2088
  }
2034
2089
  if (child.nodeName.toLowerCase() === "img") {
2035
- img(child, state, forApp);
2090
+ img(child, state, forApp, renderOptions);
2036
2091
  }
2037
2092
  if (child.nodeName.toLowerCase() === "p") {
2038
2093
  p(child);
@@ -10211,7 +10266,7 @@ function markdownToHTML(input, forApp, parentDomain = "ecency.com", seoContext,
10211
10266
  output = serializer.serializeToString(doc);
10212
10267
  } catch (error) {
10213
10268
  try {
10214
- const preSanitized = sanitizeHtml(output);
10269
+ const preSanitized = sanitizeHtml(output, renderOptions);
10215
10270
  const dom = htmlparser2.parseDocument(preSanitized, {
10216
10271
  // lenient options - don't throw on malformed HTML
10217
10272
  lowerCaseTags: false,
@@ -10222,7 +10277,7 @@ function markdownToHTML(input, forApp, parentDomain = "ecency.com", seoContext,
10222
10277
  traverse(doc, forApp, 0, { firstImageFound: false }, parentDomain, seoContext, renderOptions);
10223
10278
  output = serializer.serializeToString(doc);
10224
10279
  } catch (fallbackError) {
10225
- output = sanitizeHtml(output || md.render(input));
10280
+ output = sanitizeHtml(output || md.render(input), renderOptions);
10226
10281
  }
10227
10282
  }
10228
10283
  if (forApp && output && entityPlaceholders.length > 0) {
@@ -10232,7 +10287,7 @@ function markdownToHTML(input, forApp, parentDomain = "ecency.com", seoContext,
10232
10287
  });
10233
10288
  }
10234
10289
  output = output.replace(/ xmlns="http:\/\/www.w3.org\/1999\/xhtml"/g, "").replace(/^<\?xml[^?]*\?>/, "").replace(/^<!DOCTYPE[^>]*>/i, "").replace(/<\/?html[^>]*>/g, "").replace(/<head[^>]*>[\s\S]*?<\/head>/g, "").replace('<body id="root">', "").replace("</body>", "").trim();
10235
- return sanitizeHtml(output);
10290
+ return sanitizeHtml(output, renderOptions);
10236
10291
  }
10237
10292
 
10238
10293
  // src/methods/simple-markdown-to-html.method.ts
@@ -10289,7 +10344,7 @@ function markdown2Html(obj, forApp = true, _webp = false, parentDomain = "ecency
10289
10344
  logIfSlow(performance.now() - t02, `body_len=${obj.length}`);
10290
10345
  return res2;
10291
10346
  }
10292
- const key = `${makeEntryCacheKey(obj)}-md-${forApp ? "app" : "site"}-${parentDomain}${seoContext ? `-seo${seoContext.authorReputation ?? ""}-${seoContext.postPayout ?? ""}` : ""}${renderOptions?.embedVideosDirectly ? "-embed" : ""}${renderOptions?.inertAuthorAndTagChips ? "-inert" : ""}${renderOptions?.externalProfileBase ? "-ext" + renderOptions.externalProfileBase : ""}`;
10347
+ const key = `${makeEntryCacheKey(obj)}-md-${forApp ? "app" : "site"}-${parentDomain}${seoContext ? `-seo${seoContext.authorReputation ?? ""}-${seoContext.postPayout ?? ""}` : ""}${renderOptions?.embedVideosDirectly ? "-embed" : ""}${renderOptions?.inertAuthorAndTagChips ? "-inert" : ""}${renderOptions?.externalProfileBase ? "-ext" + renderOptions.externalProfileBase : ""}${renderOptions?.preserveImageDimensions ? "-imgdim" : ""}`;
10293
10348
  const item = entryMemoGet(key, obj.body);
10294
10349
  if (item !== MEMO_MISS) {
10295
10350
  return item;