Skip to content

Commit

Permalink
Replacing sprintf using Html tags by useTag.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Jan 23, 2011
1 parent 8cd5477 commit 5bc0f0c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 92 deletions.
122 changes: 32 additions & 90 deletions cake/libs/view/helpers/form.php
Expand Up @@ -314,12 +314,11 @@ function create($model = null, $options = array()) {
}

if (!empty($append)) {
$append = sprintf($this->Html->tags['block'], ' style="display:none;"', $append);
$append = $this->Html->useTag('block', ' style="display:none;"', $append);
}

$this->setEntity($model . '.', true);
$attributes = $this->_parseAttributes($htmlAttributes, null, '');
return sprintf($this->Html->tags['form'], $attributes) . $append;
return $this->Html->useTag('form', $htmlAttributes) . $append;
}

/**
Expand Down Expand Up @@ -372,7 +371,7 @@ public function end($options = null) {
$this->fields = array();
}
$this->setEntity(null);
$out .= $this->Html->tags['formend'];
$out .= $this->Html->useTag('formend');

$this->_View->modelScope = false;
return $out;
Expand Down Expand Up @@ -407,8 +406,7 @@ public function secure($fields = array()) {
'value' => urlencode($fields . ':' . $locked),
'id' => 'TokenFields' . mt_rand()
));
$out = sprintf($this->Html->tags['block'], ' style="display:none;"', $out);
return $out;
return $this->Html->useTag('block', ' style="display:none;"', $out);
}

/**
Expand Down Expand Up @@ -562,11 +560,7 @@ function label($fieldName = null, $text = null, $options = array()) {
$labelFor = $this->domId($fieldName);
}

return sprintf(
$this->Html->tags['label'],
$labelFor,
$this->_parseAttributes($options), $text
);
return $this->Html->useTag('label', $labelFor, $options, $text);
}

/**
Expand Down Expand Up @@ -657,13 +651,9 @@ public function inputs($fields = null, $blacklist = null) {
}

if ($fieldset && $legend) {
return sprintf(
$this->Html->tags['fieldset'],
$fieldsetClass,
sprintf($this->Html->tags['legend'], $legend) . $out
);
return $this->Html->useTag('fieldset', $fieldsetClass, $this->Html->useTag('legend', $legend) . $out);
} elseif ($fieldset) {
return sprintf($this->Html->tags['fieldset'], $fieldsetClass, $out);
return $this->Html->useTag('fieldset', $fieldsetClass, $out);
} else {
return $out;
}
Expand Down Expand Up @@ -1019,11 +1009,7 @@ public function checkbox($fieldName, $options = array()) {
}
unset($options['hiddenField']);

return $output . sprintf(
$this->Html->tags['checkbox'],
$options['name'],
$this->_parseAttributes($options, array('name'), null, ' ')
);
return $output . $this->Html->useTag('checkbox', $options['name'], array_diff_key($options, array('name' => '')));
}

/**
Expand Down Expand Up @@ -1085,20 +1071,17 @@ public function radio($fieldName, $options = array(), $attributes = array()) {
if (isset($value) && $optValue == $value) {
$optionsHere['checked'] = 'checked';
}
$parsedOptions = $this->_parseAttributes(
array_merge($attributes, $optionsHere),
array('name', 'type', 'id'), '', ' '
);
$tagName = Inflector::camelize(
$attributes['id'] . '_' . Inflector::slug($optValue)
);

if ($label) {
$optTitle = sprintf($this->Html->tags['label'], $tagName, null, $optTitle);
$optTitle = $this->Html->useTag('label', $tagName, '', $optTitle);
}
$out[] = sprintf(
$this->Html->tags['radio'], $attributes['name'],
$tagName, $parsedOptions, $optTitle
$allOptions = array_merge($attributes, $optionsHere);
$out[] = $this->Html->useTag('radio', $attributes['name'], $tagName,
array_diff_key($allOptions, array('name' => '', 'type' => '', 'id' => '')),
$optTitle
);
}
$hidden = null;
Expand All @@ -1113,10 +1096,7 @@ public function radio($fieldName, $options = array(), $attributes = array()) {
$out = $hidden . implode($inbetween, $out);

if ($legend) {
$out = sprintf(
$this->Html->tags['fieldset'], '',
sprintf($this->Html->tags['legend'], $legend) . $out
);
$out = $this->Html->useTag('fieldset', '', $this->Html->useTag('legend', $legend) . $out);
}
return $out;
}
Expand Down Expand Up @@ -1154,11 +1134,7 @@ public function __call($method, $params) {
$options['type'] = $method;
}
$options = $this->_initInputField($params[0], $options);
return sprintf(
$this->Html->tags['input'],
$options['name'],
$this->_parseAttributes($options, array('name'), null, ' ')
);
return $this->Html->useTag('input', $options['name'], array_diff_key($options, array('name' => '')));
}

/**
Expand All @@ -1185,12 +1161,7 @@ public function textarea($fieldName, $options = array()) {
}
unset($options['value']);
}
return sprintf(
$this->Html->tags['textarea'],
$options['name'],
$this->_parseAttributes($options, array('type', 'name'), null, ' '),
$value
);
return $this->Html->useTag('textarea', $options['name'], array_diff_key($options, array('type' => '', 'name' => '')), $value);
}

/**
Expand Down Expand Up @@ -1218,11 +1189,7 @@ public function hidden($fieldName, $options = array()) {
$this->__secure(null, '' . $options['value']);
}

return sprintf(
$this->Html->tags['hidden'],
$options['name'],
$this->_parseAttributes($options, array('name', 'class'), '', ' ')
);
return $this->Html->useTag('hidden', $options['name'], array_diff_key($options, array('name' => '', 'class' => '')));
}

/**
Expand All @@ -1243,8 +1210,7 @@ public function file($fieldName, $options = array()) {
$this->__secure(array_merge($field, array($suffix)));
}

$attributes = $this->_parseAttributes($options, array('name'), '', ' ');
return sprintf($this->Html->tags['file'], $options['name'], $attributes);
return $this->Html->useTag('file', $options['name'], array_diff_key($options, array('name' => '')));
}

/**
Expand All @@ -1266,12 +1232,7 @@ public function button($title, $options = array()) {
if ($options['escape']) {
$title = h($title);
}
return sprintf(
$this->Html->tags['button'],
$options['type'],
$this->_parseAttributes($options, array('type'), ' ', ''),
$title
);
return $this->Html->useTag('button', $options['type'], array_diff_key($options, array('type' => '')), $title);
}

/**
Expand Down Expand Up @@ -1408,11 +1369,7 @@ public function submit($caption = null, $options = array()) {

if (strpos($caption, '://') !== false) {
unset($options['type']);
$out .= $before . sprintf(
$this->Html->tags['submitimage'],
$caption,
$this->_parseAttributes($options, null, '', ' ')
) . $after;
$out .= $before . $this->Html->useTag('submitimage', $caption, $options) . $after;
} elseif (preg_match('/\.(jpg|jpe|jpeg|gif|png|ico)$/', $caption)) {
unset($options['type']);
if ($caption{0} !== '/') {
Expand All @@ -1421,17 +1378,10 @@ public function submit($caption = null, $options = array()) {
$caption = trim($caption, '/');
$url = $this->webroot($caption);
}
$out .= $before . sprintf(
$this->Html->tags['submitimage'],
$url,
$this->_parseAttributes($options, null, '', ' ')
) . $after;
$out .= $before . $this->Html->useTag('submitimage', $url, $options) . $after;
} else {
$options['value'] = $caption;
$out .= $before . sprintf(
$this->Html->tags['submit'],
$this->_parseAttributes($options, null, '', ' ')
). $after;
$out .= $before . $this->Html->useTag('submit', $options) . $after;
}

if (isset($divOptions)) {
Expand Down Expand Up @@ -1524,7 +1474,7 @@ public function select($fieldName, $options = array(), $attributes = array()) {
if (isset($attributes) && array_key_exists('multiple', $attributes)) {
$style = ($attributes['multiple'] === 'checkbox') ? 'checkbox' : null;
$template = ($style) ? 'checkboxmultiplestart' : 'selectmultiplestart';
$tag = $this->Html->tags[$template];
$tag = $template;
$hiddenAttributes = array(
'value' => '',
'id' => $attributes['id'] . ($style ? '' : '_'),
Expand All @@ -1533,16 +1483,14 @@ public function select($fieldName, $options = array(), $attributes = array()) {
);
$select[] = $this->hidden(null, $hiddenAttributes);
} else {
$tag = $this->Html->tags['selectstart'];
$tag = 'selectstart';
}

if (!empty($tag) || isset($template)) {
if (!isset($secure) || $secure == true) {
$this->__secure();
}
$select[] = sprintf($tag, $attributes['name'], $this->_parseAttributes(
$attributes, array('name', 'value'))
);
$select[] = $this->Html->useTag($tag, $attributes['name'], array_diff_key($attributes, array('name' => '', 'value' => '')));
}
$emptyMulti = (
$showEmpty !== null && $showEmpty !== false && !(
Expand All @@ -1566,7 +1514,7 @@ public function select($fieldName, $options = array(), $attributes = array()) {
));

$template = ($style == 'checkbox') ? 'checkboxmultipleend' : 'selectend';
$select[] = $this->Html->tags[$template];
$select[] = $this->Html->useTag($template);
return implode("\n", $select);
}

Expand Down Expand Up @@ -2057,9 +2005,9 @@ function __selectOptions($elements = array(), $parents = array(), $showParents =
if (is_array($title) && (!isset($title['name']) || !isset($title['value']))) {
if (!empty($name)) {
if ($attributes['style'] === 'checkbox') {
$select[] = $this->Html->tags['fieldsetend'];
$select[] = $this->Html->useTag('fieldsetend');
} else {
$select[] = $this->Html->tags['optiongroupend'];
$select[] = $this->Html->useTag('optiongroupend');
}
$parents[] = $name;
}
Expand All @@ -2070,9 +2018,9 @@ function __selectOptions($elements = array(), $parents = array(), $showParents =
if (!empty($name)) {
$name = $attributes['escape'] ? h($name) : $name;
if ($attributes['style'] === 'checkbox') {
$select[] = sprintf($this->Html->tags['fieldsetstart'], $name);
$select[] = $this->Html->useTag('fieldsetstart', $name);
} else {
$select[] = sprintf($this->Html->tags['optiongroup'], $name, '');
$select[] = $this->Html->useTag('optiongroup', $name, '');
}
}
$name = null;
Expand Down Expand Up @@ -2117,16 +2065,10 @@ function __selectOptions($elements = array(), $parents = array(), $showParents =
$attributes['class'] = 'checkbox';
}
$label = $this->label(null, $title, $label);
$item = sprintf(
$this->Html->tags['checkboxmultiple'], $name,
$this->_parseAttributes($htmlOptions)
);
$item = $this->Html->useTag('checkboxmultiple', $name, $htmlOptions);
$select[] = $this->Html->div($attributes['class'], $item . $label);
} else {
$select[] = sprintf(
$this->Html->tags['selectoption'],
$name, $this->_parseAttributes($htmlOptions), $title
);
$select[] = $this->Html->useTag('selectoption', $name, $htmlOptions, $title);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions cake/tests/cases/libs/view/helpers/form.test.php
Expand Up @@ -5253,7 +5253,7 @@ public function testPostButton() {
));

$result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
$this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value" />') !== false);
$this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
}

/**
Expand Down Expand Up @@ -5293,7 +5293,7 @@ public function testPostLink() {
));

$result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
$this->assertTrue(strpos($result, '<input type="hidden" name="data[id]" value="1" />') !== false);
$this->assertTrue(strpos($result, '<input type="hidden" name="data[id]" value="1"/>') !== false);
}

/**
Expand Down

0 comments on commit 5bc0f0c

Please sign in to comment.