node.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. "use strict";
  2. var __extends = this && this.__extends || function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf || {
  5. __proto__: []
  6. } instanceof Array && function (d, b) {
  7. d.__proto__ = b;
  8. } || function (d, b) {
  9. for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
  10. };
  11. return extendStatics(d, b);
  12. };
  13. return function (d, b) {
  14. if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  15. extendStatics(d, b);
  16. function __() {
  17. this.constructor = d;
  18. }
  19. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  20. };
  21. }();
  22. var __assign = this && this.__assign || function () {
  23. __assign = Object.assign || function (t) {
  24. for (var s, i = 1, n = arguments.length; i < n; i++) {
  25. s = arguments[i];
  26. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  27. }
  28. return t;
  29. };
  30. return __assign.apply(this, arguments);
  31. };
  32. Object.defineProperty(exports, "__esModule", {
  33. value: true
  34. });
  35. exports.cloneNode = exports.hasChildren = exports.isDocument = exports.isDirective = exports.isComment = exports.isText = exports.isCDATA = exports.isTag = exports.Element = exports.Document = exports.CDATA = exports.NodeWithChildren = exports.ProcessingInstruction = exports.Comment = exports.Text = exports.DataNode = exports.Node = void 0;
  36. var domelementtype_1 = require("../domelementtype/index.js");
  37. /**
  38. * This object will be used as the prototype for Nodes when creating a
  39. * DOM-Level-1-compliant structure.
  40. */
  41. var Node = /** @class */function () {
  42. function Node() {
  43. /** Parent of the node */
  44. this.parent = null;
  45. /** Previous sibling */
  46. this.prev = null;
  47. /** Next sibling */
  48. this.next = null;
  49. /** The start index of the node. Requires `withStartIndices` on the handler to be `true. */
  50. this.startIndex = null;
  51. /** The end index of the node. Requires `withEndIndices` on the handler to be `true. */
  52. this.endIndex = null;
  53. }
  54. Object.defineProperty(Node.prototype, "parentNode", {
  55. // Read-write aliases for properties
  56. /**
  57. * Same as {@link parent}.
  58. * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
  59. */
  60. get: function () {
  61. return this.parent;
  62. },
  63. set: function (parent) {
  64. this.parent = parent;
  65. },
  66. enumerable: false,
  67. configurable: true
  68. });
  69. Object.defineProperty(Node.prototype, "previousSibling", {
  70. /**
  71. * Same as {@link prev}.
  72. * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
  73. */
  74. get: function () {
  75. return this.prev;
  76. },
  77. set: function (prev) {
  78. this.prev = prev;
  79. },
  80. enumerable: false,
  81. configurable: true
  82. });
  83. Object.defineProperty(Node.prototype, "nextSibling", {
  84. /**
  85. * Same as {@link next}.
  86. * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
  87. */
  88. get: function () {
  89. return this.next;
  90. },
  91. set: function (next) {
  92. this.next = next;
  93. },
  94. enumerable: false,
  95. configurable: true
  96. });
  97. /**
  98. * Clone this node, and optionally its children.
  99. *
  100. * @param recursive Clone child nodes as well.
  101. * @returns A clone of the node.
  102. */
  103. Node.prototype.cloneNode = function (recursive) {
  104. if (recursive === void 0) {
  105. recursive = false;
  106. }
  107. return cloneNode(this, recursive);
  108. };
  109. return Node;
  110. }();
  111. exports.Node = Node;
  112. /**
  113. * A node that contains some data.
  114. */
  115. var DataNode = /** @class */function (_super) {
  116. __extends(DataNode, _super);
  117. /**
  118. * @param data The content of the data node
  119. */
  120. function DataNode(data) {
  121. var _this = _super.call(this) || this;
  122. _this.data = data;
  123. return _this;
  124. }
  125. Object.defineProperty(DataNode.prototype, "nodeValue", {
  126. /**
  127. * Same as {@link data}.
  128. * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
  129. */
  130. get: function () {
  131. return this.data;
  132. },
  133. set: function (data) {
  134. this.data = data;
  135. },
  136. enumerable: false,
  137. configurable: true
  138. });
  139. return DataNode;
  140. }(Node);
  141. exports.DataNode = DataNode;
  142. /**
  143. * Text within the document.
  144. */
  145. var Text = /** @class */function (_super) {
  146. __extends(Text, _super);
  147. function Text() {
  148. var _this = _super !== null && _super.apply(this, arguments) || this;
  149. _this.type = domelementtype_1.ElementType.Text;
  150. return _this;
  151. }
  152. Object.defineProperty(Text.prototype, "nodeType", {
  153. get: function () {
  154. return 3;
  155. },
  156. enumerable: false,
  157. configurable: true
  158. });
  159. return Text;
  160. }(DataNode);
  161. exports.Text = Text;
  162. /**
  163. * Comments within the document.
  164. */
  165. var Comment = /** @class */function (_super) {
  166. __extends(Comment, _super);
  167. function Comment() {
  168. var _this = _super !== null && _super.apply(this, arguments) || this;
  169. _this.type = domelementtype_1.ElementType.Comment;
  170. return _this;
  171. }
  172. Object.defineProperty(Comment.prototype, "nodeType", {
  173. get: function () {
  174. return 8;
  175. },
  176. enumerable: false,
  177. configurable: true
  178. });
  179. return Comment;
  180. }(DataNode);
  181. exports.Comment = Comment;
  182. /**
  183. * Processing instructions, including doc types.
  184. */
  185. var ProcessingInstruction = /** @class */function (_super) {
  186. __extends(ProcessingInstruction, _super);
  187. function ProcessingInstruction(name, data) {
  188. var _this = _super.call(this, data) || this;
  189. _this.name = name;
  190. _this.type = domelementtype_1.ElementType.Directive;
  191. return _this;
  192. }
  193. Object.defineProperty(ProcessingInstruction.prototype, "nodeType", {
  194. get: function () {
  195. return 1;
  196. },
  197. enumerable: false,
  198. configurable: true
  199. });
  200. return ProcessingInstruction;
  201. }(DataNode);
  202. exports.ProcessingInstruction = ProcessingInstruction;
  203. /**
  204. * A `Node` that can have children.
  205. */
  206. var NodeWithChildren = /** @class */function (_super) {
  207. __extends(NodeWithChildren, _super);
  208. /**
  209. * @param children Children of the node. Only certain node types can have children.
  210. */
  211. function NodeWithChildren(children) {
  212. var _this = _super.call(this) || this;
  213. _this.children = children;
  214. return _this;
  215. }
  216. Object.defineProperty(NodeWithChildren.prototype, "firstChild", {
  217. // Aliases
  218. /** First child of the node. */
  219. get: function () {
  220. var _a;
  221. return (_a = this.children[0]) !== null && _a !== void 0 ? _a : null;
  222. },
  223. enumerable: false,
  224. configurable: true
  225. });
  226. Object.defineProperty(NodeWithChildren.prototype, "lastChild", {
  227. /** Last child of the node. */
  228. get: function () {
  229. return this.children.length > 0 ? this.children[this.children.length - 1] : null;
  230. },
  231. enumerable: false,
  232. configurable: true
  233. });
  234. Object.defineProperty(NodeWithChildren.prototype, "childNodes", {
  235. /**
  236. * Same as {@link children}.
  237. * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
  238. */
  239. get: function () {
  240. return this.children;
  241. },
  242. set: function (children) {
  243. this.children = children;
  244. },
  245. enumerable: false,
  246. configurable: true
  247. });
  248. return NodeWithChildren;
  249. }(Node);
  250. exports.NodeWithChildren = NodeWithChildren;
  251. var CDATA = /** @class */function (_super) {
  252. __extends(CDATA, _super);
  253. function CDATA() {
  254. var _this = _super !== null && _super.apply(this, arguments) || this;
  255. _this.type = domelementtype_1.ElementType.CDATA;
  256. return _this;
  257. }
  258. Object.defineProperty(CDATA.prototype, "nodeType", {
  259. get: function () {
  260. return 4;
  261. },
  262. enumerable: false,
  263. configurable: true
  264. });
  265. return CDATA;
  266. }(NodeWithChildren);
  267. exports.CDATA = CDATA;
  268. /**
  269. * The root node of the document.
  270. */
  271. var Document = /** @class */function (_super) {
  272. __extends(Document, _super);
  273. function Document() {
  274. var _this = _super !== null && _super.apply(this, arguments) || this;
  275. _this.type = domelementtype_1.ElementType.Root;
  276. return _this;
  277. }
  278. Object.defineProperty(Document.prototype, "nodeType", {
  279. get: function () {
  280. return 9;
  281. },
  282. enumerable: false,
  283. configurable: true
  284. });
  285. return Document;
  286. }(NodeWithChildren);
  287. exports.Document = Document;
  288. /**
  289. * An element within the DOM.
  290. */
  291. var Element = /** @class */function (_super) {
  292. __extends(Element, _super);
  293. /**
  294. * @param name Name of the tag, eg. `div`, `span`.
  295. * @param attribs Object mapping attribute names to attribute values.
  296. * @param children Children of the node.
  297. */
  298. function Element(name, attribs, children, type) {
  299. if (children === void 0) {
  300. children = [];
  301. }
  302. if (type === void 0) {
  303. type = name === "script" ? domelementtype_1.ElementType.Script : name === "style" ? domelementtype_1.ElementType.Style : domelementtype_1.ElementType.Tag;
  304. }
  305. var _this = _super.call(this, children) || this;
  306. _this.name = name;
  307. _this.attribs = attribs;
  308. _this.type = type;
  309. return _this;
  310. }
  311. Object.defineProperty(Element.prototype, "nodeType", {
  312. get: function () {
  313. return 1;
  314. },
  315. enumerable: false,
  316. configurable: true
  317. });
  318. Object.defineProperty(Element.prototype, "tagName", {
  319. // DOM Level 1 aliases
  320. /**
  321. * Same as {@link name}.
  322. * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
  323. */
  324. get: function () {
  325. return this.name;
  326. },
  327. set: function (name) {
  328. this.name = name;
  329. },
  330. enumerable: false,
  331. configurable: true
  332. });
  333. Object.defineProperty(Element.prototype, "attributes", {
  334. get: function () {
  335. var _this = this;
  336. return Object.keys(this.attribs).map(function (name) {
  337. var _a, _b;
  338. return {
  339. name: name,
  340. value: _this.attribs[name],
  341. namespace: (_a = _this["x-attribsNamespace"]) === null || _a === void 0 ? void 0 : _a[name],
  342. prefix: (_b = _this["x-attribsPrefix"]) === null || _b === void 0 ? void 0 : _b[name]
  343. };
  344. });
  345. },
  346. enumerable: false,
  347. configurable: true
  348. });
  349. return Element;
  350. }(NodeWithChildren);
  351. exports.Element = Element;
  352. /**
  353. * @param node Node to check.
  354. * @returns `true` if the node is a `Element`, `false` otherwise.
  355. */
  356. function isTag(node) {
  357. return (0, domelementtype_1.isTag)(node);
  358. }
  359. exports.isTag = isTag;
  360. /**
  361. * @param node Node to check.
  362. * @returns `true` if the node has the type `CDATA`, `false` otherwise.
  363. */
  364. function isCDATA(node) {
  365. return node.type === domelementtype_1.ElementType.CDATA;
  366. }
  367. exports.isCDATA = isCDATA;
  368. /**
  369. * @param node Node to check.
  370. * @returns `true` if the node has the type `Text`, `false` otherwise.
  371. */
  372. function isText(node) {
  373. return node.type === domelementtype_1.ElementType.Text;
  374. }
  375. exports.isText = isText;
  376. /**
  377. * @param node Node to check.
  378. * @returns `true` if the node has the type `Comment`, `false` otherwise.
  379. */
  380. function isComment(node) {
  381. return node.type === domelementtype_1.ElementType.Comment;
  382. }
  383. exports.isComment = isComment;
  384. /**
  385. * @param node Node to check.
  386. * @returns `true` if the node has the type `ProcessingInstruction`, `false` otherwise.
  387. */
  388. function isDirective(node) {
  389. return node.type === domelementtype_1.ElementType.Directive;
  390. }
  391. exports.isDirective = isDirective;
  392. /**
  393. * @param node Node to check.
  394. * @returns `true` if the node has the type `ProcessingInstruction`, `false` otherwise.
  395. */
  396. function isDocument(node) {
  397. return node.type === domelementtype_1.ElementType.Root;
  398. }
  399. exports.isDocument = isDocument;
  400. /**
  401. * @param node Node to check.
  402. * @returns `true` if the node has children, `false` otherwise.
  403. */
  404. function hasChildren(node) {
  405. return Object.prototype.hasOwnProperty.call(node, "children");
  406. }
  407. exports.hasChildren = hasChildren;
  408. /**
  409. * Clone a node, and optionally its children.
  410. *
  411. * @param recursive Clone child nodes as well.
  412. * @returns A clone of the node.
  413. */
  414. function cloneNode(node, recursive) {
  415. if (recursive === void 0) {
  416. recursive = false;
  417. }
  418. var result;
  419. if (isText(node)) {
  420. result = new Text(node.data);
  421. } else if (isComment(node)) {
  422. result = new Comment(node.data);
  423. } else if (isTag(node)) {
  424. var children = recursive ? cloneChildren(node.children) : [];
  425. var clone_1 = new Element(node.name, __assign({}, node.attribs), children);
  426. children.forEach(function (child) {
  427. return child.parent = clone_1;
  428. });
  429. if (node.namespace != null) {
  430. clone_1.namespace = node.namespace;
  431. }
  432. if (node["x-attribsNamespace"]) {
  433. clone_1["x-attribsNamespace"] = __assign({}, node["x-attribsNamespace"]);
  434. }
  435. if (node["x-attribsPrefix"]) {
  436. clone_1["x-attribsPrefix"] = __assign({}, node["x-attribsPrefix"]);
  437. }
  438. result = clone_1;
  439. } else if (isCDATA(node)) {
  440. var children = recursive ? cloneChildren(node.children) : [];
  441. var clone_2 = new CDATA(children);
  442. children.forEach(function (child) {
  443. return child.parent = clone_2;
  444. });
  445. result = clone_2;
  446. } else if (isDocument(node)) {
  447. var children = recursive ? cloneChildren(node.children) : [];
  448. var clone_3 = new Document(children);
  449. children.forEach(function (child) {
  450. return child.parent = clone_3;
  451. });
  452. if (node["x-mode"]) {
  453. clone_3["x-mode"] = node["x-mode"];
  454. }
  455. result = clone_3;
  456. } else if (isDirective(node)) {
  457. var instruction = new ProcessingInstruction(node.name, node.data);
  458. if (node["x-name"] != null) {
  459. instruction["x-name"] = node["x-name"];
  460. instruction["x-publicId"] = node["x-publicId"];
  461. instruction["x-systemId"] = node["x-systemId"];
  462. }
  463. result = instruction;
  464. } else {
  465. throw new Error("Not implemented yet: ".concat(node.type));
  466. }
  467. result.startIndex = node.startIndex;
  468. result.endIndex = node.endIndex;
  469. if (node.sourceCodeLocation != null) {
  470. result.sourceCodeLocation = node.sourceCodeLocation;
  471. }
  472. return result;
  473. }
  474. exports.cloneNode = cloneNode;
  475. function cloneChildren(childs) {
  476. var children = childs.map(function (child) {
  477. return cloneNode(child, true);
  478. });
  479. for (var i = 1; i < children.length; i++) {
  480. children[i].prev = children[i - 1];
  481. children[i - 1].next = children[i];
  482. }
  483. return children;
  484. }