Closes #1497, Non-linked breadcrumbs

Closes #1103, List functions for ul / ol elements in HtmlHelper

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5647 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2007-09-16 01:42:33 +00:00
parent ce3f0cf140
commit 590ec3650f
2 changed files with 114 additions and 6 deletions

View file

@ -36,7 +36,6 @@ class HtmlHelper extends AppHelper {
/*************************************************************************
* Public variables
*************************************************************************/
/**#@+
* @access public
*/
@ -86,7 +85,10 @@ class HtmlHelper extends AppHelper {
'legend' => '<legend>%s</legend>',
'css' => '<link rel="%s" type="text/css" href="%s" %s/>',
'style' => '<style type="text/css"%s>%s</style>',
'charset' => '<meta http-equiv="Content-Type" content="text/html; charset=%s" />'
'charset' => '<meta http-equiv="Content-Type" content="text/html; charset=%s" />',
'ul' => '<ul%s>%s</ul>',
'ol' => '<ol%s>%s</ol>',
'li' => '<li%s>%s</li>'
);
/**
* Base URL
@ -151,9 +153,9 @@ class HtmlHelper extends AppHelper {
* Adds a link to the breadcrumbs array.
*
* @param string $name Text for link
* @param string $link URL for link
* @param string $link URL for link (if empty it won't be a link)
*/
function addCrumb($name, $link) {
function addCrumb($name, $link = null) {
$this->_crumbs[] = array($name, $link);
}
/**
@ -506,7 +508,49 @@ class HtmlHelper extends AppHelper {
}
return $this->output(sprintf($this->tags[$tag], $this->_parseAttributes($attributes, null, ' ', ''), $text));
}
/**
* Build a nested list (UL/OL) out of an associative array.
*
* @param array $list Set of elements to list
* @param array $attributes Additional HTML attributes of the list (ol/ul) tag
* @param array $itemAttributes Additional HTML attributes of the list item (LI) tag
* @param string $tag Type of list tag to use (ol/ul)
* @return string The nested list
* @access public
*/
function nestedList($list, $attributes = array(), $itemAttributes = array(), $tag = 'ul') {
$items = $this->__nestedListItem($list, $attributes, $itemAttributes, $tag);
return sprintf($this->tags[$tag], $this->_parseAttributes($attributes, null, ' ', ''), $items);
}
/**
* Internal function to build a nested list (UL/OL) out of an associative array.
*
* @param array $list Set of elements to list
* @param array $attributes Additional HTML attributes of the list (ol/ul) tag
* @param array $itemAttributes Additional HTML attributes of the list item (LI) tag
* @param string $tag Type of list tag to use (ol/ul)
* @return string The nested list element
* @access private
* @see nestedList()
*/
function __nestedListItem($items, $attributes, $itemAttributes, $tag) {
$out = '';
$index = 1;
foreach($items as $key => $item) {
if (is_array($item)) {
$item = $key . $this->nestedList($item, $attributes, $itemAttributes, $tag);
}
if (isset($itemAttributes['even']) && $index % 2 == 0) {
$itemAttributes['class'] = $itemAttributes['even'];
} else if (isset($itemAttributes['odd']) && $index % 2 != 0) {
$itemAttributes['class'] = $itemAttributes['odd'];
}
$out .= sprintf($this->tags['li'], $this->_parseAttributes(array_diff_key($itemAttributes, array_flip(array('even', 'odd'))), null, ' ', ''), $item);
$index++;
}
return $out;
}
/**
* Creates a password input widget.
*
@ -681,4 +725,4 @@ class HtmlHelper extends AppHelper {
}
}
}
?>
?>

View file

@ -102,8 +102,72 @@ class HtmlHelperTest extends UnitTestCase {
$this->assertNoPattern('/<a[^<>]+[^href]=[^<>]*>/', $result);
}
function testNestedList() {
$list = array(
'Item 1',
'Item 2' => array(
'Item 2.1'
),
'Item 3',
'Item 4' => array(
'Item 4.1',
'Item 4.2',
'Item 4.3' => array(
'Item 4.3.1',
'Item 4.3.2'
)
),
'Item 5' => array(
'Item 5.1',
'Item 5.2'
)
);
$result = $this->Html->nestedList($list);
$this->assertPattern('/^<ul>\s*<li>Item 1<\/li>\s*<li>Item 2.+<\/li><li>Item 3<\/li>\s*<li>Item 4.+<\/li><li>Item 5.+<\/li><\/ul>$/', $result);
$this->assertPattern('/<li>Item 2\s*<ul>\s*<li>Item 2.1<\/li>\s*<\/ul>\s*<\/li>/', $result);
$this->assertPattern('/<li>Item 4\s*<ul>\s*<li>Item 4.1<\/li>\s*<li>Item 4.2<\/li>\s*<li>Item 4.3.+<\/li>\s*<\/ul>\s*<\/li>/', $result);
$this->assertPattern('/<li>Item 4.3\s*<ul>\s*<li>Item 4.3.1<\/li>\s*<li>Item 4.3.2<\/li>\s*<\/ul>\s*<\/li>/', $result);
$this->assertPattern('/<li>Item 5\s*<ul>\s*<li>Item 5.1<\/li>\s*<li>Item 5.2<\/li>\s*<\/ul>\s*<\/li>/', $result);
$result = $this->Html->nestedList($list, array(), array(), 'ol');
$this->assertPattern('/^<ol>\s*<li>Item 1<\/li>\s*<li>Item 2.+<\/li><li>Item 3<\/li>\s*<li>Item 4.+<\/li><li>Item 5.+<\/li><\/ol>$/', $result);
$this->assertPattern('/<li>Item 2\s*<ol>\s*<li>Item 2.1<\/li>\s*<\/ol>\s*<\/li>/', $result);
$this->assertPattern('/<li>Item 4\s*<ol>\s*<li>Item 4.1<\/li>\s*<li>Item 4.2<\/li>\s*<li>Item 4.3.+<\/li>\s*<\/ol>\s*<\/li>/', $result);
$this->assertPattern('/<li>Item 4.3\s*<ol>\s*<li>Item 4.3.1<\/li>\s*<li>Item 4.3.2<\/li>\s*<\/ol>\s*<\/li>/', $result);
$this->assertPattern('/<li>Item 5\s*<ol>\s*<li>Item 5.1<\/li>\s*<li>Item 5.2<\/li>\s*<\/ol>\s*<\/li>/', $result);
$result = $this->Html->nestedList($list, array('class'=>'list'));
$this->assertPattern('/^<ul[^<>]*class="list"[^<>]*>\s*<li>Item 1<\/li>\s*<li>Item 2.+<\/li><li>Item 3<\/li>\s*<li>Item 4.+<\/li><li>Item 5.+<\/li><\/ul>$/', $result);
$this->assertPattern('/<li>Item 2\s*<ul[^<>]*class="list"[^<>]*>\s*<li>Item 2.1<\/li>\s*<\/ul>\s*<\/li>/', $result);
$this->assertPattern('/<li>Item 4\s*<ul[^<>]*class="list"[^<>]*>\s*<li>Item 4.1<\/li>\s*<li>Item 4.2<\/li>\s*<li>Item 4.3.+<\/li>\s*<\/ul>\s*<\/li>/', $result);
$this->assertPattern('/<li>Item 4.3\s*<ul[^<>]*class="list"[^<>]*>\s*<li>Item 4.3.1<\/li>\s*<li>Item 4.3.2<\/li>\s*<\/ul>\s*<\/li>/', $result);
$this->assertPattern('/<li>Item 5\s*<ul[^<>]*class="list"[^<>]*>\s*<li>Item 5.1<\/li>\s*<li>Item 5.2<\/li>\s*<\/ul>\s*<\/li>/', $result);
$result = $this->Html->nestedList($list, array(), array('class' => 'item'));
$this->assertPattern('/^<ul>\s*<li[^<>]*class="item"[^<>]*>Item 1<\/li>\s*<li[^<>]*class="item"[^<>]*>Item 2.+<\/li><li[^<>]*class="item"[^<>]*>Item 3<\/li>\s*<li[^<>]*class="item"[^<>]*>Item 4.+<\/li><li[^<>]*class="item"[^<>]*>Item 5.+<\/li><\/ul>$/', $result);
$this->assertPattern('/<li[^<>]*class="item"[^<>]*>Item 2\s*<ul>\s*<li[^<>]*class="item"[^<>]*>Item 2.1<\/li>\s*<\/ul>\s*<\/li>/', $result);
$this->assertPattern('/<li[^<>]*class="item"[^<>]*>Item 4\s*<ul>\s*<li[^<>]*class="item"[^<>]*>Item 4.1<\/li>\s*<li[^<>]*class="item"[^<>]*>Item 4.2<\/li>\s*<li[^<>]*class="item"[^<>]*>Item 4.3.+<\/li>\s*<\/ul>\s*<\/li>/', $result);
$this->assertPattern('/<li[^<>]*class="item"[^<>]*>Item 4.3\s*<ul>\s*<li[^<>]*class="item"[^<>]*>Item 4.3.1<\/li>\s*<li[^<>]*class="item"[^<>]*>Item 4.3.2<\/li>\s*<\/ul>\s*<\/li>/', $result);
$this->assertPattern('/<li[^<>]*class="item"[^<>]*>Item 5\s*<ul>\s*<li[^<>]*class="item"[^<>]*>Item 5.1<\/li>\s*<li[^<>]*class="item"[^<>]*>Item 5.2<\/li>\s*<\/ul>\s*<\/li>/', $result);
$result = $this->Html->nestedList($list, array(), array('even' => 'even', 'odd' => 'odd'));
$this->assertPattern('/^<ul>\s*<li[^<>]*class="odd"[^<>]*>Item 1<\/li>\s*<li[^<>]*class="even"[^<>]*>Item 2.+<\/li><li[^<>]*class="odd"[^<>]*>Item 3<\/li>\s*<li[^<>]*class="even"[^<>]*>Item 4.+<\/li><li[^<>]*class="odd"[^<>]*>Item 5.+<\/li><\/ul>$/', $result);
$this->assertPattern('/<li[^<>]*class="even"[^<>]*>Item 2\s*<ul>\s*<li[^<>]*class="odd"[^<>]*>Item 2.1<\/li>\s*<\/ul>\s*<\/li>/', $result);
$this->assertPattern('/<li[^<>]*class="even"[^<>]*>Item 4\s*<ul>\s*<li[^<>]*class="odd"[^<>]*>Item 4.1<\/li>\s*<li[^<>]*class="even"[^<>]*>Item 4.2<\/li>\s*<li[^<>]*class="odd"[^<>]*>Item 4.3.+<\/li>\s*<\/ul>\s*<\/li>/', $result);
$this->assertPattern('/<li[^<>]*class="odd"[^<>]*>Item 4.3\s*<ul>\s*<li[^<>]*class="odd"[^<>]*>Item 4.3.1<\/li>\s*<li[^<>]*class="even"[^<>]*>Item 4.3.2<\/li>\s*<\/ul>\s*<\/li>/', $result);
$this->assertPattern('/<li[^<>]*class="odd"[^<>]*>Item 5\s*<ul>\s*<li[^<>]*class="odd"[^<>]*>Item 5.1<\/li>\s*<li[^<>]*class="even"[^<>]*>Item 5.2<\/li>\s*<\/ul>\s*<\/li>/', $result);
$result = $this->Html->nestedList($list, array('class'=>'list'), array('class' => 'item'));
$this->assertPattern('/^<ul[^<>]*class="list"[^<>]*>\s*<li[^<>]*class="item"[^<>]*>Item 1<\/li>\s*<li[^<>]*class="item"[^<>]*>Item 2.+<\/li><li[^<>]*class="item"[^<>]*>Item 3<\/li>\s*<li[^<>]*class="item"[^<>]*>Item 4.+<\/li><li[^<>]*class="item"[^<>]*>Item 5.+<\/li><\/ul>$/', $result);
$this->assertPattern('/<li[^<>]*class="item"[^<>]*>Item 2\s*<ul[^<>]*class="list"[^<>]*>\s*<li[^<>]*class="item"[^<>]*>Item 2.1<\/li>\s*<\/ul>\s*<\/li>/', $result);
$this->assertPattern('/<li[^<>]*class="item"[^<>]*>Item 4\s*<ul[^<>]*class="list"[^<>]*>\s*<li[^<>]*class="item"[^<>]*>Item 4.1<\/li>\s*<li[^<>]*class="item"[^<>]*>Item 4.2<\/li>\s*<li[^<>]*class="item"[^<>]*>Item 4.3.+<\/li>\s*<\/ul>\s*<\/li>/', $result);
$this->assertPattern('/<li[^<>]*class="item"[^<>]*>Item 4.3\s*<ul[^<>]*class="list"[^<>]*>\s*<li[^<>]*class="item"[^<>]*>Item 4.3.1<\/li>\s*<li[^<>]*class="item"[^<>]*>Item 4.3.2<\/li>\s*<\/ul>\s*<\/li>/', $result);
$this->assertPattern('/<li[^<>]*class="item"[^<>]*>Item 5\s*<ul[^<>]*class="list"[^<>]*>\s*<li[^<>]*class="item"[^<>]*>Item 5.1<\/li>\s*<li[^<>]*class="item"[^<>]*>Item 5.2<\/li>\s*<\/ul>\s*<\/li>/', $result);
}
function tearDown() {
unset($this->Html);
}
}
?>
?>