Skip to content

Commit

Permalink
Html: chars '<' in attributes are encoded in XHTML
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 5, 2015
1 parent adedbf5 commit 1d98085
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Utils/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,12 @@ public function attributes()
}

$q = strpos($value, '"') === FALSE ? '"' : "'";
$s .= ' ' . $key . '='
. $q . str_replace(array('&', $q), array('&amp;', $q === '"' ? '&quot;' : '&#39;'), $value)
$s .= ' ' . $key . '=' . $q
. str_replace(
array('&', $q, '<'),
array('&amp;', $q === '"' ? '&quot;' : '&#39;', self::$xhtml ? '&lt;' : '<'),
$value
)
. (strpos($value, '`') !== FALSE && strpbrk($value, ' <>"\'') === FALSE ? ' ' : '')
. $q;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Utils/Html.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ test(function () { // small & big numbers


test(function () { // attributes escaping
Html::$xhtml = TRUE;
Assert::same('<a one=\'"\' two="\'" three="&lt;>" four="&amp;amp;"></a>', (string) Html::el('a')->one('"')->two("'")->three('<>')->four('&amp;'));

Html::$xhtml = FALSE;
Assert::same('<a one=\'"\' two="\'" three="<>" four="&amp;amp;"></a>', (string) Html::el('a')->one('"')->two("'")->three('<>')->four('&amp;'));
Assert::same('<a one="``xx "></a>', (string) Html::el('a')->one('``xx')); // mXSS
});
Expand Down

0 comments on commit 1d98085

Please sign in to comment.