Skip to content

Commit

Permalink
Html: refactoring of processing 'data' attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 5, 2015
1 parent 8b242c3 commit adedbf5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
27 changes: 10 additions & 17 deletions src/Utils/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,15 @@ public function attributes()
}

$s = '';
foreach ($this->attrs as $key => $value) {
$attrs = $this->attrs;
if (isset($attrs['data']) && is_array($attrs['data'])) { // deprecated
foreach ($attrs['data'] as $key => $value) {
$attrs['data-' . $key] = $value;
}
unset($attrs['data']);
}

foreach ($attrs as $key => $value) {
if ($value === NULL || $value === FALSE) {
continue;

Expand All @@ -520,22 +528,7 @@ public function attributes()
continue;

} elseif (is_array($value)) {
if ($key === 'data') { // deprecated
foreach ($value as $k => $v) {
if ($v !== NULL && $v !== FALSE) {
if (is_array($v)) {
$v = Json::encode($v);
}
$q = strpos($v, '"') === FALSE ? '"' : "'";
$s .= ' data-' . $k . '='
. $q . str_replace(array('&', $q), array('&', $q === '"' ? '"' : '''), $v)
. (strpos($v, '`') !== FALSE && strpbrk($v, ' <>"\'') === FALSE ? ' ' : '')
. $q;
}
}
continue;

} elseif (strncmp($key, 'data-', 5) === 0) {
if (strncmp($key, 'data-', 5) === 0) {
$value = Json::encode($value);

} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/Utils/Html.data.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test(function () { // deprecated
$el->{'data-x'} = 'x';
$el->data['mxss'] = '``two';

Assert::same('<div data-a="one" data-d="" data-e="two" data-mxss="``two " data-x="x"></div>', (string) $el);
Assert::same('<div data-x="x" data-a="one" data-d="" data-e="two" data-mxss="``two "></div>', (string) $el);
});


Expand Down

0 comments on commit adedbf5

Please sign in to comment.