Skip to content

Commit

Permalink
feature: mark component forms with component name
Browse files Browse the repository at this point in the history
closes #499
  • Loading branch information
g105b committed Jan 28, 2025
1 parent b469f18 commit 0690a51
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/ComponentExpander.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public function expand(Element $context = null):array {
src: $src,
);
$element->innerHTML = $content;
foreach($element->querySelectorAll("form[method='post']") as $form) {
$componentInput = $element->ownerDocument->createElement("input");
$componentInput->type = "hidden";
$componentInput->name = "__component";
$componentInput->value = $name;
$form->prepend($componentInput);
}
array_push($expandedComponentArray, $element);
$recursiveExpandedComponents = $this->expand($element);
$expandedComponentArray = array_merge(
Expand Down
16 changes: 16 additions & 0 deletions test/phpunit/ComponentExpanderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,20 @@ public function testExpand_nested():void {
);
self::assertSame("more complex", $document->querySelector("example-nested p strong")->innerText);
}

public function testExpand_injectComponentName():void {
$partialContent = self::mockPartialContent(
"_component", [
"example-form" => HTMLPageContent::HTML_COMPONENT_FORM,
]
);
$document = new HTMLDocument(HTMLPageContent::HTML_INCLUDING_EXAMPLE_FORM);
$sut = new ComponentExpander($document, $partialContent);
$sut->expand();

$form = $document->querySelector("example-form form");
$hiddenInput = $form->querySelector("input[type=hidden]");
self::assertSame("__component", $hiddenInput->name);
self::assertSame("example-form", $hiddenInput->value);
}
}

0 comments on commit 0690a51

Please sign in to comment.