xfa_layer.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* Copyright 2021 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. /** @typedef {import("./display_utils").PageViewport} PageViewport */
  16. /** @typedef {import("../../web/interfaces").IPDFLinkService} IPDFLinkService */
  17. import { XfaText } from "./xfa_text.js";
  18. /**
  19. * @typedef {Object} XfaLayerParameters
  20. * @property {PageViewport} viewport
  21. * @property {HTMLDivElement} div
  22. * @property {Object} xfaHtml
  23. * @property {AnnotationStorage} [annotationStorage]
  24. * @property {IPDFLinkService} linkService
  25. * @property {string} [intent] - (default value is 'display').
  26. */
  27. class XfaLayer {
  28. static setupStorage(html, id, element, storage, intent) {
  29. const storedData = storage.getValue(id, { value: null });
  30. switch (element.name) {
  31. case "textarea":
  32. if (storedData.value !== null) {
  33. html.textContent = storedData.value;
  34. }
  35. if (intent === "print") {
  36. break;
  37. }
  38. html.addEventListener("input", event => {
  39. storage.setValue(id, { value: event.target.value });
  40. });
  41. break;
  42. case "input":
  43. if (
  44. element.attributes.type === "radio" ||
  45. element.attributes.type === "checkbox"
  46. ) {
  47. if (storedData.value === element.attributes.xfaOn) {
  48. html.setAttribute("checked", true);
  49. } else if (storedData.value === element.attributes.xfaOff) {
  50. // The checked attribute may have been set when opening the file,
  51. // unset through the UI and we're here because of printing.
  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
  60. ? event.target.getAttribute("xfaOn")
  61. : event.target.getAttribute("xfaOff"),
  62. });
  63. });
  64. } else {
  65. if (storedData.value !== null) {
  66. html.setAttribute("value", storedData.value);
  67. }
  68. if (intent === "print") {
  69. break;
  70. }
  71. html.addEventListener("input", event => {
  72. storage.setValue(id, { value: event.target.value });
  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 =
  87. options.selectedIndex === -1
  88. ? ""
  89. : options[options.selectedIndex].value;
  90. storage.setValue(id, { value });
  91. });
  92. break;
  93. }
  94. }
  95. static setAttributes({ html, element, storage = null, intent, linkService }) {
  96. const { attributes } = element;
  97. const isHTMLAnchorElement = html instanceof HTMLAnchorElement;
  98. if (attributes.type === "radio") {
  99. // Avoid to have a radio group when printing with the same as one
  100. // already displayed.
  101. attributes.name = `${attributes.name}-${intent}`;
  102. }
  103. for (const [key, value] of Object.entries(attributes)) {
  104. if (value === null || value === undefined) {
  105. continue;
  106. }
  107. switch (key) {
  108. case "class":
  109. if (value.length) {
  110. html.setAttribute(key, value.join(" "));
  111. }
  112. break;
  113. case "dataId":
  114. // We don't need to add dataId in the html object but it can
  115. // be useful to know its value when writing printing tests:
  116. // in this case, don't skip dataId to have its value.
  117. break;
  118. case "id":
  119. html.setAttribute("data-element-id", value);
  120. break;
  121. case "style":
  122. Object.assign(html.style, value);
  123. break;
  124. case "textContent":
  125. html.textContent = value;
  126. break;
  127. default:
  128. if (!isHTMLAnchorElement || (key !== "href" && key !== "newWindow")) {
  129. html.setAttribute(key, value);
  130. }
  131. }
  132. }
  133. if (isHTMLAnchorElement) {
  134. linkService.addLinkAttributes(
  135. html,
  136. attributes.href,
  137. attributes.newWindow
  138. );
  139. }
  140. // Set the value after the others to be sure to overwrite any other values.
  141. if (storage && attributes.dataId) {
  142. this.setupStorage(html, attributes.dataId, element, storage);
  143. }
  144. }
  145. /**
  146. * Render the XFA layer.
  147. *
  148. * @param {XfaLayerParameters} parameters
  149. */
  150. static render(parameters) {
  151. const storage = parameters.annotationStorage;
  152. const linkService = parameters.linkService;
  153. const root = parameters.xfaHtml;
  154. const intent = parameters.intent || "display";
  155. const rootHtml = document.createElement(root.name);
  156. if (root.attributes) {
  157. this.setAttributes({
  158. html: rootHtml,
  159. element: root,
  160. intent,
  161. linkService,
  162. });
  163. }
  164. const stack = [[root, -1, rootHtml]];
  165. const rootDiv = parameters.div;
  166. rootDiv.append(rootHtml);
  167. if (parameters.viewport) {
  168. const transform = `matrix(${parameters.viewport.transform.join(",")})`;
  169. rootDiv.style.transform = transform;
  170. }
  171. // Set defaults.
  172. if (intent !== "richText") {
  173. rootDiv.setAttribute("class", "xfaLayer xfaFont");
  174. }
  175. // Text nodes used for the text highlighter.
  176. const textDivs = [];
  177. while (stack.length > 0) {
  178. const [parent, i, html] = stack.at(-1);
  179. if (i + 1 === parent.children.length) {
  180. stack.pop();
  181. continue;
  182. }
  183. const child = parent.children[++stack.at(-1)[1]];
  184. if (child === null) {
  185. continue;
  186. }
  187. const { name } = child;
  188. if (name === "#text") {
  189. const node = document.createTextNode(child.value);
  190. textDivs.push(node);
  191. html.append(node);
  192. continue;
  193. }
  194. let childHtml;
  195. if (child?.attributes?.xmlns) {
  196. childHtml = document.createElementNS(child.attributes.xmlns, name);
  197. } else {
  198. childHtml = document.createElement(name);
  199. }
  200. html.append(childHtml);
  201. if (child.attributes) {
  202. this.setAttributes({
  203. html: childHtml,
  204. element: child,
  205. storage,
  206. intent,
  207. linkService,
  208. });
  209. }
  210. if (child.children && child.children.length > 0) {
  211. stack.push([child, -1, childHtml]);
  212. } else if (child.value) {
  213. const node = document.createTextNode(child.value);
  214. if (XfaText.shouldBuildText(name)) {
  215. textDivs.push(node);
  216. }
  217. childHtml.append(node);
  218. }
  219. }
  220. /**
  221. * TODO: re-enable that stuff once we've JS implementation.
  222. * See https://bugzilla.mozilla.org/show_bug.cgi?id=1719465.
  223. *
  224. * for (const el of rootDiv.querySelectorAll(
  225. * ".xfaDisabled input, .xfaDisabled textarea"
  226. * )) {
  227. * el.setAttribute("disabled", true);
  228. * }
  229. * for (const el of rootDiv.querySelectorAll(
  230. * ".xfaReadOnly input, .xfaReadOnly textarea"
  231. * )) {
  232. * el.setAttribute("readOnly", true);
  233. * }
  234. */
  235. for (const el of rootDiv.querySelectorAll(
  236. ".xfaNonInteractive input, .xfaNonInteractive textarea"
  237. )) {
  238. el.setAttribute("readOnly", true);
  239. }
  240. return {
  241. textDivs,
  242. };
  243. }
  244. /**
  245. * Update the XFA layer.
  246. *
  247. * @param {XfaLayerParameters} parameters
  248. */
  249. static update(parameters) {
  250. const transform = `matrix(${parameters.viewport.transform.join(",")})`;
  251. parameters.div.style.transform = transform;
  252. parameters.div.hidden = false;
  253. }
  254. }
  255. export { XfaLayer };