Sha256: 02e911085e3c2ce928633bce8901b87c77405cf2e69008c8d24e6be806c90ffb

Contents?: true

Size: 1.71 KB

Versions: 4

Compression:

Stored size: 1.71 KB

Contents

module Hyalite::DOM
  class Element
    include Native
    include Node

    alias_native :set_attribute, :setAttribute
    alias_native :get_attribute, :getAttribute
    alias_native :remove_attribute, :removeAttribute
    alias_native :tag_name, :tagName

    def element?
      true
    end

    def input_type
      `self.native.type`
    end

    def [](prop_name)
      `self.native[#{prop_name}]`
    end

    def add_class(name)
      `self.native.classList.add(name)`
      self
    end

    def class_names
      Array.new(`self.native.classList`).to_a
    end

    def attributes
      @attributes ||= Attributes.new(self)
    end

    def text
      `self.native.textContent`
    end

    def text=(text)
      `self.native.textContent = text`
    end

    def value
      `self.native.value`
    end

    def style(hash)
      hash.each do |key, value|
        `self.native.style[key] = value`
      end
    end

    def add_child(child)
      `self.native.appendChild(child.native)`
    end

    def inner_html
      `self.native.innerHTML`
    end

    def inner_html=(html)
      `self.native.innerHTML = html`
    end

    def inner_dom=(dom)
      clear
      self << dom
    end

    def document
      $document
    end

    def to_s
      "<#{`self.native.tagName`} class='#{self.class_names.join(' ')}' id='#{self['id']}'/>"
    end

    def self.create(tag)
      $document.create_element(tag)
    end

    class Attributes
      def initialize(element)
        @element = element
      end

      def [](name)
        @element.get_attribute(name)
      end

      def []=(name, value)
        @element.set_attribute(name, value)
      end

      def remove(name)
        @element.remove_attribute(name)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hyalite-0.2.1 lib/hyalite/dom/element.rb
hyalite-0.2.0 lib/hyalite/dom/element.rb
hyalite-0.1.1 client/hyalite/dom/element.rb
hyalite-0.1.0 client/hyalite/dom/element.rb