source: trip-planner-front/node_modules/domutils/lib/manipulation.js@ b738035

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

initial commit

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[6a3a178]1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.prepend = exports.prependChild = exports.append = exports.appendChild = exports.replaceElement = exports.removeElement = void 0;
4/**
5 * Remove an element from the dom
6 *
7 * @param elem The element to be removed
8 */
9function removeElement(elem) {
10 if (elem.prev)
11 elem.prev.next = elem.next;
12 if (elem.next)
13 elem.next.prev = elem.prev;
14 if (elem.parent) {
15 var childs = elem.parent.children;
16 childs.splice(childs.lastIndexOf(elem), 1);
17 }
18}
19exports.removeElement = removeElement;
20/**
21 * Replace an element in the dom
22 *
23 * @param elem The element to be replaced
24 * @param replacement The element to be added
25 */
26function replaceElement(elem, replacement) {
27 var prev = (replacement.prev = elem.prev);
28 if (prev) {
29 prev.next = replacement;
30 }
31 var next = (replacement.next = elem.next);
32 if (next) {
33 next.prev = replacement;
34 }
35 var parent = (replacement.parent = elem.parent);
36 if (parent) {
37 var childs = parent.children;
38 childs[childs.lastIndexOf(elem)] = replacement;
39 }
40}
41exports.replaceElement = replaceElement;
42/**
43 * Append a child to an element.
44 *
45 * @param elem The element to append to.
46 * @param child The element to be added as a child.
47 */
48function appendChild(elem, child) {
49 removeElement(child);
50 child.next = null;
51 child.parent = elem;
52 if (elem.children.push(child) > 1) {
53 var sibling = elem.children[elem.children.length - 2];
54 sibling.next = child;
55 child.prev = sibling;
56 }
57 else {
58 child.prev = null;
59 }
60}
61exports.appendChild = appendChild;
62/**
63 * Append an element after another.
64 *
65 * @param elem The element to append after.
66 * @param next The element be added.
67 */
68function append(elem, next) {
69 removeElement(next);
70 var parent = elem.parent;
71 var currNext = elem.next;
72 next.next = currNext;
73 next.prev = elem;
74 elem.next = next;
75 next.parent = parent;
76 if (currNext) {
77 currNext.prev = next;
78 if (parent) {
79 var childs = parent.children;
80 childs.splice(childs.lastIndexOf(currNext), 0, next);
81 }
82 }
83 else if (parent) {
84 parent.children.push(next);
85 }
86}
87exports.append = append;
88/**
89 * Prepend a child to an element.
90 *
91 * @param elem The element to prepend before.
92 * @param child The element to be added as a child.
93 */
94function prependChild(elem, child) {
95 removeElement(child);
96 child.parent = elem;
97 child.prev = null;
98 if (elem.children.unshift(child) !== 1) {
99 var sibling = elem.children[1];
100 sibling.prev = child;
101 child.next = sibling;
102 }
103 else {
104 child.next = null;
105 }
106}
107exports.prependChild = prependChild;
108/**
109 * Prepend an element before another.
110 *
111 * @param elem The element to prepend before.
112 * @param prev The element be added.
113 */
114function prepend(elem, prev) {
115 removeElement(prev);
116 var parent = elem.parent;
117 if (parent) {
118 var childs = parent.children;
119 childs.splice(childs.indexOf(elem), 0, prev);
120 }
121 if (elem.prev) {
122 elem.prev.next = prev;
123 }
124 prev.parent = parent;
125 prev.prev = elem.prev;
126 prev.next = elem;
127 elem.prev = prev;
128}
129exports.prepend = prepend;
Note: See TracBrowser for help on using the repository browser.