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

Don't cleanup doc reference of unlinked node. #34

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions test/test-html-build.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function TestHTMLBuild.test_empty_root_dtd_systemid()
local uri = "file:///usr/local/share/test.dtd"
local document = HTML.build({"html"}, uri)
luaunit.assertEquals({
ffi.string(document.document.intSubset.SystemID),
ffi.string(document.raw_document.intSubset.SystemID),
document:to_html()
},
{
Expand All @@ -43,8 +43,8 @@ function TestHTMLBuild.test_empty_root_dtd_publicid()
local public_id = "-//W3C//DTD HTML 4.01//EN"
local document = HTML.build({"html"}, uri, public_id)
luaunit.assertEquals({
ffi.string(document.document.intSubset.SystemID),
ffi.string(document.document.intSubset.ExternalID),
ffi.string(document.raw_document.intSubset.SystemID),
ffi.string(document.raw_document.intSubset.ExternalID),
document:to_html()
},
{
Expand Down
2 changes: 1 addition & 1 deletion xmlua/attribute-declaration.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ end
function AttributeDeclaration.new(document, node)
local attribute_declaration = {
document = document,
node = node,
node = node
}
setmetatable(attribute_declaration, metatable)
return attribute_declaration
Expand Down
2 changes: 1 addition & 1 deletion xmlua/attribute.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ end
function Attribute.new(document, node)
local attr = {
document = document,
node = node,
node = node
}
setmetatable(attr, metatable)
return attr
Expand Down
2 changes: 1 addition & 1 deletion xmlua/cdata-section.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ end
function CDATASection.new(document, node)
local cdata_section = {
document = document,
node = node,
node = node
}
setmetatable(cdata_section, metatable)
return cdata_section
Expand Down
2 changes: 1 addition & 1 deletion xmlua/comment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ end
function Comment.new(document, node)
local comment = {
document = document,
node = node,
node = node
}
setmetatable(comment, metatable)
return comment
Expand Down
2 changes: 1 addition & 1 deletion xmlua/document-type.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ end
function DocumentType.new(document, node)
local document_type = {
document = document,
node = node,
node = node
}
setmetatable(document_type, metatable)
return document_type
Expand Down
57 changes: 33 additions & 24 deletions xmlua/document.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ local Document = {}
local libxml2 = require("xmlua.libxml2")
local ffi = require("ffi")
local converter = require("xmlua.converter")
local to_string = converter.to_string

local Serializable = require("xmlua.serializable")
local Searchable = require("xmlua.searchable")
Expand Down Expand Up @@ -40,54 +39,54 @@ function metatable.__index(document, key)
end

function methods:root()
local root_element = libxml2.xmlDocGetRootElement(self.document)
if not root_element then
local node = libxml2.xmlDocGetRootElement(self.raw_document)
if not node then
return nil
end
return Element.new(self.document, root_element)
return Element.new(self, node)
end

function methods:parent()
return nil
end

function methods:encoding()
return ffi.string(self.document.encoding)
return ffi.string(self.raw_document.encoding)
end

function methods:create_cdata_section(data)
local raw_cdata_section_node =
libxml2.xmlNewCDataBlock(self.document,
libxml2.xmlNewCDataBlock(self.raw_document,
data,
data:len())
return CDATASection.new(self.document, raw_cdata_section_node)
return CDATASection.new(self, raw_cdata_section_node)
end

function methods:create_comment(data)
local raw_comment_node =
libxml2.xmlNewComment(data)
return Comment.new(self.document, raw_comment_node)
return Comment.new(self, raw_comment_node)
end

function methods:create_document_fragment()
local raw_document_fragment_node =
libxml2.xmlNewDocFragment(self.document)
return DocumentFragment.new(self.document,
libxml2.xmlNewDocFragment(self.raw_document)
return DocumentFragment.new(self,
raw_document_fragment_node)
end

function methods:create_document_type(name, external_id, system_id)
local raw_document_type =
libxml2.xmlCreateIntSubset(self.document, name, external_id, system_id)
return DocumentType.new(self.document,
libxml2.xmlCreateIntSubset(self.raw_document, name, external_id, system_id)
return DocumentType.new(self,
raw_document_type)
end

function methods:get_internal_subset()
local raw_document_type =
libxml2.xmlGetIntSubset(self.document)
libxml2.xmlGetIntSubset(self.raw_document)
if raw_document_type ~= nil then
return DocumentType.new(self.document,
return DocumentType.new(self,
raw_document_type)
else
return nil
Expand All @@ -96,28 +95,28 @@ end

function methods:add_entity_reference(name)
local raw_entity_reference =
libxml2.xmlNewReference(self.document, name)
return EntityReference.new(self.document,
libxml2.xmlNewReference(self.raw_document, name)
return EntityReference.new(self,
raw_entity_reference)
end

function methods:create_namespace(href, prefix)
local raw_namespace =
libxml2.xmlNewNs(self.node, href, prefix)
return Namespace.new(self.document, raw_namespace)
return Namespace.new(self, raw_namespace)
end

function methods:create_processing_instruction(name, content)
local raw_processing_instruction =
libxml2.xmlNewPI(name, content)
return ProcessingInstruction.new(self.document,
return ProcessingInstruction.new(self,
raw_processing_instruction)
end

function methods:add_entity(entity_info)
local entity_type_name = entity_info["entity_type"]
local entity_type = converter.convert_entity_type_name(entity_type_name)
local raw_entity = libxml2.xmlAddDocEntity(self.document,
local raw_entity = libxml2.xmlAddDocEntity(self.raw_document,
entity_info["name"],
entity_type,
entity_info["external_id"],
Expand All @@ -127,14 +126,14 @@ function methods:add_entity(entity_info)
end

function methods:get_entity(name)
local raw_entity = libxml2.xmlGetDocEntity(self.document, name)
local raw_entity = libxml2.xmlGetDocEntity(self.raw_document, name)
return converter.convert_xml_entity(raw_entity)
end

function methods:add_dtd_entity(entity_info)
local entity_type_name = entity_info["entity_type"]
local entity_type = converter.convert_entity_type_name(entity_type_name)
local raw_dtd_entity = libxml2.xmlAddDtdEntity(self.document,
local raw_dtd_entity = libxml2.xmlAddDtdEntity(self.raw_document,
entity_info["name"],
entity_type,
entity_info["external_id"],
Expand All @@ -144,7 +143,7 @@ function methods:add_dtd_entity(entity_info)
end

function methods:get_dtd_entity(name)
local raw_dtd_entity = libxml2.xmlGetDtdEntity(self.document, name)
local raw_dtd_entity = libxml2.xmlGetDtdEntity(self.raw_document, name)
return converter.convert_xml_entity(raw_dtd_entity)
end

Expand All @@ -167,7 +166,7 @@ function Document.build(raw_document, tree)

local root = Element.build(document, tree[1], tree[2])
if not libxml2.xmlDocSetRootElement(raw_document, root.node) then
root:unlink()
--root:unlink()
return nil
end

Expand All @@ -186,10 +185,20 @@ function Document.new(raw_document, errors)
if not errors then
errors = {}
end
local unlinked = {}
local document = {
document = raw_document,
raw_document = raw_document,
errors = errors,
unlinked = unlinked
}
ffi.gc(document.raw_document, function(pdocument)
for node in pairs(unlinked) do
if node.parent == ffi.NULL then
libxml2.xmlFreeNode(node)
end
end
libxml2.xmlFreeDoc(pdocument)
end)
setmetatable(document, metatable)
return document
end
Expand Down
2 changes: 1 addition & 1 deletion xmlua/element-declaration.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ end
function ElementDeclaration.new(document, node)
local element_declaration = {
document = document,
node = node,
node = node
}
setmetatable(element_declaration, metatable)
return element_declaration
Expand Down
Loading