Created
April 12, 2017 05:10
-
-
Save g33kidd/84a8c7402915c140b9e17f5336f3231d to your computer and use it in GitHub Desktop.
Revisions
-
g33kidd created this gist
Apr 12, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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