Sha256: 9502b61259007beb521ccb2ffbc54c672b680f05b7c8793d1b25771fce8d2e67

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

module Opal
  module RSpec
    class Element
      attr_reader :native

      def self.id(id)
        new(`document.getElementById(id)`)
      end

      def self.klass(klass)
        new(`document.getElementsByClassName(#{klass})[0]`)
      end

      def self.from_string(str)
        dummy_div = `document.createElement('div')`
        `#{dummy_div}.innerHTML = #{str}`
        new(`#{dummy_div}.children[0]`)
      end

      def initialize(el, attrs={})
        if String === el
          @native = `document.createElement(el)`
        else
          @native = el
        end

        attrs.each { |name, val| __send__ "#{name}=", val }
      end

      def class_name
        `#@native.className`
      end

      def get_child_by_tag_name(tag)
        Element.new(`#@native.getElementsByTagName(#{tag})[0]`)
      end

      def class_name=(name)
        `#@native.className = #{name}`
      end

      def native
        `#@native`
      end

      def outer_html
        `#@native.outerHTML`
      end

      def html=(html)
        `#@native.innerHTML = #{html}`
      end

      def text=(text)
        self.html = text.gsub(/</, '&lt').gsub(/>/, '&gt')
      end

      def type=(type)
        `#@native.type = #{type}`
      end

      def append(child)
        `#@native.appendChild(#{child.native})`
      end

      alias << append

      def css_text=(text)
        %x{
                    if (#@native.styleSheet) {
                      #@native.styleSheet.cssText = #{text};
                    }
                    else {
                      #@native.appendChild(document.createTextNode(#{text}));
                    }
                  }
      end

      def style(name, value)
        `#@native.style[#{name}] = value`
      end

      def append_to_head
        `document.getElementsByTagName('head')[0].appendChild(#@native)`
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
opal-rspec-0.5.0.beta3 opal/opal/rspec/formatter/element.rb