Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Finish updating HtmlHelper methods to use string templates.

  • Loading branch information...
commit 87a6be0a196f599a2d5be9fc3d4d28e6c1e155d5 1 parent 7dfdfaf
@markstory markstory authored
View
137 src/View/Helper/HtmlHelper.php
@@ -858,12 +858,21 @@ public function tableHeaders($names, $trOptions = null, $thOptions = null) {
$out = array();
foreach ($names as $arg) {
if (!is_array($arg)) {
- $out[] = sprintf($this->_tags['tableheader'], $this->_parseAttributes($thOptions), $arg);
+ $out[] = $this->formatTemplate('tableheader', [
+ 'attrs' => $this->_templater->formatAttributes($thOptions),
+ 'content' => $arg
+ ]);
} else {
- $out[] = sprintf($this->_tags['tableheader'], $this->_parseAttributes(current($arg)), key($arg));
+ $out[] = $this->formatTemplate('tableheader', [
+ 'attrs' => $this->_templater->formatAttributes(current($arg)),
+ 'content' => key($arg)
+ ]);
}
}
- return sprintf($this->_tags['tablerow'], $this->_parseAttributes($trOptions), implode(' ', $out));
+ return $this->formatTemplate('tablerow', [
+ 'attrs' => $this->_templater->formatAttributes($trOptions),
+ 'content' => implode(' ', $out)
+ ]);
}
/**
@@ -912,10 +921,16 @@ public function tableCells($data, $oddTrOptions = null, $evenTrOptions = null, $
} elseif ($useCount) {
$cellOptions['class'] = 'column-' . ++$i;
}
- $cellsOut[] = sprintf($this->_tags['tablecell'], $this->_parseAttributes($cellOptions), $cell);
+ $cellsOut[] = $this->formatTemplate('tablecell', [
+ 'attrs' => $this->_templater->formatAttributes($cellOptions),
+ 'content' => $cell
+ ]);
}
- $options = $this->_parseAttributes($count % 2 ? $oddTrOptions : $evenTrOptions);
- $out[] = sprintf($this->_tags['tablerow'], $options, implode(' ', $cellsOut));
+ $opts = $count % 2 ? $oddTrOptions : $evenTrOptions;
+ $out[] = $this->formatTemplate('tablerow', [
+ 'attrs' => $this->_templater->formatAttributes($opts),
+ 'content' => implode(' ', $cellsOut),
+ ]);
}
return implode("\n", $out);
}
@@ -947,7 +962,11 @@ public function tag($name, $text = null, $options = array()) {
} else {
$tag = 'tag';
}
- return sprintf($this->_tags[$tag], $name, $this->_parseAttributes($options, null, ' ', ''), $text, $name);
+ return $this->formatTemplate($tag, [
+ 'attrs' => $this->_templater->formatAttributes($options),
+ 'tag' => $name,
+ 'content' => $text,
+ ]);
}
/**
@@ -1016,7 +1035,10 @@ public function para($class, $text, $options = array()) {
if ($text === null) {
$tag = 'parastart';
}
- return sprintf($this->_tags[$tag], $this->_parseAttributes($options, null, ' ', ''), $text);
+ return $this->formatTemplate($tag, [
+ 'attrs' => $this->_templater->formatAttributes($options),
+ 'content' => $text,
+ ]);
}
/**
@@ -1097,7 +1119,10 @@ public function media($path, $options = array()) {
$source['type'] = $this->response->getMimeType($ext);
}
$source['src'] = $this->assetUrl($source['src'], $options);
- $sourceTags .= $this->useTag('tagselfclosing', 'source', $source);
+ $sourceTags .= $this->formatTemplate('tagselfclosing', [
+ 'tag' => 'source',
+ 'attrs' => $this->_templater->formatAttributes($source)
+ ]);
}
unset($source);
$options['text'] = $sourceTags . $options['text'];
@@ -1152,7 +1177,10 @@ public function nestedList($list, $options = array(), $itemOptions = array(), $t
$options = array();
}
$items = $this->_nestedListItem($list, $options, $itemOptions, $tag);
- return sprintf($this->_tags[$tag], $this->_parseAttributes($options, null, ' ', ''), $items);
+ return $this->formatTemplate($tag, [
+ 'attrs' => $this->_templater->formatAttributes($options),
+ 'content' => $items
+ ]);
}
/**
@@ -1178,97 +1206,16 @@ protected function _nestedListItem($items, $options, $itemOptions, $tag) {
} elseif (isset($itemOptions['odd']) && $index % 2 !== 0) {
$itemOptions['class'] = $itemOptions['odd'];
}
- $out .= sprintf($this->_tags['li'], $this->_parseAttributes($itemOptions, array('even', 'odd'), ' ', ''), $item);
+ $out .= $this->formatTemplate('li', [
+ 'attrs' => $this->_templater->formatAttributes($itemOptions, ['even', 'odd']),
+ 'content' => $item
+ ]);
$index++;
}
return $out;
}
/**
- * Load Html tag configuration.
- *
- * Loads a file from APP/Config that contains tag data. By default the file is expected
- * to be compatible with PhpConfig:
- *
- * `$this->Html->loadConfig('tags.php');`
- *
- * tags.php could look like:
- *
- * {{{
- * $tags = array(
- * 'meta' => '<meta %s>'
- * );
- * }}}
- *
- * If you wish to store tag definitions in another format you can give an array
- * containing the file name, and Engine class name:
- *
- * `$this->Html->loadConfig(array('tags.ini', 'ini'));`
- *
- * Its expected that the `tags` index will exist from any configuration file that is read.
- * You can also specify the path to read the configuration file from, if APP/Config is not
- * where the file is.
- *
- * `$this->Html->loadConfig('tags.php', APP . 'Lib/');`
- *
- * Configuration files can define the following sections:
- *
- * - `tags` The tags to replace.
- * - `minimizedAttributes` The attributes that are represented like `disabled="disabled"`
- * - `docTypes` Additional doctypes to use.
- * - `attributeFormat` Format for long attributes e.g. `'%s="%s"'`
- * - `minimizedAttributeFormat` Format for minimized attributes e.g. `'%s="%s"'`
- *
- * @param string|array $configFile String with the config file (load using PhpConfig) or an array with file and engine name
- * @param string $path Path with config file
- * @return mixed False to error or loaded configs
- * @throws \Cake\Error\ConfigureException
- * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#changing-the-tags-output-by-htmlhelper
- */
- public function loadConfig($configFile, $path = null) {
- if (!$path) {
- $path = APP . 'Config/';
- }
- $file = null;
- $engine = 'php';
-
- if (!is_array($configFile)) {
- $file = $configFile;
- } elseif (isset($configFile[0])) {
- $file = $configFile[0];
- if (isset($configFile[1])) {
- $engine = $configFile[1];
- }
- } else {
- throw new Error\ConfigureException('Cannot load the configuration file. Wrong "configFile" configuration.');
- }
-
- $engineClass = App::classname(Inflector::camelize($engine), 'Configure/Engine', 'Config');
- if (!$engineClass) {
- throw new Error\ConfigureException('Cannot load the configuration file. Unknown engine.');
- }
-
- $engineObj = new $engineClass($path);
- $configs = $engineObj->read($file);
- if (isset($configs['tags']) && is_array($configs['tags'])) {
- $this->_tags = array_merge($this->_tags, $configs['tags']);
- }
- if (isset($configs['minimizedAttributes']) && is_array($configs['minimizedAttributes'])) {
- $this->_minimizedAttributes = array_merge($this->_minimizedAttributes, $configs['minimizedAttributes']);
- }
- if (isset($configs['docTypes']) && is_array($configs['docTypes'])) {
- $this->_docTypes = array_merge($this->_docTypes, $configs['docTypes']);
- }
- if (isset($configs['attributeFormat'])) {
- $this->_attributeFormat = $configs['attributeFormat'];
- }
- if (isset($configs['minimizedAttributeFormat'])) {
- $this->_minimizedAttributeFormat = $configs['minimizedAttributeFormat'];
- }
- return $configs;
- }
-
-/**
* Event listeners.
*
* @return array
View
143 tests/TestCase/View/Helper/HtmlHelperTest.php
@@ -31,63 +31,6 @@
use Cake\View\Helper\HtmlHelper;
use Cake\View\View;
-class TestHtmlHelper extends HtmlHelper {
-
-/**
- * expose a method as public
- *
- * @param string $options
- * @param string $exclude
- * @param string $insertBefore
- * @param string $insertAfter
- * @return void
- */
- public function parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
- return $this->_parseAttributes($options, $exclude, $insertBefore, $insertAfter);
- }
-
-/**
- * Get a protected attribute value
- *
- * @param string $attribute
- * @return mixed
- */
- public function getAttribute($attribute) {
- if (!isset($this->{$attribute})) {
- return null;
- }
- return $this->{$attribute};
- }
-
-}
-
-/**
- * Html5TestHelper class
- *
- */
-class Html5TestHelper extends TestHtmlHelper {
-
-/**
- * Minimized
- *
- * @var array
- */
- protected $_minimizedAttributes = array('require', 'checked');
-
-/**
- * Allow compact use in HTML
- *
- * @var string
- */
- protected $_minimizedAttributeFormat = '%s';
-
-/**
- * Test to attribute format
- *
- * @var string
- */
- protected $_attributeFormat = 'data-%s="%s"';
-}
/**
* HtmlHelperTest class
@@ -125,7 +68,7 @@ public function setUp() {
parent::setUp();
$controller = $this->getMock('Cake\Controller\Controller');
$this->View = $this->getMock('Cake\View\View', array('append'), array($controller));
- $this->Html = new TestHtmlHelper($this->View);
+ $this->Html = new HtmlHelper($this->View);
$this->Html->request = new Request();
$this->Html->request->webroot = '';
@@ -1720,7 +1663,7 @@ public function testTableCells() {
$tr = array(
'td content 1',
array('td content 2', array("width" => "100px")),
- array('td content 3', "width=100px")
+ array('td content 3', array('width' => '100px'))
);
$result = $this->Html->tableCells($tr);
$expected = array(
@@ -2071,86 +2014,4 @@ public function testCrumbListZurbStyle() {
);
}
-/**
- * testLoadConfig method
- *
- * @return void
- */
-
- public function testLoadConfig() {
- $path = TEST_APP . 'TestApp/Config/';
-
- $result = $this->Html->loadConfig('htmlhelper_tags', $path);
- $expected = array(
- 'tags' => array(
- 'form' => 'start form',
- 'formend' => 'finish form',
- 'hiddenblock' => '<div class="hidden">%s</div>'
- )
- );
- $this->assertEquals($expected, $result);
- $tags = $this->Html->getAttribute('_tags');
- $this->assertEquals('start form', $tags['form']);
- $this->assertEquals('finish form', $tags['formend']);
- $this->assertEquals('</div>', $tags['blockend']);
-
- $result = $this->Html->loadConfig(array('htmlhelper_minimized.ini', 'ini'), $path);
- $expected = array(
- 'minimizedAttributeFormat' => 'format'
- );
- $this->assertEquals($expected, $result);
- $this->assertEquals('format', $this->Html->getAttribute('_minimizedAttributeFormat'));
- }
-
-/**
- * testLoadConfigWrongFile method
- *
- * @return void
- * @expectedException \Cake\Error\ConfigureException
- */
- public function testLoadConfigWrongFile() {
- $this->Html->loadConfig('wrong_file');
- }
-
-/**
- * testLoadConfigWrongEngine method
- *
- * @return void
- * @expectedException \Cake\Error\ConfigureException
- */
- public function testLoadConfigWrongEngine() {
- $path = TEST_APP . 'TestApp/Config/';
- $this->Html->loadConfig(array('htmlhelper_tags', 'wrong_engine'), $path);
- }
-
-/**
- * test parsing attributes.
- *
- * @return void
- */
- public function testParseAttributeCompact() {
- $helper = new TestHtmlHelper($this->View);
- $compact = array('compact', 'checked', 'declare', 'readonly', 'disabled',
- 'selected', 'defer', 'ismap', 'nohref', 'noshade', 'nowrap', 'multiple', 'noresize');
-
- foreach ($compact as $attribute) {
- foreach (array('true', true, 1, '1', $attribute) as $value) {
- $attrs = array($attribute => $value);
- $expected = ' ' . $attribute . '="' . $attribute . '"';
- $this->assertEquals($expected, $helper->parseAttributes($attrs), '%s Failed on ' . $value);
- }
- }
- $this->assertEquals(' compact="compact"', $helper->parseAttributes(array('compact')));
-
- $attrs = array('class' => array('foo', 'bar'));
- $expected = ' class="foo bar"';
- $this->assertEquals(' class="foo bar"', $helper->parseAttributes($attrs));
-
- $helper = new Html5TestHelper($this->View);
- $expected = ' require';
- $this->assertEquals($expected, $helper->parseAttributes(array('require')));
- $this->assertEquals($expected, $helper->parseAttributes(array('require' => true)));
- $this->assertEquals('', $helper->parseAttributes(array('require' => false)));
- }
-
}
Please sign in to comment.
Something went wrong with that request. Please try again.