Sha256: 604d276befd7457f8c34d17a9857d065ce42fa82f0257ca1af21bcd0249e4e18

Contents?: true

Size: 986 Bytes

Versions: 1

Compression:

Stored size: 986 Bytes

Contents

module DraftjsHtml
  NokogiriNode = Struct.new(:node) do
    def to_nokogiri(_document)
      node
    end
  end

  StringNode = Struct.new(:raw) do
    def to_nokogiri(document)
      lines = raw.lines
      text_nodes = lines.flat_map.with_index do |text, i|
        nodes = [Nokogiri::XML::Text.new(text.chomp, document)]
        nodes << Nokogiri::XML::Node.new('br', document) if i < lines.size - 1
        nodes
      end

      Nokogiri::XML::NodeSet.new(document, text_nodes)
    end
  end

  Node = Struct.new(:element_name, :attributes, :content) do
    def self.of(thing)
      case thing
      when Nokogiri::XML::Node then NokogiriNode.new(thing)
      when self.class then thing
      when String then StringNode.new(thing)
      else thing
      end
    end

    def to_nokogiri(document)
      Nokogiri::XML::Node.new(element_name, document).tap do |node|
        node.content = content
        (attributes || {}).each { |k, v| node[k] = v }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
draftjs_html-0.6.0 lib/draftjs_html/node.rb