Skip to content

Commit

Permalink
bindable-fix (#75)
Browse files Browse the repository at this point in the history
* Fix return type for getElementById, closes #65

* Update PHPDoc to include inherited changes

* Rename function for removing template attributes

* Set value for certain input fields differently

* Update Dom library to fix a few small bugs
  • Loading branch information
g105b authored Jul 31, 2019
1 parent c42bd61 commit 16ae8a7
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 50 deletions.
74 changes: 37 additions & 37 deletions composer.lock

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

14 changes: 14 additions & 0 deletions src/Bindable.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,20 @@ protected function setPropertyValue(
case "innerText":
$element->innerText = $value;
break;

case "value":
$tagName = strtoupper($element->tagName);
switch($tagName) {
case "SELECT":
case "INPUT":
case "TEXTAREA":
$element->value = $value;
break;
default:
$element->setAttribute($bindProperty, $value);
}
break;

default:
$element->setAttribute($bindProperty, $value);
}
Expand Down
26 changes: 15 additions & 11 deletions src/HTMLDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
* @property-read Element $firstElementChild;
* @property-read Element $lastElementChild;
* @property-read Element $body;
* @method Node getElementById(string $id)
* @method Element createElement(string $name, string $value = null)
*
* @method Attr createAttribute(string $name)
* @method Comment createComment(string $data)
* @method DocumentFragment createDocumentFragment()
* @method Element createElement(string $name)
* @method Element createTextNode(string $content)
* @method ?Element getElementById(string $id)
*
*/
class HTMLDocument extends BaseHTMLDocument {
Expand Down Expand Up @@ -197,7 +202,7 @@ public function storeBoundAttribute(BaseAttr $attr) {
}

public function validateBinds():void {
$allBindableElements = $this->getAllBindableElements();
$allBindableElements = $this->getAllDomTemplateElements();

foreach($allBindableElements as $element) {
foreach($element->attributes as $attr) {
Expand All @@ -214,24 +219,23 @@ public function validateBinds():void {
}
}

public function removeBinds():void {
$allBindableElements = $this->getAllBindableElements();
public function removeTemplateAttributes():void {
$allBindableElements = $this->getAllDomTemplateElements();

foreach($allBindableElements as $element) {
foreach($element->attributes as $attr) {
/** @var \Gt\Dom\Attr $attr */
if(strpos($attr->name, "data-bind") !== 0) {
continue;
if(strpos($attr->name, "data-bind") === 0
|| strpos($attr->name, "data-template") === 0) {
$attr->remove();
}

$attr->remove();
}
}
}

protected function getAllBindableElements():BaseHTMLCollection {
protected function getAllDomTemplateElements():BaseHTMLCollection {
return $this->documentElement->xPath(
"descendant-or-self::*[@*[starts-with(name(), 'data-bind')]]"
"descendant-or-self::*[@*[starts-with(name(), 'data-bind')]]|descendant-or-self::*[@*[starts-with(name(), 'data-template')]]"
);
}
}
2 changes: 1 addition & 1 deletion test/unit/BindableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function testBindDataRemoved() {
"age" => $age,
]);

$document->removeBinds();
$document->removeTemplateAttributes();

$boundDataTestElement = $document->querySelector(".bound-data-test");
$spanChildren = $boundDataTestElement->querySelectorAll("span");
Expand Down
6 changes: 5 additions & 1 deletion test/unit/HTMLDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ public function testOverriddenClasses() {
self::assertInstanceOf(DocumentFragment::class, $fragment);
}


public function testGetElementById() {
$document = new HTMLDocument("<!doctype html><h1 id='test'>Test</h1>");
$element = $document->getElementById("test");
self::assertInstanceOf(Element::class, $element);
}
}

0 comments on commit 16ae8a7

Please sign in to comment.