Skip to content

Commit

Permalink
Merge pull request #9 from MrSorcus/master
Browse files Browse the repository at this point in the history
Add & remove functions for attributes
  • Loading branch information
kostya authored Jul 30, 2018
2 parents 6c7839f + 2e788cc commit 19b78ce
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions spec/node_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ describe Myhtml::Node do
node.attribute_by("class".to_slice).should eq "AAA".to_slice
end

it "add attribute" do
parser = Myhtml::Parser.new("<html><body><div class=\"foo\">Haha</div></body></html>")

node = parser.nodes(:div).first
node.attribute_add("id", "bar")
node.attributes.should eq({"class" => "foo", "id" => "bar"})
end

it "remove attribute" do
parser = Myhtml::Parser.new("<html><body><div class=\"foo\" id=\"bar\">Haha</div></body></html>")

node = parser.nodes(:div).first
node.attribute_remove("id")
node.attributes.should eq({"class" => "foo"})
end

it "ignore case attributes" do
parser = Myhtml::Parser.new("<html><body><div Class=AAA STYLE='color:red'>Haha</div></body></html>")

Expand Down
8 changes: 8 additions & 0 deletions src/myhtml/lib.cr
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ module Myhtml
fun attribute_value = myhtml_attribute_value(attr : MyhtmlTreeAttrT*, length : LibC::SizeT*) : UInt8*
fun attribute_next = myhtml_attribute_next(attr : MyhtmlTreeAttrT*) : MyhtmlTreeAttrT*

fun attribute_add = myhtml_attribute_add(node : MyhtmlTreeNodeT*, key : LibC::Char*, key_len : LibC::SizeT, value : LibC::Char*, value_len : LibC::SizeT, encoding : MyEncodingList) : MyhtmlTreeAttrT*

fun attribute_remove = myhtml_attribute_remove(node : MyhtmlTreeNodeT*, attr : MyhtmlTreeAttrT*) : MyhtmlTreeAttrT*

fun attribute_remove_by_key = myhtml_attribute_remove_by_key(node : MyhtmlTreeNodeT*, key : LibC::Char*, key_len : LibC::SizeT) : MyhtmlTreeAttrT*

fun attribute_delete = myhtml_attribute_delete(tree : MyhtmlTreeT*, node : MyhtmlTreeNodeT*, attr : MyhtmlTreeAttrT*) : Void

fun serialization = myhtml_serialization(node : MyhtmlTreeNodeT*, str : MyhtmlStringRawT*) : MyStatus
fun serialization_node = myhtml_serialization_node(node : MyhtmlTreeNodeT*, str : MyhtmlStringRawT*) : MyStatus

Expand Down
8 changes: 8 additions & 0 deletions src/myhtml/node.cr
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ module Myhtml
end
end

def attribute_add(key : String, value : String)
Lib.attribute_add(@node, key, key.bytesize, value, value.bytesize, Lib::MyEncodingList::MyENCODING_DEFAULT)
end

def attribute_remove(key : String)
Lib.attribute_remove_by_key(@node, key, key.bytesize)
end

def attributes
@attributes ||= begin
res = {} of String => String
Expand Down

0 comments on commit 19b78ce

Please sign in to comment.