2
0

xhtml.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.XhtmlNamespace = void 0;
  27. var _xfa_object = require("./xfa_object.js");
  28. var _namespaces = require("./namespaces.js");
  29. var _html_utils = require("./html_utils.js");
  30. var _utils = require("./utils.js");
  31. const XHTML_NS_ID = _namespaces.NamespaceIds.xhtml.id;
  32. const VALID_STYLES = new Set(["color", "font", "font-family", "font-size", "font-stretch", "font-style", "font-weight", "margin", "margin-bottom", "margin-left", "margin-right", "margin-top", "letter-spacing", "line-height", "orphans", "page-break-after", "page-break-before", "page-break-inside", "tab-interval", "tab-stop", "text-align", "text-decoration", "text-indent", "vertical-align", "widows", "kerning-mode", "xfa-font-horizontal-scale", "xfa-font-vertical-scale", "xfa-spacerun", "xfa-tab-stops"]);
  33. const StyleMapping = new Map([["page-break-after", "breakAfter"], ["page-break-before", "breakBefore"], ["page-break-inside", "breakInside"], ["kerning-mode", value => value === "none" ? "none" : "normal"], ["xfa-font-horizontal-scale", value => `scaleX(${Math.max(0, Math.min(parseInt(value) / 100)).toFixed(2)})`], ["xfa-font-vertical-scale", value => `scaleY(${Math.max(0, Math.min(parseInt(value) / 100)).toFixed(2)})`], ["xfa-spacerun", ""], ["xfa-tab-stops", ""], ["font-size", value => (0, _html_utils.measureToString)(1 * (0, _utils.getMeasurement)(value))], ["letter-spacing", value => (0, _html_utils.measureToString)((0, _utils.getMeasurement)(value))], ["line-height", value => (0, _html_utils.measureToString)(0.99 * (0, _utils.getMeasurement)(value))], ["margin", value => (0, _html_utils.measureToString)((0, _utils.getMeasurement)(value))], ["margin-bottom", value => (0, _html_utils.measureToString)((0, _utils.getMeasurement)(value))], ["margin-left", value => (0, _html_utils.measureToString)((0, _utils.getMeasurement)(value))], ["margin-right", value => (0, _html_utils.measureToString)((0, _utils.getMeasurement)(value))], ["margin-top", value => (0, _html_utils.measureToString)((0, _utils.getMeasurement)(value))], ["text-indent", value => (0, _html_utils.measureToString)((0, _utils.getMeasurement)(value))], ["font-family", value => (0, _html_utils.getFonts)(value)]]);
  34. const spacesRegExp = /\s+/g;
  35. const crlfRegExp = /[\r\n]+/g;
  36. function mapStyle(styleStr) {
  37. const style = Object.create(null);
  38. if (!styleStr) {
  39. return style;
  40. }
  41. for (const [key, value] of styleStr.split(";").map(s => s.split(":", 2))) {
  42. const mapping = StyleMapping.get(key);
  43. if (mapping === "") {
  44. continue;
  45. }
  46. let newValue = value;
  47. if (mapping) {
  48. if (typeof mapping === "string") {
  49. newValue = mapping;
  50. } else {
  51. newValue = mapping(value);
  52. }
  53. }
  54. if (key.endsWith("scale")) {
  55. if (style.transform) {
  56. style.transform = `${style[key]} ${newValue}`;
  57. } else {
  58. style.transform = newValue;
  59. }
  60. } else {
  61. style[key.replaceAll(/-([a-zA-Z])/g, (_, x) => x.toUpperCase())] = newValue;
  62. }
  63. }
  64. (0, _html_utils.fixTextIndent)(style);
  65. return style;
  66. }
  67. function checkStyle(style) {
  68. if (!style) {
  69. return "";
  70. }
  71. return style.trim().split(/\s*;\s*/).filter(s => !!s).map(s => s.split(/\s*:\s*/, 2)).filter(([key]) => VALID_STYLES.has(key)).map(kv => kv.join(":")).join(";");
  72. }
  73. const NoWhites = new Set(["body", "html"]);
  74. class XhtmlObject extends _xfa_object.XmlObject {
  75. constructor(attributes, name) {
  76. super(XHTML_NS_ID, name);
  77. this.style = checkStyle(attributes.style);
  78. }
  79. [_xfa_object.$acceptWhitespace]() {
  80. return !NoWhites.has(this[_xfa_object.$nodeName]);
  81. }
  82. [_xfa_object.$onText](str) {
  83. str = str.replace(crlfRegExp, "");
  84. if (!this.style.includes("xfa-spacerun:yes")) {
  85. str = str.replace(spacesRegExp, " ");
  86. }
  87. if (str) {
  88. this[_xfa_object.$content] += str;
  89. }
  90. }
  91. [_xfa_object.$toHTML](availableSpace) {
  92. const children = [];
  93. this[_xfa_object.$extra] = {
  94. children
  95. };
  96. this[_xfa_object.$childrenToHTML]({});
  97. if (children.length === 0 && !this[_xfa_object.$content]) {
  98. return _utils.HTMLResult.EMPTY;
  99. }
  100. return _utils.HTMLResult.success({
  101. name: this[_xfa_object.$nodeName],
  102. attributes: {
  103. href: this.href,
  104. style: mapStyle(this.style)
  105. },
  106. children,
  107. value: this[_xfa_object.$content] || ""
  108. });
  109. }
  110. }
  111. class A extends XhtmlObject {
  112. constructor(attributes) {
  113. super(attributes, "a");
  114. this.href = attributes.href || "";
  115. }
  116. }
  117. class B extends XhtmlObject {
  118. constructor(attributes) {
  119. super(attributes, "b");
  120. }
  121. }
  122. class Body extends XhtmlObject {
  123. constructor(attributes) {
  124. super(attributes, "body");
  125. }
  126. [_xfa_object.$toHTML](availableSpace) {
  127. const res = super[_xfa_object.$toHTML](availableSpace);
  128. const {
  129. html
  130. } = res;
  131. if (!html) {
  132. return _utils.HTMLResult.EMPTY;
  133. }
  134. html.name = "div";
  135. html.attributes.class = "xfaRich";
  136. return res;
  137. }
  138. }
  139. class Br extends XhtmlObject {
  140. constructor(attributes) {
  141. super(attributes, "br");
  142. }
  143. [_xfa_object.$text]() {
  144. return "\n";
  145. }
  146. [_xfa_object.$toHTML](availableSpace) {
  147. return _utils.HTMLResult.success({
  148. name: "br"
  149. });
  150. }
  151. }
  152. class Html extends XhtmlObject {
  153. constructor(attributes) {
  154. super(attributes, "html");
  155. }
  156. [_xfa_object.$toHTML](availableSpace) {
  157. const children = [];
  158. this[_xfa_object.$extra] = {
  159. children
  160. };
  161. this[_xfa_object.$childrenToHTML]({});
  162. if (children.length === 0) {
  163. return _utils.HTMLResult.success({
  164. name: "div",
  165. attributes: {
  166. class: "xfaRich",
  167. style: {}
  168. },
  169. value: this[_xfa_object.$content] || ""
  170. });
  171. }
  172. if (children.length === 1) {
  173. const child = children[0];
  174. if (child.attributes && child.attributes.class === "xfaRich") {
  175. return _utils.HTMLResult.success(child);
  176. }
  177. }
  178. return _utils.HTMLResult.success({
  179. name: "div",
  180. attributes: {
  181. class: "xfaRich",
  182. style: {}
  183. },
  184. children
  185. });
  186. }
  187. }
  188. class I extends XhtmlObject {
  189. constructor(attributes) {
  190. super(attributes, "i");
  191. }
  192. }
  193. class Li extends XhtmlObject {
  194. constructor(attributes) {
  195. super(attributes, "li");
  196. }
  197. }
  198. class Ol extends XhtmlObject {
  199. constructor(attributes) {
  200. super(attributes, "ol");
  201. }
  202. }
  203. class P extends XhtmlObject {
  204. constructor(attributes) {
  205. super(attributes, "p");
  206. }
  207. [_xfa_object.$text]() {
  208. return super[_xfa_object.$text]() + "\n";
  209. }
  210. }
  211. class Span extends XhtmlObject {
  212. constructor(attributes) {
  213. super(attributes, "span");
  214. }
  215. }
  216. class Sub extends XhtmlObject {
  217. constructor(attributes) {
  218. super(attributes, "sub");
  219. }
  220. }
  221. class Sup extends XhtmlObject {
  222. constructor(attributes) {
  223. super(attributes, "sup");
  224. }
  225. }
  226. class Ul extends XhtmlObject {
  227. constructor(attributes) {
  228. super(attributes, "ul");
  229. }
  230. }
  231. class XhtmlNamespace {
  232. static [_namespaces.$buildXFAObject](name, attributes) {
  233. if (XhtmlNamespace.hasOwnProperty(name)) {
  234. return XhtmlNamespace[name](attributes);
  235. }
  236. return undefined;
  237. }
  238. static a(attributes) {
  239. return new A(attributes);
  240. }
  241. static b(attributes) {
  242. return new B(attributes);
  243. }
  244. static body(attributes) {
  245. return new Body(attributes);
  246. }
  247. static br(attributes) {
  248. return new Br(attributes);
  249. }
  250. static html(attributes) {
  251. return new Html(attributes);
  252. }
  253. static i(attributes) {
  254. return new I(attributes);
  255. }
  256. static li(attributes) {
  257. return new Li(attributes);
  258. }
  259. static ol(attributes) {
  260. return new Ol(attributes);
  261. }
  262. static p(attributes) {
  263. return new P(attributes);
  264. }
  265. static span(attributes) {
  266. return new Span(attributes);
  267. }
  268. static sub(attributes) {
  269. return new Sub(attributes);
  270. }
  271. static sup(attributes) {
  272. return new Sup(attributes);
  273. }
  274. static ul(attributes) {
  275. return new Ul(attributes);
  276. }
  277. }
  278. exports.XhtmlNamespace = XhtmlNamespace;