test_harness.js 4.1 KB

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