diff --git a/CHANGELOG b/CHANGELOG
index 5f215505f..3c2da8c67 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -157,6 +157,7 @@
* 1.38.0 (2019-XX-XX)
+ * added a spaceless filter
* added max value to the "random" function
* made namespace classes the default classes (PSR-0 ones are aliases now)
* removed duplicated directory separator in FilesystemLoader
diff --git a/doc/filters/spaceless.rst b/doc/filters/spaceless.rst
new file mode 100644
index 000000000..013124d25
--- /dev/null
+++ b/doc/filters/spaceless.rst
@@ -0,0 +1,54 @@
+``spaceless``
+=============
+
+.. versionadded:: 1.38
+
+ The ``spaceless`` filter was added in Twig 1.38.
+
+Use the ``spaceless`` filter to remove whitespace *between HTML tags*, not
+whitespace within HTML tags or whitespace in plain text:
+
+.. code-block:: jinja
+
+ {{
+ "
+ foo
+
+ "|spaceless }}
+
+ {# output will be foo
#}
+
+You can combine ``spaceless`` with the ``filter`` tag to apply the
+transformation on large amounts of HTML:
+
+.. code-block:: jinja
+
+ {% filter spaceless %}
+
+ foo
+
+ {% endfilter %}
+
+ {# output will be foo
#}
+
+This tag is not meant to "optimize" the size of the generated HTML content but
+merely to avoid extra whitespace between HTML tags to avoid browser rendering
+quirks under some circumstances.
+
+.. tip::
+
+ If you want to optimize the size of the generated HTML content, gzip
+ compress the output instead.
+
+.. tip::
+
+ If you want to create a tag that actually removes all extra whitespace in
+ an HTML string, be warned that this is not as easy as it seems to be
+ (think of ``textarea`` or ``pre`` tags for instance). Using a third-party
+ library like Tidy is probably a better idea.
+
+.. tip::
+
+ For more information on whitespace control, read the
+ :ref:`dedicated section ` of the documentation and learn how
+ you can also use the whitespace control modifier on your tags.
diff --git a/doc/tags/spaceless.rst b/doc/tags/spaceless.rst
index b39cb27ef..14858243f 100644
--- a/doc/tags/spaceless.rst
+++ b/doc/tags/spaceless.rst
@@ -1,6 +1,10 @@
``spaceless``
=============
+.. tip::
+
+ As of Twig 1.38, use the :doc:`spaceless <../filters/spaceless>` filter instead.
+
Use the ``spaceless`` tag to remove whitespace *between HTML tags*, not
whitespace within HTML tags or whitespace in plain text:
diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php
index 2fba7d4c3..26a4ebfc7 100644
--- a/src/Extension/CoreExtension.php
+++ b/src/Extension/CoreExtension.php
@@ -218,6 +218,7 @@ final class CoreExtension extends AbstractExtension
new TwigFilter('striptags', 'strip_tags'),
new TwigFilter('trim', 'twig_trim_filter'),
new TwigFilter('nl2br', 'nl2br', ['pre_escape' => 'html', 'is_safe' => ['html']]),
+ new TwigFilter('spaceless', 'twig_spaceless', ['is_safe' => ['html']]),
// array helpers
new TwigFilter('join', 'twig_join_filter'),
@@ -380,6 +381,7 @@ function twig_random(Environment $env, $values = null, $max = null)
$min = $values;
$max = $max;
}
+
return mt_rand($min, $max);
}
@@ -961,6 +963,16 @@ function twig_trim_filter($string, $characterMask = null, $side = 'both')
}
}
+/**
+ * Removes whitespaces between HTML tags.
+ *
+ * @return string
+ */
+function twig_spaceless($content)
+{
+ return preg_replace('/>\s+', '><', $content);
+}
+
/**
* Escapes a string.
*
diff --git a/test/Twig/Tests/Fixtures/filters/spaceless.test b/test/Twig/Tests/Fixtures/filters/spaceless.test
new file mode 100644
index 000000000..eadc1d496
--- /dev/null
+++ b/test/Twig/Tests/Fixtures/filters/spaceless.test
@@ -0,0 +1,8 @@
+--TEST--
+"spaceless" filter
+--TEMPLATE--
+{{ " "|spaceless }}
+--DATA--
+return []
+--EXPECT--
+
diff --git a/test/Twig/Tests/Fixtures/tags/inheritance/block_expr.test b/test/Twig/Tests/Fixtures/tags/inheritance/block_expr.test
index b7bef6463..0b82d4cf2 100644
--- a/test/Twig/Tests/Fixtures/tags/inheritance/block_expr.test
+++ b/test/Twig/Tests/Fixtures/tags/inheritance/block_expr.test
@@ -8,7 +8,6 @@ block_expr
{{- parent() -}}
{% endblock %}
--TEMPLATE(base.twig)--
-{% spaceless %}
{% block element -%}
{%- if item.children is defined %}
@@ -18,7 +17,6 @@ block_expr
{%- endif -%}
{%- endblock %}
-{% endspaceless %}
--DATA--
return [
'item' => [
diff --git a/test/Twig/Tests/Fixtures/tags/inheritance/block_expr2.test b/test/Twig/Tests/Fixtures/tags/inheritance/block_expr2.test
index 2d22852a7..18f660411 100644
--- a/test/Twig/Tests/Fixtures/tags/inheritance/block_expr2.test
+++ b/test/Twig/Tests/Fixtures/tags/inheritance/block_expr2.test
@@ -10,7 +10,6 @@ block_expr2
--TEMPLATE(base2.twig)--
{% extends "base.twig" %}
--TEMPLATE(base.twig)--
-{% spaceless %}
{% block element -%}
{%- if item.children is defined %}
@@ -20,7 +19,6 @@ block_expr2
{%- endif -%}
{%- endblock %}
-{% endspaceless %}
--DATA--
return [
'item' => [