diff options
author | Steven Wittens <steven@10.no-reply.drupal.org> | 2005-04-20 02:55:20 +0000 |
---|---|---|
committer | Steven Wittens <steven@10.no-reply.drupal.org> | 2005-04-20 02:55:20 +0000 |
commit | 755da4bc0e52b8f2ce8a2a470974b6a4a322bc53 (patch) | |
tree | a6045c4d9ac631f211c6551a21e3a737c3cf516b | |
parent | 05e9c8c76ce5c0bf0079b18f27d4e7cced5ee759 (diff) | |
download | brdo-755da4bc0e52b8f2ce8a2a470974b6a4a322bc53.tar.gz brdo-755da4bc0e52b8f2ce8a2a470974b6a4a322bc53.tar.bz2 |
- Make the auto-linebreak filter also ignore the contents of <script>.
This makes it easier to use JavaScript (e.g. Google Adsense) inside blocks.
-rw-r--r-- | modules/filter.module | 28 | ||||
-rw-r--r-- | modules/filter/filter.module | 28 |
2 files changed, 44 insertions, 12 deletions
diff --git a/modules/filter.module b/modules/filter.module index dd142ca24..c77c51351 100644 --- a/modules/filter.module +++ b/modules/filter.module @@ -942,18 +942,34 @@ function _filter_html($text, $format) { * Based on: http://photomatt.net/scripts/autop */ function _filter_autop($text) { - // Split at <pre> and </pre> tags - $chunks = preg_split('@(</?pre[^>]*>)@', $text, -1, PREG_SPLIT_DELIM_CAPTURE); + // Split at <pre>, <script> and </pre>, </script> tags. + // We don't apply any processing to the contents of these tags to avoid messing + // up code. We look for matched pairs and allow basic nesting. For example: + // "processed <pre> ignored <script> ignored </script> ignored </pre> processed" + $chunks = preg_split('@(</?(?:pre|script)[^>]*>)@', $text, -1, PREG_SPLIT_DELIM_CAPTURE); // Note: PHP ensures the array consists of alternating delimiters and literals // and begins and ends with a literal (inserting NULL as required). - $pre = false; + $ignore = false; + $ignoretag = ''; $output = ''; foreach ($chunks as $i => $chunk) { if ($i % 2) { - // Opening or closing pre tag? - $pre = ($chunk{1} != '/'); + // Opening or closing tag? + $open = ($chunk{1} != '/'); + list(, $tag) = split('[< ]', $chunk); + if (!$ignore) { + if ($open) { + $ignore = true; + $ignoretag = $tag; + } + } + // Only allow a matching tag to close it. + else if (!$open && $ignoretag == $tag) { + $ignore = false; + $ignoretag = ''; + } } - else if (!$pre) { + else if (!$ignore) { $chunk = preg_replace('|\n*$|', '', $chunk) ."\n\n"; // just to make things a little easier, pad the end $chunk = preg_replace('|<br />\s*<br />|', "\n\n", $chunk); $chunk = preg_replace('!(<(?:table|ul|ol|li|pre|form|blockquote|h[1-6])[^>]*>)!', "\n$1", $chunk); // Space things out a little diff --git a/modules/filter/filter.module b/modules/filter/filter.module index dd142ca24..c77c51351 100644 --- a/modules/filter/filter.module +++ b/modules/filter/filter.module @@ -942,18 +942,34 @@ function _filter_html($text, $format) { * Based on: http://photomatt.net/scripts/autop */ function _filter_autop($text) { - // Split at <pre> and </pre> tags - $chunks = preg_split('@(</?pre[^>]*>)@', $text, -1, PREG_SPLIT_DELIM_CAPTURE); + // Split at <pre>, <script> and </pre>, </script> tags. + // We don't apply any processing to the contents of these tags to avoid messing + // up code. We look for matched pairs and allow basic nesting. For example: + // "processed <pre> ignored <script> ignored </script> ignored </pre> processed" + $chunks = preg_split('@(</?(?:pre|script)[^>]*>)@', $text, -1, PREG_SPLIT_DELIM_CAPTURE); // Note: PHP ensures the array consists of alternating delimiters and literals // and begins and ends with a literal (inserting NULL as required). - $pre = false; + $ignore = false; + $ignoretag = ''; $output = ''; foreach ($chunks as $i => $chunk) { if ($i % 2) { - // Opening or closing pre tag? - $pre = ($chunk{1} != '/'); + // Opening or closing tag? + $open = ($chunk{1} != '/'); + list(, $tag) = split('[< ]', $chunk); + if (!$ignore) { + if ($open) { + $ignore = true; + $ignoretag = $tag; + } + } + // Only allow a matching tag to close it. + else if (!$open && $ignoretag == $tag) { + $ignore = false; + $ignoretag = ''; + } } - else if (!$pre) { + else if (!$ignore) { $chunk = preg_replace('|\n*$|', '', $chunk) ."\n\n"; // just to make things a little easier, pad the end $chunk = preg_replace('|<br />\s*<br />|', "\n\n", $chunk); $chunk = preg_replace('!(<(?:table|ul|ol|li|pre|form|blockquote|h[1-6])[^>]*>)!', "\n$1", $chunk); // Space things out a little |