Skip to content

Commit

Permalink
Output template's original name to t- class
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed May 7, 2018
1 parent 307cebe commit 2224d64
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/HTMLDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public function getNamedTemplateChildren(string...$namesToMatch):array {
}

public function setNamedTemplate(string $name, BaseDocumentFragment $fragment):void {
foreach($fragment->children as $child) {
$child->classList->add("t-$name");
}

$this->templateFragmentMap[$name] = $fragment;
}

Expand Down
27 changes: 27 additions & 0 deletions test/unit/Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,33 @@ class Helper {
<ordered-list></ordered-list>
</section>
</main>
HTML;

const HTML_COMPONENTS_WITH_NAMED_TEMPLATE = <<<HTML
<!doctype html>
<meta charset="utf-8" />
<title>This document has some components</title>
<main>
<section>
<h1>Hello, World!</h1>
<ul>
<li data-template="list-item">This is a list item</li>
</ul>
<p>
Above this paragraph is an unordered list, with the LI elements templated.
</p>
<title-definition-list data-template="tdlist"></title-definition-list>
<p>
Above this paragraph is a custom component, which has a nested component within it.
</p>
<p>
Below is another custom component.
</p>
<ordered-list></ordered-list>
</section>
</main>
HTML;

const HTML_TEMPLATE_WITH_NESTED_COMPONENT = <<<HTML
Expand Down
36 changes: 36 additions & 0 deletions test/unit/TemplateParentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,40 @@ public function testComponentAndTemplatePrefixAddedToTemplateComponentElement()
$inserted->classList->contains("t-title-definition-list")
);
}

public function testComponentAndTemplatePrefixAddedCorrectlyWithNamedTemplate() {
$componentDir = self::TEST_DIR . "/" . self::COMPONENT_PATH;
file_put_contents(
"$componentDir/title-definition-list.html",
Helper::COMPONENT_TITLE_DEFINITION_LIST
);
file_put_contents(
"$componentDir/title-definition.html",
Helper::COMPONENT_TITLE_DEFINITION
);
file_put_contents(
"$componentDir/ordered-list.html",
Helper::COMPONENT_ORDERED_LIST
);

$document = new HTMLDocument(
Helper::HTML_COMPONENTS_WITH_NAMED_TEMPLATE,
$componentDir
);
$document->extractTemplates();
$document->expandComponents();

$t = $document->getTemplate("tdlist");
$inserted = $t->insertTemplate();

self::assertTrue(
$inserted->classList->contains("c-title-definition-list")
);
self::assertTrue(
$inserted->classList->contains("t-tdlist")
);
self::assertFalse(
$inserted->classList->contains("t-title-definition-list")
);
}
}

0 comments on commit 2224d64

Please sign in to comment.