Skip to content

Commit

Permalink
Rename each & remove methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-eb committed Mar 3, 2016
1 parent cd26dfe commit 7dfed2e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 68 deletions.
32 changes: 16 additions & 16 deletions API.md
Expand Up @@ -227,13 +227,13 @@ Arguments:

* `node`: The node to substitute the original with.

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

Removes the node from its parent node.

```js
if (node.type === 'id') {
node.removeSelf();
node.remove();
}
```

Expand Down Expand Up @@ -400,13 +400,13 @@ Arguments:
* `callback (function)`: A function to call for each node, which receives `node`
and `index` arguments.

### `container.eachInside(callback)`
### `container.walk(callback)`

Like `container#each`, but will also iterate child nodes as long as they are
`container` types.

```js
selectors.eachInside(function (selector, index) {
selectors.walk(function (selector, index) {
// all nodes
});
```
Expand All @@ -419,20 +419,20 @@ Arguments:
This iterator is safe to use whilst mutating `container.nodes`,
like `container#each`.

### `container.eachInside` proxies
### `container.walk` proxies

The container class provides proxy methods for iterating over types of nodes,
so that it is easier to write modules that target specific selectors. Those
methods are:

* `container.eachAttribute`
* `container.eachClass`
* `container.eachCombinator`
* `container.eachComment`
* `container.eachId`
* `container.eachPseudo`
* `container.eachTag`
* `container.eachUniversal`
* `container.walkAttributes`
* `container.walkClasses`
* `container.walkCombinators`
* `container.walkComments`
* `container.walkIds`
* `container.walkPseudos`
* `container.walkTags`
* `container.walkUniversals`

### `container.split(callback)`

Expand Down Expand Up @@ -486,10 +486,10 @@ Arguments:
* `old`: The existing node in the container.
* `new`: The new node to add before/after the existing node.

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

Remove the node from the container. Note that you can also use
`node.removeSelf()` if you would like to remove just a single node.
`node.remove()` if you would like to remove just a single node.

