@@ -13,65 +13,65 @@ |
||
| 13 | 13 | class RemoveComments implements ManipulationInterface |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Patterns for white-listing comments inside content |
|
| 18 | - * |
|
| 19 | - * @var array |
|
| 20 | - */ |
|
| 21 | - protected $whiteListCommentsPatterns = []; |
|
| 16 | + /** |
|
| 17 | + * Patterns for white-listing comments inside content |
|
| 18 | + * |
|
| 19 | + * @var array |
|
| 20 | + */ |
|
| 21 | + protected $whiteListCommentsPatterns = []; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * @param string $html The original HTML |
|
| 25 | - * @param array $configuration Configuration |
|
| 26 | - * |
|
| 27 | - * @return string the manipulated HTML |
|
| 28 | - */ |
|
| 29 | - public function manipulate($html, array $configuration = []) |
|
| 30 | - { |
|
| 31 | - if (isset($configuration['keep.'])) { |
|
| 32 | - $this->whiteListCommentsPatterns = $configuration['keep.']; |
|
| 33 | - } |
|
| 23 | + /** |
|
| 24 | + * @param string $html The original HTML |
|
| 25 | + * @param array $configuration Configuration |
|
| 26 | + * |
|
| 27 | + * @return string the manipulated HTML |
|
| 28 | + */ |
|
| 29 | + public function manipulate($html, array $configuration = []) |
|
| 30 | + { |
|
| 31 | + if (isset($configuration['keep.'])) { |
|
| 32 | + $this->whiteListCommentsPatterns = $configuration['keep.']; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - // match all styles, scripts and comments |
|
| 36 | - $matches = []; |
|
| 37 | - preg_match_all( |
|
| 38 | - '/(?s)((<!--.*?-->)|(<[ \n\r]*style[^>]*>.*?<[ \n\r]*\/style[^>]*>)|(<[ \n\r]*script[^>]*>.*?<[ \n\r]*\/script[^>]*>))/im', |
|
| 39 | - $html, |
|
| 40 | - $matches |
|
| 41 | - ); |
|
| 42 | - foreach ($matches[0] as $tag) { |
|
| 43 | - if ($this->keepComment($tag) === false) { |
|
| 44 | - $html = str_replace($tag, '', $html); |
|
| 45 | - } |
|
| 46 | - } |
|
| 47 | - return $html; |
|
| 48 | - } |
|
| 35 | + // match all styles, scripts and comments |
|
| 36 | + $matches = []; |
|
| 37 | + preg_match_all( |
|
| 38 | + '/(?s)((<!--.*?-->)|(<[ \n\r]*style[^>]*>.*?<[ \n\r]*\/style[^>]*>)|(<[ \n\r]*script[^>]*>.*?<[ \n\r]*\/script[^>]*>))/im', |
|
| 39 | + $html, |
|
| 40 | + $matches |
|
| 41 | + ); |
|
| 42 | + foreach ($matches[0] as $tag) { |
|
| 43 | + if ($this->keepComment($tag) === false) { |
|
| 44 | + $html = str_replace($tag, '', $html); |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | + return $html; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Check if a comment is defined to be kept in a pattern whiteListOfComments |
|
| 52 | - * |
|
| 53 | - * @param string $commentHtml |
|
| 54 | - * |
|
| 55 | - * @return boolean |
|
| 56 | - */ |
|
| 57 | - protected function keepComment($commentHtml) |
|
| 58 | - { |
|
| 59 | - // if not even a comment, skip this |
|
| 60 | - if (!preg_match('/^\<\!\-\-(.*?)\-\-\>$/usi', $commentHtml)) { |
|
| 61 | - return true; |
|
| 62 | - } |
|
| 50 | + /** |
|
| 51 | + * Check if a comment is defined to be kept in a pattern whiteListOfComments |
|
| 52 | + * |
|
| 53 | + * @param string $commentHtml |
|
| 54 | + * |
|
| 55 | + * @return boolean |
|
| 56 | + */ |
|
| 57 | + protected function keepComment($commentHtml) |
|
| 58 | + { |
|
| 59 | + // if not even a comment, skip this |
|
| 60 | + if (!preg_match('/^\<\!\-\-(.*?)\-\-\>$/usi', $commentHtml)) { |
|
| 61 | + return true; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - // if not defined in white list |
|
| 65 | - if (!empty($this->whiteListCommentsPatterns)) { |
|
| 66 | - $commentHtml = str_replace("<!--", "", $commentHtml); |
|
| 67 | - $commentHtml = str_replace("-->", "", $commentHtml); |
|
| 68 | - $commentHtml = trim($commentHtml); |
|
| 69 | - foreach ($this->whiteListCommentsPatterns as $pattern) { |
|
| 70 | - if (!empty($pattern) && preg_match($pattern, $commentHtml)) { |
|
| 71 | - return true; |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - return false; |
|
| 76 | - } |
|
| 64 | + // if not defined in white list |
|
| 65 | + if (!empty($this->whiteListCommentsPatterns)) { |
|
| 66 | + $commentHtml = str_replace("<!--", "", $commentHtml); |
|
| 67 | + $commentHtml = str_replace("-->", "", $commentHtml); |
|
| 68 | + $commentHtml = trim($commentHtml); |
|
| 69 | + foreach ($this->whiteListCommentsPatterns as $pattern) { |
|
| 70 | + if (!empty($pattern) && preg_match($pattern, $commentHtml)) { |
|
| 71 | + return true; |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + return false; |
|
| 76 | + } |
|
| 77 | 77 | } |