Skip to content

Commit 6b7bdad

Browse files
committed
Add text-source vdiff and demo
1 parent f6dc70f commit 6b7bdad

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

demo/blank.html

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ <h2>Blank page: default</h2>
2121
></test-harness>
2222
</template>
2323
</d2l-demo-snippet>
24-
</d2l-demo-page>
24+
25+
<h2>Blank page: text source</h2>
26+
<d2l-demo-snippet>
27+
<template>
28+
<test-harness
29+
controls
30+
file="blank"
31+
text-source
32+
></test-harness>
33+
</template>
34+
</d2l-demo-snippet>
35+
</d2l-demo-page>
2536
</body>
2637
</html>

test/util/test-harness.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@ import { css, html, LitElement, nothing } from 'lit';
22
import { LoadingCompleteMixin } from '@brightspace-ui/core/mixins/loading-complete/loading-complete-mixin.js';
33

44
const IFRAME_SCRIPTS_URL = new URL('./iframe-scripts.js', import.meta.url).href;
5+
const TEXT_SOURCE = `
6+
<span id="target">Safe</span> text
7+
<img src=x onerror="document.querySelector('#target').innerHTML = 'Onerror'">
8+
<script>document.querySelector('#target').innerHTML = 'Script'</script>
9+
`;
510
const stub = (object, method, fake) => {
611
const original = object[method];
712
object[method] = fake;
813
return () => object[method] = original;
914
};
1015

1116
const commands = {
12-
default: (window, element, settings) => window.html2pdf().set(settings).from(element).outputPdf('arraybuffer'),
13-
legacy: async (window, element, settings) => {
17+
default: (window, src, settings) => window.html2pdf().set(settings).from(src).outputPdf('arraybuffer'),
18+
legacy: async (window, src, settings) => {
1419
const restore = stub(window.html2pdf.Worker.prototype, 'save', function () { return this.then(function save() { }); });
15-
const arrayBuffer = await window.html2pdf(element, settings).outputPdf('arraybuffer');
20+
const arrayBuffer = await window.html2pdf(src, settings).outputPdf('arraybuffer');
1621
restore();
1722
return arrayBuffer;
1823
},
@@ -41,6 +46,7 @@ class TestHarness extends LoadingCompleteMixin(LitElement) {
4146
selector: { type: String },
4247
settings: { type: String },
4348
show: { type: String, reflect: true },
49+
textSource: { type: Boolean, attribute: 'text-source' },
4450
_arrayBuffer: { state: true },
4551
};
4652

@@ -140,10 +146,10 @@ class TestHarness extends LoadingCompleteMixin(LitElement) {
140146
}
141147

142148
async _handleScriptLoad() {
143-
const element = this._pdfIframeWindow.document.querySelector(this.selector);
149+
const src = this.textSource ? TEXT_SOURCE : this._pdfIframeWindow.document.querySelector(this.selector);
144150

145151
const command = commands[this.command || 'default'];
146-
const arrayBuffer = await command(this._pdfIframeWindow, element, settings[this.settings || 'default']);
152+
const arrayBuffer = await command(this._pdfIframeWindow, src, settings[this.settings || 'default']);
147153
await this._pdfIframeWindow.renderPdf(arrayBuffer);
148154

149155
this._resizeIframe(this._pdfIframe, true);

test/vdiff/html2pdf.vdiff.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const conditions = {
1515
pagebreakCss: { settings: 'pagebreakCss' },
1616
pagebreakAvoidAll: { settings: 'pagebreakAvoidAll' },
1717
pagebreakSpecify: { settings: 'pagebreakSpecify' },
18+
textSource: { textSource: true },
1819
};
1920

2021
const fileConditions = {
@@ -38,6 +39,7 @@ describe('html2pdf', () => {
3839
selector=${ifDefined(condition.selector)}
3940
settings=${ifDefined(condition.settings)}
4041
show="pdf"
42+
text-source=${ifDefined(condition.textSource)}
4143
></test-harness>
4244
`, { viewport });
4345

0 commit comments

Comments
 (0)