Sha256: 69972d363f0fd90a5fcd51cac2069152b4bb17a96663f52e675d0974acc1718d

Contents?: true

Size: 920 Bytes

Versions: 4

Compression:

Stored size: 920 Bytes

Contents

# encoding: utf-8
module TruncateHtml
  class HtmlString < String

    UNPAIRED_TAGS = %w(br hr img).freeze

    def initialize(original_html)
      super(original_html)
    end

    def html_tokens
      scan(regex).map do |token|
        HtmlString.new(
          token.gsub(
            /\n/,'' #remove newline characters
          ).gsub(
            /\s+/, ' ' #clean out extra consecutive whitespace
          )
        )
      end
    end

    def html_tag?
      /<\/?[^>]+>/ === self && !html_comment?
    end

    def open_tag?
      /<(?!(?:#{UNPAIRED_TAGS.join('|')}|script|\/))[^>]+>/i === self
    end

    def html_comment?
      /<\s?!--.*-->/ === self
    end

    def matching_close_tag
      gsub(/<(\w+)\s?.*>/, '</\1>').strip
    end

    private
    def regex
      /(?:<script.*>.*<\/script>)+|<\/?[^>]+>|[[[:alpha:]]\w\|`~!@#\$%^&*\(\)\-_\+=\[\]{}:;'",\.\/?]+|\s+|[[:punct:]]/
    end

  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
solidus_backend-1.0.0.pre3 vendor/bundle/gems/truncate_html-0.9.2/lib/truncate_html/html_string.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/truncate_html-0.9.2/lib/truncate_html/html_string.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/truncate_html-0.9.2/lib/truncate_html/html_string.rb
truncate_html-0.9.2 lib/truncate_html/html_string.rb