Sha256: ea523d6ac2c556c0661e1863573343494dc1b9c6ea5aa1fdbed176fd71ed5c5f

Contents?: true

Size: 1.73 KB

Versions: 7

Compression:

Stored size: 1.73 KB

Contents

class Roda
  class Component
    class DOM
      attr_accessor :dom, :raw_html

      def initialize html
        @raw_html = html

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

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

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

        if server?
          self
        else
          node
        end
      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.inner_html
          else
            node ? node.html : dom.html
          end
        else
          self.html = content
        end
      end

      def node
        @node || dom
      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

7 entries across 7 versions & 1 rubygems

Version Path
roda-component-0.1.11 lib/roda/component/dom.rb
roda-component-0.1.10 lib/roda/component/dom.rb
roda-component-0.1.9 lib/roda/component/dom.rb
roda-component-0.1.8 lib/roda/component/dom.rb
roda-component-0.1.7 lib/roda/component/dom.rb
roda-component-0.1.6 lib/roda/component/dom.rb
roda-component-0.1.5 lib/roda/component/dom.rb