Protect cdata

This commit is contained in:
Alex Kirk 2023-01-27 16:59:15 +01:00
parent e7894f4c4a
commit 6ea46c5024
3 changed files with 27 additions and 25 deletions

View file

@ -44,24 +44,25 @@ class Hashtag {
*/ */
public static function the_content( $the_content ) { public static function the_content( $the_content ) {
$protected_tags = array(); $protected_tags = array();
$the_content = preg_replace_callback( $protect = function( $m ) use ( &$protected_tags ) {
'#<(pre|code|textarea|style)\b[^>]*>.*?</\1[^>]*>#is',
function( $m ) use ( &$protected_tags ) {
$c = count( $protected_tags ); $c = count( $protected_tags );
$protect = '!#!#PROTECT' . $c . '#!#!'; $protect = '!#!#PROTECT' . $c . '#!#!';
$protected_tags[ $protect ] = $m[0]; $protected_tags[ $protect ] = $m[0];
return $protect; return $protect;
}, };
$the_content = preg_replace_callback(
'#<![CDATA[.*?]]>#is',
$protect,
$the_content
);
$the_content = preg_replace_callback(
'#<(pre|code|textarea|style)\b[^>]*>.*?</\1[^>]*>#is',
$protect,
$the_content $the_content
); );
$the_content = preg_replace_callback( $the_content = preg_replace_callback(
'#<[^>]+>#i', '#<[^>]+>#i',
function( $m ) use ( &$protected_tags ) { $protect,
$c = count( $protected_tags );
$protect = '!#!#PROTECT' . $c . '#!#!';
$protected_tags[ $protect ] = $m[0];
return $protect;
},
$the_content $the_content
); );

View file

@ -24,24 +24,25 @@ class Mention {
*/ */
public static function the_content( $the_content ) { public static function the_content( $the_content ) {
$protected_tags = array(); $protected_tags = array();
$the_content = preg_replace_callback( $protect = function( $m ) use ( &$protected_tags ) {
'#<(pre|code|textarea|style)\b[^>]*>.*?</\1[^>]*>#is',
function( $m ) use ( &$protected_tags ) {
$c = count( $protected_tags ); $c = count( $protected_tags );
$protect = '!#!#PROTECT' . $c . '#!#!'; $protect = '!#!#PROTECT' . $c . '#!#!';
$protected_tags[ $protect ] = $m[0]; $protected_tags[ $protect ] = $m[0];
return $protect; return $protect;
}, };
$the_content = preg_replace_callback(
'#<![CDATA[.*?]]>#is',
$protect,
$the_content
);
$the_content = preg_replace_callback(
'#<(pre|code|textarea|style)\b[^>]*>.*?</\1[^>]*>#is',
$protect,
$the_content $the_content
); );
$the_content = preg_replace_callback( $the_content = preg_replace_callback(
'#<a.*?href=[^>]+>.*?</a>#i', '#<a.*?href=[^>]+>.*?</a>#i',
function( $m ) use ( &$protected_tags ) { $protect,
$c = count( $protected_tags );
$protect = '!#!#PROTECT' . $c . '#!#!';
$protected_tags[ $protect ] = $m[0];
return $protect;
},
$the_content $the_content
); );

View file

@ -19,7 +19,7 @@ class Test_Activitypub_Hashtag extends WP_UnitTestCase {
$code = '<code>text with some #object and <a> tag inside</code>'; $code = '<code>text with some #object and <a> tag inside</code>';
$style = <<<ENDSTYLE $style = <<<ENDSTYLE
<style type="text/css"> <style type="text/css">
<![[ <![CDATA[
color: #ccc; color: #ccc;
]]> ]]>
</style> </style>