Skip to content

Commit 5bc0f0c

Browse files
committed
Replacing sprintf using Html tags by useTag.
1 parent 8cd5477 commit 5bc0f0c

File tree

2 files changed

+34
-92
lines changed

2 files changed

+34
-92
lines changed

cake/libs/view/helpers/form.php

Lines changed: 32 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,11 @@ function create($model = null, $options = array()) {
314314
}
315315

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

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

325324
/**
@@ -372,7 +371,7 @@ public function end($options = null) {
372371
$this->fields = array();
373372
}
374373
$this->setEntity(null);
375-
$out .= $this->Html->tags['formend'];
374+
$out .= $this->Html->useTag('formend');
376375

377376
$this->_View->modelScope = false;
378377
return $out;
@@ -407,8 +406,7 @@ public function secure($fields = array()) {
407406
'value' => urlencode($fields . ':' . $locked),
408407
'id' => 'TokenFields' . mt_rand()
409408
));
410-
$out = sprintf($this->Html->tags['block'], ' style="display:none;"', $out);
411-
return $out;
409+
return $this->Html->useTag('block', ' style="display:none;"', $out);
412410
}
413411

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

565-
return sprintf(
566-
$this->Html->tags['label'],
567-
$labelFor,
568-
$this->_parseAttributes($options), $text
569-
);
563+
return $this->Html->useTag('label', $labelFor, $options, $text);
570564
}
571565

572566
/**
@@ -657,13 +651,9 @@ public function inputs($fields = null, $blacklist = null) {
657651
}
658652

659653
if ($fieldset && $legend) {
660-
return sprintf(
661-
$this->Html->tags['fieldset'],
662-
$fieldsetClass,
663-
sprintf($this->Html->tags['legend'], $legend) . $out
664-
);
654+
return $this->Html->useTag('fieldset', $fieldsetClass, $this->Html->useTag('legend', $legend) . $out);
665655
} elseif ($fieldset) {
666-
return sprintf($this->Html->tags['fieldset'], $fieldsetClass, $out);
656+
return $this->Html->useTag('fieldset', $fieldsetClass, $out);
667657
} else {
668658
return $out;
669659
}
@@ -1019,11 +1009,7 @@ public function checkbox($fieldName, $options = array()) {
10191009
}
10201010
unset($options['hiddenField']);
10211011

1022-
return $output . sprintf(
1023-
$this->Html->tags['checkbox'],
1024-
$options['name'],
1025-
$this->_parseAttributes($options, array('name'), null, ' ')
1026-
);
1012+
return $output . $this->Html->useTag('checkbox', $options['name'], array_diff_key($options, array('name' => '')));
10271013
}
10281014

10291015
/**
@@ -1085,20 +1071,17 @@ public function radio($fieldName, $options = array(), $attributes = array()) {
10851071
if (isset($value) && $optValue == $value) {
10861072
$optionsHere['checked'] = 'checked';
10871073
}
1088-
$parsedOptions = $this->_parseAttributes(
1089-
array_merge($attributes, $optionsHere),
1090-
array('name', 'type', 'id'), '', ' '
1091-
);
10921074
$tagName = Inflector::camelize(
10931075
$attributes['id'] . '_' . Inflector::slug($optValue)
10941076
);
10951077

10961078
if ($label) {
1097-
$optTitle = sprintf($this->Html->tags['label'], $tagName, null, $optTitle);
1079+
$optTitle = $this->Html->useTag('label', $tagName, '', $optTitle);
10981080
}
1099-
$out[] = sprintf(
1100-
$this->Html->tags['radio'], $attributes['name'],
1101-
$tagName, $parsedOptions, $optTitle
1081+
$allOptions = array_merge($attributes, $optionsHere);
1082+
$out[] = $this->Html->useTag('radio', $attributes['name'], $tagName,
1083+
array_diff_key($allOptions, array('name' => '', 'type' => '', 'id' => '')),
1084+
$optTitle
11021085
);
11031086
}
11041087
$hidden = null;
@@ -1113,10 +1096,7 @@ public function radio($fieldName, $options = array(), $attributes = array()) {
11131096
$out = $hidden . implode($inbetween, $out);
11141097

11151098
if ($legend) {
1116-
$out = sprintf(
1117-
$this->Html->tags['fieldset'], '',
1118-
sprintf($this->Html->tags['legend'], $legend) . $out
1119-
);
1099+
$out = $this->Html->useTag('fieldset', '', $this->Html->useTag('legend', $legend) . $out);
11201100
}
11211101
return $out;
11221102
}
@@ -1154,11 +1134,7 @@ public function __call($method, $params) {
11541134
$options['type'] = $method;
11551135
}
11561136
$options = $this->_initInputField($params[0], $options);
1157-
return sprintf(
1158-
$this->Html->tags['input'],
1159-
$options['name'],
1160-
$this->_parseAttributes($options, array('name'), null, ' ')
1161-
);
1137+
return $this->Html->useTag('input', $options['name'], array_diff_key($options, array('name' => '')));
11621138
}
11631139

11641140
/**
@@ -1185,12 +1161,7 @@ public function textarea($fieldName, $options = array()) {
11851161
}
11861162
unset($options['value']);
11871163
}
1188-
return sprintf(
1189-
$this->Html->tags['textarea'],
1190-
$options['name'],
1191-
$this->_parseAttributes($options, array('type', 'name'), null, ' '),
1192-
$value
1193-
);
1164+
return $this->Html->useTag('textarea', $options['name'], array_diff_key($options, array('type' => '', 'name' => '')), $value);
11941165
}
11951166

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

1221-
return sprintf(
1222-
$this->Html->tags['hidden'],
1223-
$options['name'],
1224-
$this->_parseAttributes($options, array('name', 'class'), '', ' ')
1225-
);
1192+
return $this->Html->useTag('hidden', $options['name'], array_diff_key($options, array('name' => '', 'class' => '')));
12261193
}
12271194

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

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

12501216
/**
@@ -1266,12 +1232,7 @@ public function button($title, $options = array()) {
12661232
if ($options['escape']) {
12671233
$title = h($title);
12681234
}
1269-
return sprintf(
1270-
$this->Html->tags['button'],
1271-
$options['type'],
1272-
$this->_parseAttributes($options, array('type'), ' ', ''),
1273-
$title
1274-
);
1235+
return $this->Html->useTag('button', $options['type'], array_diff_key($options, array('type' => '')), $title);
12751236
}
12761237

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

14091370
if (strpos($caption, '://') !== false) {
14101371
unset($options['type']);
1411-
$out .= $before . sprintf(
1412-
$this->Html->tags['submitimage'],
1413-
$caption,
1414-
$this->_parseAttributes($options, null, '', ' ')
1415-
) . $after;
1372+
$out .= $before . $this->Html->useTag('submitimage', $caption, $options) . $after;
14161373
} elseif (preg_match('/\.(jpg|jpe|jpeg|gif|png|ico)$/', $caption)) {
14171374
unset($options['type']);
14181375
if ($caption{0} !== '/') {
@@ -1421,17 +1378,10 @@ public function submit($caption = null, $options = array()) {
14211378
$caption = trim($caption, '/');
14221379
$url = $this->webroot($caption);
14231380
}
1424-
$out .= $before . sprintf(
1425-
$this->Html->tags['submitimage'],
1426-
$url,
1427-
$this->_parseAttributes($options, null, '', ' ')
1428-
) . $after;
1381+
$out .= $before . $this->Html->useTag('submitimage', $url, $options) . $after;
14291382
} else {
14301383
$options['value'] = $caption;
1431-
$out .= $before . sprintf(
1432-
$this->Html->tags['submit'],
1433-
$this->_parseAttributes($options, null, '', ' ')
1434-
). $after;
1384+
$out .= $before . $this->Html->useTag('submit', $options) . $after;
14351385
}
14361386

14371387
if (isset($divOptions)) {
@@ -1524,7 +1474,7 @@ public function select($fieldName, $options = array(), $attributes = array()) {
15241474
if (isset($attributes) && array_key_exists('multiple', $attributes)) {
15251475
$style = ($attributes['multiple'] === 'checkbox') ? 'checkbox' : null;
15261476
$template = ($style) ? 'checkboxmultiplestart' : 'selectmultiplestart';
1527-
$tag = $this->Html->tags[$template];
1477+
$tag = $template;
15281478
$hiddenAttributes = array(
15291479
'value' => '',
15301480
'id' => $attributes['id'] . ($style ? '' : '_'),
@@ -1533,16 +1483,14 @@ public function select($fieldName, $options = array(), $attributes = array()) {
15331483
);
15341484
$select[] = $this->hidden(null, $hiddenAttributes);
15351485
} else {
1536-
$tag = $this->Html->tags['selectstart'];
1486+
$tag = 'selectstart';
15371487
}
15381488

15391489
if (!empty($tag) || isset($template)) {
15401490
if (!isset($secure) || $secure == true) {
15411491
$this->__secure();
15421492
}
1543-
$select[] = sprintf($tag, $attributes['name'], $this->_parseAttributes(
1544-
$attributes, array('name', 'value'))
1545-
);
1493+
$select[] = $this->Html->useTag($tag, $attributes['name'], array_diff_key($attributes, array('name' => '', 'value' => '')));
15461494
}
15471495
$emptyMulti = (
15481496
$showEmpty !== null && $showEmpty !== false && !(
@@ -1566,7 +1514,7 @@ public function select($fieldName, $options = array(), $attributes = array()) {
15661514
));
15671515

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

@@ -2057,9 +2005,9 @@ function __selectOptions($elements = array(), $parents = array(), $showParents =
20572005
if (is_array($title) && (!isset($title['name']) || !isset($title['value']))) {
20582006
if (!empty($name)) {
20592007
if ($attributes['style'] === 'checkbox') {
2060-
$select[] = $this->Html->tags['fieldsetend'];
2008+
$select[] = $this->Html->useTag('fieldsetend');
20612009
} else {
2062-
$select[] = $this->Html->tags['optiongroupend'];
2010+
$select[] = $this->Html->useTag('optiongroupend');
20632011
}
20642012
$parents[] = $name;
20652013
}
@@ -2070,9 +2018,9 @@ function __selectOptions($elements = array(), $parents = array(), $showParents =
20702018
if (!empty($name)) {
20712019
$name = $attributes['escape'] ? h($name) : $name;
20722020
if ($attributes['style'] === 'checkbox') {
2073-
$select[] = sprintf($this->Html->tags['fieldsetstart'], $name);
2021+
$select[] = $this->Html->useTag('fieldsetstart', $name);
20742022
} else {
2075-
$select[] = sprintf($this->Html->tags['optiongroup'], $name, '');
2023+
$select[] = $this->Html->useTag('optiongroup', $name, '');
20762024
}
20772025
}
20782026
$name = null;
@@ -2117,16 +2065,10 @@ function __selectOptions($elements = array(), $parents = array(), $showParents =
21172065
$attributes['class'] = 'checkbox';
21182066
}
21192067
$label = $this->label(null, $title, $label);
2120-
$item = sprintf(
2121-
$this->Html->tags['checkboxmultiple'], $name,
2122-
$this->_parseAttributes($htmlOptions)
2123-
);
2068+
$item = $this->Html->useTag('checkboxmultiple', $name, $htmlOptions);
21242069
$select[] = $this->Html->div($attributes['class'], $item . $label);
21252070
} else {
2126-
$select[] = sprintf(
2127-
$this->Html->tags['selectoption'],
2128-
$name, $this->_parseAttributes($htmlOptions), $title
2129-
);
2071+
$select[] = $this->Html->useTag('selectoption', $name, $htmlOptions, $title);
21302072
}
21312073
}
21322074
}

cake/tests/cases/libs/view/helpers/form.test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5253,7 +5253,7 @@ public function testPostButton() {
52535253
));
52545254

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

52595259
/**
@@ -5293,7 +5293,7 @@ public function testPostLink() {
52935293
));
52945294

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

52995299
/**

0 commit comments

Comments
 (0)