-
-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Filters::spacelessHtml() Add support for use <script> as link to JS #196
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -316,26 +316,33 @@ public static function strip(FilterInfo $info, string $s): string | |
* @param bool $strip stripping mode | ||
* @return string HTML | ||
*/ | ||
public static function spacelessHtml(string $s, int $phase = null, bool &$strip = true): string | ||
public static function spacelessHtml(string $s, ?int $phase = null, bool &$strip = true): string | ||
{ | ||
if ($phase & PHP_OUTPUT_HANDLER_START) { | ||
$s = ltrim($s); | ||
} | ||
if ($phase & PHP_OUTPUT_HANDLER_FINAL) { | ||
$s = rtrim($s); | ||
} | ||
return preg_replace_callback( | ||
$return = (string) preg_replace_callback( // Other cases | ||
'#[ \t\r\n]+|<(/)?(textarea|pre|script)(?=\W)#si', | ||
function ($m) use (&$strip) { | ||
static function (array $m) use (&$strip): string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PHP does not have to create a new instance every time. |
||
if (empty($m[2])) { | ||
return $strip ? ' ' : $m[0]; | ||
} else { | ||
$strip = !empty($m[1]); | ||
return $m[0]; | ||
return (string) $strip ? ' ' : $m[0]; | ||
} | ||
$strip = !empty($m[1]); | ||
return (string) $m[0]; | ||
}, | ||
$s | ||
); | ||
$return = (string) preg_replace_callback( // <script> for include JS file | ||
'/<script\s*([^>]+?)>(?:\s*)<\/script>/', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this regexp work with input like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This input is not supported now. I'll try to come up with a better solution. |
||
static function (array $m) use (&$strip): string { | ||
return $strip ? '<script ' . trim(preg_replace('/\s+/', ' ', $m[1])) . '></script>' : (string) $m[0]; | ||
}, | ||
$return | ||
); | ||
return $return; | ||
} | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
(string)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because PhpStorm hint to me new type can be returned in future. For example
null
in case of error.