liteAdaptor.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. var __assign = (this && this.__assign) || function () {
  18. __assign = Object.assign || function(t) {
  19. for (var s, i = 1, n = arguments.length; i < n; i++) {
  20. s = arguments[i];
  21. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  22. t[p] = s[p];
  23. }
  24. return t;
  25. };
  26. return __assign.apply(this, arguments);
  27. };
  28. var __values = (this && this.__values) || function(o) {
  29. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  30. if (m) return m.call(o);
  31. if (o && typeof o.length === "number") return {
  32. next: function () {
  33. if (o && i >= o.length) o = void 0;
  34. return { value: o && o[i++], done: !o };
  35. }
  36. };
  37. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  38. };
  39. var __read = (this && this.__read) || function (o, n) {
  40. var m = typeof Symbol === "function" && o[Symbol.iterator];
  41. if (!m) return o;
  42. var i = m.call(o), r, ar = [], e;
  43. try {
  44. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  45. }
  46. catch (error) { e = { error: error }; }
  47. finally {
  48. try {
  49. if (r && !r.done && (m = i["return"])) m.call(i);
  50. }
  51. finally { if (e) throw e.error; }
  52. }
  53. return ar;
  54. };
  55. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  56. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  57. if (ar || !(i in from)) {
  58. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  59. ar[i] = from[i];
  60. }
  61. }
  62. return to.concat(ar || Array.prototype.slice.call(from));
  63. };
  64. Object.defineProperty(exports, "__esModule", { value: true });
  65. exports.liteAdaptor = exports.LiteAdaptor = exports.LiteBase = void 0;
  66. var DOMAdaptor_js_1 = require("../core/DOMAdaptor.js");
  67. var NodeMixin_js_1 = require("./NodeMixin.js");
  68. var Document_js_1 = require("./lite/Document.js");
  69. var Element_js_1 = require("./lite/Element.js");
  70. var Text_js_1 = require("./lite/Text.js");
  71. var Window_js_1 = require("./lite/Window.js");
  72. var Parser_js_1 = require("./lite/Parser.js");
  73. var Styles_js_1 = require("../util/Styles.js");
  74. var LiteBase = (function (_super) {
  75. __extends(LiteBase, _super);
  76. function LiteBase() {
  77. var _this = _super.call(this) || this;
  78. _this.parser = new Parser_js_1.LiteParser();
  79. _this.window = new Window_js_1.LiteWindow();
  80. return _this;
  81. }
  82. LiteBase.prototype.parse = function (text, format) {
  83. return this.parser.parseFromString(text, format, this);
  84. };
  85. LiteBase.prototype.create = function (kind, _ns) {
  86. if (_ns === void 0) { _ns = null; }
  87. return new Element_js_1.LiteElement(kind);
  88. };
  89. LiteBase.prototype.text = function (text) {
  90. return new Text_js_1.LiteText(text);
  91. };
  92. LiteBase.prototype.comment = function (text) {
  93. return new Text_js_1.LiteComment(text);
  94. };
  95. LiteBase.prototype.createDocument = function () {
  96. return new Document_js_1.LiteDocument();
  97. };
  98. LiteBase.prototype.head = function (doc) {
  99. return doc.head;
  100. };
  101. LiteBase.prototype.body = function (doc) {
  102. return doc.body;
  103. };
  104. LiteBase.prototype.root = function (doc) {
  105. return doc.root;
  106. };
  107. LiteBase.prototype.doctype = function (doc) {
  108. return doc.type;
  109. };
  110. LiteBase.prototype.tags = function (node, name, ns) {
  111. if (ns === void 0) { ns = null; }
  112. var stack = [];
  113. var tags = [];
  114. if (ns) {
  115. return tags;
  116. }
  117. var n = node;
  118. while (n) {
  119. var kind = n.kind;
  120. if (kind !== '#text' && kind !== '#comment') {
  121. n = n;
  122. if (kind === name) {
  123. tags.push(n);
  124. }
  125. if (n.children.length) {
  126. stack = n.children.concat(stack);
  127. }
  128. }
  129. n = stack.shift();
  130. }
  131. return tags;
  132. };
  133. LiteBase.prototype.elementById = function (node, id) {
  134. var stack = [];
  135. var n = node;
  136. while (n) {
  137. if (n.kind !== '#text' && n.kind !== '#comment') {
  138. n = n;
  139. if (n.attributes['id'] === id) {
  140. return n;
  141. }
  142. if (n.children.length) {
  143. stack = n.children.concat(stack);
  144. }
  145. }
  146. n = stack.shift();
  147. }
  148. return null;
  149. };
  150. LiteBase.prototype.elementsByClass = function (node, name) {
  151. var stack = [];
  152. var tags = [];
  153. var n = node;
  154. while (n) {
  155. if (n.kind !== '#text' && n.kind !== '#comment') {
  156. n = n;
  157. var classes = (n.attributes['class'] || '').trim().split(/ +/);
  158. if (classes.includes(name)) {
  159. tags.push(n);
  160. }
  161. if (n.children.length) {
  162. stack = n.children.concat(stack);
  163. }
  164. }
  165. n = stack.shift();
  166. }
  167. return tags;
  168. };
  169. LiteBase.prototype.getElements = function (nodes, document) {
  170. var e_1, _a;
  171. var containers = [];
  172. var body = this.body(document);
  173. try {
  174. for (var nodes_1 = __values(nodes), nodes_1_1 = nodes_1.next(); !nodes_1_1.done; nodes_1_1 = nodes_1.next()) {
  175. var node = nodes_1_1.value;
  176. if (typeof (node) === 'string') {
  177. if (node.charAt(0) === '#') {
  178. var n = this.elementById(body, node.slice(1));
  179. if (n) {
  180. containers.push(n);
  181. }
  182. }
  183. else if (node.charAt(0) === '.') {
  184. containers = containers.concat(this.elementsByClass(body, node.slice(1)));
  185. }
  186. else if (node.match(/^[-a-z][-a-z0-9]*$/i)) {
  187. containers = containers.concat(this.tags(body, node));
  188. }
  189. }
  190. else if (Array.isArray(node)) {
  191. containers = containers.concat(node);
  192. }
  193. else if (node instanceof this.window.NodeList || node instanceof this.window.HTMLCollection) {
  194. containers = containers.concat(node.nodes);
  195. }
  196. else {
  197. containers.push(node);
  198. }
  199. }
  200. }
  201. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  202. finally {
  203. try {
  204. if (nodes_1_1 && !nodes_1_1.done && (_a = nodes_1.return)) _a.call(nodes_1);
  205. }
  206. finally { if (e_1) throw e_1.error; }
  207. }
  208. return containers;
  209. };
  210. LiteBase.prototype.contains = function (container, node) {
  211. while (node && node !== container) {
  212. node = this.parent(node);
  213. }
  214. return !!node;
  215. };
  216. LiteBase.prototype.parent = function (node) {
  217. return node.parent;
  218. };
  219. LiteBase.prototype.childIndex = function (node) {
  220. return (node.parent ? node.parent.children.findIndex(function (n) { return n === node; }) : -1);
  221. };
  222. LiteBase.prototype.append = function (node, child) {
  223. if (child.parent) {
  224. this.remove(child);
  225. }
  226. node.children.push(child);
  227. child.parent = node;
  228. return child;
  229. };
  230. LiteBase.prototype.insert = function (nchild, ochild) {
  231. if (nchild.parent) {
  232. this.remove(nchild);
  233. }
  234. if (ochild && ochild.parent) {
  235. var i = this.childIndex(ochild);
  236. ochild.parent.children.splice(i, 0, nchild);
  237. nchild.parent = ochild.parent;
  238. }
  239. };
  240. LiteBase.prototype.remove = function (child) {
  241. var i = this.childIndex(child);
  242. if (i >= 0) {
  243. child.parent.children.splice(i, 1);
  244. }
  245. child.parent = null;
  246. return child;
  247. };
  248. LiteBase.prototype.replace = function (nnode, onode) {
  249. var i = this.childIndex(onode);
  250. if (i >= 0) {
  251. onode.parent.children[i] = nnode;
  252. nnode.parent = onode.parent;
  253. onode.parent = null;
  254. }
  255. return onode;
  256. };
  257. LiteBase.prototype.clone = function (node) {
  258. var _this = this;
  259. var nnode = new Element_js_1.LiteElement(node.kind);
  260. nnode.attributes = __assign({}, node.attributes);
  261. nnode.children = node.children.map(function (n) {
  262. if (n.kind === '#text') {
  263. return new Text_js_1.LiteText(n.value);
  264. }
  265. else if (n.kind === '#comment') {
  266. return new Text_js_1.LiteComment(n.value);
  267. }
  268. else {
  269. var m = _this.clone(n);
  270. m.parent = nnode;
  271. return m;
  272. }
  273. });
  274. return nnode;
  275. };
  276. LiteBase.prototype.split = function (node, n) {
  277. var text = new Text_js_1.LiteText(node.value.slice(n));
  278. node.value = node.value.slice(0, n);
  279. node.parent.children.splice(this.childIndex(node) + 1, 0, text);
  280. text.parent = node.parent;
  281. return text;
  282. };
  283. LiteBase.prototype.next = function (node) {
  284. var parent = node.parent;
  285. if (!parent)
  286. return null;
  287. var i = this.childIndex(node) + 1;
  288. return (i >= 0 && i < parent.children.length ? parent.children[i] : null);
  289. };
  290. LiteBase.prototype.previous = function (node) {
  291. var parent = node.parent;
  292. if (!parent)
  293. return null;
  294. var i = this.childIndex(node) - 1;
  295. return (i >= 0 ? parent.children[i] : null);
  296. };
  297. LiteBase.prototype.firstChild = function (node) {
  298. return node.children[0];
  299. };
  300. LiteBase.prototype.lastChild = function (node) {
  301. return node.children[node.children.length - 1];
  302. };
  303. LiteBase.prototype.childNodes = function (node) {
  304. return __spreadArray([], __read(node.children), false);
  305. };
  306. LiteBase.prototype.childNode = function (node, i) {
  307. return node.children[i];
  308. };
  309. LiteBase.prototype.kind = function (node) {
  310. return node.kind;
  311. };
  312. LiteBase.prototype.value = function (node) {
  313. return (node.kind === '#text' ? node.value :
  314. node.kind === '#comment' ? node.value.replace(/^<!(--)?((?:.|\n)*)\1>$/, '$2') : '');
  315. };
  316. LiteBase.prototype.textContent = function (node) {
  317. var _this = this;
  318. return node.children.reduce(function (s, n) {
  319. return s + (n.kind === '#text' ? n.value :
  320. n.kind === '#comment' ? '' : _this.textContent(n));
  321. }, '');
  322. };
  323. LiteBase.prototype.innerHTML = function (node) {
  324. return this.parser.serializeInner(this, node);
  325. };
  326. LiteBase.prototype.outerHTML = function (node) {
  327. return this.parser.serialize(this, node);
  328. };
  329. LiteBase.prototype.serializeXML = function (node) {
  330. return this.parser.serialize(this, node, true);
  331. };
  332. LiteBase.prototype.setAttribute = function (node, name, value, ns) {
  333. if (ns === void 0) { ns = null; }
  334. if (typeof value !== 'string') {
  335. value = String(value);
  336. }
  337. if (ns) {
  338. name = ns.replace(/.*\//, '') + ':' + name.replace(/^.*:/, '');
  339. }
  340. node.attributes[name] = value;
  341. if (name === 'style') {
  342. node.styles = null;
  343. }
  344. };
  345. LiteBase.prototype.getAttribute = function (node, name) {
  346. return node.attributes[name];
  347. };
  348. LiteBase.prototype.removeAttribute = function (node, name) {
  349. delete node.attributes[name];
  350. };
  351. LiteBase.prototype.hasAttribute = function (node, name) {
  352. return node.attributes.hasOwnProperty(name);
  353. };
  354. LiteBase.prototype.allAttributes = function (node) {
  355. var e_2, _a;
  356. var attributes = node.attributes;
  357. var list = [];
  358. try {
  359. for (var _b = __values(Object.keys(attributes)), _c = _b.next(); !_c.done; _c = _b.next()) {
  360. var name_1 = _c.value;
  361. list.push({ name: name_1, value: attributes[name_1] });
  362. }
  363. }
  364. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  365. finally {
  366. try {
  367. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  368. }
  369. finally { if (e_2) throw e_2.error; }
  370. }
  371. return list;
  372. };
  373. LiteBase.prototype.addClass = function (node, name) {
  374. var classes = (node.attributes['class'] || '').split(/ /);
  375. if (!classes.find(function (n) { return n === name; })) {
  376. classes.push(name);
  377. node.attributes['class'] = classes.join(' ');
  378. }
  379. };
  380. LiteBase.prototype.removeClass = function (node, name) {
  381. var classes = (node.attributes['class'] || '').split(/ /);
  382. var i = classes.findIndex(function (n) { return n === name; });
  383. if (i >= 0) {
  384. classes.splice(i, 1);
  385. node.attributes['class'] = classes.join(' ');
  386. }
  387. };
  388. LiteBase.prototype.hasClass = function (node, name) {
  389. var classes = (node.attributes['class'] || '').split(/ /);
  390. return !!classes.find(function (n) { return n === name; });
  391. };
  392. LiteBase.prototype.setStyle = function (node, name, value) {
  393. if (!node.styles) {
  394. node.styles = new Styles_js_1.Styles(this.getAttribute(node, 'style'));
  395. }
  396. node.styles.set(name, value);
  397. node.attributes['style'] = node.styles.cssText;
  398. };
  399. LiteBase.prototype.getStyle = function (node, name) {
  400. if (!node.styles) {
  401. var style = this.getAttribute(node, 'style');
  402. if (!style) {
  403. return '';
  404. }
  405. node.styles = new Styles_js_1.Styles(style);
  406. }
  407. return node.styles.get(name);
  408. };
  409. LiteBase.prototype.allStyles = function (node) {
  410. return this.getAttribute(node, 'style');
  411. };
  412. LiteBase.prototype.insertRules = function (node, rules) {
  413. node.children = [this.text(rules.join('\n\n') + '\n\n' + this.textContent(node))];
  414. };
  415. LiteBase.prototype.fontSize = function (_node) {
  416. return 0;
  417. };
  418. LiteBase.prototype.fontFamily = function (_node) {
  419. return '';
  420. };
  421. LiteBase.prototype.nodeSize = function (_node, _em, _local) {
  422. if (_em === void 0) { _em = 1; }
  423. if (_local === void 0) { _local = null; }
  424. return [0, 0];
  425. };
  426. LiteBase.prototype.nodeBBox = function (_node) {
  427. return { left: 0, right: 0, top: 0, bottom: 0 };
  428. };
  429. return LiteBase;
  430. }(DOMAdaptor_js_1.AbstractDOMAdaptor));
  431. exports.LiteBase = LiteBase;
  432. var LiteAdaptor = (function (_super) {
  433. __extends(LiteAdaptor, _super);
  434. function LiteAdaptor() {
  435. return _super !== null && _super.apply(this, arguments) || this;
  436. }
  437. return LiteAdaptor;
  438. }((0, NodeMixin_js_1.NodeMixin)(LiteBase)));
  439. exports.LiteAdaptor = LiteAdaptor;
  440. function liteAdaptor(options) {
  441. if (options === void 0) { options = null; }
  442. return new LiteAdaptor(null, options);
  443. }
  444. exports.liteAdaptor = liteAdaptor;
  445. //# sourceMappingURL=liteAdaptor.js.map