Sha256: 715f1349d40c2452efac25fe7a1655c8f05537bb16d87d491473f50e5a949df3

Contents?: true

Size: 1.22 KB

Versions: 7

Compression:

Stored size: 1.22 KB

Contents

require "rexml/document"

module EPUB
  class Parser
    class XMLDocument
      module Refinements
        module REXML
          [::REXML::Element, ::REXML::Text].each do |klass|
            refine klass do
              %i[document element text].each do |type|
                define_method "#{type}?" do
                  node_type == type
                end
              end
            end
          end

          refine ::REXML::Element do
            def each_element_by_xpath(xpath, namespaces = nil, &block)
              ::REXML::XPath.each self, xpath, namespaces, &block
            end

            def attribute_with_prefix(name, prefix = nil)
              attribute(name, EPUB::NAMESPACES[prefix])&.value
            end

            alias namespace_uri namespace

            def content
              each_child.inject("") {|text, node|
                case node.node_type
                when :document, :element
                  text << node.content
                when :text
                  text << node.value
                end
              }
            end
          end

          refine ::REXML::Text do
            alias content value
          end
        end

        include REXML
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
epub-parser-0.4.8 lib/epub/parser/xml_document/refinements/rexml.rb
epub-parser-0.4.7 lib/epub/parser/xml_document/refinements/rexml.rb
epub-parser-0.4.6 lib/epub/parser/xml_document/refinements/rexml.rb
epub-parser-0.4.5 lib/epub/parser/xml_document/refinements/rexml.rb
epub-parser-0.4.4 lib/epub/parser/xml_document/refinements/rexml.rb
epub-parser-0.4.3 lib/epub/parser/xml_document/refinements/rexml.rb
epub-parser-0.4.2 lib/epub/parser/xml_document/refinements/rexml.rb