Sha256: 3488d7f55eb0181e377d041528e589f92c5b07de59aaf34946fd86ef8a639757

Contents?: true

Size: 1.73 KB

Versions: 3

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(html.dup)
        else
          @dom = html.is_a?(String) ? Element[html.dup] : 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

3 entries across 3 versions & 1 rubygems

Version Path
roda-component-0.1.2 lib/roda/component/dom.rb
roda-component-0.1.1 lib/roda/component/dom.rb
roda-component-0.1.0 lib/roda/component/dom.rb