Sha256: b62087097a0c978a85defa7f55ebda8f67a302b76f1b1bcf188c9bed0f28e200

Contents?: true

Size: 1.44 KB

Versions: 12

Compression:

Stored size: 1.44 KB

Contents

require 'nokogiri'

module EmmetMatchers
  class EmmetStructureMatcher
    def initialize(emmet_string)
      @test_string = emmet_string
    end

    def matches?(html_snippet)
      frag = Nokogiri::HTML.fragment html_snippet
      emmet_string = ''
      elem_stack = []
      frag.traverse {|node| elem_stack.push node unless node.text?}

      until elem_stack.empty? do
        elem = elem_stack.pop
        unless elem.name == '#document-fragment' || elem.text.rstrip.empty?
          emmet_string << elem.name unless elem.text.chop.empty?
          if elem.attr('id')
            emmet_string << "#{'#'}#{elem.attr('id')}"
          end
          if elem.attr('class')
            emmet_string << ".#{elem.attr('class')}"
          end
          if elem.child
            child = elem.child
            #if child.text? && !child.text.rstrip.empty?
            #emmet_string << "{#{child.text.gsub(/\s+/, ' ')}}"
            #elem_stack.delete child
            #end
            elem_stack.delete child
            emmet_string << '>' unless elem_stack.last.nil?
            next
          end
        end
      end

      @emmet_string = emmet_string

      @emmet_string == @test_string 
    end

    def failure_message_for_should
      "expected to match #{@test_string} against #{@emmet_string}, but they are not equal"
    end 
  end

  def have_structure(expect)
    EmmetStructureMatcher.new(expect)
  end
end

RSpec.configure do |c|
  c.include EmmetMatchers
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
awestruct-0.5.6.beta8 spec/support/emmet_matchers.rb
awestruct-0.5.6.beta7 spec/support/emmet_matchers.rb
awestruct-0.5.6.beta6 spec/support/emmet_matchers.rb
awestruct-0.5.6.beta5 spec/support/emmet_matchers.rb
awestruct-0.5.6.beta4 spec/support/emmet_matchers.rb
awestruct-0.5.6.beta3 spec/support/emmet_matchers.rb
awestruct-0.5.6.beta2 spec/support/emmet_matchers.rb
awestruct-0.5.6.beta spec/support/emmet_matchers.rb
awestruct-0.5.5 spec/support/emmet_matchers.rb
awestruct-0.5.4.2 spec/support/emmet_matchers.rb
awestruct-0.5.4.1 spec/support/emmet_matchers.rb
awestruct-0.5.4 spec/support/emmet_matchers.rb