Skip to content

Commit

Permalink
Move create_node to Tree class
Browse files Browse the repository at this point in the history
It works the same but you need to create a tree first, then a node in
that tree. The node can then be inserted into a document.
  • Loading branch information
edwardloveall committed Oct 7, 2018
1 parent 49cfcef commit ea3a12a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
22 changes: 8 additions & 14 deletions spec/node_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,12 @@ describe Myhtml::Node do
parser.root!.data.null?.should eq true
end

describe ".create" do
it "returns a new node" do
node = Myhtml::Node.create(:a)

node.should be_a(Myhtml::Node)
node.tag_id.should eq(Myhtml::Lib::MyhtmlTags::MyHTML_TAG_A)
end
end

describe "#append" do
it "adds a node at the end" do
parent = Myhtml::Node.create(:div)
child = Myhtml::Node.create(:a)
grandchild = Myhtml::Node.create(:span)
tree = Myhtml::Tree.new
parent = tree.create_node(:div)
child = tree.create_node(:a)
grandchild = tree.create_node(:span)

parent.append(child)
child.append(grandchild)
Expand All @@ -239,7 +231,8 @@ describe Myhtml::Node do
it "adds a node just prior to this node" do
document = Myhtml::Parser.new("<html><body><main></main></body></html>")
main = document.css("main").first
header = Myhtml::Node.create(:header)
tree = Myhtml::Tree.new
header = tree.create_node(:header)

main.before(header)

Expand All @@ -253,7 +246,8 @@ describe Myhtml::Node do
html_string = "<html><body><header></header></body></html>"
document = Myhtml::Parser.new(html_string)
header = document.css("header").first
main = Myhtml::Node.create(:main)
tree = Myhtml::Tree.new
main = tree.create_node(:main)

header.after(main)

Expand Down
14 changes: 14 additions & 0 deletions spec/tree_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "./spec_helper"

describe Myhtml::Tree do
describe "#create_node" do
it "returns a new Myhtml::Node" do
tree = Myhtml::Tree.new

node = tree.create_node(:a)

node.should be_a(Myhtml::Node)
node.tag_id.should eq(Myhtml::Lib::MyhtmlTags::MyHTML_TAG_A)
end
end
end
14 changes: 0 additions & 14 deletions src/myhtml/node.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,6 @@ struct Myhtml::Node
def initialize(@tree, @raw_node)
end

def self.create(tag_sym : Symbol)
parser = Parser.new
raw_myhtml = Lib.create
Lib.init(raw_myhtml, Myhtml::Lib::MyhtmlOptions::MyHTML_OPTIONS_PARSE_MODE_SINGLE, 1, 0)
raw_tree = Lib.tree_create
Lib.tree_init(raw_tree, raw_myhtml)
raw_node = Lib.node_create(
raw_tree,
Utils::TagConverter.sym_to_id(tag_sym),
Myhtml::Lib::MyhtmlNamespace::MyHTML_NAMESPACE_HTML
)
new(parser, raw_node)
end

#
# Tag ID
# node.tag_id => Myhtml::Lib::MyhtmlTags::MyHTML_TAG_DIV
Expand Down
9 changes: 9 additions & 0 deletions src/myhtml/tree.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ class Myhtml::Tree
@finalized = false
end

def create_node(tag_sym : Symbol)
raw_node = Lib.node_create(
raw_tree,
Utils::TagConverter.sym_to_id(tag_sym),
Myhtml::Lib::MyhtmlNamespace::MyHTML_NAMESPACE_HTML
)
Node.new(self, raw_node)
end

def set_flags(flags : Lib::MyhtmlTreeParseFlags)
Lib.tree_parse_flags_set(@raw_tree, flags)
end
Expand Down

0 comments on commit ea3a12a

Please sign in to comment.