xhtml.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 _namespaces = require("./namespaces.js");
  28. var _xfa_object = require("./xfa_object.js");
  29. const XHTML_NS_ID = _namespaces.NamespaceIds.xhtml.id;
  30. 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-decoration", "text-indent", "vertical-align", "widows", "kerning-mode", "xfa-font-horizontal-scale", "xfa-font-vertical-scale", "xfa-tab-stops"]);
  31. function checkStyle(style) {
  32. if (!style) {
  33. return "";
  34. }
  35. 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(";");
  36. }
  37. class A extends _xfa_object.XmlObject {
  38. constructor(attributes) {
  39. super(XHTML_NS_ID, "a");
  40. this.href = attributes.href || "";
  41. this.style = checkStyle(attributes.style);
  42. }
  43. }
  44. class B extends _xfa_object.XmlObject {
  45. constructor(attributes) {
  46. super(XHTML_NS_ID, "b");
  47. this.style = checkStyle(attributes.style);
  48. }
  49. }
  50. class Body extends _xfa_object.XmlObject {
  51. constructor(attributes) {
  52. super(XHTML_NS_ID, "body");
  53. this.style = checkStyle(attributes.style);
  54. }
  55. }
  56. class Br extends _xfa_object.XmlObject {
  57. constructor(attributes) {
  58. super(XHTML_NS_ID, "br");
  59. this.style = checkStyle(attributes.style);
  60. }
  61. [_xfa_object.$text]() {
  62. return "\n";
  63. }
  64. }
  65. class Html extends _xfa_object.XmlObject {
  66. constructor(attributes) {
  67. super(XHTML_NS_ID, "html");
  68. this.style = checkStyle(attributes.style);
  69. }
  70. }
  71. class I extends _xfa_object.XmlObject {
  72. constructor(attributes) {
  73. super(XHTML_NS_ID, "i");
  74. this.style = checkStyle(attributes.style);
  75. }
  76. }
  77. class Li extends _xfa_object.XmlObject {
  78. constructor(attributes) {
  79. super(XHTML_NS_ID, "li");
  80. this.style = checkStyle(attributes.style);
  81. }
  82. }
  83. class Ol extends _xfa_object.XmlObject {
  84. constructor(attributes) {
  85. super(XHTML_NS_ID, "ol");
  86. this.style = checkStyle(attributes.style);
  87. }
  88. }
  89. class P extends _xfa_object.XmlObject {
  90. constructor(attributes) {
  91. super(XHTML_NS_ID, "p");
  92. this.style = checkStyle(attributes.style);
  93. }
  94. }
  95. class Span extends _xfa_object.XmlObject {
  96. constructor(attributes) {
  97. super(XHTML_NS_ID, "span");
  98. this.style = checkStyle(attributes.style);
  99. }
  100. }
  101. class Sub extends _xfa_object.XmlObject {
  102. constructor(attributes) {
  103. super(XHTML_NS_ID, "sub");
  104. this.style = checkStyle(attributes.style);
  105. }
  106. }
  107. class Sup extends _xfa_object.XmlObject {
  108. constructor(attributes) {
  109. super(XHTML_NS_ID, "sup");
  110. this.style = checkStyle(attributes.style);
  111. }
  112. }
  113. class Ul extends _xfa_object.XmlObject {
  114. constructor(attributes) {
  115. super(XHTML_NS_ID, "ul");
  116. this.style = checkStyle(attributes.style);
  117. }
  118. }
  119. class XhtmlNamespace {
  120. static [_namespaces.$buildXFAObject](name, attributes) {
  121. if (XhtmlNamespace.hasOwnProperty(name)) {
  122. return XhtmlNamespace[name](attributes);
  123. }
  124. return undefined;
  125. }
  126. static a(attributes) {
  127. return new A(attributes);
  128. }
  129. static b(attributes) {
  130. return new B(attributes);
  131. }
  132. static body(attributes) {
  133. return new Body(attributes);
  134. }
  135. static br(attributes) {
  136. return new Br(attributes);
  137. }
  138. static html(attributes) {
  139. return new Html(attributes);
  140. }
  141. static i(attributes) {
  142. return new I(attributes);
  143. }
  144. static li(attributes) {
  145. return new Li(attributes);
  146. }
  147. static ol(attributes) {
  148. return new Ol(attributes);
  149. }
  150. static p(attributes) {
  151. return new P(attributes);
  152. }
  153. static span(attributes) {
  154. return new Span(attributes);
  155. }
  156. static sub(attributes) {
  157. return new Sub(attributes);
  158. }
  159. static sup(attributes) {
  160. return new Sup(attributes);
  161. }
  162. static ul(attributes) {
  163. return new Ul(attributes);
  164. }
  165. }
  166. exports.XhtmlNamespace = XhtmlNamespace;