Browse Source

Merge pull request #269 from LordSimal/master

Fix TreeHelper problem when type and itemType are both the same
Mark Scherer 4 years ago
parent
commit
c2bb19dd67
2 changed files with 45 additions and 4 deletions
  1. 5 4
      src/View/Helper/TreeHelper.php
  2. 40 0
      tests/TestCase/View/Helper/TreeHelperTest.php

+ 5 - 4
src/View/Helper/TreeHelper.php

@@ -299,7 +299,7 @@ class TreeHelper extends Helper {
 				$return .= "\r\n" . $whiteSpace . $indentWith;
 			}
 			if ($itemType) {
-				$itemAttributes = $this->_attributes($itemType, $elementData);
+				$itemAttributes = $this->_attributes($itemType, $elementData, true, true);
 				$return .= '<' . $itemType . $itemAttributes . '>';
 			}
 			$return .= $content;
@@ -477,11 +477,12 @@ class TreeHelper extends Helper {
 	 * @param string $rType rType
 	 * @param array $elementData Element data
 	 * @param bool $clear Clear
+	 * @param bool $isItem is this an item or type call
 	 * @return string
 	 */
-	protected function _attributes($rType, array $elementData = [], $clear = true) {
+	protected function _attributes($rType, array $elementData = [], $clear = true, bool $isItem = false) {
 		extract($this->_config);
-		if ($rType === $type) {
+		if ($rType === $type && !$isItem) {
 			$attributes = $this->_typeAttributes;
 			if ($clear) {
 				$this->_typeAttributes = $this->_typeAttributesNext;
@@ -494,7 +495,7 @@ class TreeHelper extends Helper {
 				$this->_itemAttributes = [];
 			}
 		}
-		if ($rType === $itemType && $elementData['activePathElement']) {
+		if ($rType === $itemType && !empty($elementData['activePathElement'])) {
 			if ($elementData['activePathElement'] === true) {
 				$attributes['class'][] = $autoPath[2];
 			} else {

+ 40 - 0
tests/TestCase/View/Helper/TreeHelperTest.php

@@ -253,6 +253,46 @@ TEXT;
 	/**
 	 * @return void
 	 */
+	public function testGenerateWithSameTypeAndItemType() {
+		$tree = $this->Table->find('threaded')->toArray();
+
+		$output = $this->Tree->generate($tree, ['type' => 'div', 'itemType' => 'div']);
+		$expected = <<<TEXT
+
+<div>
+	<div>One
+	<div>
+		<div>One-SubA</div>
+	</div>
+	</div>
+	<div>Two
+	<div>
+		<div>Two-SubA
+		<div>
+			<div>Two-SubA-1
+			<div>
+				<div>Two-SubA-1-1</div>
+			</div>
+			</div>
+		</div>
+		</div>
+	</div>
+	</div>
+	<div>Three</div>
+	<div>Four
+	<div>
+		<div>Four-SubA</div>
+	</div>
+	</div>
+</div>
+
+TEXT;
+		$this->assertTextEquals($expected, $output);
+	}
+
+	/**
+	 * @return void
+	 */
 	public function testGenerateWithMaxDepth() {
 		$tree = $this->Table->find('threaded')->toArray();