MCPcopy Index your code
hub / github.com/nodejs/node / element

Method element

deps/v8/tools/js/web-api-helper.mjs:183–211  ·  view source on GitHub ↗
(type, options)

Source from the content-addressed store, hash-verified

181
182export class DOM {
183 static element(type, options) {
184 const node = document.createElement(type);
185 if (options === undefined) return node;
186 if (typeof options === 'string') {
187 // Old behaviour: options = class string
188 node.className = options;
189 } else if (Array.isArray(options)) {
190 // Old behaviour: options = class array
191 DOM.addClasses(node, options);
192 } else {
193 // New behaviour: options = attribute dict
194 for (const [key, value] of Object.entries(options)) {
195 if (key == 'className') {
196 node.className = value;
197 } else if (key == 'classList') {
198 DOM.addClasses(node, value);
199 } else if (key == 'textContent') {
200 node.textContent = value;
201 } else if (key == 'children') {
202 for (const child of value) {
203 node.appendChild(child);
204 }
205 } else {
206 node.setAttribute(key, value);
207 }
208 }
209 }
210 return node;
211 }
212
213 static addClasses(node, classes) {
214 const classList = node.classList;

Callers 7

divMethod · 0.95
spanMethod · 0.95
tableMethod · 0.95
tbodyMethod · 0.95
tdMethod · 0.95
trMethod · 0.95
buttonMethod · 0.45

Calls 3

createElementMethod · 0.80
addClassesMethod · 0.80
entriesMethod · 0.45

Tested by

no test coverage detected