Skip to content

Commit

Permalink
#110 remove redundant Element class methods and code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed Sep 24, 2022
1 parent 56ae9e4 commit e462631
Show file tree
Hide file tree
Showing 57 changed files with 520 additions and 645 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,12 +412,12 @@ Example using ast

```php

use TBela\CSS\Ast\Traverser;
use TBela\CSS\Ast\NodeWalker;
use TBela\CSS\Parser;
use TBela\CSS\Renderer;

$parser = (new Parser())->load('ast/media.css');
$traverser = new Traverser();
$traverser = new NodeWalker();
$renderer = new Renderer(['remove_empty_nodes' => true]);

$ast = $parser->getAst();
Expand All @@ -427,7 +427,7 @@ $traverser->on('enter', function ($node) {

if ($node->type == 'AtRule' && $node->name == 'media' && $node->value == 'print') {

return Traverser::IGNORE_NODE;
return NodeWalker::IGNORE_NODE;
}
});

Expand All @@ -439,12 +439,12 @@ Example using an Element instance

```php

use TBela\CSS\Ast\Traverser;
use TBela\CSS\Ast\NodeWalker;
use TBela\CSS\Parser;
use TBela\CSS\Renderer;

$parser = (new Parser())->load('ast/media.css');
$traverser = new Traverser();
$traverser = new NodeWalker();
$renderer = new Renderer(['remove_empty_nodes' => true]);

$element = $parser->parse();
Expand All @@ -454,7 +454,7 @@ $traverser->on('enter', function ($node) {

if ($node->type == 'AtRule' && $node->name == 'media' && $node->value == 'print') {

return Traverser::IGNORE_NODE;
return NodeWalker::IGNORE_NODE;
}
});

Expand Down
12 changes: 6 additions & 6 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,12 +412,12 @@ Example using ast

```php

use TBela\CSS\Ast\Traverser;
use TBela\CSS\Ast\NodeWalker;
use TBela\CSS\Parser;
use TBela\CSS\Renderer;

$parser = (new Parser())->load('ast/media.css');
$traverser = new Traverser();
$traverser = new NodeWalker();
$renderer = new Renderer(['remove_empty_nodes' => true]);

$ast = $parser->getAst();
Expand All @@ -427,7 +427,7 @@ $traverser->on('enter', function ($node) {

if ($node->type == 'AtRule' && $node->name == 'media' && $node->value == 'print') {

return Traverser::IGNORE_NODE;
return NodeWalker::IGNORE_NODE;
}
});

Expand All @@ -439,12 +439,12 @@ Example using an Element instance

```php

use TBela\CSS\Ast\Traverser;
use TBela\CSS\Ast\NodeWalker;
use TBela\CSS\Parser;
use TBela\CSS\Renderer;

$parser = (new Parser())->load('ast/media.css');
$traverser = new Traverser();
$traverser = new NodeWalker();
$renderer = new Renderer(['remove_empty_nodes' => true]);

$element = $parser->parse();
Expand All @@ -454,7 +454,7 @@ $traverser->on('enter', function ($node) {

if ($node->type == 'AtRule' && $node->name == 'media' && $node->value == 'print') {

return Traverser::IGNORE_NODE;
return NodeWalker::IGNORE_NODE;
}
});

Expand Down
12 changes: 6 additions & 6 deletions docs/traverser.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ add a callback for the specified event

```php

use TBela\CSS\Ast\Traverser;
use TBela\CSS\Ast\NodeWalker;

$traverser = new Traverser();
$traverser = new NodeWalker();
$callable = function ($node) {

// remove @media print {}
if ($node->type == 'AtRule' && $node->value == 'print') {

return Traverser::IGNORE_NODE;
return NodeWalker::IGNORE_NODE;
}
};

Expand All @@ -52,15 +52,15 @@ remove a callback for the specified event

```php

use TBela\CSS\Ast\Traverser;
use TBela\CSS\Ast\NodeWalker;

$traverser = new Traverser();
$traverser = new NodeWalker();
$callable = function ($node) {

// remove @media print {}
if ($node->type == 'AtRule' && $node->value == 'print') {

return Traverser::IGNORE_NODE;
return NodeWalker::IGNORE_NODE;
}
};

Expand Down
54 changes: 54 additions & 0 deletions src/Ast/NodeWalker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace TBela\CSS\Ast;

use IteratorAggregate;
use TBela\CSS\Interfaces\ElementInterface;

/**
* Ast|Element walker.
* @package TBela\CSS\Ast
*/
class NodeWalker implements IteratorAggregate
{

protected \stdClass|ElementInterface $node;

public function __construct(\stdClass|ElementInterface $node) {

$this->node = $node;
}

public function next(): \Generator
{

yield from $this->process($this->node);
}

protected function process(\stdClass|ElementInterface $node): \Generator
{

yield 'enter' => $node;

if (!empty($node->children)) {

foreach ($node->children as $child) {

yield from $this->process($child);
}
}

if (!str_starts_with($node->type, 'Invalid') && !in_array($node->type, ['Comment', 'Declaration'])) {

yield 'exit' => $node;
}
}

/**
* @inheritDoc
*/
public function getIterator(): iterable
{
return $this->process($this->node);
}
}
2 changes: 1 addition & 1 deletion src/Ast/Traverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use TBela\CSS\Value;

/**
* Ast|Element traverser
* Ast|Element traverser. return a modified or new tree if the callback changes a node
* @package TBela\CSS\Ast
*/
class Traverser extends Event
Expand Down
Loading

0 comments on commit e462631

Please sign in to comment.