Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add & remove functions for attributes #9

Merged
merged 1 commit into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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