Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: insert(Before|After) support multiple new node #302

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ Arguments:

* `node`: The node to add.

### `container.insertBefore(old, new)` & `container.insertAfter(old, new)`
### `container.insertBefore(old, new[, ...newNodes])` & `container.insertAfter(old, new[, ...newNodes])`

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

Expand Down
4 changes: 2 additions & 2 deletions postcss-selector-parser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ declare namespace parser {
removeChild(child: Child): this;
removeAll(): this;
empty(): this;
insertAfter(oldNode: Child, newNode: Child): this;
insertBefore(oldNode: Child, newNode: Child): this;
insertAfter(oldNode: Child, ...newNode: Child[]): this;
insertBefore(oldNode: Child, ...newNode: Child[]): this;
each(callback: (node: Child, index: number) => boolean | void): boolean | undefined;
walk(
callback: (node: Node, index: number) => boolean | void
Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/container.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,16 @@ test('container#insertBefore', (t) => {
t.deepEqual(out, 'h1,h2');
});

test('container#insertBefore (multiple node)', (t) => {
let out = parse('h2', (selectors) => {
let selector = selectors.first;
let clone1 = selector.first.clone({value: 'h1'});
let clone2 = selector.first.clone({value: 'h0'});
selectors.insertBefore(selector, clone1, clone2);
});
t.deepEqual(out, 'h1,h0,h2');
});

test('container#insertBefore and node#remove', (t) => {
let out = parse('h2', (selectors) => {
let selector = selectors.first;
Expand All @@ -368,6 +378,16 @@ test('container#insertAfter', (t) => {
t.deepEqual(out, 'h1,h2');
});

test('container#insertAfter (multiple node)', (t) => {
let out = parse('h1', (selectors) => {
let selector = selectors.first;
let clone1 = selector.first.clone({value: 'h2'});
let clone2 = selector.first.clone({value: 'h3'});
selectors.insertAfter(selector, clone1, clone2);
});
t.deepEqual(out, 'h1,h2,h3');
})

test('container#insertAfter and node#remove', (t) => {
let out = parse('h2', (selectors) => {
let selector = selectors.first;
Expand Down
18 changes: 13 additions & 5 deletions src/selectors/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,19 @@ export default class Container extends Node {
insertAfter (oldNode, newNode) {
newNode.parent = this;
let oldIndex = this.index(oldNode);
this.nodes.splice(oldIndex + 1, 0, newNode);
const resetNode = [];
for (let i = 2; i < arguments.length; i++) {
resetNode.push(arguments[i]);
}
this.nodes.splice(oldIndex + 1, 0, newNode, ...resetNode);

newNode.parent = this;

let index;
for ( let id in this.indexes ) {
index = this.indexes[id];
if ( oldIndex < index ) {
this.indexes[id] = index + 1;
this.indexes[id] = index + arguments.length - 1;
}
}

Expand All @@ -96,15 +100,19 @@ export default class Container extends Node {
insertBefore (oldNode, newNode) {
newNode.parent = this;
let oldIndex = this.index(oldNode);
this.nodes.splice(oldIndex, 0, newNode);
const resetNode = [];
for (let i = 2; i < arguments.length; i++) {
resetNode.push(arguments[i]);
}
this.nodes.splice(oldIndex, 0, newNode, ...resetNode);

newNode.parent = this;

let index;
for ( let id in this.indexes ) {
index = this.indexes[id];
if ( index >= oldIndex ) {
this.indexes[id] = index + 1;
this.indexes[id] = index + arguments.length - 1;
}
}

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