mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-16 09:20:42 -07:00
Add spaceless tag
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user