Skip to content

Commit

Permalink
Merge pull request #56 from goetas/fix-55
Browse files Browse the repository at this point in the history
Handle "t:block-inner" within an "extends"
  • Loading branch information
goetas committed Feb 28, 2018
2 parents 4bec6f1 + bceb64f commit a58ea3c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/.project
/vendor
.idea
composer.lock
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,25 @@ matrix:
dist: trusty
group: edge
- php: 5.3
dist: precise
- php: 5.4
- php: 5.5
- php: 5.6
- php: 7.0
- php: 7.1
- php: 7.2

# Twig 2
- php: 7.0
env: TWIG_VERSION='^2.0'
- php: 7.1
env: TWIG_VERSION='^2.0'
- php: 7.2
env: TWIG_VERSION='^2.0'

# oldest possible deps for twig 1.x
- php: 5.3 # for twig 1.x
dist: precise
env: COMPOSER_FLAGS='--prefer-lowest --prefer-stable' TWIG_VERSION=^1.28
- php: 7.0 # for twig 2.x
env: COMPOSER_FLAGS='--prefer-lowest --prefer-stable' TWIG_VERSION=^2.0
Expand Down
3 changes: 0 additions & 3 deletions src/Attribute/BlockInnerAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,5 @@ public function visit(\DOMAttr $att, Compiler $context)
$sandbox->appendChild($end);

DOMHelper::replaceWithSet($sandbox, iterator_to_array($sandbox->childNodes));
if ($node->parentNode === $node->ownerDocument->documentElement) {
DOMHelper::replaceWithSet($node, iterator_to_array($node->childNodes));
}
}
}
37 changes: 37 additions & 0 deletions src/Attribute/ExtendsAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public function visit(\DOMAttr $att, Compiler $context)
}
}

// having block-inner makes no sense when child of an t:extends (t:extend can have only t:block child)
// so lets convert them to t:block nodes
$candidates = $this->convertBlockInnerIntoBlock($candidates, $node);

foreach ($candidates as $candidate) {
if ($candidate->parentNode !== $node) {
$candidate->parentNode->removeChild($candidate);
Expand All @@ -57,4 +61,37 @@ public function visit(\DOMAttr $att, Compiler $context)

DOMHelper::replaceWithSet($node, $set);
}

/**
* @param array $candidates
* @param \DOMNode $node
* @return array
*/
private function convertBlockInnerIntoBlock(array $candidates, \DOMNode $node)
{
/**
* @var $candidate \DOMElement
*/
foreach ($candidates as $k => $candidate) {
if ($candidate->hasAttributeNS(Twital::NS, "block-inner")) {

$blockName = $candidate->getAttributeNS(Twital::NS, "block-inner");

$block = $node->ownerDocument->createElementNS(Twital::NS, "block");
$block->setAttribute("name", $blockName);

$candidate->parentNode->insertBefore($block, $candidate);

// move all child to the new block node
while ($candidate->firstChild) {
$child = $candidate->removeChild($candidate->firstChild);
$block->appendChild($child);
}
$candidate->parentNode->removeChild($candidate);
$candidates[$k] = $block;
}
}

return $candidates;
}
}
7 changes: 5 additions & 2 deletions tests/Tests/CoreNodesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ public function getData()

array('<t:omit>foo</t:omit>', 'foo'),
array('<t:omit><div>foo</div></t:omit>', '<div>foo</div>'),

array('<t:omit><div t:block="aaa">foo</div></t:omit>', '<div>{% block aaa %}foo{% endblock %}</div>'),
array('<t:omit><div t:block="aaa">foo</div><div t:block="bbb">bar</div></t:omit>', '<div>{% block aaa %}foo{% endblock %}</div><div>{% block bbb %}bar{% endblock %}</div>'),
array('<t:omit><div t:block="aaa">foo<div t:block="bbb">bar</div></div></t:omit>', '<div>{% block aaa %}foo<div>{% block bbb %}bar{% endblock %}</div>{% endblock %}</div>'),

array('<div><t:omit t:if="0">foo</t:omit>bar</div>', '<div>{% if 0 %}foo{% endif %}bar</div>'),
array('<div><![CDATA[aa]]></div>', '<div><![CDATA[aa]]></div>'),
array('<div><!-- foo --></div>', '<div><!-- foo --></div>'),
Expand Down Expand Up @@ -181,7 +186,6 @@ public function getDataFormTemplates()
$expected,
);
}

return $data;
}

Expand All @@ -191,7 +195,6 @@ public function getDataFormTemplates()
public function testVisitNodeTemplates($source, $expected)
{
$compiled = $this->twital->compile($this->sourceAdapter, $source);

$cleanup = function ($str) {
return preg_replace("/\s+/", "", $str);
};
Expand Down

0 comments on commit a58ea3c

Please sign in to comment.