From fa0641ba344cedd2170583f618fb8923c7e01988 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Sat, 31 May 2014 14:40:13 -0500
Subject: [PATCH] Issue #2274795 by Wim Leers | chx: Simplify (and defuse) the
 filter skipping code.

---
 core/modules/filter/filter.module | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)

diff --git a/core/modules/filter/filter.module b/core/modules/filter/filter.module
index 3efe9ad568e1..f25fe79830dc 100644
--- a/core/modules/filter/filter.module
+++ b/core/modules/filter/filter.module
@@ -362,11 +362,6 @@ function check_markup($text, $format_id = NULL, $langcode = '', $cache = FALSE,
     return '';
   }
 
-  // Prevent FilterInterface::TYPE_HTML_RESTRICTOR from being skipped.
-  if (in_array(FilterInterface::TYPE_HTML_RESTRICTOR, $filter_types_to_skip)) {
-    $filter_types_to_skip = array_diff($filter_types_to_skip, array(FilterInterface::TYPE_HTML_RESTRICTOR));
-  }
-
   // When certain filters should be skipped, don't perform caching.
   if ($filter_types_to_skip) {
     $cache = FALSE;
@@ -389,24 +384,24 @@ function check_markup($text, $format_id = NULL, $langcode = '', $cache = FALSE,
   // Get a complete list of filters, ordered properly.
   $filters = $format->filters();
 
+  $filter_must_be_applied = function($filter) use ($filter_types_to_skip) {
+    $enabled = $filter->status === TRUE;
+    $type = $filter->getType();
+    // Prevent FilterInterface::TYPE_HTML_RESTRICTOR from being skipped.
+    $filter_type_must_be_applied = $type == FilterInterface::TYPE_HTML_RESTRICTOR || !in_array($type, $filter_types_to_skip);
+    return $enabled && $filter_type_must_be_applied;
+  };
+
   // Give filters the chance to escape HTML-like data such as code or formulas.
   foreach ($filters as $filter) {
-    // If necessary, skip filters of a certain type.
-    if (in_array($filter->getType(), $filter_types_to_skip)) {
-      continue;
-    }
-    if ($filter->status) {
+    if ($filter_must_be_applied($filter)) {
       $text = $filter->prepare($text, $langcode, $cache, $cache_id);
     }
   }
 
   // Perform filtering.
   foreach ($filters as $filter) {
-    // If necessary, skip filters of a certain type.
-    if (in_array($filter->getType(), $filter_types_to_skip)) {
-      continue;
-    }
-    if ($filter->status) {
+    if ($filter_must_be_applied($filter)) {
       $text = $filter->process($text, $langcode, $cache, $cache_id);
     }
   }
-- 
GitLab