xfa_layer.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.XfaLayer = void 0;
  27. var _xfa_text = require("./xfa_text.js");
  28. class XfaLayer {
  29. static setupStorage(html, id, element, storage, intent) {
  30. const storedData = storage.getValue(id, {
  31. value: null
  32. });
  33. switch (element.name) {
  34. case "textarea":
  35. if (storedData.value !== null) {
  36. html.textContent = storedData.value;
  37. }
  38. if (intent === "print") {
  39. break;
  40. }
  41. html.addEventListener("input", event => {
  42. storage.setValue(id, {
  43. value: event.target.value
  44. });
  45. });
  46. break;
  47. case "input":
  48. if (element.attributes.type === "radio" || element.attributes.type === "checkbox") {
  49. if (storedData.value === element.attributes.xfaOn) {
  50. html.setAttribute("checked", true);
  51. } else if (storedData.value === element.attributes.xfaOff) {
  52. html.removeAttribute("checked");
  53. }
  54. if (intent === "print") {
  55. break;
  56. }
  57. html.addEventListener("change", event => {
  58. storage.setValue(id, {
  59. value: event.target.checked ? event.target.getAttribute("xfaOn") : event.target.getAttribute("xfaOff")
  60. });
  61. });
  62. } else {
  63. if (storedData.value !== null) {
  64. html.setAttribute("value", storedData.value);
  65. }
  66. if (intent === "print") {
  67. break;
  68. }
  69. html.addEventListener("input", event => {
  70. storage.setValue(id, {
  71. value: event.target.value
  72. });
  73. });
  74. }
  75. break;
  76. case "select":
  77. if (storedData.value !== null) {
  78. for (const option of element.children) {
  79. if (option.attributes.value === storedData.value) {
  80. option.attributes.selected = true;
  81. }
  82. }
  83. }
  84. html.addEventListener("input", event => {
  85. const options = event.target.options;
  86. const value = options.selectedIndex === -1 ? "" : options[options.selectedIndex].value;
  87. storage.setValue(id, {
  88. value
  89. });
  90. });
  91. break;
  92. }
  93. }
  94. static setAttributes({
  95. html,
  96. element,
  97. storage = null,
  98. intent,
  99. linkService
  100. }) {
  101. const {
  102. attributes
  103. } = element;
  104. const isHTMLAnchorElement = html instanceof HTMLAnchorElement;
  105. if (attributes.type === "radio") {
  106. attributes.name = `${attributes.name}-${intent}`;
  107. }
  108. for (const [key, value] of Object.entries(attributes)) {
  109. if (value === null || value === undefined || key === "dataId") {
  110. continue;
  111. }
  112. if (key !== "style") {
  113. if (key === "textContent") {
  114. html.textContent = value;
  115. } else if (key === "class") {
  116. if (value.length) {
  117. html.setAttribute(key, value.join(" "));
  118. }
  119. } else {
  120. if (isHTMLAnchorElement && (key === "href" || key === "newWindow")) {
  121. continue;
  122. }
  123. html.setAttribute(key, value);
  124. }
  125. } else {
  126. Object.assign(html.style, value);
  127. }
  128. }
  129. if (isHTMLAnchorElement) {
  130. linkService.addLinkAttributes(html, attributes.href, attributes.newWindow);
  131. }
  132. if (storage && attributes.dataId) {
  133. this.setupStorage(html, attributes.dataId, element, storage);
  134. }
  135. }
  136. static render(parameters) {
  137. const storage = parameters.annotationStorage;
  138. const linkService = parameters.linkService;
  139. const root = parameters.xfaHtml;
  140. const intent = parameters.intent || "display";
  141. const rootHtml = document.createElement(root.name);
  142. if (root.attributes) {
  143. this.setAttributes({
  144. html: rootHtml,
  145. element: root,
  146. intent,
  147. linkService
  148. });
  149. }
  150. const stack = [[root, -1, rootHtml]];
  151. const rootDiv = parameters.div;
  152. rootDiv.appendChild(rootHtml);
  153. if (parameters.viewport) {
  154. const transform = `matrix(${parameters.viewport.transform.join(",")})`;
  155. rootDiv.style.transform = transform;
  156. }
  157. if (intent !== "richText") {
  158. rootDiv.setAttribute("class", "xfaLayer xfaFont");
  159. }
  160. const textDivs = [];
  161. while (stack.length > 0) {
  162. const [parent, i, html] = stack[stack.length - 1];
  163. if (i + 1 === parent.children.length) {
  164. stack.pop();
  165. continue;
  166. }
  167. const child = parent.children[++stack[stack.length - 1][1]];
  168. if (child === null) {
  169. continue;
  170. }
  171. const {
  172. name
  173. } = child;
  174. if (name === "#text") {
  175. const node = document.createTextNode(child.value);
  176. textDivs.push(node);
  177. html.appendChild(node);
  178. continue;
  179. }
  180. let childHtml;
  181. if (child?.attributes?.xmlns) {
  182. childHtml = document.createElementNS(child.attributes.xmlns, name);
  183. } else {
  184. childHtml = document.createElement(name);
  185. }
  186. html.appendChild(childHtml);
  187. if (child.attributes) {
  188. this.setAttributes({
  189. html: childHtml,
  190. element: child,
  191. storage,
  192. intent,
  193. linkService
  194. });
  195. }
  196. if (child.children && child.children.length > 0) {
  197. stack.push([child, -1, childHtml]);
  198. } else if (child.value) {
  199. const node = document.createTextNode(child.value);
  200. if (_xfa_text.XfaText.shouldBuildText(name)) {
  201. textDivs.push(node);
  202. }
  203. childHtml.appendChild(node);
  204. }
  205. }
  206. for (const el of rootDiv.querySelectorAll(".xfaNonInteractive input, .xfaNonInteractive textarea")) {
  207. el.setAttribute("readOnly", true);
  208. }
  209. return {
  210. textDivs
  211. };
  212. }
  213. static update(parameters) {
  214. const transform = `matrix(${parameters.viewport.transform.join(",")})`;
  215. parameters.div.style.transform = transform;
  216. parameters.div.hidden = false;
  217. }
  218. }
  219. exports.XfaLayer = XfaLayer;