2
0

xfa_tohtml_spec.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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. var _factory = require("../../core/xfa/factory.js");
  24. describe("XFAFactory", function () {
  25. function searchHtmlNode(root, name, value) {
  26. if (root[name] === value) {
  27. return root;
  28. }
  29. if (!root.children) {
  30. return null;
  31. }
  32. for (const child of root.children) {
  33. const node = searchHtmlNode(child, name, value);
  34. if (node) {
  35. return node;
  36. }
  37. }
  38. return null;
  39. }
  40. describe("toHTML", function () {
  41. it("should convert some basic properties to CSS", function () {
  42. const xml = `
  43. <?xml version="1.0"?>
  44. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  45. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  46. <subform name="root" mergeMode="matchTemplate">
  47. <pageSet>
  48. <pageArea>
  49. <contentArea x="123pt" w="456pt" h="789pt"/>
  50. <medium stock="default" short="456pt" long="789pt"/>
  51. <draw y="1pt" w="11pt" h="22pt" rotate="90" x="2pt">
  52. <assist><toolTip>A tooltip !!</toolTip></assist>
  53. <font size="7pt" typeface="FooBar" baselineShift="2pt">
  54. <fill>
  55. <color value="12,23,34"/>
  56. <solid/>
  57. </fill>
  58. </font>
  59. <value/>
  60. <margin topInset="1pt" bottomInset="2pt" leftInset="3pt" rightInset="4pt"/>
  61. <para spaceAbove="1pt" spaceBelow="2pt" textIndent="3pt" marginLeft="4pt" marginRight="5pt"/>
  62. </draw>
  63. </pageArea>
  64. </pageSet>
  65. <subform name="second">
  66. <breakBefore targetType="pageArea" startNew="1"/>
  67. <subform>
  68. <draw w="1pt" h="1pt"><value><text>foo</text></value></draw>
  69. </subform>
  70. </subform>
  71. <subform name="third">
  72. <breakBefore targetType="pageArea" startNew="1"/>
  73. <subform>
  74. <draw w="1pt" h="1pt"><value><text>bar</text></value></draw>
  75. </subform>
  76. </subform>
  77. </subform>
  78. </template>
  79. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  80. <xfa:data>
  81. </xfa:data>
  82. </xfa:datasets>
  83. </xdp:xdp>
  84. `;
  85. const factory = new _factory.XFAFactory({
  86. "xdp:xdp": xml
  87. });
  88. factory.setFonts([]);
  89. expect(factory.numberPages).toEqual(2);
  90. const pages = factory.getPages();
  91. const page1 = pages.children[0];
  92. expect(page1.attributes.style).toEqual({
  93. height: "789px",
  94. width: "456px"
  95. });
  96. expect(page1.children.length).toEqual(2);
  97. const container = page1.children[1];
  98. expect(container.attributes.class).toEqual(["xfaContentarea"]);
  99. expect(container.attributes.style).toEqual({
  100. height: "789px",
  101. width: "456px",
  102. left: "123px",
  103. top: "0px"
  104. });
  105. const wrapper = page1.children[0];
  106. const draw = wrapper.children[0];
  107. expect(wrapper.attributes.class).toEqual(["xfaWrapper"]);
  108. expect(wrapper.attributes.style).toEqual({
  109. alignSelf: "start",
  110. height: "22px",
  111. left: "2px",
  112. position: "absolute",
  113. top: "1px",
  114. transform: "rotate(-90deg)",
  115. transformOrigin: "top left",
  116. width: "11px"
  117. });
  118. expect(draw.attributes.class).toEqual(["xfaDraw", "xfaFont", "xfaWrapped"]);
  119. expect(draw.attributes.title).toEqual("A tooltip !!");
  120. expect(draw.attributes.style).toEqual({
  121. color: "#0c1722",
  122. fontFamily: '"FooBar"',
  123. fontKerning: "none",
  124. letterSpacing: "0px",
  125. fontStyle: "normal",
  126. fontWeight: "normal",
  127. fontSize: "6.93px",
  128. padding: "1px 4px 2px 3px",
  129. verticalAlign: "2px"
  130. });
  131. expect(draw.attributes.style).toEqual(pages.children[1].children[0].children[0].attributes.style);
  132. });
  133. it("should have a maxLength property", function () {
  134. const xml = `
  135. <?xml version="1.0"?>
  136. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  137. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  138. <subform name="root" mergeMode="matchTemplate">
  139. <pageSet>
  140. <pageArea>
  141. <contentArea x="0pt" w="456pt" h="789pt"/>
  142. <medium stock="default" short="456pt" long="789pt"/>
  143. <field y="1pt" w="11pt" h="22pt" x="2pt">
  144. <ui>
  145. <textEdit multiLine="0"/>
  146. </ui>
  147. <value>
  148. <text maxChars="123"/>
  149. </value>
  150. </field>
  151. </pageArea>
  152. </pageSet>
  153. <subform name="first">
  154. <draw w="1pt" h="1pt"><value><text>foo</text></value></draw>
  155. </subform>
  156. </subform>
  157. </template>
  158. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  159. <xfa:data>
  160. </xfa:data>
  161. </xfa:datasets>
  162. </xdp:xdp>
  163. `;
  164. const factory = new _factory.XFAFactory({
  165. "xdp:xdp": xml
  166. });
  167. expect(factory.numberPages).toEqual(1);
  168. const pages = factory.getPages();
  169. const field = searchHtmlNode(pages, "name", "input");
  170. expect(field.attributes.maxLength).toEqual(123);
  171. });
  172. it("should have an input or textarea", function () {
  173. const xml = `
  174. <?xml version="1.0"?>
  175. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  176. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  177. <subform name="root" mergeMode="matchTemplate">
  178. <pageSet>
  179. <pageArea>
  180. <contentArea x="123pt" w="456pt" h="789pt"/>
  181. <medium stock="default" short="456pt" long="789pt"/>
  182. <field y="1pt" w="11pt" h="22pt" x="2pt">
  183. <ui>
  184. <textEdit/>
  185. </ui>
  186. </field>
  187. <field y="1pt" w="11pt" h="22pt" x="2pt">
  188. <ui>
  189. <textEdit multiLine="1"/>
  190. </ui>
  191. </field>
  192. </pageArea>
  193. </pageSet>
  194. <subform name="first">
  195. <draw w="1pt" h="1pt"><value><text>foo</text></value></draw>
  196. </subform>
  197. </subform>
  198. </template>
  199. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  200. <xfa:data>
  201. </xfa:data>
  202. </xfa:datasets>
  203. </xdp:xdp>
  204. `;
  205. const factory = new _factory.XFAFactory({
  206. "xdp:xdp": xml
  207. });
  208. expect(factory.numberPages).toEqual(1);
  209. const pages = factory.getPages();
  210. const field1 = searchHtmlNode(pages, "name", "input");
  211. expect(field1).not.toEqual(null);
  212. const field2 = searchHtmlNode(pages, "name", "textarea");
  213. expect(field2).not.toEqual(null);
  214. });
  215. });
  216. it("should have an input or textarea", function () {
  217. const xml = `
  218. <?xml version="1.0"?>
  219. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  220. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  221. <subform name="root" mergeMode="matchTemplate">
  222. <pageSet>
  223. <pageArea>
  224. <contentArea x="123pt" w="456pt" h="789pt"/>
  225. <medium stock="default" short="456pt" long="789pt"/>
  226. <field y="1pt" w="11pt" h="22pt" x="2pt">
  227. <ui>
  228. <textEdit multiLine="1"/>
  229. </ui>
  230. </field>
  231. </pageArea>
  232. </pageSet>
  233. <subform name="first">
  234. <field y="1pt" w="11pt" h="22pt" x="2pt" name="hello">
  235. <ui>
  236. <textEdit/>
  237. </ui>
  238. <value>
  239. <integer/>
  240. </value>
  241. </field>
  242. </subform>
  243. </subform>
  244. </template>
  245. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  246. <xfa:data>
  247. <toto>
  248. <first>
  249. <hello>123
  250. </hello>
  251. </first>
  252. </toto>
  253. </xfa:data>
  254. </xfa:datasets>
  255. </xdp:xdp>
  256. `;
  257. const factory = new _factory.XFAFactory({
  258. "xdp:xdp": xml
  259. });
  260. expect(factory.numberPages).toEqual(1);
  261. const pages = factory.getPages();
  262. const field1 = searchHtmlNode(pages, "name", "input");
  263. expect(field1).not.toEqual(null);
  264. expect(field1.attributes.value).toEqual("123");
  265. });
  266. });