node.js 13 KB

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