```js
selector.length // => 2
Expand Down Expand Up @@ -532,5 +532,5 @@ It has no special functionality of its own.

A pseudo selector extends a container node; if it has any parameters of its
own (such as `h1:not(h2, h3)`), they will be its children. Note that the pseudo
`value` will always contain the colons preceeding the pseudo identifier. This
`value` will always contain the colons preceding the pseudo identifier. This
is so that both `:before` and `::before` are properly represented in the AST.
58 changes: 29 additions & 29 deletions src/__tests__/container.js
Expand Up @@ -14,10 +14,10 @@ test('container#each', (t) => {
t.same(str, 'h1h2');
});

test('container#eachInside', (t) => {
test('container#walk', (t) => {
let str = '';
parse('h1, h2:not(h3, h4)', (selectors) => {
selectors.eachInside((selector) => {
selectors.walk((selector) => {
if (selector.type === 'tag') {
str += selector.value;
}
Expand All @@ -26,87 +26,87 @@ test('container#eachInside', (t) => {
t.same(str, 'h1h2h3h4');
});

test('container#eachInside (safe iteration)', (t) => {
test('container#walk (safe iteration)', (t) => {
let out = parse('[class] + *[href] *:not(*.green)', (selectors) => {
selectors.eachUniversal((selector) => {
selectors.walkUniversals((selector) => {
let next = selector.next();
if (next && next.type !== 'combinator') {
selector.removeSelf();
selector.remove();
}
});
});
t.same(out, '[class] + [href] :not(.green)');
});

test('container#eachAttribute', (t) => {
test('container#walkAttribute', (t) => {
let out = parse('[href][class].class', (selectors) => {
selectors.eachAttribute((attr) => {
selectors.walkAttributes((attr) => {
if (attr.attribute === 'class') {
attr.removeSelf();
attr.remove();
}
});
});
t.same(out, '[href].class');
});

test('container#eachClass', (t) => {
test('container#walkClass', (t) => {
let out = parse('.one, .two, .three:not(.four, .five)', (selectors) => {
selectors.eachClass((className) => {
selectors.walkClasses((className) => {
className.value = className.value.slice(0, 1);
});
});
t.same(out, '.o, .t, .t:not(.f, .f)');
});

test('container#eachCombinator', (t) => {
test('container#walkCombinator', (t) => {
let out = parse('h1 h2 h3 h4', (selectors) => {
selectors.eachCombinator((comment) => {
comment.removeSelf();
selectors.walkCombinators((comment) => {
comment.remove();
});
});
t.same(out, 'h1h2h3h4');
});

test('container#eachComment', (t) => {
test('container#walkComment', (t) => {
let out = parse('.one/*test*/.two', (selectors) => {
selectors.eachComment((comment) => {
comment.removeSelf();
selectors.walkComments((comment) => {
comment.remove();
});
});
t.same(out, '.one.two');
});

test('container#eachId', (t) => {
test('container#walkId', (t) => {
let out = parse('h1#one, h2#two', (selectors) => {
selectors.eachId((id) => {
selectors.walkIds((id) => {
id.value = id.value.slice(0, 1);
});
});
t.same(out, 'h1#o, h2#t');
});

test('container#eachPseudo', (t) => {
test('container#walkPseudo', (t) => {
let out = parse('a:before, a:after', (selectors) => {
selectors.eachPseudo((pseudo) => {
selectors.walkPseudos((pseudo) => {
pseudo.value = pseudo.value.slice(0, 2);
});
});
t.same(out, 'a:b, a:a');
});

test('container#eachTag', (t) => {
test('container#walkTag', (t) => {
let out = parse('1 2 3', (selectors) => {
selectors.eachTag((tag) => {
selectors.walkTags((tag) => {
tag.value = 'h' + tag.value;
});
});
t.same(out, 'h1 h2 h3');
});

test('container#eachUniversal', (t) => {
test('container#walkUniversal', (t) => {
let out = parse('*.class,*.class,*.class', (selectors) => {
selectors.eachUniversal((universal) => {
universal.removeSelf();
selectors.walkUniversals((universal) => {
universal.remove();
});
});
t.same(out, '.class,.class,.class');
Expand Down Expand Up @@ -210,11 +210,11 @@ test('container#length', (t) => {
});
});

test('container#remove', (t) => {
test('container#removeChild', (t) => {
let out = parse('h1.class h2.class h3.class', (selectors) => {
selectors.eachInside((selector) => {
selectors.walk((selector) => {
if (selector.type === 'class') {
selector.parent.remove(selector);
selector.parent.removeChild(selector);
}
});
});
Expand Down Expand Up @@ -251,7 +251,7 @@ test('container#insertAfter', (t) => {

test('container#insertAfter (during iteration)', (t) => {
let out = parse('h1, h2, h3', (selectors) => {
selectors.eachTag(selector => {
selectors.walkTags(selector => {
let attribute = parser.attribute({attribute: 'class'});
selector.parent.insertAfter(selector, attribute);
});
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/parser.js
Expand Up @@ -61,7 +61,7 @@ test('no operation', (t) => {
test('empty selector string', (t) => {
t.notThrows(() => {
return parser((selectors) => {
selectors.eachInside((selector) => {
selectors.walk((selector) => {
selector.type = 'tag';
});
}).process('').result;
Expand Down
38 changes: 19 additions & 19 deletions src/selectors/container.js
Expand Up @@ -43,7 +43,7 @@ export default class Container extends Node {
return this.nodes.length;
}

remove (child) {
removeChild (child) {
child = this.index(child);
this.at(child).parent = undefined;
this.nodes.splice(child, 1);
Expand Down Expand Up @@ -135,12 +135,12 @@ export default class Container extends Node {
}
}

eachInside (callback) {
walk (callback) {
return this.each((node, i) => {
let result = callback(node, i);

if (result !== false && node.length) {
result = node.eachInside(callback);
result = node.walk(callback);
}

if (result === false) {
Expand All @@ -149,64 +149,64 @@ export default class Container extends Node {
});
}

eachAttribute (callback) {
return this.eachInside((selector) => {
walkAttributes (callback) {
return this.walk((selector) => {
if (selector.type === 'attribute') {
return callback.call(this, selector);
}
});
}

eachClass (callback) {
return this.eachInside((selector) => {
walkClasses (callback) {
return this.walk((selector) => {
if (selector.type === 'class') {
return callback.call(this, selector);
}
});
}

eachCombinator (callback) {
return this.eachInside((selector) => {
walkCombinators (callback) {
return this.walk((selector) => {
if (selector.type === 'combinator') {
return callback.call(this, selector);
}
});
}

eachComment (callback) {
return this.eachInside((selector) => {
walkComments (callback) {
return this.walk((selector) => {
if (selector.type === 'comment') {
return callback.call(this, selector);
}
});
}

eachId (callback) {
return this.eachInside((selector) => {
walkIds (callback) {
return this.walk((selector) => {
if (selector.type === 'id') {
return callback.call(this, selector);
}
});
}

eachPseudo (callback) {
return this.eachInside((selector) => {
walkPseudos (callback) {
return this.walk((selector) => {
if (selector.type === 'pseudo') {
return callback.call(this, selector);
}
});
}

eachTag (callback) {
return this.eachInside((selector) => {
walkTags (callback) {
return this.walk((selector) => {
if (selector.type === 'tag') {
return callback.call(this, selector);
}
});
}

eachUniversal (callback) {
return this.eachInside((selector) => {
walkUniversals (callback) {
return this.walk((selector) => {
if (selector.type === 'universal') {
return callback.call(this, selector);
}
Expand Down
6 changes: 3 additions & 3 deletions src/selectors/node.js
Expand Up @@ -34,9 +34,9 @@ export default class {
this.spaces = {before: '', after: ''};
}

removeSelf () {
remove () {
if (this.parent) {
this.parent.remove(this);
this.parent.removeChild(this);
}
this.parent = undefined;
return this;
Expand All @@ -47,7 +47,7 @@ export default class {
for (let index in arguments) {
this.parent.insertBefore(this, arguments[index]);
}
this.removeSelf();
this.remove();
}
return this;
}
Expand Down

0 comments on commit 7dfed2e

Please sign in to comment.