Skip to content

Commit

Permalink
357 placeholders in ids (#358)
Browse files Browse the repository at this point in the history
* test: isolate bug for #357

* test: implement functionality of #357 but blocked by php bug

* test: workaround php bug 81506

* test: workaround php bug 81506

* test: workaround php bug 81506
  • Loading branch information
g105b authored Jul 12, 2022
1 parent 1075f8d commit 7dfc852
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 19 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"require": {
"php": ">=8.1",
"ext-dom": "*",
"phpgt/dom": "^v4.0.0"
"phpgt/dom": "dev-master"
},
"require-dev": {
"phpstan/phpstan": "v1.8.0",
Expand Down
21 changes: 12 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/PlaceholderBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function bind(
$context
);

$placeholderTextList = [];
foreach($xpathResult as $attributeOrText) {
/** @var Text|Attr $text */
$text = $attributeOrText;
Expand All @@ -49,6 +50,10 @@ public function bind(
continue;
}

array_push($placeholderTextList, $placeholderText);
}

foreach($placeholderTextList as $placeholderText) {
$placeholderText->setValue($value);
}
}
Expand Down
42 changes: 38 additions & 4 deletions src/PlaceholderText.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php /** @noinspection PhpPropertyOnlyWrittenInspection tracked in issue #290 */
namespace Gt\DomTemplate;

use Gt\Dom\Attr;
use Gt\Dom\Element;
use Gt\Dom\Text;

Expand All @@ -9,7 +10,7 @@ class PlaceholderText {
private ?string $default;

public function __construct(
private Text $originalText
private readonly Text $originalText
) {
$this->process();
}
Expand Down Expand Up @@ -44,10 +45,43 @@ public function setValue(mixed $value):void {
$stringValue = (string)$value;

if(strlen($stringValue) === 0) {
$this->originalText->data = $this->default ?: "";
$stringValue = $this->default ?: "";
}
else {
$this->originalText->data = $stringValue;
$this->originalText->data = $stringValue;

$parent = $this->originalText->parentNode;
/** @phpstan-ignore-next-line */
if($parent instanceof Attr) {
$this->originalText->normalize();
$qualifiedName = $parent->name;
$wholeText = $this->originalText->wholeText;
/** @var Element $ownerElement */
$ownerElement = $parent->ownerElement;
// https://bugs.php.net/bug.php?id=81506
$ownerElement->setAttribute("data-temp-$qualifiedName", $wholeText);

/**
* @var string $attrName
* @var Attr $attr
*/
foreach($ownerElement->attributes as $attrName => $attr) {
if($attrName !== $qualifiedName) {
continue;
}

// Workaround for PHP bug 81506 (don't lose reference to text)
// https://bugs.php.net/bug.php?id=81506
$attr->appendChild($this->originalText);
}
if($qualifiedName === "id") {
$ownerElement->id = $wholeText;
}
else {
$ownerElement->setAttribute($qualifiedName, $wholeText);
}

// https://bugs.php.net/bug.php?id=81506
$ownerElement->removeAttribute("data-temp-$qualifiedName");
}
}
}
27 changes: 27 additions & 0 deletions test/phpunit/DocumentBinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,33 @@ public function testBindList_twoListsWithSamePath():void {
}
}

public function testBindList_readOnlyProperties():void {
$userObject1 = new class(1, "g105b", 3) {
public function __construct(
public readonly int $userId,
public readonly string $username,
public readonly int $orderCount,
) {}
};
$userObject2 = new class(2, "codyboy", 21) {
public function __construct(
public readonly int $userId,
public readonly string $username,
public readonly int $orderCount,
) {}
};

$document = new HTMLDocument(DocumentTestFactory::HTML_USER_ORDER_LIST);
$sut = new DocumentBinder($document);
$sut->bindList([$userObject1, $userObject2]);

$li1 = $document->getElementById("user-1");
$li2 = $document->getElementById("user-2");
self::assertNotSame($li1, $li2);
self::assertSame($userObject1->username, $li1->querySelector("h2 span")->textContent);
self::assertSame($userObject2->username, $li2->querySelector("h2 span")->textContent);
}

public function test_onlyBindOnce():void {
$document = new HTMLDocument(DocumentTestFactory::HTML_BIND_KEY_REUSED);
$sut = new DocumentBinder($document);
Expand Down
3 changes: 0 additions & 3 deletions test/phpunit/ListBinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ public function testBindListData_kvpList_array():void {
$sut->bindListData($kvpList, $orderList);

foreach($orderList->children as $i => $li) {
/** @var HTMLLiElement $li */
self::assertEquals($kvpList[$i]["userId"], $li->querySelector("h3 span")->textContent);
self::assertEquals($kvpList[$i]["username"], $li->querySelector("h2 span")->textContent);
self::assertEquals($kvpList[$i]["orderCount"], $li->querySelector("p span")->textContent);
Expand All @@ -251,7 +250,6 @@ public function testBindListData_kvpList_object():void {
$sut->bindListData($kvpList, $orderList);

foreach($orderList->children as $i => $li) {
/** @var HTMLLiElement $li */
self::assertEquals($kvpList[$i]->{"userId"}, $li->querySelector("h3 span")->textContent);
self::assertEquals($kvpList[$i]->{"username"}, $li->querySelector("h2 span")->textContent);
self::assertEquals($kvpList[$i]->{"orderCount"}, $li->querySelector("p span")->textContent);
Expand All @@ -277,7 +275,6 @@ public function testBindListData_kvpList_instanceObject():void {
$sut->bindListData($kvpList, $orderList);

foreach($orderList->children as $i => $li) {
/** @var HTMLLiElement $li */
self::assertEquals($kvpList[$i]->{"userId"}, $li->querySelector("h3 span")->textContent);
self::assertEquals($kvpList[$i]->{"username"}, $li->querySelector("h2 span")->textContent);
self::assertEquals($kvpList[$i]->{"orderCount"}, $li->querySelector("p span")->textContent);
Expand Down
4 changes: 2 additions & 2 deletions test/phpunit/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" processIsolation="true">
<coverage>
<include>
<directory suffix=".php">../../src</directory>
Expand All @@ -15,4 +15,4 @@
</testsuite>
</testsuites>
<logging/>
</phpunit>
</phpunit>

0 comments on commit 7dfc852

Please sign in to comment.