jsPDF icon indicating copy to clipboard operation
jsPDF copied to clipboard

html: remove overlay on html2canvas errors; add cleanup test; optional colorSpaceFallback='srgb' for inline color(display-p3 …)

Open chethanac15 opened this issue 1 year ago • 0 comments

Summary

  • Ensures the HTML plugin always removes its overlay when html2canvas throws, so the page doesn’t get stuck behind a non-interactive overlay.
  • Adds a test that verifies overlay cleanup on error.
  • Introduces an optional, opt-in fallback to convert inline CSS Color 4 values like color(display-p3 …) to approximate sRGB rgb/rgba before rendering.

What changed

  • src/modules/html.js

    • Worker.prototype.toCanvas() and Worker.prototype.toContext2d()
      • On success and in .catch(...), safely remove .html2pdf__overlay if present and null it.
    • New helpers
      • replaceDisplayP3InStyle(styleText): rewrites color(display-p3 …) → rgb/rgba (lossy).
      • applyColorFallbackInlineStyles(root): walks the cloned subtree and updates inline style attributes.
    • toContainer()
      • If options.colorSpaceFallback === 'srgb', apply the inline-style fallback to the cloned source before rendering.
  • test/specs/html.spec.js

    • New test: “removes overlay when html2canvas throws an error.”
      • Stubs window.html2canvas to reject, asserts error propagation and overlay removal.
  • types/index.d.ts

    • HTMLOptions: adds colorSpaceFallback?: 'srgb' | 'none'.
  • README.md

    • “HTML rendering notes” section with usage and limitations for colorSpaceFallback.

Usage (optional fallback)

import { jsPDF } from 'jspdf';

const doc = new jsPDF();
await doc.html(elementOrHtmlString, {
  // Converts inline style attributes with color(display-p3 ...) to rgb()/rgba() before rendering
  colorSpaceFallback: 'srgb'
});

Notes and limitations

  • The fallback is optional and off by default. It only transforms inline style attributes of the provided subtree.
  • External stylesheets and general computed styles are not changed.
  • Conversion to sRGB is approximate and lossy by design.
  • Full CSS Color 4 Display‑P3 support depends on upstream html2canvas (e.g., niklasvh/html2canvas#2700).

Testing

  • Full test suite passes locally (pre-existing pending specs unchanged).
  • New test covers overlay cleanup on html2canvas rejection.

Housekeeping

  • Excludes dist/ artifacts from the PR (reset to upstream).

Related

  • Partially addresses parallax/jsPDF#3856
    • Fully fixes the “overlay left after crash” aspect.
    • Provides an optional, inline-only mitigation for color(display-p3 …) until upstream support lands.

Checklist

  • [x] Overlay is removed on success and error paths in html() rendering
  • [x] Added test for overlay cleanup on error
  • [x] Optional inline color fallback behind colorSpaceFallback: 'srgb'
  • [x] Types updated (HTMLOptions)
  • [x] Docs updated (README)
  • [x] No dist/ artifacts included in the PR

chethanac15 avatar Oct 01 '25 10:10 chethanac15