Sha256: c0f1f93da5454646cb9f927d1ca5f1d130ae2e5bafa94d3f9a06e55c4c59afea

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

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

    def wrap(tagname, attrs = {})
      Node.new(tagname, attrs, self)
    end

    def to_s
      node.to_s
    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

    def wrap(tagname, attrs = {})
      Node.new(tagname, attrs, self)
    end

    def to_s
      raw.to_s
    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.to_nokogiri(document) if content
        (attributes || {}).each { |k, v| node[k] = v }
      end
    end

    def wrap(tagname, attrs = {})
      Node.new(tagname, attrs, self)
    end

    def to_s
      content.to_s
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
draftjs_html-0.8.0 lib/draftjs_html/node.rb
draftjs_html-0.7.0 lib/draftjs_html/node.rb