Skip to content

Commit

Permalink
added MacroNode::$innerContent [Closes nette/nette#1361]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 26, 2016
1 parent 7b72549 commit 982d639
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/Latte/Compiler.php
Expand Up @@ -454,9 +454,19 @@ public function closeMacro($name, $args = NULL, $modifiers = NULL, $isRightmost

$isLeftmost = $node->content ? trim(substr($this->output, strrpos("\n$this->output", "\n"))) === '' : FALSE;

if ($node->prefix === MacroNode::PREFIX_NONE) {
$parts = explode('<' . spl_object_hash($node) . '>', $node->content);
$node->content = implode('', $parts);
$node->innerContent = $parts[1];
}

$node->closing = TRUE;
$node->macro->nodeClosed($node);

if ($node->prefix === MacroNode::PREFIX_NONE && $node->innerContent !== $parts[1]) {
$node->content = $parts[0] . $node->innerContent . $parts[2];
}

if ($node->prefix && $node->prefix !== MacroNode::PREFIX_TAG) {
$this->htmlNode->attrCode .= $node->attrCode;
}
Expand Down Expand Up @@ -496,8 +506,8 @@ private function writeCode($code, $isReplaced, $isRightmost, $isLeftmost = NULL)
*/
public function writeAttrsMacro($html)
{
// none-2 none-1 tag-1 tag-2 <el attr-1 attr-2> /tag-2 /tag-1 inner-2 inner-1
// /inner-1 /inner-2 tag-1 tag-2 </el> /tag-2 /tag-1 /none-1 /none-2
// none-2 none-1 tag-1 tag-2 <el attr-1 attr-2> /tag-2 /tag-1 [none-1] [none-2] inner-2 inner-1
// /inner-1 /inner-2 [none-2] [none-1] tag-1 tag-2 </el> /tag-2 /tag-1 /none-1 /none-2
$attrs = $this->htmlNode->macroAttrs;
$left = $right = [];

Expand All @@ -519,6 +529,18 @@ public function writeAttrsMacro($html)
}
}

$innerMarkers = '';
if ($this->htmlNode->closing) {
$left[] = function () {
$this->output .= $this->htmlNode->innerMarkers;
};
} else {
array_unshift($right, function () use (& $innerMarkers) {
$this->output .= $innerMarkers;
});
}


foreach (array_reverse($this->macros) as $name => $foo) {
$attrName = MacroNode::PREFIX_TAG . "-$name";
if (isset($attrs[$attrName])) {
Expand All @@ -541,9 +563,13 @@ public function writeAttrsMacro($html)
$this->closeMacro($name, '', NULL, NULL, MacroNode::PREFIX_NONE);
};
} else {
array_unshift($left, function () use ($name, $attrs) {
if ($this->openMacro($name, $attrs[$name], NULL, NULL, MacroNode::PREFIX_NONE)->isEmpty) {
array_unshift($left, function () use ($name, $attrs, & $innerMarkers) {
$node = $this->openMacro($name, $attrs[$name], NULL, NULL, MacroNode::PREFIX_NONE);
if ($node->isEmpty) {
unset($this->htmlNode->macroAttrs[$name]); // don't call closeMacro
} else {
$this->htmlNode->innerMarkers .= $uniq = '<' . spl_object_hash($node) . '>';
$innerMarkers = $uniq . $innerMarkers;
}
});
}
Expand Down
3 changes: 3 additions & 0 deletions src/Latte/HtmlNode.php
Expand Up @@ -36,6 +36,9 @@ class HtmlNode
/** @var string */
public $attrCode;

/** @var string @internal */
public $innerMarkers;


public function __construct($name, self $parentNode = NULL)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Latte/MacroNode.php
Expand Up @@ -58,6 +58,9 @@ class MacroNode
/** @var string */
public $content;

/** @var string */
public $innerContent;

/** @var \stdClass user data */
public $data;

Expand Down
53 changes: 53 additions & 0 deletions tests/Latte/Compiler.writeAttrsMacro.innerContent.phpt
@@ -0,0 +1,53 @@
<?php

use Latte\IMacro;
use Latte\MacroNode;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


class TestMacro implements IMacro
{
function initialize() {}

function finalize() {}

function nodeOpened(MacroNode $node)
{}

function nodeClosed(MacroNode $node)
{
$node->innerContent = '[' . $node->innerContent . ']';
}
}


$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);

$latte->addMacro('one', new TestMacro);


Assert::match(
'%A%<div>[]</div>%A%',
$latte->compile('<div n:one></div>')
);

Assert::match(
"%A%<div>\n[<br>\n]</div>%A%",
$latte->compile("<div n:one>\n<br>\n</div>")
);

// ignore innerContent
Assert::match(
'%A%<div>@</div>%A%',
$latte->compile('<div n:inner-one>@</div>')
);

// ignore innerContent
Assert::match(
'%A%<div>@</div>%A%',
$latte->compile('<div n:tag-one>@</div>')
);

0 comments on commit 982d639

Please sign in to comment.