Skip to content

Commit 06e3870

Browse files
committed
added header and body to test harness
1 parent 38bc719 commit 06e3870

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

test/test_harness.js

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
onload = function() {
2+
3+
var harness = new pdf_test_harness();
4+
5+
var body = document.getElementsByTagName('body')[0];
6+
body.style.display = 'flex';
7+
8+
var div = document.createElement('div');
9+
div.setAttribute('style', 'position:fixed;height:20px;width:100%;background:lightblue');
10+
body.appendChild(div);
11+
harness.header = div;
12+
13+
var div2 = document.createElement('div');
14+
div2.setAttribute('style', 'position:fixed;display:flex;top:20px; bottom:0;width:100%');
15+
body.appendChild(div2);
16+
harness.body = div2;
17+
18+
var btn1 = document.createElement('input');
19+
btn1.setAttribute('type', 'radio');
20+
btn1.setAttribute('name', 'view');
21+
div.appendChild(btn1);
22+
btn1.checked = true;
23+
24+
var lbl1 = document.createElement('label');
25+
lbl1.setAttribute('for', 'btn1');
26+
lbl1.innerHTML = 'PDF'
27+
div.appendChild(lbl1);
28+
29+
var btn2 = document.createElement('input');
30+
btn2.setAttribute('type', 'radio');
31+
btn2.setAttribute('name', 'view');
32+
div.appendChild(btn2);
33+
34+
var lbl2 = document.createElement('label');
35+
lbl2.setAttribute('for', 'btn2');
36+
lbl2.innerHTML = 'Source'
37+
div.appendChild(lbl2);
38+
39+
var btn3 = document.createElement('input');
40+
btn3.setAttribute('type', 'radio');
41+
btn3.setAttribute('name', 'view');
42+
div.appendChild(btn3);
43+
44+
var lbl3 = document.createElement('label');
45+
lbl3.setAttribute('for', 'btn3');
46+
lbl3.innerHTML = 'Both'
47+
div.appendChild(lbl3);
48+
49+
harness.source = document.createElement('pre');
50+
harness.source.setAttribute('style', 'margin-top:0;width:100%;height:100%;position:absolute;top:0px;bottom:0px;overflow:auto');
51+
div2.appendChild(harness.source);
52+
53+
harness.iframe = document.createElement('iframe');
54+
harness.iframe.setAttribute('style', 'width:100%;height:100%;position:absolute;overflow:auto;top:0px;bottom:0px');
55+
div2.appendChild(harness.iframe);
56+
57+
if (pdf_test_harness.onload) {
58+
harness.pdf = pdf_test_harness.onload(harness);
59+
if (harness.message){
60+
var popup = document.createElement('div');
61+
popup.setAttribute('style', 'position:fixed;margin:auto;top:50px;background-color:beige;padding:1em;border:1px solid black');
62+
popup.innerHTML = harness.message;
63+
body.appendChild(popup);
64+
popup.onclick = function(){
65+
popup.parentNode.removeChild(popup);
66+
}
67+
68+
}
69+
}
70+
71+
harness.render('pdf');
72+
73+
btn1.onclick = function() {
74+
harness.render('pdf');
75+
}
76+
btn2.onclick = function() {
77+
harness.render('source');
78+
}
79+
btn3.onclick = function() {
80+
harness.render('both');
81+
}
82+
}
83+
84+
pdf_test_harness = function(pdf) {
85+
this.pdf = pdf;
86+
this.onload = undefined;
87+
this.iframe = undefined;
88+
89+
this.entityMap = {
90+
"&" : "&",
91+
"<" : "&lt;",
92+
">" : "&gt;",
93+
'"' : '&quot;',
94+
"'" : '&#39;',
95+
"/" : '&#x2F;'
96+
};
97+
98+
this.escapeHtml = function(string) {
99+
return String(string).replace(/[&<>"'\/]/g, function(s) {
100+
return this.entityMap[s];
101+
}.bind(this));
102+
};
103+
104+
this.getParameterByName = function(name) {
105+
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
106+
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(location.search);
107+
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
108+
};
109+
110+
this.setPdf = function(pdf) {
111+
this.pdf = pdf;
112+
this.rendered = undefined;
113+
this.render(this.view);
114+
};
115+
116+
// generate the pdf, the source code, or both
117+
this.render = function(view) {
118+
this.view = view;
119+
//Current code only lets us render one time.
120+
if (!this.rendered) {
121+
this.rendered = this.pdf.output('datauristring');
122+
this.iframe.src = this.rendered;
123+
var raw = this.pdf.output();
124+
raw = this.escapeHtml(raw);
125+
this.source.innerHTML = raw;
126+
}
127+
if ('pdf' === view) {
128+
this.source.style.display = 'none';
129+
this.iframe.style.display = 'block';
130+
this.iframe.style.width = '100%';
131+
} else if ('source' === view) {
132+
this.iframe.style.display = 'none';
133+
this.source.style.display = 'block';
134+
this.source.style.width = '100%';
135+
}
136+
137+
if ('both' === view) {
138+
raw = this.escapeHtml(raw);
139+
this.iframe.style.width = '50%';
140+
this.iframe.style.position = 'relative';
141+
this.iframe.style.display = 'inline-block';
142+
this.source.style.width = '50%';
143+
this.source.style.position = 'relative';
144+
this.source.style.display = 'inline-block';
145+
}
146+
}
147+
}

0 commit comments

Comments
 (0)