Skip to content

Commit 68370c8

Browse files
committed
Add acceptable alternative result for attrs-data.complex test case
1 parent c3438d3 commit 68370c8

2 files changed

Lines changed: 55 additions & 15 deletions

File tree

tests/Phug/Component/ComponentExtensionTest.php

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,8 @@ public function getPugPhpTestsTemplates(): array
408408
return array_map(function ($file) {
409409
$file = basename($file);
410410

411-
return [$file, substr($file, 0, -5).'.pug'];
412-
}, glob(__DIR__.'/../../templates/*.html'));
411+
return [substr($file, 0, -4).'.html', $file];
412+
}, glob(__DIR__.'/../../templates/*.pug'));
413413
}
414414

415415
/**
@@ -431,18 +431,27 @@ public function testPugPhpTestsTemplates(string $htmlFile, string $pugFile)
431431
]);
432432
ComponentExtension::enable($pug);
433433

434+
$actualContent = $this->rawHtml($pug->renderFile($templateFolder . $pugFile, []));
435+
$altHtmlFile = $templateFolder . strtr($htmlFile, ['.html' => '.alt.html']);
436+
437+
if (file_exists($altHtmlFile)) {
438+
if ($this->rawHtml(file_get_contents($altHtmlFile)) === $actualContent) {
439+
$this->assertTrue(true);
440+
441+
return;
442+
}
443+
}
444+
434445
$this->assertSame(
435446
$this->rawHtml(file_get_contents($templateFolder . $htmlFile)),
436-
$this->rawHtml($pug->renderFile($templateFolder . $pugFile, [])),
447+
$actualContent,
437448
basename($pugFile)
438449
);
439450
}
440451

441452
private function rawHtml($html)
442453
{
443-
$html = strtr($html, [
444-
"\r" => '',
445-
]);
454+
$html = strtr($html, ["\r" => '']);
446455
$html = preg_replace('`\n{2,}`', "\n", $html);
447456
$html = preg_replace('`(?<!\n) {2,}`', ' ', $html);
448457
$html = preg_replace('` *$`m', '', $html);
@@ -484,22 +493,48 @@ function ($matches) use ($document) {
484493

485494
return '<' . $matches['tag'] .
486495
implode('', array_map(
487-
static function (DOMAttr $value): string {
488-
$name = $value->name;
489-
490-
if ($value->textContent === '') {
491-
return " $name";
492-
}
493-
494-
return " $name=\"" . htmlentities($value->textContent) . '"';
495-
},
496+
[$this, 'formatAttribute'],
496497
$attributes
497498
)) .
498499
'>';
499500
},
500501
$html
501502
);
503+
$html = preg_replace_callback(
504+
'/(?<start><(\w+)(?:\s[^>]+)?>)(?<content>[^<]+)(?<end><\/\2>)/',
505+
function ($matches) use ($document) {
506+
return $matches['start'] .
507+
$this->escapeQuotes($matches['content']) .
508+
$matches['end'];
509+
},
510+
$html
511+
);
502512

503513
return $html;
504514
}
515+
516+
private function formatAttribute(DOMAttr $attribute): string
517+
{
518+
$name = $attribute->name;
519+
$value = $attribute->textContent;
520+
521+
if ($name === 'data-items') {
522+
var_dump($value);
523+
exit;
524+
}
525+
526+
if ($value === '') {
527+
return " $name";
528+
}
529+
530+
return " $name=\"" . $this->escapeQuotes($value) . '"';
531+
}
532+
533+
private function escapeQuotes(string $input): string
534+
{
535+
return strtr($input, [
536+
'"' => '&quot;',
537+
"'" => '&#039;',
538+
]);
539+
}
505540
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<foo data-items="{&quot;foo&quot;:&quot;bar&quot;,&quot;yah&quot;:42,&quot;yoh&quot;:&quot;yah: 42&quot;,&quot;bar&quot;:&quot;a&#039;a\&quot;\\\&quot;\\&#039;\\\\&quot;}"></foo>
2+
<foo data-items="{&quot;foo&quot;:&quot;#{foo}, \\#{foo}&quot;,&quot;bar&quot;:&quot;#{foo}, \\#{foo}&quot;}"></foo>
3+
<foo data-items="[&quot;#{foo} [a]&quot;,&quot;#{foo} =&gt;&quot;]"></foo>
4+
<foo data-items="{&quot;a\\\&quot;\\&quot;:&quot;a&quot;}"></foo>
5+
<foo a'="&#039;a"></foo>

0 commit comments

Comments
 (0)