Sha256: 3d19c17d8753a77a92da474e107726959ae209d4886ee08075aaa95349bbc7b8

Contents?: true

Size: 790 Bytes

Versions: 1

Compression:

Stored size: 790 Bytes

Contents

class Slacken::DocumentComponent
  module ElimBlanks
    # Private: Reject blank elements
    def elim_blanks
      case type.name
      when :pre
        self
      else
        derive(children.reject(&:blank?).map(&:elim_blanks))
      end
    end

    def blank?
      return @is_blank if !@is_blank.nil?
      @is_blank =
        case type.name
        when :pre, :ul, :li, :br, :hr, :img, :checkbox
          false
        when :text, :emoji
          content = attrs[:content]
          content.nil? || !content.match(/\A\s*\Z/).nil?
        else
          children.empty? || children.all?(&:blank?)
        end
    end

    def has_no_blanks?
      case type.name
      when :pre
        true
      else
        !blank? && children.all?(&:has_no_blanks?)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slacken-0.1.0 lib/slacken/document_component/elim_blanks.rb