Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion demo/blank.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ <h2>Blank page: default</h2>
></test-harness>
</template>
</d2l-demo-snippet>
</d2l-demo-page>

<h2>Blank page: text source</h2>
<d2l-demo-snippet>
<template>
<test-harness
controls
file="blank"
text-source
></test-harness>
</template>
</d2l-demo-snippet>
</d2l-demo-page>
</body>
</html>
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"homepage": "https://ekoopmans.github.io/html2pdf.js/",
"dependencies": {
"dompurify": "^3.3.1",
"html2canvas": "^1.0.0",
"jspdf": "^4.0.0"
},
Expand Down
13 changes: 5 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import DOMPurify from 'dompurify';

// Determine the type of a variable/object.
export const objType = function objType(obj) {
var type = typeof obj;
Expand All @@ -14,14 +16,9 @@ export const objType = function objType(obj) {
// Create an HTML element with optional className, innerHTML, and style.
export const createElement = function createElement(tagName, opt) {
var el = document.createElement(tagName);
if (opt.className) el.className = opt.className;
if (opt.innerHTML) {
el.innerHTML = opt.innerHTML;
var scripts = el.getElementsByTagName('script');
for (var i = scripts.length; i-- > 0; null) {
scripts[i].parentNode.removeChild(scripts[i]);
}
}
if (opt.className) el.className = opt.className;
if (opt.innerHTML) el.innerHTML = DOMPurify.sanitize(opt.innerHTML);

for (var key in opt.style) {
el.style[key] = opt.style[key];
}
Expand Down
16 changes: 11 additions & 5 deletions test/util/test-harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ import { css, html, LitElement, nothing } from 'lit';
import { LoadingCompleteMixin } from '@brightspace-ui/core/mixins/loading-complete/loading-complete-mixin.js';

const IFRAME_SCRIPTS_URL = new URL('./iframe-scripts.js', import.meta.url).href;
const TEXT_SOURCE = `
<span id="target">Safe</span> text
<img src=x onerror="document.querySelector('#target').innerHTML = 'Onerror'">
<script>document.querySelector('#target').innerHTML = 'Script'</script>
`;
const stub = (object, method, fake) => {
const original = object[method];
object[method] = fake;
return () => object[method] = original;
};

const commands = {
default: (window, element, settings) => window.html2pdf().set(settings).from(element).outputPdf('arraybuffer'),
legacy: async (window, element, settings) => {
default: (window, src, settings) => window.html2pdf().set(settings).from(src).outputPdf('arraybuffer'),
legacy: async (window, src, settings) => {
const restore = stub(window.html2pdf.Worker.prototype, 'save', function () { return this.then(function save() { }); });
const arrayBuffer = await window.html2pdf(element, settings).outputPdf('arraybuffer');
const arrayBuffer = await window.html2pdf(src, settings).outputPdf('arraybuffer');
restore();
return arrayBuffer;
},
Expand Down Expand Up @@ -41,6 +46,7 @@ class TestHarness extends LoadingCompleteMixin(LitElement) {
selector: { type: String },
settings: { type: String },
show: { type: String, reflect: true },
textSource: { type: Boolean, attribute: 'text-source' },
_arrayBuffer: { state: true },
};

Expand Down Expand Up @@ -140,10 +146,10 @@ class TestHarness extends LoadingCompleteMixin(LitElement) {
}

async _handleScriptLoad() {
const element = this._pdfIframeWindow.document.querySelector(this.selector);
const src = this.textSource ? TEXT_SOURCE : this._pdfIframeWindow.document.querySelector(this.selector);

const command = commands[this.command || 'default'];
const arrayBuffer = await command(this._pdfIframeWindow, element, settings[this.settings || 'default']);
const arrayBuffer = await command(this._pdfIframeWindow, src, settings[this.settings || 'default']);
await this._pdfIframeWindow.renderPdf(arrayBuffer);

this._resizeIframe(this._pdfIframe, true);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion test/vdiff/html2pdf.vdiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ const conditions = {
pagebreakCss: { settings: 'pagebreakCss' },
pagebreakAvoidAll: { settings: 'pagebreakAvoidAll' },
pagebreakSpecify: { settings: 'pagebreakSpecify' },
textSource: { textSource: true },
};

const fileConditions = {
'blank': ['default'],
'blank': ['default', 'textSource'],
'lorem-ipsum': ['default', 'legacy', 'margin'],
'all-tags': ['default', 'selectCanvas'],
'css-selectors': ['default', 'selectMainId'],
Expand All @@ -38,6 +39,7 @@ describe('html2pdf', () => {
selector=${ifDefined(condition.selector)}
settings=${ifDefined(condition.settings)}
show="pdf"
text-source=${ifDefined(condition.textSource)}
></test-harness>
`, { viewport });

Expand Down