Skip to content

Commit a4a146e

Browse files
authored
Merge pull request #263 from pfefferle/protect-code-html
2 parents b9f8294 + 7e3a5f4 commit a4a146e

File tree

4 files changed

+70
-18
lines changed

4 files changed

+70
-18
lines changed

includes/class-hashtag.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static function init() {
2020
/**
2121
* Filter to save #tags as real WordPress tags
2222
*
23-
* @param int $id the rev-id
23+
* @param int $id the rev-id
2424
* @param WP_Post $post the post
2525
*
2626
* @return
@@ -44,20 +44,31 @@ public static function insert_post( $id, $post ) {
4444
*/
4545
public static function the_content( $the_content ) {
4646
$protected_tags = array();
47+
$protect = function( $m ) use ( &$protected_tags ) {
48+
$c = count( $protected_tags );
49+
$protect = '!#!#PROTECT' . $c . '#!#!';
50+
$protected_tags[ $protect ] = $m[0];
51+
return $protect;
52+
};
53+
$the_content = preg_replace_callback(
54+
'#<!\[CDATA\[.*?\]\]>#is',
55+
$protect,
56+
$the_content
57+
);
58+
$the_content = preg_replace_callback(
59+
'#<(pre|code|textarea|style)\b[^>]*>.*?</\1[^>]*>#is',
60+
$protect,
61+
$the_content
62+
);
4763
$the_content = preg_replace_callback(
4864
'#<[^>]+>#i',
49-
function( $m ) use ( &$protected_tags ) {
50-
$c = count( $protected_tags );
51-
$protect = '!#!#PROTECT' . $c . '#!#!';
52-
$protected_tags[ $protect ] = $m[0];
53-
return $protect;
54-
},
65+
$protect,
5566
$the_content
5667
);
5768

5869
$the_content = \preg_replace_callback( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', array( '\Activitypub\Hashtag', 'replace_with_links' ), $the_content );
5970

60-
$the_content = str_replace( array_keys( $protected_tags ), array_values( $protected_tags ), $the_content );
71+
$the_content = str_replace( array_reverse( array_keys( $protected_tags ) ), array_reverse( array_values( $protected_tags ) ), $the_content );
6172

6273
return $the_content;
6374
}

includes/class-mention.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,31 @@ public static function init() {
2424
*/
2525
public static function the_content( $the_content ) {
2626
$protected_tags = array();
27+
$protect = function( $m ) use ( &$protected_tags ) {
28+
$c = count( $protected_tags );
29+
$protect = '!#!#PROTECT' . $c . '#!#!';
30+
$protected_tags[ $protect ] = $m[0];
31+
return $protect;
32+
};
33+
$the_content = preg_replace_callback(
34+
'#<!\[CDATA\[.*?\]\]>#is',
35+
$protect,
36+
$the_content
37+
);
38+
$the_content = preg_replace_callback(
39+
'#<(pre|code|textarea|style)\b[^>]*>.*?</\1[^>]*>#is',
40+
$protect,
41+
$the_content
42+
);
2743
$the_content = preg_replace_callback(
2844
'#<a.*?href=[^>]+>.*?</a>#i',
29-
function( $m ) use ( &$protected_tags ) {
30-
$c = count( $protected_tags );
31-
$protect = '!#!#PROTECT' . $c . '#!#!';
32-
$protected_tags[ $protect ] = $m[0];
33-
return $protect;
34-
},
45+
$protect,
3546
$the_content
3647
);
3748

3849
$the_content = \preg_replace_callback( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/', array( '\Activitypub\Mention', 'replace_with_links' ), $the_content );
3950

40-
$the_content = str_replace( array_keys( $protected_tags ), array_values( $protected_tags ), $the_content );
51+
$the_content = str_replace( array_reverse( array_keys( $protected_tags ) ), array_reverse( array_values( $protected_tags ) ), $the_content );
4152

4253
return $the_content;
4354
}
@@ -68,7 +79,7 @@ public static function replace_with_links( $result ) {
6879
/**
6980
* Extract the mentions from the post_content.
7081
*
71-
* @param array $mentions The already found mentions.
82+
* @param array $mentions The already found mentions.
7283
* @param string $post_content The post content.
7384
* @return mixed The discovered mentions.
7485
*/

tests/test-class-activitypub-hashtag.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ class Test_Activitypub_Hashtag extends WP_UnitTestCase {
55
*/
66
public function test_the_content( $content, $content_with_hashtag ) {
77
\wp_create_term( 'object', 'post_tag' );
8+
\wp_create_term( 'touch', 'post_tag' );
9+
\wp_create_term( 'ccc', 'post_tag' );
810
$object = \get_term_by( 'name', 'object', 'post_tag' );
911
$link = \get_term_link( $object, 'post_tag' );
1012

@@ -14,6 +16,21 @@ public function test_the_content( $content, $content_with_hashtag ) {
1416
}
1517

1618
public function the_content_provider() {
19+
$code = '<code>text with some #object and <a> tag inside</code>';
20+
$style = <<<ENDSTYLE
21+
<style type="text/css">
22+
<![CDATA[
23+
color: #ccc;
24+
]]>
25+
</style>
26+
ENDSTYLE;
27+
$pre = <<<ENDPRE
28+
<pre>
29+
Please don't #touch
30+
this.
31+
</pre>
32+
ENDPRE;
33+
$textarea = '<textarea name="test" rows="20">color: #ccc</textarea>';
1734
return array(
1835
array( 'test', 'test' ),
1936
array( '#test', '#test' ),
@@ -27,6 +44,10 @@ public function the_content_provider() {
2744
array( '<div>#object</div>', '<div>#object</div>' ),
2845
array( '<a>#object</a>', '<a>#object</a>' ),
2946
array( '<div style="color: #ccc;">object</a>', '<div style="color: #ccc;">object</a>' ),
47+
array( $code, $code ),
48+
array( $style, $style ),
49+
array( $textarea, $textarea ),
50+
array( $pre, $pre ),
3051
);
3152
}
3253
}

tests/test-class-activitypub-mention.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
class Test_Activitypub_Mention extends ActivityPub_TestCase_Cache_HTTP {
33
public static $users = array(
44
'[email protected]' => array(
5-
'url' => 'https://example.org/users/username',
6-
'name' => 'username',
5+
'url' => 'https://example.org/users/username',
6+
'name' => 'username',
77
),
88
);
99
/**
@@ -18,12 +18,21 @@ public function test_the_content( $content, $content_with_mention ) {
1818
}
1919

2020
public function the_content_provider() {
21+
$code = 'hallo <code>@[email protected]</code> test';
22+
$pre = <<<ENDPRE
23+
<pre>
24+
Please don't mention @[email protected]
25+
here.
26+
</pre>
27+
ENDPRE;
2128
return array(
2229
array( 'hallo @[email protected] test', 'hallo <a rel="mention" class="u-url mention" href="https://example.org/users/username">@<span>username</span></a> test' ),
2330
array( 'hallo @[email protected] test', 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@<span>pfefferle</span></a> test' ),
2431
array( 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@<span>pfefferle</span>@notiz.blog</a> test', 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@<span>pfefferle</span>@notiz.blog</a> test' ),
2532
array( 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@[email protected]</a> test', 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@[email protected]</a> test' ),
2633
array( 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/@pfefferle/">@[email protected]</a> test', 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/@pfefferle/">@[email protected]</a> test' ),
34+
array( $code, $code ),
35+
array( $pre, $pre ),
2736
);
2837
}
2938

0 commit comments

Comments
 (0)