Skip to content

Commit 43b3eb3

Browse files
committed
Only for "keywords" and "description" meta tags "content" attr should be treated as string
1 parent bbac0fa commit 43b3eb3

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
## Unreleased
44

5-
- HtmlFilter: <script> content should be filtered out.
5+
### Fixed
66

7+
- HtmlFilter: <script> content should be filtered out.
8+
- HtmlFilter: only for "keywords" and "description" meta tags "content" attr should be treated as
9+
string.
710

811
## 1.7.1 - 2017-05-01
912

src/Source/Filter/HtmlFilter.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ class HtmlFilter implements Filter
6464
'title'
6565
];
6666

67+
/**
68+
* Meta tag names with text content.
69+
*
70+
* @var string[]
71+
*/
72+
static private $textMetaTags = [
73+
'description',
74+
'keywords'
75+
];
76+
6777
/**
6878
* Filter string.
6979
*
@@ -78,7 +88,7 @@ public function filter($string)
7888
$result = '';
7989

8090
$string = $this->filterEntities($string);
81-
$string = $this->filterHttpEquivMetaTags($string);
91+
$string = $this->filterMetaTags($string);
8292

8393
// Current/last tag name
8494
$tagName = null;
@@ -208,18 +218,24 @@ function ($match) {
208218
}
209219

210220
/**
211-
* Replace meta tags with HTTP header equivalents.
221+
* Replace non-text meta tags.
212222
*
213223
* @param string $string
214224
*
215225
* @return string
216226
*/
217-
private function filterHttpEquivMetaTags($string)
227+
private function filterMetaTags($string)
218228
{
219229
return preg_replace_callback(
220-
'/<meta[^>]+http-equiv=[^>]+>/i',
230+
'/<meta[^>]+(http-equiv\s*=|name\s*=\s*["\']?([^>"\']+))[^>]*>/i',
221231
function ($match) {
222-
return str_repeat(' ', strlen($match[0]));
232+
if (count($match) < 3
233+
|| !in_array(strtolower($match[2]), self::$textMetaTags, true)
234+
) {
235+
return str_repeat(' ', strlen($match[0]));
236+
}
237+
238+
return $match[0];
223239
},
224240
$string
225241
);

tests/Source/Filter/HtmlFilterTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ public function testMetaContent()
3939
$html =
4040
'<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html" />' . "\n" .
4141
'<meta name="Keywords" content="Foo">' . "\n" .
42+
'<meta name="foo" content="Foobar">' . "\n" .
4243
'<meta name="description" content="Bar">';
4344
$text =
4445
" \n" .
4546
" Foo \n" .
47+
" \n" .
4648
' Bar ';
4749
static::assertEquals($text, $filter->filter($html));
4850
}

0 commit comments

Comments
 (0)