class Wedge::DOM

Attributes

dom[RW]
raw_html[RW]

Public Class Methods

[](html) click to toggle source

Shortcut for creating dom @param html [String] @return dom [DOM]

# File lib/wedge/dom.rb, line 11
def [] html
  new html
end
new(html) click to toggle source
# File lib/wedge/dom.rb, line 16
def initialize html
  @raw_html = html

  if server?
    @dom = raw_html.is_a?(String) ? HTML[raw_html.dup] : raw_html
  else
    @dom = raw_html.is_a?(String) ? Element[raw_html.dup] : raw_html
  end
end

Public Instance Methods

add_class(classes) click to toggle source
# File lib/wedge/dom.rb, line 63
def add_class classes
  classes = (classes || '').split ' ' unless classes.is_a? Array
  new_classes =  ((node.attr('class') || '').split(' ') << classes).uniq.join(' ')
  node['class'] = new_classes
end
attr(key, value = false) click to toggle source
Calls superclass method
# File lib/wedge/dom.rb, line 74
def attr key, value = false
  if value
    value = value.join ' ' if value.is_a? Array
    node[key] = value
  else
    super key
  end
end
data(key = false, value = false) click to toggle source
# File lib/wedge/dom.rb, line 47
def data key = false, value = false
  d = Hash[node.xpath("@*[starts-with(name(), 'data-')]").map{|a| [a.name, a.value]}]

  if !key
    d
  elsif key && !value
    d[key]
  else
    node["data-#{key}"] = value
  end
end
find(string, &block) click to toggle source
# File lib/wedge/dom.rb, line 26
def find string, &block
  if client?
    node = DOM.new dom.find(string)
  elsif server?
    if block_given?
      node = DOM.new dom.css(string)
    else
      node = DOM.new dom.at(string)
    end
  end

  if block_given?
    node.each_with_index do |n, i|
      block.call DOM.new(n), i
    end
  end

  node
end
html(content = false) click to toggle source
# File lib/wedge/dom.rb, line 112
def html content = false
  if !content
    if server?
      node.inner_html
    else
      node ? node.html : dom.html
    end
  else
    self.html = content
  end
end
html=(content) click to toggle source
# File lib/wedge/dom.rb, line 84
def html= content
  if server?
    node.inner_html = content
  else
    content = content.dom if content.is_a? Wedge::DOM
    node.html content
  end

  node
end
method_missing(method, *args, &block) click to toggle source

This allows you to use all the nokogiri or opal jquery methods if a global one isn't set

Calls superclass method
# File lib/wedge/dom.rb, line 130
def method_missing method, *args, &block
  # respond_to?(symbol, include_all=false)
  if dom.respond_to? method, true
    dom.send method, *args, &block
  else
    super
  end
end
node() click to toggle source
# File lib/wedge/dom.rb, line 124
def node
  @node || dom
end
remove_class(classes) click to toggle source
# File lib/wedge/dom.rb, line 69
def remove_class classes
  classes = (classes || '').split ' ' unless classes.is_a? Array
  (node.attr('class') || '').split(' ').reject { |n| n =~ /active|asc|desc/i }.join(' ')
end
to_html() click to toggle source
# File lib/wedge/dom.rb, line 105
def to_html
  @dom ||= DOM.new '<div>'
  el = dom.first
  DOM.new('<div>').append(el).html
end
val(value) click to toggle source
# File lib/wedge/dom.rb, line 59
def val value
  node.content = value
end