Skip to content

Commit

Permalink
faster check substr at beginning of string
Browse files Browse the repository at this point in the history
  • Loading branch information
aidantwoods committed May 6, 2017
1 parent dc30cb4 commit 2e4afde
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions Parsedown.php
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,7 @@ protected function sanitiseElement(array $Element)
unset($Element['attributes'][$att]);
}
# dump onevent attribute
elseif (preg_match('/^on/i', $att))
elseif (self::striAtStart($att, 'on'))
{
unset($Element['attributes'][$att]);
}
Expand All @@ -1551,7 +1551,7 @@ protected function filterUnsafeUrlInAttribute(array $Element, $attribute)

foreach ($this->safeLinksWhitelist as $scheme)
{
if (stripos($Element['attributes'][$attribute], $scheme) === 0)
if (self::striAtStart($Element['attributes'][$attribute], $scheme))
{
$safe = true;

Expand Down Expand Up @@ -1584,6 +1584,20 @@ protected static function escape($text, $allowQuotes = false)
return htmlspecialchars($text, $allowQuotes ? ENT_NOQUOTES : ENT_QUOTES, 'UTF-8');
}

protected static function striAtStart($string, $needle)
{
$len = strlen($needle);

if ($len > strlen($string))
{
return false;
}
else
{
return strtolower(substr($string, 0, $len)) === strtolower($needle);
}
}

static function instance($name = 'default')
{
if (isset(self::$instances[$name]))
Expand Down

0 comments on commit 2e4afde

Please sign in to comment.