Sha256: c1c45f9befc7f9c0a71f1bfcbba628669919b490e5d57ef03f6a9268adffbbb9

Contents?: true

Size: 1.34 KB

Versions: 26

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 text.end_with?("\n")
        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

26 entries across 26 versions & 1 rubygems

Version Path
draftjs_html-0.34.0 lib/draftjs_html/node.rb
draftjs_html-0.33.0 lib/draftjs_html/node.rb
draftjs_html-0.32.0 lib/draftjs_html/node.rb
draftjs_html-0.31.0 lib/draftjs_html/node.rb
draftjs_html-0.30.0 lib/draftjs_html/node.rb
draftjs_html-0.29.0 lib/draftjs_html/node.rb
draftjs_html-0.28.0 lib/draftjs_html/node.rb
draftjs_html-0.27.0 lib/draftjs_html/node.rb
draftjs_html-0.26.0 lib/draftjs_html/node.rb
draftjs_html-0.25.0 lib/draftjs_html/node.rb
draftjs_html-0.24.0 lib/draftjs_html/node.rb
draftjs_html-0.23.0 lib/draftjs_html/node.rb
draftjs_html-0.22.0 lib/draftjs_html/node.rb
draftjs_html-0.21.0 lib/draftjs_html/node.rb
draftjs_html-0.20.0 lib/draftjs_html/node.rb
draftjs_html-0.19.0 lib/draftjs_html/node.rb
draftjs_html-0.18.0 lib/draftjs_html/node.rb
draftjs_html-0.17.0 lib/draftjs_html/node.rb
draftjs_html-0.16.0 lib/draftjs_html/node.rb
draftjs_html-0.15.0 lib/draftjs_html/node.rb