mirror of
https://github.com/kemko/liquid.git
synced 2026-01-01 15:55:40 +03:00
Compare commits
1 Commits
usage-upda
...
spaceless-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
74f9bad513 |
@@ -105,6 +105,11 @@ module Liquid
|
||||
input.to_s.gsub(/\r?\n/, ''.freeze)
|
||||
end
|
||||
|
||||
# Remove whitespace between HTML tags
|
||||
def strip_html_whitespace(input)
|
||||
Spaceless.strip_html_whitespace(input.to_s)
|
||||
end
|
||||
|
||||
# Join elements of the array with certain character between them
|
||||
def join(input, glue = ' '.freeze)
|
||||
InputIterator.new(input).join(glue)
|
||||
|
||||
24
lib/liquid/tags/spaceless.rb
Normal file
24
lib/liquid/tags/spaceless.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
module Liquid
|
||||
# The spaceless tag strips whitespace between HTML tags using a simple regex.
|
||||
#
|
||||
# == Usage:
|
||||
# {% spaceless %}
|
||||
# <h1>
|
||||
# Hello
|
||||
# </h1>
|
||||
# {% endspaceless %}
|
||||
#
|
||||
class Spaceless < Block
|
||||
HTML_STRIP_SPACE_REGEXP = />\s+</
|
||||
|
||||
def render(context)
|
||||
self.class.strip_html_whitespace(super)
|
||||
end
|
||||
|
||||
def self.strip_html_whitespace(input)
|
||||
input.gsub(HTML_STRIP_SPACE_REGEXP, '><')
|
||||
end
|
||||
end
|
||||
|
||||
Template.register_tag('spaceless'.freeze, Spaceless)
|
||||
end
|
||||
@@ -417,4 +417,9 @@ class StandardFiltersTest < Minitest::Test
|
||||
def test_cannot_access_private_methods
|
||||
assert_template_result('a', "{{ 'a' | to_number }}")
|
||||
end
|
||||
|
||||
def test_strip_html_whitespace
|
||||
html = "<a>\n\n <b> \n <c>a</c> \r\n </b> </a>"
|
||||
assert_template_result('<a><b><c>a</c></b></a>', "{{ a | strip_html_whitespace }}", { 'a' => html })
|
||||
end
|
||||
end # StandardFiltersTest
|
||||
|
||||
@@ -293,4 +293,9 @@ class StandardTagTest < Minitest::Test
|
||||
def test_multiline_tag
|
||||
assert_template_result '0 1 2 3', "0{%\nfor i in (1..3)\n%} {{\ni\n}}{%\nendfor\n%}"
|
||||
end
|
||||
|
||||
def test_spaceless
|
||||
html = "<a>\n\n <b> \n <c>a</c> \r\n </b> </a>"
|
||||
assert_template_result '<a><b><c>a</c></b></a>', "{% spaceless %}#{html}{% endspaceless %}"
|
||||
end
|
||||
end # StandardTagTest
|
||||
|
||||
Reference in New Issue
Block a user