diff --git a/API.md b/API.md index c8e55ee..e564830 100644 --- a/API.md +++ b/API.md @@ -254,7 +254,7 @@ if (next && next.type !== 'combinator') { } ``` -### `node.replaceWith(node)` +### `node.replaceWith(node[,...nodeN])` Replace a node with another. @@ -267,6 +267,8 @@ attr.replaceWith(className); Arguments: * `node`: The node to substitute the original with. +... +* `nodeN`: The node to substitute the original with. ### `node.remove()` @@ -531,7 +533,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: diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c0d1fb..9a8956a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 7.1.0 + +- feat: insert(Before|After) support multiple new node + # 7.0.0 - Feat: make insertions during iteration safe (major) diff --git a/package.json b/package.json index 0483942..f8b1d36 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "postcss-selector-parser", - "version": "7.0.0", + "version": "7.1.0", "devDependencies": { "@babel/cli": "^7.11.6", "@babel/core": "^7.11.6", diff --git a/postcss-selector-parser.d.ts b/postcss-selector-parser.d.ts index af609e6..4efbc10 100644 --- a/postcss-selector-parser.d.ts +++ b/postcss-selector-parser.d.ts @@ -86,12 +86,12 @@ declare namespace parser { interface Options { /** - * Preserve whitespace when true. Default: false; + * Preserve whitespace when true. Default: true; */ lossless: boolean; /** * When true and a postcss.Rule is passed, set the result of - * processing back onto the rule when done. Default: false. + * processing back onto the rule when done. Default: true. */ updateSelector: boolean; } @@ -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, ...restNode: Child[]): this; + insertBefore(oldNode: Child, newNode: Child, ...restNode: Child[]): this; each(callback: (node: Child, index: number) => boolean | void): boolean | undefined; walk( callback: (node: Node, index: number) => boolean | void diff --git a/src/__tests__/container.mjs b/src/__tests__/container.mjs index e852aac..e435ed7 100644 --- a/src/__tests__/container.mjs +++ b/src/__tests__/container.mjs @@ -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; @@ -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; diff --git a/src/selectors/container.js b/src/selectors/container.js index 2575dde..ee347b4 100644 --- a/src/selectors/container.js +++ b/src/selectors/container.js @@ -78,7 +78,11 @@ 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; @@ -86,7 +90,7 @@ export default class Container extends Node { 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; } } @@ -96,7 +100,11 @@ 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; @@ -104,7 +112,7 @@ export default class Container extends Node { 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; } } @@ -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. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy