Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Changelog

## 1.4.2

### Fixed

- A node inside a `<template>` matches no selector, as in a browser. Before, a selector found it,
though the page does not show it: a check could pass on markup that only a script would render.
`attributeValues()` leaves out its attributes too.

### Documentation

- The README opens with why the package exists, with a string check that stays green on a broken
page. The demo shows the same case.

### Upgrade

- A check that found its node only inside a `<template>` now finds none. Check the node the page
shows, or the `<template>` element itself.

## 1.4.1

### Changed
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ itself is not a match: in `within('[data-cart]')`, the selector `[data-cart]` fi
names the element of the region. Symfony translates `:scope` by position, so `:scope.x` or
`:scope > li` would find the wrong nodes. `Html` refuses both.

**A node inside a `<template>` matches no selector**, as in a browser: the page shows it only when a
script copies it. The `<template>` element itself matches.

In plain PHP, `Html` has the same methods: `Html::of($value)->within($selector)`, `->frame()`,
`->matches()`, `->texts()`, `->rawTexts()`, `->attributes()`, `->attributeValues()`, `->count()`,
`->text()`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ expect(html($response)->attributeValues())->each->not->toContain('javascript:');
expect($response)->eachMatch('[data-avatar]', fn ($avatar) => $avatar->toHaveSelectorClass(':scope', 'rounded-full'));
```

A region reads like `querySelectorAll`: it finds nodes below its element, not the element itself. `:scope` alone names the element of the region. `:scope > li` and `:scope.x` are refused.
A region reads like `querySelectorAll`: it finds nodes below its element, not the element itself. `:scope` alone names the element of the region. `:scope > li` and `:scope.x` are refused. A node inside a `<template>` matches no selector, as in a browser.

In plain PHP: `Html::of($value)->within($selector)`, `->frame()`, `->matches()`, `->texts()`, `->rawTexts()`, `->attributes()`, `->attributeValues()`, `->count()`, `->text()`.

Expand Down
16 changes: 14 additions & 2 deletions src/PHPUnit/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function attributeValues(): array
$values = [];

foreach ($this->roots() as $root) {
foreach ([$root, ...$this->elementsBelow($root)] as $element) {
foreach ([$root, ...array_filter($this->elementsBelow($root), fn (DOMElement $element): bool => ! $this->isInTemplate($element))] as $element) {
foreach ($element->attributes ?? [] as $attribute) {
$values[] = $attribute->value;
}
Expand Down Expand Up @@ -188,7 +188,7 @@ public function elements(string $selector): array

$matches = array_values(array_filter(
iterator_to_array($this->document->filter($selector), false),
fn (DOMNode $node): bool => $node instanceof DOMElement,
fn (DOMNode $node): bool => $node instanceof DOMElement && ! $this->isInTemplate($node),
));

$scope = $this->scope;
Expand Down Expand Up @@ -238,6 +238,18 @@ private static function content(TestResponse $response): string
: (string) $response->baseResponse->getContent();
}

/** A node in a `template` is not part of the page: a browser renders it only when a script copies it. */
private function isInTemplate(DOMNode $node): bool
{
for ($parent = $node->parentNode; $parent instanceof DOMNode; $parent = $parent->parentNode) {
if ($parent instanceof DOMElement && mb_strtolower($parent->localName ?? '') === 'template') {
return true;
}
}

return false;
}

private function isBelow(DOMNode $node, DOMElement $scope): bool
{
for ($parent = $node->parentNode; $parent instanceof DOMNode; $parent = $parent->parentNode) {
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPUnit/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ public function test_attribute_values_lists_every_value_in_the_region(): void
self::assertNotContains('/out?u=1', $values);
}

public function test_a_node_in_a_template_is_not_part_of_the_page(): void
{
$html = Html::of('<main><p data-total></p><template><p data-total>42.00 EUR</p><a data-in="template">x</a></template></main>');

self::assertSame([''], $html->texts('[data-total]'));
self::assertSame(1, $html->count('template'));
self::assertSame(0, $html->within('template')->count('p'));
self::assertNotContains('template', $html->attributeValues());
}

public function test_within_stops_where_the_region_is_missing_or_there_twice(): void
{
foreach (['[data-here]' => 0, '[data-name]' => 2] as $selector => $count) {
Expand Down
Loading