Skip to content

Commit

Permalink
Deprecate container#remove and node#removeSelf. Fixes #191.
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-eb committed Aug 10, 2015
1 parent 5678acc commit cdae155
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 40 deletions.
20 changes: 10 additions & 10 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var remove = postcss.plugin('postcss-remove', function (opts) {
var filter = opts.prop || 'z-index';
return function (css, result) {
css.eachDecl(filter, function (decl) {
decl.removeSelf();
decl.remove();
});
};
});
Expand Down Expand Up @@ -1106,14 +1106,14 @@ Returns the `Root` instance of the node’s tree.
root.nodes[0].nodes[0].root() == root
```

### `node.removeSelf()`
### `node.remove()`

Removes the node from its parent, and cleans the `parent` property in the node
and its children.

```js
if ( decl.prop.match(/^-webkit-/) ) {
decl.removeSelf();
decl.remove();
}
```

Expand Down Expand Up @@ -1356,7 +1356,7 @@ calling `callback` for each.
```js
root.eachDecl(function (decl) {
if ( decl.prop.match(/^-webkit-/) ) {
decl.removeSelf();
decl.remove();
}
});
```
Expand All @@ -1373,7 +1373,7 @@ If you pass a `propFilter`, only those declarations whose property matches
```js
// Make flat design
root.eachDecl('border-radius', function (decl) {
decl.removeSelf();
decl.remove();
});
root.eachDecl(/^background/, function (decl) {
decl.value = takeFirstColorFromGradient(decl.value);
Expand All @@ -1391,7 +1391,7 @@ calling `callback` for each.

```js
root.eachAtRule(function (rule) {
if ( rule.name.match(/^-webkit-/) ) rule.removeSelf();
if ( rule.name.match(/^-webkit-/) ) rule.remove();
});
```

Expand All @@ -1410,7 +1410,7 @@ root.eachAtRule('charset', function (rule) {
if ( !first ) {
first = true;
} else {
rule.removeSelf();
rule.remove();
}
});
```
Expand Down Expand Up @@ -1450,7 +1450,7 @@ Recursively iterates through all comment nodes within the container, calling

```js
root.eachComment(function (comment) {
comment.removeSelf();
comment.remove();
});
```

Expand Down Expand Up @@ -1544,14 +1544,14 @@ Arguments:
* `oldNode (Node|number)`: child or child’s index.
* `node (Node|object|string)`: new node.

### `container.remove(node)`
### `container.removeChild(node)`

Removes `node` from the container, and the `parent` properties of `node`
and its children.

```js
rule.nodes.length //=> 5
rule.remove(decl);
rule.removeChild(decl);
rule.nodes.length //=> 4
decl.parent //=> undefined
```
Expand Down
19 changes: 15 additions & 4 deletions lib/container.es6
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ export default class Container extends Node {
}

remove(child) {
if (child) {
warnOnce('Container#remove is deprecated. ' +
'Use Container#removeChild');
this.removeChild(child);
} else {
super.remove();
}
return this;
}

removeChild(child) {
child = this.index(child);
this.nodes[child].parent = undefined;
this.nodes.splice(child, 1);
Expand Down Expand Up @@ -305,22 +316,22 @@ export default class Container extends Node {
}

get semicolon() {
warnOnce('Node#semicolon was deprecated. Use Node#raw.semicolon');
warnOnce('Node#semicolon is deprecated. Use Node#raw.semicolon');
return this.raw.semicolon;
}

set semicolon(val) {
warnOnce('Node#semicolon was deprecated. Use Node#raw.semicolon');
warnOnce('Node#semicolon is deprecated. Use Node#raw.semicolon');
this.raw.semicolon = val;
}

get after() {
warnOnce('Node#after was deprecated. Use Node#raw.after');
warnOnce('Node#after is deprecated. Use Node#raw.after');
return this.raw.after;
}

set after(val) {
warnOnce('Node#after was deprecated. Use Node#raw.after');
warnOnce('Node#after is deprecated. Use Node#raw.after');
this.raw.after = val;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/map-generator.es6
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default class {
node = this.root.nodes[i];
if ( node.type !== 'comment' ) continue;
if ( node.text.indexOf('# sourceMappingURL=') === 0 ) {
this.root.remove(i);
this.root.removeChild(i);
}
}
}
Expand Down
25 changes: 15 additions & 10 deletions lib/node.es6
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,20 @@ export default class Node {
}

removeSelf() {
warnOnce('Node#removeSelf is deprecated. Use Node#remove.');
this.remove();
}

remove() {
if ( this.parent ) {
this.parent.remove(this);
this.parent.removeChild(this);
}
this.parent = undefined;
return this;
}

replace(nodes) {
warnOnce('Node#replace was deprecated. Use Node#replaceWith');
warnOnce('Node#replace is deprecated. Use Node#replaceWith');
return this.replaceWith(nodes);
}

Expand Down Expand Up @@ -91,29 +96,29 @@ export default class Node {
this.parent.insertBefore(this, node);
}

this.removeSelf();
this.remove();
}

return this;
}

moveTo(container) {
this.cleanStyles(this.root() === container.root());
this.removeSelf();
this.remove();
container.append(this);
return this;
}

moveBefore(node) {
this.cleanStyles(this.root() === node.root());
this.removeSelf();
this.remove();
node.parent.insertBefore(node, this);
return this;
}

moveAfter(node) {
this.cleanStyles(this.root() === node.root());
this.removeSelf();
this.remove();
node.parent.insertAfter(node, this);
return this;
}
Expand Down Expand Up @@ -200,22 +205,22 @@ export default class Node {
}

get before() {
warnOnce('Node#before was deprecated. Use Node#raw.before');
warnOnce('Node#before is deprecated. Use Node#raw.before');
return this.raw.before;
}

set before(val) {
warnOnce('Node#before was deprecated. Use Node#raw.before');
warnOnce('Node#before is deprecated. Use Node#raw.before');
this.raw.before = val;
}

get between() {
warnOnce('Node#between was deprecated. Use Node#raw.between');
warnOnce('Node#between is deprecated. Use Node#raw.between');
return this.raw.between;
}

set between(val) {
warnOnce('Node#between was deprecated. Use Node#raw.between');
warnOnce('Node#between is deprecated. Use Node#raw.between');
this.raw.between = val;
}

Expand Down
8 changes: 7 additions & 1 deletion lib/root.es6
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Container from './container';
import warnOnce from './warn-once';

export default class Root extends Container {

Expand All @@ -9,13 +10,18 @@ export default class Root extends Container {
}

remove(child) {
warnOnce('Root#remove is deprecated. Use Root#removeChild');
this.removeChild(child);
}

removeChild(child) {
child = this.index(child);

if ( child === 0 && this.nodes.length > 1 ) {
this.nodes[1].raw.before = this.nodes[child].raw.before;
}

return super.remove(child);
return super.removeChild(child);
}

normalize(child, sample, type) {
Expand Down
4 changes: 2 additions & 2 deletions lib/rule.es6
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ export default class Rule extends Container {
}

get _selector() {
warnOnce('Rule#_selector was deprecated. Use Rule#raw.selector');
warnOnce('Rule#_selector is deprecated. Use Rule#raw.selector');
return this.raw.selector;
}

set _selector(val) {
warnOnce('Rule#_selector was deprecated. Use Rule#raw.selector');
warnOnce('Rule#_selector is deprecated. Use Rule#raw.selector');
this.raw.selector = val;
}

Expand Down
16 changes: 8 additions & 8 deletions test/container.es6
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ describe('Container', () => {
it('iterates with changes', () => {
let size = 0;
parse(example).eachDecl( (decl, i) => {
decl.parent.remove(i);
decl.parent.removeChild(i);
size += 1;
});
expect(size).to.eql(5);
Expand Down Expand Up @@ -280,7 +280,7 @@ describe('Container', () => {
it('iterates with changes', () => {
let size = 0;
parse(example).eachComment( (comment, i) => {
comment.parent.remove(i);
comment.parent.removeChild(i);
size += 1;
});
expect(size).to.eql(3);
Expand Down Expand Up @@ -319,7 +319,7 @@ describe('Container', () => {
it('iterates with changes', () => {
let size = 0;
parse(example).eachRule( (rule, i) => {
rule.parent.remove(i);
rule.parent.removeChild(i);
size += 1;
});
expect(size).to.eql(3);
Expand Down Expand Up @@ -358,7 +358,7 @@ describe('Container', () => {
it('iterates with changes', () => {
let size = 0;
parse(example).eachAtRule( (atrule, i) => {
atrule.parent.remove(i);
atrule.parent.removeChild(i);
size += 1;
});
expect(size).to.eql(3);
Expand Down Expand Up @@ -590,24 +590,24 @@ describe('Container', () => {

});

describe('remove()', () => {
describe('removeChild()', () => {

it('removes by index', () => {
let rule = parse('a { a: 1; b: 2 }').first;
rule.remove(1);
rule.removeChild(1);
expect(rule.toString()).to.eql('a { a: 1 }');
});

it('removes by node', () => {
let rule = parse('a { a: 1; b: 2 }').first;
rule.remove(rule.last);
rule.removeChild(rule.last);
expect(rule.toString()).to.eql('a { a: 1 }');
});

it('cleans parent in removed node', () => {
let rule = parse('a { a: 1; b: 2 }').first;
let decl = rule.first;
rule.remove(decl);
rule.removeChild(decl);
expect(decl.parent).to.not.exist;
});

Expand Down
4 changes: 2 additions & 2 deletions test/node.es6
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ describe('Node', () => {

});

describe('removeSelf()', () => {
describe('remove()', () => {

it('removes node from parent', () => {
let rule = new Rule({ selector: 'a' });
let decl = new Declaration({ prop: 'color', value: 'black' });
rule.append(decl);

decl.removeSelf();
decl.remove();
expect(rule.nodes).to.be.empty;
expect(decl.parent).to.not.exist;
});
Expand Down
2 changes: 1 addition & 1 deletion test/postcss.es6
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('postcss()', () => {
let plugin = postcss.plugin('test', (filter) => {
return function (css) {
css.eachDecl(filter || 'two', function (decl) {
decl.removeSelf();
decl.remove();
});
};
});
Expand Down
2 changes: 1 addition & 1 deletion test/root.es6
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('Root', () => {

it('fixes spaces on removing first rule', () => {
let css = parse('a{}\nb{}\n');
css.first.removeSelf();
css.first.remove();
expect(css.toString()).to.eql('b{}\n');
});

Expand Down

0 comments on commit cdae155

Please sign in to comment.