main.js 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  1. var __extends = (this && this.__extends) || (function () {
  2. var extendStatics = function (d, b) {
  3. extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  6. return extendStatics(d, b);
  7. };
  8. return function (d, b) {
  9. if (typeof b !== "function" && b !== null)
  10. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  11. extendStatics(d, b);
  12. function __() { this.constructor = d; }
  13. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  14. };
  15. })();
  16. var __importDefault = (this && this.__importDefault) || function (mod) {
  17. return (mod && mod.__esModule) ? mod : { "default": mod };
  18. };
  19. var __assign = (this && this.__assign) || function () {
  20. __assign = Object.assign || function(t) {
  21. for (var s, i = 1, n = arguments.length; i < n; i++) {
  22. s = arguments[i];
  23. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  24. t[p] = s[p];
  25. }
  26. return t;
  27. };
  28. return __assign.apply(this, arguments);
  29. };
  30. var __spreadArray = (this && this.__spreadArray) || function (to, from) {
  31. for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
  32. to[j] = from[i];
  33. return to;
  34. };
  35. define("back", ["require", "exports"], function (require, exports) {
  36. "use strict";
  37. Object.defineProperty(exports, "__esModule", { value: true });
  38. function arr_back(arr) {
  39. return arr[arr.length - 1];
  40. }
  41. exports.default = arr_back;
  42. });
  43. define("nodes/type", ["require", "exports"], function (require, exports) {
  44. "use strict";
  45. Object.defineProperty(exports, "__esModule", { value: true });
  46. var NodeType;
  47. (function (NodeType) {
  48. NodeType[NodeType["ELEMENT_NODE"] = 1] = "ELEMENT_NODE";
  49. NodeType[NodeType["TEXT_NODE"] = 3] = "TEXT_NODE";
  50. NodeType[NodeType["COMMENT_NODE"] = 8] = "COMMENT_NODE";
  51. })(NodeType || (NodeType = {}));
  52. exports.default = NodeType;
  53. });
  54. define("nodes/text", ["require", "exports", "nodes/type", "nodes/node"], function (require, exports, type_1, node_1) {
  55. "use strict";
  56. Object.defineProperty(exports, "__esModule", { value: true });
  57. type_1 = __importDefault(type_1);
  58. node_1 = __importDefault(node_1);
  59. /**
  60. * TextNode to contain a text element in DOM tree.
  61. * @param {string} value [description]
  62. */
  63. var TextNode = /** @class */ (function (_super) {
  64. __extends(TextNode, _super);
  65. function TextNode(rawText, parentNode) {
  66. var _this = _super.call(this, parentNode) || this;
  67. _this.rawText = rawText;
  68. /**
  69. * Node Type declaration.
  70. * @type {Number}
  71. */
  72. _this.nodeType = type_1.default.TEXT_NODE;
  73. return _this;
  74. }
  75. Object.defineProperty(TextNode.prototype, "trimmedText", {
  76. /**
  77. * Returns text with all whitespace trimmed except single leading/trailing non-breaking space
  78. */
  79. get: function () {
  80. if (this._trimmedText !== undefined)
  81. return this._trimmedText;
  82. var text = this.rawText;
  83. var i = 0;
  84. var startPos;
  85. var endPos;
  86. while (i >= 0 && i < text.length) {
  87. if (/\S/.test(text[i])) {
  88. if (startPos === undefined) {
  89. startPos = i;
  90. i = text.length;
  91. }
  92. else {
  93. endPos = i;
  94. i = void 0;
  95. }
  96. }
  97. if (startPos === undefined)
  98. i++;
  99. else
  100. i--;
  101. }
  102. if (startPos === undefined)
  103. startPos = 0;
  104. if (endPos === undefined)
  105. endPos = text.length - 1;
  106. var hasLeadingSpace = startPos > 0 && /[^\S\r\n]/.test(text[startPos - 1]);
  107. var hasTrailingSpace = endPos < (text.length - 1) && /[^\S\r\n]/.test(text[endPos + 1]);
  108. this._trimmedText = (hasLeadingSpace ? ' ' : '') + text.slice(startPos, endPos + 1) + (hasTrailingSpace ? ' ' : '');
  109. return this._trimmedText;
  110. },
  111. enumerable: false,
  112. configurable: true
  113. });
  114. Object.defineProperty(TextNode.prototype, "text", {
  115. /**
  116. * Get unescaped text value of current node and its children.
  117. * @return {string} text content
  118. */
  119. get: function () {
  120. return this.rawText;
  121. },
  122. enumerable: false,
  123. configurable: true
  124. });
  125. Object.defineProperty(TextNode.prototype, "isWhitespace", {
  126. /**
  127. * Detect if the node contains only white space.
  128. * @return {bool}
  129. */
  130. get: function () {
  131. return /^(\s|&nbsp;)*$/.test(this.rawText);
  132. },
  133. enumerable: false,
  134. configurable: true
  135. });
  136. TextNode.prototype.toString = function () {
  137. return this.text;
  138. };
  139. return TextNode;
  140. }(node_1.default));
  141. exports.default = TextNode;
  142. });
  143. define("matcher", ["require", "exports", "nodes/type"], function (require, exports, type_2) {
  144. "use strict";
  145. Object.defineProperty(exports, "__esModule", { value: true });
  146. type_2 = __importDefault(type_2);
  147. function isTag(node) {
  148. return node && node.nodeType === type_2.default.ELEMENT_NODE;
  149. }
  150. function getAttributeValue(elem, name) {
  151. return isTag(elem) ? elem.getAttribute(name) : undefined;
  152. }
  153. function getName(elem) {
  154. return ((elem && elem.rawTagName) || '').toLowerCase();
  155. }
  156. function getChildren(node) {
  157. return node && node.childNodes;
  158. }
  159. function getParent(node) {
  160. return node ? node.parentNode : null;
  161. }
  162. function getText(node) {
  163. return node.text;
  164. }
  165. function removeSubsets(nodes) {
  166. var idx = nodes.length;
  167. var node;
  168. var ancestor;
  169. var replace;
  170. // Check if each node (or one of its ancestors) is already contained in the
  171. // array.
  172. while (--idx > -1) {
  173. node = ancestor = nodes[idx];
  174. // Temporarily remove the node under consideration
  175. nodes[idx] = null;
  176. replace = true;
  177. while (ancestor) {
  178. if (nodes.indexOf(ancestor) > -1) {
  179. replace = false;
  180. nodes.splice(idx, 1);
  181. break;
  182. }
  183. ancestor = getParent(ancestor);
  184. }
  185. // If the node has been found to be unique, re-insert it.
  186. if (replace) {
  187. nodes[idx] = node;
  188. }
  189. }
  190. return nodes;
  191. }
  192. function existsOne(test, elems) {
  193. return elems.some(function (elem) {
  194. return isTag(elem) ? test(elem) || existsOne(test, getChildren(elem)) : false;
  195. });
  196. }
  197. function getSiblings(node) {
  198. var parent = getParent(node);
  199. return parent && getChildren(parent);
  200. }
  201. function hasAttrib(elem, name) {
  202. return getAttributeValue(elem, name) !== undefined;
  203. }
  204. function findOne(test, elems) {
  205. var elem = null;
  206. for (var i = 0, l = elems.length; i < l && !elem; i++) {
  207. var el = elems[i];
  208. if (test(el)) {
  209. elem = el;
  210. }
  211. else {
  212. var childs = getChildren(el);
  213. if (childs && childs.length > 0) {
  214. elem = findOne(test, childs);
  215. }
  216. }
  217. }
  218. return elem;
  219. }
  220. function findAll(test, nodes) {
  221. var result = [];
  222. for (var i = 0, j = nodes.length; i < j; i++) {
  223. if (!isTag(nodes[i]))
  224. continue;
  225. if (test(nodes[i]))
  226. result.push(nodes[i]);
  227. var childs = getChildren(nodes[i]);
  228. if (childs)
  229. result = result.concat(findAll(test, childs));
  230. }
  231. return result;
  232. }
  233. exports.default = {
  234. isTag: isTag,
  235. getAttributeValue: getAttributeValue,
  236. getName: getName,
  237. getChildren: getChildren,
  238. getParent: getParent,
  239. getText: getText,
  240. removeSubsets: removeSubsets,
  241. existsOne: existsOne,
  242. getSiblings: getSiblings,
  243. hasAttrib: hasAttrib,
  244. findOne: findOne,
  245. findAll: findAll
  246. };
  247. });
  248. define("nodes/html", ["require", "exports", "he", "css-select", "nodes/node", "nodes/type", "nodes/text", "matcher", "back", "nodes/comment"], function (require, exports, he_1, css_select_1, node_2, type_3, text_1, matcher_1, back_1, comment_1) {
  249. "use strict";
  250. Object.defineProperty(exports, "__esModule", { value: true });
  251. exports.parse = exports.base_parse = void 0;
  252. he_1 = __importDefault(he_1);
  253. node_2 = __importDefault(node_2);
  254. type_3 = __importDefault(type_3);
  255. text_1 = __importDefault(text_1);
  256. matcher_1 = __importDefault(matcher_1);
  257. back_1 = __importDefault(back_1);
  258. comment_1 = __importDefault(comment_1);
  259. // const { decode } = he;
  260. function decode(val) {
  261. // clone string
  262. return JSON.parse(JSON.stringify(he_1.default.decode(val)));
  263. }
  264. // https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements
  265. var kBlockElements = new Set();
  266. kBlockElements.add('address');
  267. kBlockElements.add('ADDRESS');
  268. kBlockElements.add('article');
  269. kBlockElements.add('ARTICLE');
  270. kBlockElements.add('aside');
  271. kBlockElements.add('ASIDE');
  272. kBlockElements.add('blockquote');
  273. kBlockElements.add('BLOCKQUOTE');
  274. kBlockElements.add('br');
  275. kBlockElements.add('BR');
  276. kBlockElements.add('details');
  277. kBlockElements.add('DETAILS');
  278. kBlockElements.add('dialog');
  279. kBlockElements.add('DIALOG');
  280. kBlockElements.add('dd');
  281. kBlockElements.add('DD');
  282. kBlockElements.add('div');
  283. kBlockElements.add('DIV');
  284. kBlockElements.add('dl');
  285. kBlockElements.add('DL');
  286. kBlockElements.add('dt');
  287. kBlockElements.add('DT');
  288. kBlockElements.add('fieldset');
  289. kBlockElements.add('FIELDSET');
  290. kBlockElements.add('figcaption');
  291. kBlockElements.add('FIGCAPTION');
  292. kBlockElements.add('figure');
  293. kBlockElements.add('FIGURE');
  294. kBlockElements.add('footer');
  295. kBlockElements.add('FOOTER');
  296. kBlockElements.add('form');
  297. kBlockElements.add('FORM');
  298. kBlockElements.add('h1');
  299. kBlockElements.add('H1');
  300. kBlockElements.add('h2');
  301. kBlockElements.add('H2');
  302. kBlockElements.add('h3');
  303. kBlockElements.add('H3');
  304. kBlockElements.add('h4');
  305. kBlockElements.add('H4');
  306. kBlockElements.add('h5');
  307. kBlockElements.add('H5');
  308. kBlockElements.add('h6');
  309. kBlockElements.add('H6');
  310. kBlockElements.add('header');
  311. kBlockElements.add('HEADER');
  312. kBlockElements.add('hgroup');
  313. kBlockElements.add('HGROUP');
  314. kBlockElements.add('hr');
  315. kBlockElements.add('HR');
  316. kBlockElements.add('li');
  317. kBlockElements.add('LI');
  318. kBlockElements.add('main');
  319. kBlockElements.add('MAIN');
  320. kBlockElements.add('nav');
  321. kBlockElements.add('NAV');
  322. kBlockElements.add('ol');
  323. kBlockElements.add('OL');
  324. kBlockElements.add('p');
  325. kBlockElements.add('P');
  326. kBlockElements.add('pre');
  327. kBlockElements.add('PRE');
  328. kBlockElements.add('section');
  329. kBlockElements.add('SECTION');
  330. kBlockElements.add('table');
  331. kBlockElements.add('TABLE');
  332. kBlockElements.add('td');
  333. kBlockElements.add('TD');
  334. kBlockElements.add('tr');
  335. kBlockElements.add('TR');
  336. kBlockElements.add('ul');
  337. kBlockElements.add('UL');
  338. var DOMTokenList = /** @class */ (function () {
  339. function DOMTokenList(valuesInit, afterUpdate) {
  340. if (valuesInit === void 0) { valuesInit = []; }
  341. if (afterUpdate === void 0) { afterUpdate = (function () { return null; }); }
  342. this._set = new Set(valuesInit);
  343. this._afterUpdate = afterUpdate;
  344. }
  345. DOMTokenList.prototype._validate = function (c) {
  346. if (/\s/.test(c)) {
  347. throw new Error("DOMException in DOMTokenList.add: The token '" + c + "' contains HTML space characters, which are not valid in tokens.");
  348. }
  349. };
  350. DOMTokenList.prototype.add = function (c) {
  351. this._validate(c);
  352. this._set.add(c);
  353. this._afterUpdate(this); // eslint-disable-line @typescript-eslint/no-unsafe-call
  354. };
  355. DOMTokenList.prototype.replace = function (c1, c2) {
  356. this._validate(c2);
  357. this._set.delete(c1);
  358. this._set.add(c2);
  359. this._afterUpdate(this); // eslint-disable-line @typescript-eslint/no-unsafe-call
  360. };
  361. DOMTokenList.prototype.remove = function (c) {
  362. this._set.delete(c) &&
  363. this._afterUpdate(this); // eslint-disable-line @typescript-eslint/no-unsafe-call
  364. };
  365. DOMTokenList.prototype.toggle = function (c) {
  366. this._validate(c);
  367. if (this._set.has(c))
  368. this._set.delete(c);
  369. else
  370. this._set.add(c);
  371. this._afterUpdate(this); // eslint-disable-line @typescript-eslint/no-unsafe-call
  372. };
  373. DOMTokenList.prototype.contains = function (c) {
  374. return this._set.has(c);
  375. };
  376. Object.defineProperty(DOMTokenList.prototype, "length", {
  377. get: function () {
  378. return this._set.size;
  379. },
  380. enumerable: false,
  381. configurable: true
  382. });
  383. DOMTokenList.prototype.values = function () {
  384. return this._set.values();
  385. };
  386. Object.defineProperty(DOMTokenList.prototype, "value", {
  387. get: function () {
  388. return Array.from(this._set.values());
  389. },
  390. enumerable: false,
  391. configurable: true
  392. });
  393. DOMTokenList.prototype.toString = function () {
  394. return Array.from(this._set.values()).join(' ');
  395. };
  396. return DOMTokenList;
  397. }());
  398. /**
  399. * HTMLElement, which contains a set of children.
  400. *
  401. * Note: this is a minimalist implementation, no complete tree
  402. * structure provided (no parentNode, nextSibling,
  403. * previousSibling etc).
  404. * @class HTMLElement
  405. * @extends {Node}
  406. */
  407. var HTMLElement = /** @class */ (function (_super) {
  408. __extends(HTMLElement, _super);
  409. /**
  410. * Creates an instance of HTMLElement.
  411. * @param keyAttrs id and class attribute
  412. * @param [rawAttrs] attributes in string
  413. *
  414. * @memberof HTMLElement
  415. */
  416. function HTMLElement(tagName, keyAttrs, rawAttrs, parentNode) {
  417. if (rawAttrs === void 0) { rawAttrs = ''; }
  418. var _this = _super.call(this, parentNode) || this;
  419. _this.rawAttrs = rawAttrs;
  420. /**
  421. * Node Type declaration.
  422. */
  423. _this.nodeType = type_3.default.ELEMENT_NODE;
  424. _this.rawTagName = tagName;
  425. _this.rawAttrs = rawAttrs || '';
  426. _this.id = keyAttrs.id || '';
  427. _this.childNodes = [];
  428. _this.classList = new DOMTokenList(keyAttrs.class ? keyAttrs.class.split(/\s+/) : [], function (classList) { return (_this.setAttribute('class', classList.toString()) // eslint-disable-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
  429. ); });
  430. if (keyAttrs.id) {
  431. if (!rawAttrs) {
  432. _this.rawAttrs = "id=\"" + keyAttrs.id + "\"";
  433. }
  434. }
  435. if (keyAttrs.class) {
  436. if (!rawAttrs) {
  437. var cls = "class=\"" + _this.classList.toString() + "\"";
  438. if (_this.rawAttrs) {
  439. _this.rawAttrs += " " + cls;
  440. }
  441. else {
  442. _this.rawAttrs = cls;
  443. }
  444. }
  445. }
  446. return _this;
  447. }
  448. /**
  449. * Quote attribute values
  450. * @param attr attribute value
  451. * @returns {string} quoted value
  452. */
  453. HTMLElement.prototype.quoteAttribute = function (attr) {
  454. if (attr === null) {
  455. return "null";
  456. }
  457. return JSON.stringify(attr.replace(/"/g, '&quot;'));
  458. };
  459. /**
  460. * Remove current element
  461. */
  462. HTMLElement.prototype.remove = function () {
  463. var _this = this;
  464. if (this.parentNode) {
  465. var children = this.parentNode.childNodes;
  466. this.parentNode.childNodes = children.filter(function (child) {
  467. return _this !== child;
  468. });
  469. }
  470. };
  471. /**
  472. * Remove Child element from childNodes array
  473. * @param {HTMLElement} node node to remove
  474. */
  475. HTMLElement.prototype.removeChild = function (node) {
  476. this.childNodes = this.childNodes.filter(function (child) {
  477. return (child !== node);
  478. });
  479. };
  480. /**
  481. * Exchanges given child with new child
  482. * @param {HTMLElement} oldNode node to exchange
  483. * @param {HTMLElement} newNode new node
  484. */
  485. HTMLElement.prototype.exchangeChild = function (oldNode, newNode) {
  486. var children = this.childNodes;
  487. this.childNodes = children.map(function (child) {
  488. if (child === oldNode) {
  489. return newNode;
  490. }
  491. return child;
  492. });
  493. };
  494. Object.defineProperty(HTMLElement.prototype, "tagName", {
  495. get: function () {
  496. return this.rawTagName ? this.rawTagName.toUpperCase() : this.rawTagName;
  497. },
  498. enumerable: false,
  499. configurable: true
  500. });
  501. Object.defineProperty(HTMLElement.prototype, "localName", {
  502. get: function () {
  503. return this.rawTagName.toLowerCase();
  504. },
  505. enumerable: false,
  506. configurable: true
  507. });
  508. Object.defineProperty(HTMLElement.prototype, "rawText", {
  509. /**
  510. * Get escpaed (as-it) text value of current node and its children.
  511. * @return {string} text content
  512. */
  513. get: function () {
  514. return this.childNodes.reduce(function (pre, cur) {
  515. return (pre += cur.rawText);
  516. }, '');
  517. },
  518. enumerable: false,
  519. configurable: true
  520. });
  521. Object.defineProperty(HTMLElement.prototype, "textContent", {
  522. get: function () {
  523. return this.rawText;
  524. },
  525. set: function (val) {
  526. var content = [new text_1.default(val, this)];
  527. this.childNodes = content;
  528. },
  529. enumerable: false,
  530. configurable: true
  531. });
  532. Object.defineProperty(HTMLElement.prototype, "text", {
  533. /**
  534. * Get unescaped text value of current node and its children.
  535. * @return {string} text content
  536. */
  537. get: function () {
  538. return decode(this.rawText);
  539. },
  540. enumerable: false,
  541. configurable: true
  542. });
  543. Object.defineProperty(HTMLElement.prototype, "structuredText", {
  544. /**
  545. * Get structured Text (with '\n' etc.)
  546. * @return {string} structured text
  547. */
  548. get: function () {
  549. var currentBlock = [];
  550. var blocks = [currentBlock];
  551. function dfs(node) {
  552. if (node.nodeType === type_3.default.ELEMENT_NODE) {
  553. if (kBlockElements.has(node.rawTagName)) {
  554. if (currentBlock.length > 0) {
  555. blocks.push(currentBlock = []);
  556. }
  557. node.childNodes.forEach(dfs);
  558. if (currentBlock.length > 0) {
  559. blocks.push(currentBlock = []);
  560. }
  561. }
  562. else {
  563. node.childNodes.forEach(dfs);
  564. }
  565. }
  566. else if (node.nodeType === type_3.default.TEXT_NODE) {
  567. if (node.isWhitespace) {
  568. // Whitespace node, postponed output
  569. currentBlock.prependWhitespace = true;
  570. }
  571. else {
  572. var text = node.trimmedText;
  573. if (currentBlock.prependWhitespace) {
  574. text = " " + text;
  575. currentBlock.prependWhitespace = false;
  576. }
  577. currentBlock.push(text);
  578. }
  579. }
  580. }
  581. dfs(this);
  582. return blocks.map(function (block) {
  583. // Normalize each line's whitespace
  584. return block.join('').replace(/\s{2,}/g, ' ');
  585. })
  586. .join('\n').replace(/\s+$/, ''); // trimRight;
  587. },
  588. enumerable: false,
  589. configurable: true
  590. });
  591. HTMLElement.prototype.toString = function () {
  592. var tag = this.rawTagName;
  593. if (tag) {
  594. // const void_tags = new Set('area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr'.split('|'));
  595. // const is_void = void_tags.has(tag);
  596. var is_void = /^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/i.test(tag);
  597. var attrs = this.rawAttrs ? " " + this.rawAttrs : '';
  598. if (is_void) {
  599. return "<" + tag + attrs + ">";
  600. }
  601. return "<" + tag + attrs + ">" + this.innerHTML + "</" + tag + ">";
  602. }
  603. return this.innerHTML;
  604. };
  605. Object.defineProperty(HTMLElement.prototype, "innerHTML", {
  606. get: function () {
  607. return this.childNodes.map(function (child) {
  608. return child.toString();
  609. }).join('');
  610. },
  611. set: function (content) {
  612. //const r = parse(content, global.options); // TODO global.options ?
  613. var r = parse(content);
  614. this.childNodes = r.childNodes.length ? r.childNodes : [new text_1.default(content, this)];
  615. },
  616. enumerable: false,
  617. configurable: true
  618. });
  619. HTMLElement.prototype.set_content = function (content, options) {
  620. if (options === void 0) { options = {}; }
  621. if (content instanceof node_2.default) {
  622. content = [content];
  623. }
  624. else if (typeof content == 'string') {
  625. var r = parse(content, options);
  626. content = r.childNodes.length ? r.childNodes : [new text_1.default(content, this)];
  627. }
  628. this.childNodes = content;
  629. };
  630. HTMLElement.prototype.replaceWith = function () {
  631. var _this = this;
  632. var nodes = [];
  633. for (var _i = 0; _i < arguments.length; _i++) {
  634. nodes[_i] = arguments[_i];
  635. }
  636. var content = nodes.map(function (node) {
  637. if (node instanceof node_2.default) {
  638. return [node];
  639. }
  640. else if (typeof node == 'string') {
  641. // const r = parse(content, global.options); // TODO global.options ?
  642. var r = parse(node);
  643. return r.childNodes.length ? r.childNodes : [new text_1.default(node, _this)];
  644. }
  645. return [];
  646. }).flat();
  647. var idx = this.parentNode.childNodes.findIndex(function (child) {
  648. return child === _this;
  649. });
  650. this.parentNode.childNodes = __spreadArray(__spreadArray(__spreadArray([], this.parentNode.childNodes.slice(0, idx)), content), this.parentNode.childNodes.slice(idx + 1));
  651. };
  652. Object.defineProperty(HTMLElement.prototype, "outerHTML", {
  653. get: function () {
  654. return this.toString();
  655. },
  656. enumerable: false,
  657. configurable: true
  658. });
  659. /**
  660. * Trim element from right (in block) after seeing pattern in a TextNode.
  661. * @param {RegExp} pattern pattern to find
  662. * @return {HTMLElement} reference to current node
  663. */
  664. HTMLElement.prototype.trimRight = function (pattern) {
  665. for (var i = 0; i < this.childNodes.length; i++) {
  666. var childNode = this.childNodes[i];
  667. if (childNode.nodeType === type_3.default.ELEMENT_NODE) {
  668. childNode.trimRight(pattern);
  669. }
  670. else {
  671. var index = childNode.rawText.search(pattern);
  672. if (index > -1) {
  673. childNode.rawText = childNode.rawText.substr(0, index);
  674. // trim all following nodes.
  675. this.childNodes.length = i + 1;
  676. }
  677. }
  678. }
  679. return this;
  680. };
  681. Object.defineProperty(HTMLElement.prototype, "structure", {
  682. /**
  683. * Get DOM structure
  684. * @return {string} strucutre
  685. */
  686. get: function () {
  687. var res = [];
  688. var indention = 0;
  689. function write(str) {
  690. res.push(' '.repeat(indention) + str);
  691. }
  692. function dfs(node) {
  693. var idStr = node.id ? ("#" + node.id) : '';
  694. var classStr = node.classList.length ? ("." + node.classList.value.join('.')) : ''; // eslint-disable-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-unsafe-call
  695. write("" + node.rawTagName + idStr + classStr);
  696. indention++;
  697. node.childNodes.forEach(function (childNode) {
  698. if (childNode.nodeType === type_3.default.ELEMENT_NODE) {
  699. dfs(childNode);
  700. }
  701. else if (childNode.nodeType === type_3.default.TEXT_NODE) {
  702. if (!childNode.isWhitespace) {
  703. write('#text');
  704. }
  705. }
  706. });
  707. indention--;
  708. }
  709. dfs(this);
  710. return res.join('\n');
  711. },
  712. enumerable: false,
  713. configurable: true
  714. });
  715. /**
  716. * Remove whitespaces in this sub tree.
  717. * @return {HTMLElement} pointer to this
  718. */
  719. HTMLElement.prototype.removeWhitespace = function () {
  720. var _this = this;
  721. var o = 0;
  722. this.childNodes.forEach(function (node) {
  723. if (node.nodeType === type_3.default.TEXT_NODE) {
  724. if (node.isWhitespace) {
  725. return;
  726. }
  727. node.rawText = node.trimmedText;
  728. }
  729. else if (node.nodeType === type_3.default.ELEMENT_NODE) {
  730. node.removeWhitespace();
  731. }
  732. _this.childNodes[o++] = node;
  733. });
  734. this.childNodes.length = o;
  735. return this;
  736. };
  737. /**
  738. * Query CSS selector to find matching nodes.
  739. * @param {string} selector Simplified CSS selector
  740. * @return {HTMLElement[]} matching elements
  741. */
  742. HTMLElement.prototype.querySelectorAll = function (selector) {
  743. return css_select_1.selectAll(selector, this, {
  744. xmlMode: true,
  745. adapter: matcher_1.default
  746. });
  747. // let matcher: Matcher;
  748. // if (selector instanceof Matcher) {
  749. // matcher = selector;
  750. // matcher.reset();
  751. // } else {
  752. // if (selector.includes(',')) {
  753. // const selectors = selector.split(',');
  754. // return Array.from(selectors.reduce((pre, cur) => {
  755. // const result = this.querySelectorAll(cur.trim());
  756. // return result.reduce((p, c) => {
  757. // return p.add(c);
  758. // }, pre);
  759. // }, new Set<HTMLElement>()));
  760. // }
  761. // matcher = new Matcher(selector);
  762. // }
  763. // interface IStack {
  764. // 0: Node; // node
  765. // 1: number; // children
  766. // 2: boolean; // found flag
  767. // }
  768. // const stack = [] as IStack[];
  769. // return this.childNodes.reduce((res, cur) => {
  770. // stack.push([cur, 0, false]);
  771. // while (stack.length) {
  772. // const state = arr_back(stack); // get last element
  773. // const el = state[0];
  774. // if (state[1] === 0) {
  775. // // Seen for first time.
  776. // if (el.nodeType !== NodeType.ELEMENT_NODE) {
  777. // stack.pop();
  778. // continue;
  779. // }
  780. // const html_el = el as HTMLElement;
  781. // state[2] = matcher.advance(html_el);
  782. // if (state[2]) {
  783. // if (matcher.matched) {
  784. // res.push(html_el);
  785. // res.push(...(html_el.querySelectorAll(selector)));
  786. // // no need to go further.
  787. // matcher.rewind();
  788. // stack.pop();
  789. // continue;
  790. // }
  791. // }
  792. // }
  793. // if (state[1] < el.childNodes.length) {
  794. // stack.push([el.childNodes[state[1]++], 0, false]);
  795. // } else {
  796. // if (state[2]) {
  797. // matcher.rewind();
  798. // }
  799. // stack.pop();
  800. // }
  801. // }
  802. // return res;
  803. // }, [] as HTMLElement[]);
  804. };
  805. /**
  806. * Query CSS Selector to find matching node.
  807. * @param {string} selector Simplified CSS selector
  808. * @return {HTMLElement} matching node
  809. */
  810. HTMLElement.prototype.querySelector = function (selector) {
  811. return css_select_1.selectOne(selector, this, {
  812. xmlMode: true,
  813. adapter: matcher_1.default
  814. });
  815. // let matcher: Matcher;
  816. // if (selector instanceof Matcher) {
  817. // matcher = selector;
  818. // matcher.reset();
  819. // } else {
  820. // matcher = new Matcher(selector);
  821. // }
  822. // const stack = [] as { 0: Node; 1: 0 | 1; 2: boolean }[];
  823. // for (const node of this.childNodes) {
  824. // stack.push([node, 0, false]);
  825. // while (stack.length) {
  826. // const state = arr_back(stack);
  827. // const el = state[0];
  828. // if (state[1] === 0) {
  829. // // Seen for first time.
  830. // if (el.nodeType !== NodeType.ELEMENT_NODE) {
  831. // stack.pop();
  832. // continue;
  833. // }
  834. // state[2] = matcher.advance(el as HTMLElement);
  835. // if (state[2]) {
  836. // if (matcher.matched) {
  837. // return el as HTMLElement;
  838. // }
  839. // }
  840. // }
  841. // if (state[1] < el.childNodes.length) {
  842. // stack.push([el.childNodes[state[1]++], 0, false]);
  843. // } else {
  844. // if (state[2]) {
  845. // matcher.rewind();
  846. // }
  847. // stack.pop();
  848. // }
  849. // }
  850. // }
  851. // return null;
  852. };
  853. /**
  854. * traverses the Element and its parents (heading toward the document root) until it finds a node that matches the provided selector string. Will return itself or the matching ancestor. If no such element exists, it returns null.
  855. * @param selector a DOMString containing a selector list
  856. */
  857. HTMLElement.prototype.closest = function (selector) {
  858. var mapChild = new Map();
  859. var el = this;
  860. var old = null;
  861. function findOne(test, elems) {
  862. var elem = null;
  863. for (var i = 0, l = elems.length; i < l && !elem; i++) {
  864. var el_1 = elems[i];
  865. if (test(el_1)) {
  866. elem = el_1;
  867. }
  868. else {
  869. var child = mapChild.get(el_1);
  870. if (child) {
  871. elem = findOne(test, [child]);
  872. }
  873. }
  874. }
  875. return elem;
  876. }
  877. while (el) {
  878. mapChild.set(el, old);
  879. old = el;
  880. el = el.parentNode;
  881. }
  882. el = this;
  883. while (el) {
  884. var e = css_select_1.selectOne(selector, el, {
  885. xmlMode: true,
  886. adapter: __assign(__assign({}, matcher_1.default), { getChildren: function (node) {
  887. var child = mapChild.get(node);
  888. return child && [child];
  889. },
  890. getSiblings: function (node) {
  891. return [node];
  892. },
  893. findOne: findOne,
  894. findAll: function () {
  895. return [];
  896. } })
  897. });
  898. if (e) {
  899. return e;
  900. }
  901. el = el.parentNode;
  902. }
  903. return null;
  904. };
  905. /**
  906. * Append a child node to childNodes
  907. * @param {Node} node node to append
  908. * @return {Node} node appended
  909. */
  910. HTMLElement.prototype.appendChild = function (node) {
  911. // node.parentNode = this;
  912. this.childNodes.push(node);
  913. node.parentNode = this;
  914. return node;
  915. };
  916. Object.defineProperty(HTMLElement.prototype, "firstChild", {
  917. /**
  918. * Get first child node
  919. * @return {Node} first child node
  920. */
  921. get: function () {
  922. return this.childNodes[0];
  923. },
  924. enumerable: false,
  925. configurable: true
  926. });
  927. Object.defineProperty(HTMLElement.prototype, "lastChild", {
  928. /**
  929. * Get last child node
  930. * @return {Node} last child node
  931. */
  932. get: function () {
  933. return back_1.default(this.childNodes);
  934. },
  935. enumerable: false,
  936. configurable: true
  937. });
  938. Object.defineProperty(HTMLElement.prototype, "attrs", {
  939. /**
  940. * Get attributes
  941. * @access private
  942. * @return {Object} parsed and unescaped attributes
  943. */
  944. get: function () {
  945. if (this._attrs) {
  946. return this._attrs;
  947. }
  948. this._attrs = {};
  949. var attrs = this.rawAttributes;
  950. for (var key in attrs) {
  951. var val = attrs[key] || '';
  952. this._attrs[key.toLowerCase()] = decode(val);
  953. }
  954. return this._attrs;
  955. },
  956. enumerable: false,
  957. configurable: true
  958. });
  959. Object.defineProperty(HTMLElement.prototype, "attributes", {
  960. get: function () {
  961. var ret_attrs = {};
  962. var attrs = this.rawAttributes;
  963. for (var key in attrs) {
  964. var val = attrs[key] || '';
  965. ret_attrs[key] = decode(val);
  966. }
  967. return ret_attrs;
  968. },
  969. enumerable: false,
  970. configurable: true
  971. });
  972. Object.defineProperty(HTMLElement.prototype, "rawAttributes", {
  973. /**
  974. * Get escaped (as-it) attributes
  975. * @return {Object} parsed attributes
  976. */
  977. get: function () {
  978. if (this._rawAttrs) {
  979. return this._rawAttrs;
  980. }
  981. var attrs = {};
  982. if (this.rawAttrs) {
  983. var re = /\b([a-z][a-z0-9-_:]*)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|(\S+)))?/ig;
  984. var match = void 0;
  985. while ((match = re.exec(this.rawAttrs))) {
  986. attrs[match[1]] = match[2] || match[3] || match[4] || null;
  987. }
  988. }
  989. this._rawAttrs = attrs;
  990. return attrs;
  991. },
  992. enumerable: false,
  993. configurable: true
  994. });
  995. HTMLElement.prototype.removeAttribute = function (key) {
  996. var attrs = this.rawAttributes;
  997. delete attrs[key];
  998. // Update this.attribute
  999. if (this._attrs) {
  1000. delete this._attrs[key];
  1001. }
  1002. // Update rawString
  1003. this.rawAttrs = Object.keys(attrs).map(function (name) {
  1004. var val = JSON.stringify(attrs[name]);
  1005. if (val === undefined || val === 'null') {
  1006. return name;
  1007. }
  1008. return name + "=" + val;
  1009. }).join(' ');
  1010. // Update this.id
  1011. if (key === 'id') {
  1012. this.id = '';
  1013. }
  1014. };
  1015. HTMLElement.prototype.hasAttribute = function (key) {
  1016. return key.toLowerCase() in this.attrs;
  1017. };
  1018. /**
  1019. * Get an attribute
  1020. * @return {string} value of the attribute
  1021. */
  1022. HTMLElement.prototype.getAttribute = function (key) {
  1023. return this.attrs[key.toLowerCase()];
  1024. };
  1025. /**
  1026. * Set an attribute value to the HTMLElement
  1027. * @param {string} key The attribute name
  1028. * @param {string} value The value to set, or null / undefined to remove an attribute
  1029. */
  1030. HTMLElement.prototype.setAttribute = function (key, value) {
  1031. var _this = this;
  1032. if (arguments.length < 2) {
  1033. throw new Error('Failed to execute \'setAttribute\' on \'Element\'');
  1034. }
  1035. var k2 = key.toLowerCase();
  1036. var attrs = this.rawAttributes;
  1037. for (var k in attrs) {
  1038. if (k.toLowerCase() === k2) {
  1039. key = k;
  1040. break;
  1041. }
  1042. }
  1043. attrs[key] = String(value);
  1044. // update this.attrs
  1045. if (this._attrs) {
  1046. this._attrs[k2] = decode(attrs[key]);
  1047. }
  1048. // Update rawString
  1049. this.rawAttrs = Object.keys(attrs).map(function (name) {
  1050. var val = _this.quoteAttribute(attrs[name]);
  1051. if (val === 'null' || val === '""') {
  1052. return name;
  1053. }
  1054. return name + "=" + val;
  1055. }).join(' ');
  1056. // Update this.id
  1057. if (key === 'id') {
  1058. this.id = value;
  1059. }
  1060. };
  1061. /**
  1062. * Replace all the attributes of the HTMLElement by the provided attributes
  1063. * @param {Attributes} attributes the new attribute set
  1064. */
  1065. HTMLElement.prototype.setAttributes = function (attributes) {
  1066. var _this = this;
  1067. // Invalidate current this.attributes
  1068. if (this._attrs) {
  1069. delete this._attrs;
  1070. }
  1071. // Invalidate current this.rawAttributes
  1072. if (this._rawAttrs) {
  1073. delete this._rawAttrs;
  1074. }
  1075. // Update rawString
  1076. this.rawAttrs = Object.keys(attributes).map(function (name) {
  1077. var val = attributes[name];
  1078. if (val === 'null' || val === '""') {
  1079. return name;
  1080. }
  1081. return name + "=" + _this.quoteAttribute(String(val));
  1082. }).join(' ');
  1083. };
  1084. HTMLElement.prototype.insertAdjacentHTML = function (where, html) {
  1085. var _a, _b, _c;
  1086. var _this = this;
  1087. if (arguments.length < 2) {
  1088. throw new Error('2 arguments required');
  1089. }
  1090. var p = parse(html);
  1091. if (where === 'afterend') {
  1092. var idx = this.parentNode.childNodes.findIndex(function (child) {
  1093. return child === _this;
  1094. });
  1095. (_a = this.parentNode.childNodes).splice.apply(_a, __spreadArray([idx + 1, 0], p.childNodes));
  1096. p.childNodes.forEach(function (n) {
  1097. if (n instanceof HTMLElement) {
  1098. n.parentNode = _this.parentNode;
  1099. }
  1100. });
  1101. }
  1102. else if (where === 'afterbegin') {
  1103. (_b = this.childNodes).unshift.apply(_b, p.childNodes);
  1104. }
  1105. else if (where === 'beforeend') {
  1106. p.childNodes.forEach(function (n) {
  1107. _this.appendChild(n);
  1108. });
  1109. }
  1110. else if (where === 'beforebegin') {
  1111. var idx = this.parentNode.childNodes.findIndex(function (child) {
  1112. return child === _this;
  1113. });
  1114. (_c = this.parentNode.childNodes).splice.apply(_c, __spreadArray([idx, 0], p.childNodes));
  1115. p.childNodes.forEach(function (n) {
  1116. if (n instanceof HTMLElement) {
  1117. n.parentNode = _this.parentNode;
  1118. }
  1119. });
  1120. }
  1121. else {
  1122. throw new Error("The value provided ('" + where + "') is not one of 'beforebegin', 'afterbegin', 'beforeend', or 'afterend'");
  1123. }
  1124. // if (!where || html === undefined || html === null) {
  1125. // return;
  1126. // }
  1127. };
  1128. Object.defineProperty(HTMLElement.prototype, "nextSibling", {
  1129. get: function () {
  1130. if (this.parentNode) {
  1131. var children = this.parentNode.childNodes;
  1132. var i = 0;
  1133. while (i < children.length) {
  1134. var child = children[i++];
  1135. if (this === child) {
  1136. return children[i] || null;
  1137. }
  1138. }
  1139. return null;
  1140. }
  1141. },
  1142. enumerable: false,
  1143. configurable: true
  1144. });
  1145. Object.defineProperty(HTMLElement.prototype, "nextElementSibling", {
  1146. get: function () {
  1147. if (this.parentNode) {
  1148. var children = this.parentNode.childNodes;
  1149. var i = 0;
  1150. var find = false;
  1151. while (i < children.length) {
  1152. var child = children[i++];
  1153. if (find) {
  1154. if (child instanceof HTMLElement) {
  1155. return child || null;
  1156. }
  1157. }
  1158. else if (this === child) {
  1159. find = true;
  1160. }
  1161. }
  1162. return null;
  1163. }
  1164. },
  1165. enumerable: false,
  1166. configurable: true
  1167. });
  1168. Object.defineProperty(HTMLElement.prototype, "classNames", {
  1169. get: function () {
  1170. return this.classList.toString();
  1171. },
  1172. enumerable: false,
  1173. configurable: true
  1174. });
  1175. return HTMLElement;
  1176. }(node_2.default));
  1177. exports.default = HTMLElement;
  1178. // https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
  1179. var kMarkupPattern = /<!--[^]*?(?=-->)-->|<(\/?)([a-z][-.:0-9_a-z]*)\s*([^>]*?)(\/?)>/ig;
  1180. // <(?<tag>[^\s]*)(.*)>(.*)</\k<tag>>
  1181. // <([a-z][-.:0-9_a-z]*)\s*\/>
  1182. // <(area|base|br|col|hr|img|input|link|meta|source)\s*(.*)\/?>
  1183. // <(area|base|br|col|hr|img|input|link|meta|source)\s*(.*)\/?>|<(?<tag>[^\s]*)(.*)>(.*)</\k<tag>>
  1184. var kAttributePattern = /(^|\s)(id|class)\s*=\s*("([^"]*)"|'([^']*)'|(\S+))/ig;
  1185. var kSelfClosingElements = {
  1186. area: true,
  1187. AREA: true,
  1188. base: true,
  1189. BASE: true,
  1190. br: true,
  1191. BR: true,
  1192. col: true,
  1193. COL: true,
  1194. hr: true,
  1195. HR: true,
  1196. img: true,
  1197. IMG: true,
  1198. input: true,
  1199. INPUT: true,
  1200. link: true,
  1201. LINK: true,
  1202. meta: true,
  1203. META: true,
  1204. source: true,
  1205. SOURCE: true,
  1206. embed: true,
  1207. EMBED: true,
  1208. param: true,
  1209. PARAM: true,
  1210. track: true,
  1211. TRACK: true,
  1212. wbr: true,
  1213. WBR: true
  1214. };
  1215. var kElementsClosedByOpening = {
  1216. li: { li: true, LI: true },
  1217. LI: { li: true, LI: true },
  1218. p: { p: true, div: true, P: true, DIV: true },
  1219. P: { p: true, div: true, P: true, DIV: true },
  1220. b: { div: true, DIV: true },
  1221. B: { div: true, DIV: true },
  1222. td: { td: true, th: true, TD: true, TH: true },
  1223. TD: { td: true, th: true, TD: true, TH: true },
  1224. th: { td: true, th: true, TD: true, TH: true },
  1225. TH: { td: true, th: true, TD: true, TH: true },
  1226. h1: { h1: true, H1: true },
  1227. H1: { h1: true, H1: true },
  1228. h2: { h2: true, H2: true },
  1229. H2: { h2: true, H2: true },
  1230. h3: { h3: true, H3: true },
  1231. H3: { h3: true, H3: true },
  1232. h4: { h4: true, H4: true },
  1233. H4: { h4: true, H4: true },
  1234. h5: { h5: true, H5: true },
  1235. H5: { h5: true, H5: true },
  1236. h6: { h6: true, H6: true },
  1237. H6: { h6: true, H6: true }
  1238. };
  1239. var kElementsClosedByClosing = {
  1240. li: { ul: true, ol: true, UL: true, OL: true },
  1241. LI: { ul: true, ol: true, UL: true, OL: true },
  1242. a: { div: true, DIV: true },
  1243. A: { div: true, DIV: true },
  1244. b: { div: true, DIV: true },
  1245. B: { div: true, DIV: true },
  1246. i: { div: true, DIV: true },
  1247. I: { div: true, DIV: true },
  1248. p: { div: true, DIV: true },
  1249. P: { div: true, DIV: true },
  1250. td: { tr: true, table: true, TR: true, TABLE: true },
  1251. TD: { tr: true, table: true, TR: true, TABLE: true },
  1252. th: { tr: true, table: true, TR: true, TABLE: true },
  1253. TH: { tr: true, table: true, TR: true, TABLE: true }
  1254. };
  1255. var frameflag = 'documentfragmentcontainer';
  1256. /**
  1257. * Parses HTML and returns a root element
  1258. * Parse a chuck of HTML source.
  1259. * @param {string} data html
  1260. * @return {HTMLElement} root element
  1261. */
  1262. function base_parse(data, options) {
  1263. if (options === void 0) { options = { lowerCaseTagName: false, comment: false }; }
  1264. var elements = options.blockTextElements || {
  1265. script: true,
  1266. noscript: true,
  1267. style: true,
  1268. pre: true
  1269. };
  1270. var element_names = Object.keys(elements);
  1271. var kBlockTextElements = element_names.map(function (it) {
  1272. return new RegExp(it, 'i');
  1273. });
  1274. var kIgnoreElements = element_names.filter(function (it) {
  1275. return elements[it];
  1276. }).map(function (it) {
  1277. return new RegExp(it, 'i');
  1278. });
  1279. function element_should_be_ignore(tag) {
  1280. return kIgnoreElements.some(function (it) {
  1281. return it.test(tag);
  1282. });
  1283. }
  1284. function is_block_text_element(tag) {
  1285. return kBlockTextElements.some(function (it) {
  1286. return it.test(tag);
  1287. });
  1288. }
  1289. var root = new HTMLElement(null, {}, '', null);
  1290. var currentParent = root;
  1291. var stack = [root];
  1292. var lastTextPos = -1;
  1293. var match;
  1294. // https://github.com/taoqf/node-html-parser/issues/38
  1295. data = "<" + frameflag + ">" + data + "</" + frameflag + ">";
  1296. var _loop_1 = function () {
  1297. if (lastTextPos > -1) {
  1298. if (lastTextPos + match[0].length < kMarkupPattern.lastIndex) {
  1299. // if has content
  1300. var text = data.substring(lastTextPos, kMarkupPattern.lastIndex - match[0].length);
  1301. currentParent.appendChild(new text_1.default(text, currentParent));
  1302. }
  1303. }
  1304. lastTextPos = kMarkupPattern.lastIndex;
  1305. if (match[2] === frameflag) {
  1306. return "continue";
  1307. }
  1308. if (match[0][1] === '!') {
  1309. // this is a comment
  1310. if (options.comment) {
  1311. // Only keep what is in between <!-- and -->
  1312. var text = data.substring(lastTextPos - 3, lastTextPos - match[0].length + 4);
  1313. currentParent.appendChild(new comment_1.default(text, currentParent));
  1314. }
  1315. return "continue";
  1316. }
  1317. if (options.lowerCaseTagName) {
  1318. match[2] = match[2].toLowerCase();
  1319. }
  1320. if (!match[1]) {
  1321. // not </ tags
  1322. var attrs = {};
  1323. for (var attMatch = void 0; (attMatch = kAttributePattern.exec(match[3]));) {
  1324. attrs[attMatch[2].toLowerCase()] = attMatch[4] || attMatch[5] || attMatch[6];
  1325. }
  1326. var tagName = currentParent.rawTagName;
  1327. if (!match[4] && kElementsClosedByOpening[tagName]) {
  1328. if (kElementsClosedByOpening[tagName][match[2]]) {
  1329. stack.pop();
  1330. currentParent = back_1.default(stack);
  1331. }
  1332. }
  1333. // ignore container tag we add above
  1334. // https://github.com/taoqf/node-html-parser/issues/38
  1335. currentParent = currentParent.appendChild(new HTMLElement(match[2], attrs, match[3], null));
  1336. stack.push(currentParent);
  1337. if (is_block_text_element(match[2])) {
  1338. // a little test to find next </script> or </style> ...
  1339. var closeMarkup_1 = "</" + match[2] + ">";
  1340. var index = (function () {
  1341. if (options.lowerCaseTagName) {
  1342. return data.toLocaleLowerCase().indexOf(closeMarkup_1, kMarkupPattern.lastIndex);
  1343. }
  1344. return data.indexOf(closeMarkup_1, kMarkupPattern.lastIndex);
  1345. })();
  1346. if (element_should_be_ignore(match[2])) {
  1347. var text = void 0;
  1348. if (index === -1) {
  1349. // there is no matching ending for the text element.
  1350. text = data.substr(kMarkupPattern.lastIndex);
  1351. }
  1352. else {
  1353. text = data.substring(kMarkupPattern.lastIndex, index);
  1354. }
  1355. if (text.length > 0) {
  1356. currentParent.appendChild(new text_1.default(text, currentParent));
  1357. }
  1358. }
  1359. if (index === -1) {
  1360. lastTextPos = kMarkupPattern.lastIndex = data.length + 1;
  1361. }
  1362. else {
  1363. lastTextPos = kMarkupPattern.lastIndex = index + closeMarkup_1.length;
  1364. match[1] = 'true';
  1365. }
  1366. }
  1367. }
  1368. if (match[1] || match[4] || kSelfClosingElements[match[2]]) {
  1369. // </ or /> or <br> etc.
  1370. while (true) {
  1371. if (currentParent.rawTagName === match[2]) {
  1372. stack.pop();
  1373. currentParent = back_1.default(stack);
  1374. break;
  1375. }
  1376. else {
  1377. var tagName = currentParent.tagName;
  1378. // Trying to close current tag, and move on
  1379. if (kElementsClosedByClosing[tagName]) {
  1380. if (kElementsClosedByClosing[tagName][match[2]]) {
  1381. stack.pop();
  1382. currentParent = back_1.default(stack);
  1383. continue;
  1384. }
  1385. }
  1386. // Use aggressive strategy to handle unmatching markups.
  1387. break;
  1388. }
  1389. }
  1390. }
  1391. };
  1392. while ((match = kMarkupPattern.exec(data))) {
  1393. _loop_1();
  1394. }
  1395. return stack;
  1396. }
  1397. exports.base_parse = base_parse;
  1398. /**
  1399. * Parses HTML and returns a root element
  1400. * Parse a chuck of HTML source.
  1401. */
  1402. function parse(data, options) {
  1403. if (options === void 0) { options = { lowerCaseTagName: false, comment: false }; }
  1404. var stack = base_parse(data, options);
  1405. var root = stack[0];
  1406. var _loop_2 = function () {
  1407. // Handle each error elements.
  1408. var last = stack.pop();
  1409. var oneBefore = back_1.default(stack);
  1410. if (last.parentNode && last.parentNode.parentNode) {
  1411. if (last.parentNode === oneBefore && last.tagName === oneBefore.tagName) {
  1412. // Pair error case <h3> <h3> handle : Fixes to <h3> </h3>
  1413. oneBefore.removeChild(last);
  1414. last.childNodes.forEach(function (child) {
  1415. oneBefore.parentNode.appendChild(child);
  1416. });
  1417. stack.pop();
  1418. }
  1419. else {
  1420. // Single error <div> <h3> </div> handle: Just removes <h3>
  1421. oneBefore.removeChild(last);
  1422. last.childNodes.forEach(function (child) {
  1423. oneBefore.appendChild(child);
  1424. });
  1425. }
  1426. }
  1427. else {
  1428. // If it's final element just skip.
  1429. }
  1430. };
  1431. while (stack.length > 1) {
  1432. _loop_2();
  1433. }
  1434. // response.childNodes.forEach((node) => {
  1435. // if (node instanceof HTMLElement) {
  1436. // node.parentNode = null;
  1437. // }
  1438. // });
  1439. return root;
  1440. }
  1441. exports.parse = parse;
  1442. });
  1443. define("nodes/node", ["require", "exports"], function (require, exports) {
  1444. "use strict";
  1445. Object.defineProperty(exports, "__esModule", { value: true });
  1446. /**
  1447. * Node Class as base class for TextNode and HTMLElement.
  1448. */
  1449. var Node = /** @class */ (function () {
  1450. function Node(parentNode) {
  1451. if (parentNode === void 0) { parentNode = null; }
  1452. this.parentNode = parentNode;
  1453. this.childNodes = [];
  1454. }
  1455. Object.defineProperty(Node.prototype, "innerText", {
  1456. get: function () {
  1457. return this.rawText;
  1458. },
  1459. enumerable: false,
  1460. configurable: true
  1461. });
  1462. Object.defineProperty(Node.prototype, "textContent", {
  1463. get: function () {
  1464. return this.rawText;
  1465. },
  1466. set: function (val) {
  1467. this.rawText = val;
  1468. },
  1469. enumerable: false,
  1470. configurable: true
  1471. });
  1472. return Node;
  1473. }());
  1474. exports.default = Node;
  1475. });
  1476. define("nodes/comment", ["require", "exports", "nodes/node", "nodes/type"], function (require, exports, node_3, type_4) {
  1477. "use strict";
  1478. Object.defineProperty(exports, "__esModule", { value: true });
  1479. node_3 = __importDefault(node_3);
  1480. type_4 = __importDefault(type_4);
  1481. var CommentNode = /** @class */ (function (_super) {
  1482. __extends(CommentNode, _super);
  1483. function CommentNode(rawText, parentNode) {
  1484. var _this = _super.call(this, parentNode) || this;
  1485. _this.rawText = rawText;
  1486. /**
  1487. * Node Type declaration.
  1488. * @type {Number}
  1489. */
  1490. _this.nodeType = type_4.default.COMMENT_NODE;
  1491. return _this;
  1492. }
  1493. Object.defineProperty(CommentNode.prototype, "text", {
  1494. /**
  1495. * Get unescaped text value of current node and its children.
  1496. * @return {string} text content
  1497. */
  1498. get: function () {
  1499. return this.rawText;
  1500. },
  1501. enumerable: false,
  1502. configurable: true
  1503. });
  1504. CommentNode.prototype.toString = function () {
  1505. return "<!--" + this.rawText + "-->";
  1506. };
  1507. return CommentNode;
  1508. }(node_3.default));
  1509. exports.default = CommentNode;
  1510. });
  1511. define("parse", ["require", "exports", "nodes/html"], function (require, exports, html_1) {
  1512. "use strict";
  1513. Object.defineProperty(exports, "__esModule", { value: true });
  1514. exports.default = void 0;
  1515. Object.defineProperty(exports, "default", { enumerable: true, get: function () { return html_1.parse; } });
  1516. });
  1517. define("valid", ["require", "exports", "nodes/html"], function (require, exports, html_2) {
  1518. "use strict";
  1519. Object.defineProperty(exports, "__esModule", { value: true });
  1520. /**
  1521. * Parses HTML and returns a root element
  1522. * Parse a chuck of HTML source.
  1523. */
  1524. function valid(data, options) {
  1525. if (options === void 0) { options = { lowerCaseTagName: false, comment: false }; }
  1526. var stack = html_2.base_parse(data, options);
  1527. return Boolean(stack.length === 1);
  1528. }
  1529. exports.default = valid;
  1530. });
  1531. define("index", ["require", "exports", "nodes/comment", "nodes/html", "parse", "valid", "nodes/node", "nodes/text", "nodes/type"], function (require, exports, comment_2, html_3, parse_1, valid_1, node_4, text_2, type_5) {
  1532. "use strict";
  1533. Object.defineProperty(exports, "__esModule", { value: true });
  1534. exports.NodeType = exports.TextNode = exports.Node = exports.valid = exports.default = exports.parse = exports.HTMLElement = exports.CommentNode = void 0;
  1535. Object.defineProperty(exports, "CommentNode", { enumerable: true, get: function () { return __importDefault(comment_2).default; } });
  1536. Object.defineProperty(exports, "HTMLElement", { enumerable: true, get: function () { return __importDefault(html_3).default; } });
  1537. Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return __importDefault(parse_1).default; } });
  1538. Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(parse_1).default; } });
  1539. Object.defineProperty(exports, "valid", { enumerable: true, get: function () { return __importDefault(valid_1).default; } });
  1540. Object.defineProperty(exports, "Node", { enumerable: true, get: function () { return __importDefault(node_4).default; } });
  1541. Object.defineProperty(exports, "TextNode", { enumerable: true, get: function () { return __importDefault(text_2).default; } });
  1542. Object.defineProperty(exports, "NodeType", { enumerable: true, get: function () { return __importDefault(type_5).default; } });
  1543. });