node.js 15 KB

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