Skip to content

Instantly share code, notes, and snippets.

@g33kidd
Created April 12, 2017 05:10
Show Gist options
  • Select an option

  • Save g33kidd/84a8c7402915c140b9e17f5336f3231d to your computer and use it in GitHub Desktop.

Select an option

Save g33kidd/84a8c7402915c140b9e17f5336f3231d to your computer and use it in GitHub Desktop.

Revisions

  1. g33kidd created this gist Apr 12, 2017.
    46 changes: 46 additions & 0 deletions dom.cr
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    module DOM
    module NodeType
    struct Text
    property string

    def initialize(@string : String)
    end
    end

    struct Element
    property tag_name, attributes

    def initialize(@tag_name : String, @attributes : Hash(String, String))
    end
    end
    end

    class Node
    @children : (Array(Node))?
    @node_type : (NodeType::Text | NodeType::Element)?

    def initialize(children, node_type)
    @children = children
    @node_type = node_type
    end

    def initialize
    @children = nil
    @node_type = nil
    end
    end

    def text(data : String) : Node
    Node.new children: [] of Node, node_type: NodeType::Text.new(data)
    end

    def element(name : String, attrs : Hash(String, String), children : Array(Node)) : Node
    element_node = NodeType::Element.new tag_name: name, attributes: attrs
    Node.new children: children, node_type: element_node
    end
    end

    include DOM

    dom = element("p", {"id" => "something", "href" => "else"}, [text("hello")] of Node)
    p dom