Sha256: 29a49e0326b01c76eba9c06510a57d3be6dbc9f3e82f6c30e64e27bdbd1fdaae

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

require 'nokogiri'

# Public: a DOM tree container parsed by Nokogiri.
module Slacken
  class DomContainer
    attr_reader :root

    # Public: Parse a html source with nokogiri and create a container.
    def self.parse_html(body)
      new(Nokogiri::HTML(body))
    end

    def initialize(root)
      @root = root
    end

    def to_component(node = root)
      children = node.children.map { |nd| to_component(nd) }.compact
      leave(node, children)
    end

    private

    def leave(node, children)
      if !(node.respond_to?(:html_dtd?) && node.html_dtd?)
        DocumentComponent.new(node.name.downcase, children, attrs_of(node))
      end
    end

    def attrs_of(node)
      case node.name.to_sym
      when :text
        { content: node.content }
      when :iframe, :a
        { href: node['href'] }
      when :input
        { type: node['type'], checked: node['checked'] }
      when :img
        { src: node['src'], alt: node['alt'], class: (node['class'] || '').split }
      else
        {}
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slacken-0.1.0 lib/slacken/dom_container.rb