Skip to content

Commit b80b11a

Browse files
committed
feat: insert(Before|After) support multiple new node
1 parent 56d7fea commit b80b11a

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

API.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ Arguments:
531531

532532
* `node`: The node to add.
533533

534-
### `container.insertBefore(old, new)` & `container.insertAfter(old, new)`
534+
### `container.insertBefore(old, new[, ...newNodes])` & `container.insertAfter(old, new[, ...newNodes])`
535535

536536
Add a node before or after an existing node in a container:
537537

postcss-selector-parser.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ declare namespace parser {
235235
removeChild(child: Child): this;
236236
removeAll(): this;
237237
empty(): this;
238-
insertAfter(oldNode: Child, newNode: Child): this;
239-
insertBefore(oldNode: Child, newNode: Child): this;
238+
insertAfter(oldNode: Child, ...newNode: Child[]): this;
239+
insertBefore(oldNode: Child, ...newNode: Child[]): this;
240240
each(callback: (node: Child, index: number) => boolean | void): boolean | undefined;
241241
walk(
242242
callback: (node: Node, index: number) => boolean | void

src/__tests__/container.mjs

+20
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,16 @@ test('container#insertBefore', (t) => {
349349
t.deepEqual(out, 'h1,h2');
350350
});
351351

352+
test('container#insertBefore (multiple node)', (t) => {
353+
let out = parse('h2', (selectors) => {
354+
let selector = selectors.first;
355+
let clone1 = selector.first.clone({value: 'h1'});
356+
let clone2 = selector.first.clone({value: 'h0'});
357+
selectors.insertBefore(selector, clone1, clone2);
358+
});
359+
t.deepEqual(out, 'h1,h0,h2');
360+
});
361+
352362
test('container#insertBefore and node#remove', (t) => {
353363
let out = parse('h2', (selectors) => {
354364
let selector = selectors.first;
@@ -368,6 +378,16 @@ test('container#insertAfter', (t) => {
368378
t.deepEqual(out, 'h1,h2');
369379
});
370380

381+
test('container#insertAfter (multiple node)', (t) => {
382+
let out = parse('h1', (selectors) => {
383+
let selector = selectors.first;
384+
let clone1 = selector.first.clone({value: 'h2'});
385+
let clone2 = selector.first.clone({value: 'h3'});
386+
selectors.insertAfter(selector, clone1, clone2);
387+
});
388+
t.deepEqual(out, 'h1,h2,h3');
389+
})
390+
371391
test('container#insertAfter and node#remove', (t) => {
372392
let out = parse('h2', (selectors) => {
373393
let selector = selectors.first;

src/selectors/container.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,19 @@ export default class Container extends Node {
7878
insertAfter (oldNode, newNode) {
7979
newNode.parent = this;
8080
let oldIndex = this.index(oldNode);
81-
this.nodes.splice(oldIndex + 1, 0, newNode);
81+
const resetNode = [];
82+
for (let i = 2; i < arguments.length; i++) {
83+
resetNode.push(arguments[i]);
84+
}
85+
this.nodes.splice(oldIndex + 1, 0, newNode, ...resetNode);
8286

8387
newNode.parent = this;
8488

8589
let index;
8690
for ( let id in this.indexes ) {
8791
index = this.indexes[id];
8892
if ( oldIndex < index ) {
89-
this.indexes[id] = index + 1;
93+
this.indexes[id] = index + arguments.length - 1;
9094
}
9195
}
9296

@@ -96,15 +100,19 @@ export default class Container extends Node {
96100
insertBefore (oldNode, newNode) {
97101
newNode.parent = this;
98102
let oldIndex = this.index(oldNode);
99-
this.nodes.splice(oldIndex, 0, newNode);
103+
const resetNode = [];
104+
for (let i = 2; i < arguments.length; i++) {
105+
resetNode.push(arguments[i]);
106+
}
107+
this.nodes.splice(oldIndex, 0, newNode, ...resetNode);
100108

101109
newNode.parent = this;
102110

103111
let index;
104112
for ( let id in this.indexes ) {
105113
index = this.indexes[id];
106114
if ( index >= oldIndex ) {
107-
this.indexes[id] = index + 1;
115+
this.indexes[id] = index + arguments.length - 1;
108116
}
109117
}
110118

@@ -132,7 +140,7 @@ export default class Container extends Node {
132140
* Return the most specific node at the line and column number given.
133141
* The source location is based on the original parsed location, locations aren't
134142
* updated as selector nodes are mutated.
135-
*
143+
*
136144
* Note that this location is relative to the location of the first character
137145
* of the selector, and not the location of the selector in the overall document
138146
* when used in conjunction with postcss.

0 commit comments

Comments
 (0)