2
0

xhtml.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 $richText = Symbol();
  33. 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"]);
  34. 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, original) => {
  35. value = original.fontSize = (0, _utils.getMeasurement)(value);
  36. return (0, _html_utils.measureToString)(0.99 * value);
  37. }], ["letter-spacing", value => (0, _html_utils.measureToString)((0, _utils.getMeasurement)(value))], ["line-height", value => (0, _html_utils.measureToString)((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 => value], ["vertical-align", value => (0, _html_utils.measureToString)((0, _utils.getMeasurement)(value))]]);
  38. const spacesRegExp = /\s+/g;
  39. const crlfRegExp = /[\r\n]+/g;
  40. const crlfForRichTextRegExp = /\r\n?/g;
  41. function mapStyle(styleStr, node, richText) {
  42. const style = Object.create(null);
  43. if (!styleStr) {
  44. return style;
  45. }
  46. const original = Object.create(null);
  47. for (const [key, value] of styleStr.split(";").map(s => s.split(":", 2))) {
  48. const mapping = StyleMapping.get(key);
  49. if (mapping === "") {
  50. continue;
  51. }
  52. let newValue = value;
  53. if (mapping) {
  54. if (typeof mapping === "string") {
  55. newValue = mapping;
  56. } else {
  57. newValue = mapping(value, original);
  58. }
  59. }
  60. if (key.endsWith("scale")) {
  61. if (style.transform) {
  62. style.transform = `${style[key]} ${newValue}`;
  63. } else {
  64. style.transform = newValue;
  65. }
  66. } else {
  67. style[key.replaceAll(/-([a-zA-Z])/g, (_, x) => x.toUpperCase())] = newValue;
  68. }
  69. }
  70. if (style.fontFamily) {
  71. (0, _html_utils.setFontFamily)({
  72. typeface: style.fontFamily,
  73. weight: style.fontWeight || "normal",
  74. posture: style.fontStyle || "normal",
  75. size: original.fontSize || 0
  76. }, node, node[_xfa_object.$globalData].fontFinder, style);
  77. }
  78. if (richText && style.verticalAlign && style.verticalAlign !== "0px" && style.fontSize) {
  79. const SUB_SUPER_SCRIPT_FACTOR = 0.583;
  80. const VERTICAL_FACTOR = 0.333;
  81. const fontSize = (0, _utils.getMeasurement)(style.fontSize);
  82. style.fontSize = (0, _html_utils.measureToString)(fontSize * SUB_SUPER_SCRIPT_FACTOR);
  83. style.verticalAlign = (0, _html_utils.measureToString)(Math.sign((0, _utils.getMeasurement)(style.verticalAlign)) * fontSize * VERTICAL_FACTOR);
  84. }
  85. (0, _html_utils.fixTextIndent)(style);
  86. return style;
  87. }
  88. function checkStyle(node) {
  89. if (!node.style) {
  90. return "";
  91. }
  92. return node.style.trim().split(/\s*;\s*/).filter(s => !!s).map(s => s.split(/\s*:\s*/, 2)).filter(([key, value]) => {
  93. if (key === "font-family") {
  94. node[_xfa_object.$globalData].usedTypefaces.add(value);
  95. }
  96. return VALID_STYLES.has(key);
  97. }).map(kv => kv.join(":")).join(";");
  98. }
  99. const NoWhites = new Set(["body", "html"]);
  100. class XhtmlObject extends _xfa_object.XmlObject {
  101. constructor(attributes, name) {
  102. super(XHTML_NS_ID, name);
  103. this[$richText] = false;
  104. this.style = attributes.style || "";
  105. }
  106. [_xfa_object.$clean](builder) {
  107. super[_xfa_object.$clean](builder);
  108. this.style = checkStyle(this);
  109. }
  110. [_xfa_object.$acceptWhitespace]() {
  111. return !NoWhites.has(this[_xfa_object.$nodeName]);
  112. }
  113. [_xfa_object.$onText](str, richText = false) {
  114. if (!richText) {
  115. str = str.replace(crlfRegExp, "");
  116. if (!this.style.includes("xfa-spacerun:yes")) {
  117. str = str.replace(spacesRegExp, " ");
  118. }
  119. } else {
  120. this[$richText] = true;
  121. }
  122. if (str) {
  123. this[_xfa_object.$content] += str;
  124. }
  125. }
  126. [_xfa_object.$pushGlyphs](measure, mustPop = true) {
  127. const xfaFont = Object.create(null);
  128. const margin = {
  129. top: NaN,
  130. bottom: NaN,
  131. left: NaN,
  132. right: NaN
  133. };
  134. let lineHeight = null;
  135. for (const [key, value] of this.style.split(";").map(s => s.split(":", 2))) {
  136. switch (key) {
  137. case "font-family":
  138. xfaFont.typeface = (0, _utils.stripQuotes)(value);
  139. break;
  140. case "font-size":
  141. xfaFont.size = (0, _utils.getMeasurement)(value);
  142. break;
  143. case "font-weight":
  144. xfaFont.weight = value;
  145. break;
  146. case "font-style":
  147. xfaFont.posture = value;
  148. break;
  149. case "letter-spacing":
  150. xfaFont.letterSpacing = (0, _utils.getMeasurement)(value);
  151. break;
  152. case "margin":
  153. const values = value.split(/ \t/).map(x => (0, _utils.getMeasurement)(x));
  154. switch (values.length) {
  155. case 1:
  156. margin.top = margin.bottom = margin.left = margin.right = values[0];
  157. break;
  158. case 2:
  159. margin.top = margin.bottom = values[0];
  160. margin.left = margin.right = values[1];
  161. break;
  162. case 3:
  163. margin.top = values[0];
  164. margin.bottom = values[2];
  165. margin.left = margin.right = values[1];
  166. break;
  167. case 4:
  168. margin.top = values[0];
  169. margin.left = values[1];
  170. margin.bottom = values[2];
  171. margin.right = values[3];
  172. break;
  173. }
  174. break;
  175. case "margin-top":
  176. margin.top = (0, _utils.getMeasurement)(value);
  177. break;
  178. case "margin-bottom":
  179. margin.bottom = (0, _utils.getMeasurement)(value);
  180. break;
  181. case "margin-left":
  182. margin.left = (0, _utils.getMeasurement)(value);
  183. break;
  184. case "margin-right":
  185. margin.right = (0, _utils.getMeasurement)(value);
  186. break;
  187. case "line-height":
  188. lineHeight = (0, _utils.getMeasurement)(value);
  189. break;
  190. }
  191. }
  192. measure.pushData(xfaFont, margin, lineHeight);
  193. if (this[_xfa_object.$content]) {
  194. measure.addString(this[_xfa_object.$content]);
  195. } else {
  196. for (const child of this[_xfa_object.$getChildren]()) {
  197. if (child[_xfa_object.$nodeName] === "#text") {
  198. measure.addString(child[_xfa_object.$content]);
  199. continue;
  200. }
  201. child[_xfa_object.$pushGlyphs](measure);
  202. }
  203. }
  204. if (mustPop) {
  205. measure.popFont();
  206. }
  207. }
  208. [_xfa_object.$toHTML](availableSpace) {
  209. const children = [];
  210. this[_xfa_object.$extra] = {
  211. children
  212. };
  213. this[_xfa_object.$childrenToHTML]({});
  214. if (children.length === 0 && !this[_xfa_object.$content]) {
  215. return _utils.HTMLResult.EMPTY;
  216. }
  217. let value;
  218. if (this[$richText]) {
  219. value = this[_xfa_object.$content] ? this[_xfa_object.$content].replace(crlfForRichTextRegExp, "\n") : undefined;
  220. } else {
  221. value = this[_xfa_object.$content] || undefined;
  222. }
  223. return _utils.HTMLResult.success({
  224. name: this[_xfa_object.$nodeName],
  225. attributes: {
  226. href: this.href,
  227. style: mapStyle(this.style, this, this[$richText])
  228. },
  229. children,
  230. value
  231. });
  232. }
  233. }
  234. class A extends XhtmlObject {
  235. constructor(attributes) {
  236. super(attributes, "a");
  237. this.href = (0, _html_utils.fixURL)(attributes.href) || "";
  238. }
  239. }
  240. class B extends XhtmlObject {
  241. constructor(attributes) {
  242. super(attributes, "b");
  243. }
  244. [_xfa_object.$pushGlyphs](measure) {
  245. measure.pushFont({
  246. weight: "bold"
  247. });
  248. super[_xfa_object.$pushGlyphs](measure);
  249. measure.popFont();
  250. }
  251. }
  252. class Body extends XhtmlObject {
  253. constructor(attributes) {
  254. super(attributes, "body");
  255. }
  256. [_xfa_object.$toHTML](availableSpace) {
  257. const res = super[_xfa_object.$toHTML](availableSpace);
  258. const {
  259. html
  260. } = res;
  261. if (!html) {
  262. return _utils.HTMLResult.EMPTY;
  263. }
  264. html.name = "div";
  265. html.attributes.class = ["xfaRich"];
  266. return res;
  267. }
  268. }
  269. class Br extends XhtmlObject {
  270. constructor(attributes) {
  271. super(attributes, "br");
  272. }
  273. [_xfa_object.$text]() {
  274. return "\n";
  275. }
  276. [_xfa_object.$pushGlyphs](measure) {
  277. measure.addString("\n");
  278. }
  279. [_xfa_object.$toHTML](availableSpace) {
  280. return _utils.HTMLResult.success({
  281. name: "br"
  282. });
  283. }
  284. }
  285. class Html extends XhtmlObject {
  286. constructor(attributes) {
  287. super(attributes, "html");
  288. }
  289. [_xfa_object.$toHTML](availableSpace) {
  290. const children = [];
  291. this[_xfa_object.$extra] = {
  292. children
  293. };
  294. this[_xfa_object.$childrenToHTML]({});
  295. if (children.length === 0) {
  296. return _utils.HTMLResult.success({
  297. name: "div",
  298. attributes: {
  299. class: ["xfaRich"],
  300. style: {}
  301. },
  302. value: this[_xfa_object.$content] || ""
  303. });
  304. }
  305. if (children.length === 1) {
  306. const child = children[0];
  307. if (child.attributes && child.attributes.class.includes("xfaRich")) {
  308. return _utils.HTMLResult.success(child);
  309. }
  310. }
  311. return _utils.HTMLResult.success({
  312. name: "div",
  313. attributes: {
  314. class: ["xfaRich"],
  315. style: {}
  316. },
  317. children
  318. });
  319. }
  320. }
  321. class I extends XhtmlObject {
  322. constructor(attributes) {
  323. super(attributes, "i");
  324. }
  325. [_xfa_object.$pushGlyphs](measure) {
  326. measure.pushFont({
  327. posture: "italic"
  328. });
  329. super[_xfa_object.$pushGlyphs](measure);
  330. measure.popFont();
  331. }
  332. }
  333. class Li extends XhtmlObject {
  334. constructor(attributes) {
  335. super(attributes, "li");
  336. }
  337. }
  338. class Ol extends XhtmlObject {
  339. constructor(attributes) {
  340. super(attributes, "ol");
  341. }
  342. }
  343. class P extends XhtmlObject {
  344. constructor(attributes) {
  345. super(attributes, "p");
  346. }
  347. [_xfa_object.$pushGlyphs](measure) {
  348. super[_xfa_object.$pushGlyphs](measure, false);
  349. measure.addString("\n");
  350. measure.addPara();
  351. measure.popFont();
  352. }
  353. [_xfa_object.$text]() {
  354. const siblings = this[_xfa_object.$getParent]()[_xfa_object.$getChildren]();
  355. if (siblings[siblings.length - 1] === this) {
  356. return super[_xfa_object.$text]();
  357. }
  358. return super[_xfa_object.$text]() + "\n";
  359. }
  360. }
  361. class Span extends XhtmlObject {
  362. constructor(attributes) {
  363. super(attributes, "span");
  364. }
  365. }
  366. class Sub extends XhtmlObject {
  367. constructor(attributes) {
  368. super(attributes, "sub");
  369. }
  370. }
  371. class Sup extends XhtmlObject {
  372. constructor(attributes) {
  373. super(attributes, "sup");
  374. }
  375. }
  376. class Ul extends XhtmlObject {
  377. constructor(attributes) {
  378. super(attributes, "ul");
  379. }
  380. }
  381. class XhtmlNamespace {
  382. static [_namespaces.$buildXFAObject](name, attributes) {
  383. if (XhtmlNamespace.hasOwnProperty(name)) {
  384. return XhtmlNamespace[name](attributes);
  385. }
  386. return undefined;
  387. }
  388. static a(attributes) {
  389. return new A(attributes);
  390. }
  391. static b(attributes) {
  392. return new B(attributes);
  393. }
  394. static body(attributes) {
  395. return new Body(attributes);
  396. }
  397. static br(attributes) {
  398. return new Br(attributes);
  399. }
  400. static html(attributes) {
  401. return new Html(attributes);
  402. }
  403. static i(attributes) {
  404. return new I(attributes);
  405. }
  406. static li(attributes) {
  407. return new Li(attributes);
  408. }
  409. static ol(attributes) {
  410. return new Ol(attributes);
  411. }
  412. static p(attributes) {
  413. return new P(attributes);
  414. }
  415. static span(attributes) {
  416. return new Span(attributes);
  417. }
  418. static sub(attributes) {
  419. return new Sub(attributes);
  420. }
  421. static sup(attributes) {
  422. return new Sup(attributes);
  423. }
  424. static ul(attributes) {
  425. return new Ul(attributes);
  426. }
  427. }
  428. exports.XhtmlNamespace = XhtmlNamespace;