source: trip-planner-front/node_modules/parse5/lib/tree-adapters/default.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.2 KB
Line 
1'use strict';
2
3const { DOCUMENT_MODE } = require('../common/html');
4
5//Node construction
6exports.createDocument = function() {
7 return {
8 nodeName: '#document',
9 mode: DOCUMENT_MODE.NO_QUIRKS,
10 childNodes: []
11 };
12};
13
14exports.createDocumentFragment = function() {
15 return {
16 nodeName: '#document-fragment',
17 childNodes: []
18 };
19};
20
21exports.createElement = function(tagName, namespaceURI, attrs) {
22 return {
23 nodeName: tagName,
24 tagName: tagName,
25 attrs: attrs,
26 namespaceURI: namespaceURI,
27 childNodes: [],
28 parentNode: null
29 };
30};
31
32exports.createCommentNode = function(data) {
33 return {
34 nodeName: '#comment',
35 data: data,
36 parentNode: null
37 };
38};
39
40const createTextNode = function(value) {
41 return {
42 nodeName: '#text',
43 value: value,
44 parentNode: null
45 };
46};
47
48//Tree mutation
49const appendChild = (exports.appendChild = function(parentNode, newNode) {
50 parentNode.childNodes.push(newNode);
51 newNode.parentNode = parentNode;
52});
53
54const insertBefore = (exports.insertBefore = function(parentNode, newNode, referenceNode) {
55 const insertionIdx = parentNode.childNodes.indexOf(referenceNode);
56
57 parentNode.childNodes.splice(insertionIdx, 0, newNode);
58 newNode.parentNode = parentNode;
59});
60
61exports.setTemplateContent = function(templateElement, contentElement) {
62 templateElement.content = contentElement;
63};
64
65exports.getTemplateContent = function(templateElement) {
66 return templateElement.content;
67};
68
69exports.setDocumentType = function(document, name, publicId, systemId) {
70 let doctypeNode = null;
71
72 for (let i = 0; i < document.childNodes.length; i++) {
73 if (document.childNodes[i].nodeName === '#documentType') {
74 doctypeNode = document.childNodes[i];
75 break;
76 }
77 }
78
79 if (doctypeNode) {
80 doctypeNode.name = name;
81 doctypeNode.publicId = publicId;
82 doctypeNode.systemId = systemId;
83 } else {
84 appendChild(document, {
85 nodeName: '#documentType',
86 name: name,
87 publicId: publicId,
88 systemId: systemId
89 });
90 }
91};
92
93exports.setDocumentMode = function(document, mode) {
94 document.mode = mode;
95};
96
97exports.getDocumentMode = function(document) {
98 return document.mode;
99};
100
101exports.detachNode = function(node) {
102 if (node.parentNode) {
103 const idx = node.parentNode.childNodes.indexOf(node);
104
105 node.parentNode.childNodes.splice(idx, 1);
106 node.parentNode = null;
107 }
108};
109
110exports.insertText = function(parentNode, text) {
111 if (parentNode.childNodes.length) {
112 const prevNode = parentNode.childNodes[parentNode.childNodes.length - 1];
113
114 if (prevNode.nodeName === '#text') {
115 prevNode.value += text;
116 return;
117 }
118 }
119
120 appendChild(parentNode, createTextNode(text));
121};
122
123exports.insertTextBefore = function(parentNode, text, referenceNode) {
124 const prevNode = parentNode.childNodes[parentNode.childNodes.indexOf(referenceNode) - 1];
125
126 if (prevNode && prevNode.nodeName === '#text') {
127 prevNode.value += text;
128 } else {
129 insertBefore(parentNode, createTextNode(text), referenceNode);
130 }
131};
132
133exports.adoptAttributes = function(recipient, attrs) {
134 const recipientAttrsMap = [];
135
136 for (let i = 0; i < recipient.attrs.length; i++) {
137 recipientAttrsMap.push(recipient.attrs[i].name);
138 }
139
140 for (let j = 0; j < attrs.length; j++) {
141 if (recipientAttrsMap.indexOf(attrs[j].name) === -1) {
142 recipient.attrs.push(attrs[j]);
143 }
144 }
145};
146
147//Tree traversing
148exports.getFirstChild = function(node) {
149 return node.childNodes[0];
150};
151
152exports.getChildNodes = function(node) {
153 return node.childNodes;
154};
155
156exports.getParentNode = function(node) {
157 return node.parentNode;
158};
159
160exports.getAttrList = function(element) {
161 return element.attrs;
162};
163
164//Node data
165exports.getTagName = function(element) {
166 return element.tagName;
167};
168
169exports.getNamespaceURI = function(element) {
170 return element.namespaceURI;
171};
172
173exports.getTextNodeContent = function(textNode) {
174 return textNode.value;
175};
176
177exports.getCommentNodeContent = function(commentNode) {
178 return commentNode.data;
179};
180
181exports.getDocumentTypeNodeName = function(doctypeNode) {
182 return doctypeNode.name;
183};
184
185exports.getDocumentTypeNodePublicId = function(doctypeNode) {
186 return doctypeNode.publicId;
187};
188
189exports.getDocumentTypeNodeSystemId = function(doctypeNode) {
190 return doctypeNode.systemId;
191};
192
193//Node types
194exports.isTextNode = function(node) {
195 return node.nodeName === '#text';
196};
197
198exports.isCommentNode = function(node) {
199 return node.nodeName === '#comment';
200};
201
202exports.isDocumentTypeNode = function(node) {
203 return node.nodeName === '#documentType';
204};
205
206exports.isElementNode = function(node) {
207 return !!node.tagName;
208};
209
210// Source code location
211exports.setNodeSourceCodeLocation = function(node, location) {
212 node.sourceCodeLocation = location;
213};
214
215exports.getNodeSourceCodeLocation = function(node) {
216 return node.sourceCodeLocation;
217};
218
219exports.updateNodeSourceCodeLocation = function(node, endLocation) {
220 node.sourceCodeLocation = Object.assign(node.sourceCodeLocation, endLocation);
221};
Note: See TracBrowser for help on using the repository browser.