Skip to content

Commit

Permalink
Fix segfault! (#386)
Browse files Browse the repository at this point in the history
* fix: bind placeholders using regex
for #385

* fix: bind keyless placeholders using new method
fixes #385

* maintenance: remove unused class

* maintenance: remove commented out code

* maintenance: remove unused variable
  • Loading branch information
g105b authored Oct 19, 2022
1 parent 9ecc381 commit 2e0df6c
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 105 deletions.
37 changes: 20 additions & 17 deletions src/PlaceholderBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public function bind(
$context
);

$placeholderTextList = [];
foreach($xpathResult as $attributeOrText) {
/** @var Text|Attr $text */
$text = $attributeOrText;
Expand All @@ -37,24 +36,28 @@ public function bind(
$text = $text->lastChild;
}

$placeholder = $text->splitText(
strpos($text->data, "{{")
);
$placeholder->splitText(
strpos($placeholder->data, "}}") + 2
);

$placeholderText = new PlaceholderText($placeholder);
if((string)$key !== $placeholderText->getBindKey()) {
$text->parentNode->nodeValue = $text->wholeText;
continue;
}
$nodeValue = $text->nodeValue;

array_push($placeholderTextList, $placeholderText);
}
$regex = "/{{ *(?P<KEY>$key) *(\?\? ?(?P<DEFAULT>\w+))? ?}}/";
preg_match_all($regex, $nodeValue, $matches);

foreach($matches[0] as $i => $subjectToReplace) {
if($key != $matches["KEY"][$i]) {
continue;
}

foreach($placeholderTextList as $placeholderText) {
$placeholderText->setValue($value);
$valueToUse = $matches["DEFAULT"][$i];
if(!is_null($value) && $value !== "") {
$valueToUse = $value;
}

$nodeValue = str_replace(
$subjectToReplace,
$valueToUse,
$nodeValue
);
}
$text->nodeValue = $nodeValue;
}
}
}
87 changes: 0 additions & 87 deletions src/PlaceholderText.php

This file was deleted.

41 changes: 41 additions & 0 deletions test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
use Gt\Dom\HTMLDocument;
use Gt\DomTemplate\ListBinder;
use Gt\DomTemplate\TemplateCollection;
use Gt\DomTemplate\TemplateElement;

require "vendor/autoload.php";

$html = <<<HTML
<!doctype html>
<ul>
<li data-template>
<a href="/orders/{{userId}}/{{username??defaultUser}}/">view</a>
</li>
</ul>
HTML;

$kvpList = [
(object)["userId" => 543, "username" => "win95", "orderCount" => 55],
(object)["userId" => 559, "username" => "seafoam", "orderCount" => 30],
(object)["userId" => 274, "username" => "hammatime", "orderCount" => 23],
];

$string = "";
for($iteration = 0; $iteration < 1000; $iteration++) {
$document = new HTMLDocument($html);
$orderList = $document->querySelector("ul");

$templateElement = new TemplateElement($document->querySelector("ul li[data-template]"));
$templateCollection = new TemplateCollection($document);
$templateElement->removeOriginalElement();

$sut = new ListBinder($templateCollection);
$sut->bindListData($kvpList, $orderList);
$string .= $document;
echo memory_get_usage(), PHP_EOL;
}

echo $document;
10 changes: 10 additions & 0 deletions test/phpunit/TestFactory/DocumentTestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,16 @@ class DocumentTestFactory {
<p><a href="https://github.com/sponsors/{{org}}/sponsorships?tier_id={{tierId}}">Sponsor via GitHub.</a></p>
</main>
HTML;

const HTML_PLACEHOLDER_LIST = <<<HTML
<!doctype html>
<h1>A list with placeholders</h1>
<ul>
<li data-template id="user-{{userId}}">Username: {{username}}</li>
</ul>
HTML;

const HTML_MUSIC_EXPLICIT_TEMPLATE_NAMES = <<<HTML
<!doctype html>
<h1>Music library</h1>
Expand Down
2 changes: 1 addition & 1 deletion 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" processIsolation="true">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">../../src</directory>
Expand Down

0 comments on commit 2e0df6c

Please sign in to comment.