Sha256: db75c78df29f2439b8fe780822c300cba8a82e9036e5987995d440d704ae17dd

Contents?: true

Size: 1.54 KB

Versions: 9

Compression:

Stored size: 1.54 KB

Contents

unless RUBY_ENGINE == 'opal'
  require 'nokogiri'
end

class Roda
  class Component
    class DOM
      attr_accessor :dom

      def initialize dom
        @dom = dom
      end

      def find string, &block
        if server?
          @node = dom.css string
        else
          @node = dom.find string
        end

        if block
          if server?
            @node.each do |node|
              block.call node
            end
          else
            block.call @node
          end
        else
          if server?
            @node = @node.first
          end
        end

        self
      end

      def html= content
        if server?
          @node.inner_html = content
        else
          @node.html content
        end

        @node
      end

      def html content = false
        if !content
          if server?
            @node ? @node.to_html : dom.to_html
          else
            @node ? @node.html : dom.html
          end
        else
          self.html = content
        end
      end

      # This allows you to use all the nokogiri or opal jquery methods if a
      # global one isn't set
      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

      private

      def server? &block
        RUBY_ENGINE == 'ruby'
      end
      alias :server :server?

      def client?
        RUBY_ENGINE == 'opal'
      end
      alias :client :client?
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
roda-component-0.0.10 lib/roda/component/dom.rb
roda-component-0.0.9 lib/roda/component/dom.rb
roda-component-0.0.8 lib/roda/component/dom.rb
roda-component-0.0.7 lib/roda/component/dom.rb
roda-component-0.0.6 lib/roda/component/dom.rb
roda-component-0.0.5 lib/roda/component/dom.rb
roda-component-0.0.4 lib/roda/component/dom.rb
roda-component-0.0.3 lib/roda/component/dom.rb
roda-component-0.0.2 lib/roda/component/dom.rb