Skip to content

Commit

Permalink
FFI - nodes may be subclassed. closes #54
Browse files Browse the repository at this point in the history
  • Loading branch information
flavorjones committed May 16, 2009
1 parent 67558da commit cfff9cc
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/nokogiri/ffi/xml/attr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.new(document, name) # :nodoc:
node_cstruct[:doc] = document.cstruct[:doc]
LibXML.xmlXPathNodeSetAdd(node_cstruct.document.node_set, node_cstruct)

node = Node.wrap(node_cstruct)
node = Node.wrap(node_cstruct, self)
yield node if block_given?
node
end
Expand Down
2 changes: 1 addition & 1 deletion lib/nokogiri/ffi/xml/cdata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def self.new(document, content, &block) # :nodoc:

LibXML.xmlXPathNodeSetAdd(node_cstruct.document.node_set, node_cstruct);

node = Node.wrap(node_cstruct)
node = Node.wrap(node_cstruct, self)
yield node if block_given?
node
end
Expand Down
2 changes: 1 addition & 1 deletion lib/nokogiri/ffi/xml/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def self.new(document, content, &block) # :nodoc:

LibXML.xmlXPathNodeSetAdd(node_cstruct.document.node_set, node_cstruct);

node = Node.wrap(node_ptr)
node = Node.wrap(node_ptr, self)

yield node if block_given?

Expand Down
2 changes: 1 addition & 1 deletion lib/nokogiri/ffi/xml/document_fragment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.new(document, &block) # :nodoc:

LibXML.xmlXPathNodeSetAdd(node_cstruct.document.node_set, node_cstruct)

node = Node.wrap(node_cstruct)
node = Node.wrap(node_cstruct, self)

if node.document.child && node.document.child.node_type == ELEMENT_NODE
# TODO: node_type check should be ported into master, because of e.g. DTD nodes
Expand Down
2 changes: 1 addition & 1 deletion lib/nokogiri/ffi/xml/entity_reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.new(document, name, &block) # :nodoc:

LibXML.xmlXPathNodeSetAdd(node_cstruct.document.node_set, node_cstruct);

node = Node.wrap(node_cstruct)
node = Node.wrap(node_cstruct, self)

yield node if block_given?

Expand Down
13 changes: 9 additions & 4 deletions lib/nokogiri/ffi/xml/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def internal_subset # :nodoc:
def dup(deep = 1) # :nodoc:
dup_ptr = LibXML.xmlDocCopyNode(cstruct, cstruct.document, deep)
return nil if dup_ptr.null?
Node.wrap(dup_ptr)
Node.wrap(dup_ptr, self.class)
end

def unlink # :nodoc:
Expand Down Expand Up @@ -231,7 +231,7 @@ def self.new(name, document, &block) # :nodoc:

LibXML.xmlXPathNodeSetAdd(node_cstruct.document.node_set, node_cstruct);

node = Node.wrap(node_cstruct)
node = Node.wrap(node_cstruct, self)

yield node if block_given?

Expand All @@ -249,7 +249,7 @@ def compare(other) # :nodoc:
LibXML.xmlXPathCmpNodes(other.cstruct, self.cstruct)
end

def self.wrap(node_struct) # :nodoc:
def self.wrap(node_struct, klass=nil) # :nodoc:
if node_struct.is_a?(FFI::Pointer)
# cast native pointers up into a node cstruct
return nil if node_struct.null?
Expand Down Expand Up @@ -280,7 +280,12 @@ def self.wrap(node_struct) # :nodoc:
when DTD_NODE then [XML::DTD, LibXML::XmlDtd]
else [XML::Node]
end
node = klasses.first.allocate

if klass
node = klass.allocate
else
node = klasses.first.allocate
end
node.cstruct = klasses[1] ? klasses[1].new(node_struct.pointer) : node_struct

node.cstruct.ruby_node = node
Expand Down
2 changes: 1 addition & 1 deletion lib/nokogiri/ffi/xml/processing_instruction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def self.new(document, name, content) # :nodoc:
node_cstruct[:doc] = document.cstruct[:doc]
LibXML.xmlXPathNodeSetAdd(node_cstruct.document.node_set, node_cstruct)

node = Node.wrap(node_cstruct)
node = Node.wrap(node_cstruct, self)
yield node if block_given?
node
end
Expand Down
2 changes: 1 addition & 1 deletion lib/nokogiri/ffi/xml/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def self.new(string, document) # :nodoc:
node_ptr = LibXML.xmlNewText(string)
node_cstruct = LibXML::XmlNode.new(node_ptr)
node_cstruct[:doc] = document.cstruct
node = Node.wrap(node_cstruct)
node = Node.wrap(node_cstruct, self)
yield node if block_given?
node
end
Expand Down

0 comments on commit cfff9cc

Please sign in to comment.