Skip to content

Commit 004c6ce

Browse files
committed
change printer to take tokens in constructor
1 parent f360e10 commit 004c6ce

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ The printer takes an HTML input or the resulting AST and renders it as neatly fo
9595
use Sinnbeck\HtmlAst\Printer;
9696

9797
// Create a Printer instance and render the HTML string
98-
echo Printer::make()->render($nodeTree);
98+
echo Printer::make($nodeTree)->render();
9999
```
100100

101101
## Testing

src/Printer.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
class Printer
99
{
10+
protected array $nodes;
1011
protected string $indentStr = " ";
1112
protected string $newline = "\n";
1213
protected array $voidElements = [
@@ -27,15 +28,20 @@ class Printer
2728
'wbr',
2829
];
2930

30-
public static function make(): self
31+
public function __construct(array $nodes)
3132
{
32-
return new self();
33+
$this->nodes = $nodes;
3334
}
3435

35-
public function render(array $nodes, int $level = 0): string
36+
public static function make(array $nodes): self
37+
{
38+
return new self($nodes);
39+
}
40+
41+
public function render(int $level = 0): string
3642
{
3743
$html = '';
38-
foreach ($nodes as $node) {
44+
foreach ($this->nodes as $node) {
3945
$html .= $this->renderNode($node, $level);
4046
}
4147

tests/Feature/PrintTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
$nodes = $ast->parse();
1414

15-
$printer = new Printer();
16-
expect($printer->render($nodes))->toEqual($html);
15+
$printer = new Printer($nodes);
16+
expect($printer->render())->toEqual($html);
1717
});
1818

1919
it('can fix basic-scrambled', function () {
@@ -24,9 +24,9 @@
2424

2525
$nodes = $ast->parse();
2626

27-
$printer = new Printer();
27+
$printer = new Printer($nodes);
2828
$html = getFixture('basic.html');
29-
expect($printer->render($nodes))->toEqual($html);
29+
expect($printer->render())->toEqual($html);
3030
});
3131

3232
it('can print complex', function () {
@@ -37,8 +37,8 @@
3737

3838
$nodes = $ast->parse();
3939

40-
$printer = new Printer();
41-
expect($printer->render($nodes))->toEqual($html);
40+
$printer = new Printer($nodes);
41+
expect($printer->render())->toEqual($html);
4242
});
4343

4444
it('can print fragments', function () {
@@ -49,8 +49,8 @@
4949

5050
$nodes = $ast->parse();
5151

52-
$printer = new Printer();
53-
expect($printer->render($nodes))->toEqual($html);
52+
$printer = new Printer($nodes);
53+
expect($printer->render())->toEqual($html);
5454
});
5555

5656
it('can print comments', function () {
@@ -61,15 +61,15 @@
6161

6262
$nodes = $ast->parse();
6363

64-
$printer = new Printer();
65-
expect($printer->render($nodes))->toEqual($html);
64+
$printer = new Printer($nodes);
65+
expect($printer->render())->toEqual($html);
6666
});
6767

6868
it('can handle all inline tags', function () {
6969
$html = getFixture('inline-tags.html');
7070
$lexer = new Lexer($html);
7171
$nodes = Parser::make($lexer->lex())->parse();
72-
expect(Printer::make()->render($nodes))->toEqual(
72+
expect(Printer::make($nodes)->render())->toEqual(
7373
<<<HTML
7474
<img src="logo.png" alt="logo" />
7575
<img src="logo.png" alt="logo" />

0 commit comments

Comments
 (0)