updating HtmlHelper, closes #2592, closes #2406, closes #2220, closes #2080, tests added

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6282 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-12-30 21:14:15 +00:00
parent 48bf9e420d
commit f6b71c29e9
2 changed files with 85 additions and 25 deletions

View file

@ -339,19 +339,24 @@ class HtmlHelper extends AppHelper {
return;
}
if (strpos($path, '.css') === false && strpos($path, '?') === false) {
$path .= '.css';
if (strpos($path, '://') !== false) {
$url = $path;
} else {
if (strpos($path, '.css') === false && strpos($path, '?') === false) {
$path .= '.css';
}
if ($path{0} !== '/') {
$path = CSS_URL . $path;
}
if (COMPRESS_CSS) {
$path = str_replace('css/', 'ccss/', $path);
}
$url = $this->webroot($path);
}
if ($path{0} !== '/') {
$path = CSS_URL . $path;
}
if (COMPRESS_CSS) {
$path = str_replace('css/', 'ccss/', $path);
}
$url = $this->webroot($path);
if ($rel == 'import') {
$out = sprintf($this->tags['style'], $this->_parseAttributes($htmlAttributes, null, '', ' '), '@import url(' . $url . ');');
} else {
@ -421,21 +426,34 @@ class HtmlHelper extends AppHelper {
* @param array $htmlAttributes Array of HTML attributes.
* @return string
*/
function image($path, $htmlAttributes = array()) {
function image($path, $options = array()) {
if (is_array($path)) {
$url = Router::url($path);
$path = Router::url($path);
} elseif ($path{0} === '/') {
$url = $this->webroot($path);
$path = $this->webroot($path);
} elseif (strpos($path, '://') !== false) {
$url = $path;
$path = $path;
} else {
$url = $this->webroot(IMAGES_URL . $path);
$path = $this->webroot(IMAGES_URL . $path);
}
if (!isset($htmlAttributes['alt'])) {
$htmlAttributes['alt'] = '';
if (!isset($options['alt'])) {
$options['alt'] = '';
}
return $this->output(sprintf($this->tags['image'], $url, $this->_parseAttributes($htmlAttributes, null, '', ' ')));
$url = false;
if (!empty($options['url'])) {
$url = $options['url'];
unset($options['url']);
}
$image = sprintf($this->tags['image'], $path, $this->_parseAttributes($options, null, '', ' '));
if ($url) {
return $this->output(sprintf($this->tags['link'], $this->url($url), null, $image));
}
return $this->output($image);
}
/**
* Creates a set of radio widgets.
@ -482,22 +500,33 @@ class HtmlHelper extends AppHelper {
* Returns a formatted string of table rows (TR's with TD's in them).
*
* @param array $data Array of table data
* @param array $oddTrOptionsHTML options for odd TR elements
* @param array $evenTrOptionsHTML options for even TR elements
* @param array $oddTrOptions HTML options for odd TR elements if true useCount is used
* @param array $evenTrOptions HTML options for even TR elements
* @param bool $useCount adds class "column-$i"
* @return string Formatted HTML
*/
function tableCells($data, $oddTrOptions = null, $evenTrOptions = null) {
function tableCells($data, $oddTrOptions = null, $evenTrOptions = null, $useCount = false) {
if (empty($data[0]) || !is_array($data[0])) {
$data = array($data);
}
static $count = 0;
if ($oddTrOptions === true) {
$useCount = true;
$oddTrOptions = null;
}
foreach ($data as $line) {
$count++;
$cellsOut = array();
$cellsOut = $cellOptions = array();
$i = 0;
foreach ($line as $cell) {
$cellsOut[] = sprintf($this->tags['tablecell'], null, $cell);
if (is_array($cell)) {
$cellOptions = $cell[1];
$cell = $cell[0];
} elseif ($useCount) {
$cellOptions['class'] = 'column-' . ++$i;
}
$cellsOut[] = sprintf($this->tags['tablecell'], $this->_parseAttributes($cellOptions), $cell);
}
$options = $this->_parseAttributes($count % 2 ? $oddTrOptions : $evenTrOptions);
$out[] = sprintf($this->tags['tablerow'], $options, join(' ', $cellsOut));

View file

@ -55,6 +55,10 @@ class HtmlHelperTest extends UnitTestCase {
function testImageLink() {
$result = $this->Html->link($this->Html->image('test.gif'), '#', array(), false, false, false);
$this->assertPattern('/^<a href="#"><img\s+src="img\/test.gif"\s+alt=""\s+\/><\/a>$/', $result);
$result = $this->Html->image('test.gif', array('url' => '#'));
$this->assertPattern('/^<a href="#"><img\s+src="img\/test.gif"\s+alt=""\s+\/><\/a>$/', $result);
}
function testImageTag() {
@ -102,6 +106,13 @@ class HtmlHelperTest extends UnitTestCase {
$this->assertPattern('/^<link[^<>]+type="text\/css"[^<>]+\/>$/', $result);
$this->assertPattern('/^<link[^<>]+href=".*css\/screen\.css\?1234"[^<>]+\/>$/', $result);
$this->assertNoPattern('/^<link[^<>]+[^rel|type|href]=[^<>]*>/', $result);
$result = $this->Html->css('http://whatever.com/screen.css?1234');
$this->assertPattern('/^<link[^<>]+\/>$/', $result);
$this->assertPattern('/^<link[^<>]+rel="stylesheet"[^<>]+\/>$/', $result);
$this->assertPattern('/^<link[^<>]+type="text\/css"[^<>]+\/>$/', $result);
$this->assertPattern('/^<link[^<>]+href="http:\/\/.*\/screen\.css\?1234"[^<>]+\/>$/', $result);
$this->assertNoPattern('/^<link[^<>]+[^rel|type|href]=[^<>]*>/', $result);
}
function testBreadcrumb() {
@ -246,6 +257,26 @@ class HtmlHelperTest extends UnitTestCase {
$this->assertPattern('/^<meta[^<>]+content="ALL"\/>$/', $result);
}
function testTableCells() {
$tr = array('td content 1',
array('td content 2', array("width"=>"100px")),
array('td content 3', "width=100px")
);
$result = $this->Html->tableCells($tr);
$this->assertEqual('<tr><td>td content 1</td> <td width="100px">td content 2</td> <td width=100px>td content 3</td></tr>', $result);
$tr = array('td content 1', 'td content 2', 'td content 3');
$result = $this->Html->tableCells($tr, null, null, true);
$this->assertEqual('<tr><td class="column-1">td content 1</td> <td class="column-2">td content 2</td> <td class="column-3">td content 3</td></tr>', $result);
$tr = array('td content 1', 'td content 2', 'td content 3');
$result = $this->Html->tableCells($tr, true);
$this->assertEqual('<tr><td class="column-1">td content 1</td> <td class="column-2">td content 2</td> <td class="column-3">td content 3</td></tr>', $result);
}
function tearDown() {
unset($this->Html);
}