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

miscellaneous TypedData changes #2825

Merged
merged 2 commits into from
Mar 8, 2023
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ Nokogiri follows [Semantic Versioning](https://semver.org/), please see the [REA

### Deprecated

* Passing a `Nokogiri::XML::Node` as the first parameter to `CDATA.new` is deprecated and will generate a warning. This parameter should be a kind of `Nokogiri::XML::Document`. This will become an error in a future version of Nokogiri.
* Passing a `Nokogiri::XML::Node` as the first parameter to `Schema.from_document` is deprecated and will generate a warning. This parameter should be a kind of `Nokogiri::XML::Document`. This will become an error in a future version of Nokogiri.
* Passing a `Nokogiri::XML::Node` as the second parameter to `Text.new` is deprecated and will generate a warning. This parameter should be a kind of `Nokogiri::XML::Document`. This will become an error in a future version of Nokogiri.


### Performance

### Security
Expand Down
6 changes: 6 additions & 0 deletions ext/java/nokogiri/XmlCdata.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public class XmlCdata extends XmlText
}
IRubyObject doc = args[0];
content = args[1];

if (!(doc instanceof XmlDocument)) {
// TODO: deprecate allowing Node
context.runtime.getWarnings().warn("Passing a Node as the first parameter to CDATA.new is deprecated. Please pass a Document instead. This will become an error in a future release of Nokogiri.");
}

Document document = ((XmlNode) doc).getOwnerDocument();
Node node = document.createCDATASection(rubyStringToString(content));
setNode(context.runtime, node);
Expand Down
5 changes: 5 additions & 0 deletions ext/java/nokogiri/XmlSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ public class XmlSchema extends RubyObject
parseOptions = args[1];
}

if (!(document instanceof XmlDocument)) {
// TODO: deprecate allowing Node
context.runtime.getWarnings().warn("Passing a Node as the first parameter to Schema.from_document is deprecated. Please pass a Document instead. This will become an error in a future release of Nokogiri.");
}

XmlDocument doc = ((XmlDocument)((XmlNode) document).document(context));

RubyArray<?> errors = (RubyArray) doc.getInstanceVariable("@errors");
Expand Down
5 changes: 5 additions & 0 deletions ext/java/nokogiri/XmlText.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public class XmlText extends XmlNode
content = args[0];
IRubyObject xNode = args[1];

if (!(xNode instanceof XmlDocument)) {
// TODO: deprecate allowing Node
context.runtime.getWarnings().warn("Passing a Node as the second parameter to Text.new is deprecated. Please pass a Document instead. This will become an error in a future release of Nokogiri.");
}

Document document = asXmlNode(context, xNode).getOwnerDocument();
// text node content should not be encoded when it is created by Text node.
// while content should be encoded when it is created by Element node.
Expand Down
3 changes: 1 addition & 2 deletions ext/nokogiri/html4_document.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ rb_html_document_s_read_memory(VALUE klass, VALUE rb_html, VALUE rb_url, VALUE r
static VALUE
rb_html_document_type(VALUE self)
{
htmlDocPtr doc;
TypedData_Get_Struct(self, xmlDoc, &noko_xml_document_data_type, doc);
htmlDocPtr doc = noko_xml_document_unwrap(self);
return INT2NUM(doc->type);
}

Expand Down
6 changes: 1 addition & 5 deletions ext/nokogiri/html4_element_description.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

static const rb_data_type_t html4_element_description_type = {
.wrap_struct_name = "Nokogiri::HTML4::ElementDescription",
.function = {
.dmark = NULL,
.dfree = NULL,
},
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
};

VALUE cNokogiriHtml4ElementDescription ;
Expand Down
2 changes: 1 addition & 1 deletion ext/nokogiri/html4_sax_parser_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ parse_with(VALUE self, VALUE sax_handler)
}

Data_Get_Struct(self, htmlParserCtxt, ctxt);
TypedData_Get_Struct(sax_handler, htmlSAXHandler, &noko_sax_handler_type, sax);
sax = noko_sax_handler_unwrap(sax_handler);

/* Free the sax handler since we'll assign our own */
if (ctxt->sax && ctxt->sax != (xmlSAXHandlerPtr)&xmlDefaultSAXHandler) {
Expand Down
2 changes: 1 addition & 1 deletion ext/nokogiri/html4_sax_push_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ initialize_native(VALUE self, VALUE _xml_sax, VALUE _filename,
htmlParserCtxtPtr ctx;
xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;

TypedData_Get_Struct(_xml_sax, xmlSAXHandler, &noko_sax_handler_type, sax);
sax = noko_sax_handler_unwrap(_xml_sax);

if (_filename != Qnil) { filename = StringValueCStr(_filename); }

Expand Down
6 changes: 3 additions & 3 deletions ext/nokogiri/nokogiri.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,6 @@ typedef struct _nokogiriXsltStylesheetTuple {
VALUE func_instances;
} nokogiriXsltStylesheetTuple;

extern const rb_data_type_t noko_xml_document_data_type;
extern const rb_data_type_t noko_sax_handler_type;

void noko_xml_document_pin_node(xmlNodePtr);
void noko_xml_document_pin_namespace(xmlNsPtr, xmlDocPtr);

Expand All @@ -193,9 +190,12 @@ xmlNodeSetPtr noko_xml_node_set_unwrap(VALUE rb_node_set) ;

VALUE noko_xml_document_wrap_with_init_args(VALUE klass, xmlDocPtr doc, int argc, VALUE *argv);
VALUE noko_xml_document_wrap(VALUE klass, xmlDocPtr doc);
xmlDocPtr noko_xml_document_unwrap(VALUE rb_document);
NOKOPUBFUN VALUE Nokogiri_wrap_xml_document(VALUE klass,
xmlDocPtr doc); /* deprecated. use noko_xml_document_wrap() instead. */

xmlSAXHandlerPtr noko_sax_handler_unwrap(VALUE rb_sax_handler);

#define DOC_RUBY_OBJECT_TEST(x) ((nokogiriTuplePtr)(x->_private))
#define DOC_RUBY_OBJECT(x) (((nokogiriTuplePtr)(x->_private))->doc)
#define DOC_UNLINKED_NODE_HASH(x) (((nokogiriTuplePtr)(x->_private))->unlinkedNodes)
Expand Down
2 changes: 1 addition & 1 deletion ext/nokogiri/xml_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ new (int argc, VALUE *argv, VALUE klass)
rb_raise(rb_eArgError, "parameter must be a Nokogiri::XML::Document");
}

Noko_Node_Get_Struct(document, xmlDoc, xml_doc);
xml_doc = noko_xml_document_unwrap(document);

node = xmlNewDocProp(
xml_doc,
Expand Down
12 changes: 10 additions & 2 deletions ext/nokogiri/xml_cdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,22 @@ new (int argc, VALUE *argv, VALUE klass)

rb_scan_args(argc, argv, "2*", &doc, &content, &rest);

Noko_Node_Get_Struct(doc, xmlDoc, xml_doc);
if (rb_obj_is_kind_of(doc, cNokogiriXmlDocument)) {
xml_doc = noko_xml_document_unwrap(doc);
} else {
xmlNodePtr deprecated_node_type_arg;
// TODO: deprecate allowing Node
NOKO_WARN_DEPRECATION("Passing a Node as the first parameter to CDATA.new is deprecated. Please pass a Document instead. This will become an error in a future release of Nokogiri.");
Noko_Node_Get_Struct(doc, xmlNode, deprecated_node_type_arg);
xml_doc = deprecated_node_type_arg->doc;
}

if (!NIL_P(content)) {
content_str = (xmlChar *)StringValuePtr(content);
content_str_len = RSTRING_LENINT(content);
}

node = xmlNewCDataBlock(xml_doc->doc, content_str, content_str_len);
node = xmlNewCDataBlock(xml_doc, content_str, content_str_len);

noko_xml_document_pin_node(node);

Expand Down
2 changes: 1 addition & 1 deletion ext/nokogiri/xml_comment.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ new (int argc, VALUE *argv, VALUE klass)
rb_raise(rb_eArgError, "first argument must be a XML::Document or XML::Node");
}

TypedData_Get_Struct(document, xmlDoc, &noko_xml_document_data_type, xml_doc);
xml_doc = noko_xml_document_unwrap(document);

node = xmlNewDocComment(
xml_doc,
Expand Down
37 changes: 20 additions & 17 deletions ext/nokogiri/xml_document.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,14 @@ memsize(const void *data)
return memsize;
}

const rb_data_type_t noko_xml_document_data_type = {
static const rb_data_type_t noko_xml_document_data_type = {
.wrap_struct_name = "Nokogiri::XML::Document",
.function = {
.dmark = mark,
.dfree = dealloc,
.dsize = memsize,
}
},
// .flags = RUBY_TYPED_FREE_IMMEDIATELY, // TODO see https://github.com/sparklemotion/nokogiri/issues/2822
};

static void
Expand Down Expand Up @@ -167,8 +168,7 @@ recursively_remove_namespaces_from_node(xmlNodePtr node)
static VALUE
url(VALUE self)
{
xmlDocPtr doc;
TypedData_Get_Struct(self, xmlDoc, &noko_xml_document_data_type, doc);
xmlDocPtr doc = noko_xml_document_unwrap(self);

if (doc->URL) { return NOKOGIRI_STR_NEW2(doc->URL); }

Expand All @@ -187,7 +187,7 @@ rb_xml_document_root_set(VALUE self, VALUE rb_new_root)
xmlDocPtr c_document;
xmlNodePtr c_new_root = NULL, c_current_root;

TypedData_Get_Struct(self, xmlDoc, &noko_xml_document_data_type, c_document);
c_document = noko_xml_document_unwrap(self);

c_current_root = xmlDocGetRootElement(c_document);
if (c_current_root) {
Expand Down Expand Up @@ -231,7 +231,7 @@ rb_xml_document_root(VALUE self)
xmlDocPtr c_document;
xmlNodePtr c_root;

TypedData_Get_Struct(self, xmlDoc, &noko_xml_document_data_type, c_document);
c_document = noko_xml_document_unwrap(self);

c_root = xmlDocGetRootElement(c_document);
if (!c_root) {
Expand All @@ -250,8 +250,7 @@ rb_xml_document_root(VALUE self)
static VALUE
set_encoding(VALUE self, VALUE encoding)
{
xmlDocPtr doc;
TypedData_Get_Struct(self, xmlDoc, &noko_xml_document_data_type, doc);
xmlDocPtr doc = noko_xml_document_unwrap(self);

if (doc->encoding) {
xmlFree(DISCARD_CONST_QUAL_XMLCHAR(doc->encoding));
Expand All @@ -271,8 +270,7 @@ set_encoding(VALUE self, VALUE encoding)
static VALUE
encoding(VALUE self)
{
xmlDocPtr doc;
TypedData_Get_Struct(self, xmlDoc, &noko_xml_document_data_type, doc);
xmlDocPtr doc = noko_xml_document_unwrap(self);

if (!doc->encoding) { return Qnil; }
return NOKOGIRI_STR_NEW2(doc->encoding);
Expand All @@ -287,8 +285,7 @@ encoding(VALUE self)
static VALUE
version(VALUE self)
{
xmlDocPtr doc;
TypedData_Get_Struct(self, xmlDoc, &noko_xml_document_data_type, doc);
xmlDocPtr doc = noko_xml_document_unwrap(self);

if (!doc->version) { return Qnil; }
return NOKOGIRI_STR_NEW2(doc->version);
Expand Down Expand Up @@ -410,7 +407,7 @@ duplicate_document(int argc, VALUE *argv, VALUE self)
level = INT2NUM((long)1);
}

TypedData_Get_Struct(self, xmlDoc, &noko_xml_document_data_type, doc);
doc = noko_xml_document_unwrap(self);

dup = xmlCopyDoc(doc, (int)NUM2INT(level));

Expand Down Expand Up @@ -483,8 +480,7 @@ new (int argc, VALUE *argv, VALUE klass)
static VALUE
remove_namespaces_bang(VALUE self)
{
xmlDocPtr doc ;
TypedData_Get_Struct(self, xmlDoc, &noko_xml_document_data_type, doc);
xmlDocPtr doc = noko_xml_document_unwrap(self);

recursively_remove_namespaces_from_node((xmlNodePtr)doc);
return self;
Expand Down Expand Up @@ -512,7 +508,7 @@ create_entity(int argc, VALUE *argv, VALUE self)
xmlEntityPtr ptr;
xmlDocPtr doc ;

TypedData_Get_Struct(self, xmlDoc, &noko_xml_document_data_type, doc);
doc = noko_xml_document_unwrap(self);

rb_scan_args(argc, argv, "14", &name, &type, &external_id, &system_id,
&content);
Expand Down Expand Up @@ -600,7 +596,7 @@ rb_xml_document_canonicalize(int argc, VALUE *argv, VALUE self)
}
}

TypedData_Get_Struct(self, xmlDoc, &noko_xml_document_data_type, c_doc);
c_doc = noko_xml_document_unwrap(self);

rb_cStringIO = rb_const_get_at(rb_cObject, rb_intern("StringIO"));
rb_io = rb_class_new_instance(0, 0, rb_cStringIO);
Expand Down Expand Up @@ -681,6 +677,13 @@ noko_xml_document_wrap(VALUE klass, xmlDocPtr doc)
return noko_xml_document_wrap_with_init_args(klass, doc, 0, NULL);
}

xmlDocPtr
noko_xml_document_unwrap(VALUE rb_document)
{
xmlDocPtr c_document;
TypedData_Get_Struct(rb_document, xmlDoc, &noko_xml_document_data_type, c_document);
return c_document;
}

void
noko_xml_document_pin_node(xmlNodePtr node)
Expand Down
2 changes: 1 addition & 1 deletion ext/nokogiri/xml_document_fragment.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ new (int argc, VALUE *argv, VALUE klass)

rb_scan_args(argc, argv, "1*", &document, &rest);

TypedData_Get_Struct(document, xmlDoc, &noko_xml_document_data_type, xml_doc);
xml_doc = noko_xml_document_unwrap(document);

node = xmlNewDocFragment(xml_doc->doc);

Expand Down
2 changes: 1 addition & 1 deletion ext/nokogiri/xml_dtd.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ validate(VALUE self, VALUE document)
VALUE error_list;

Noko_Node_Get_Struct(self, xmlDtd, dtd);
Noko_Node_Get_Struct(document, xmlDoc, doc);
doc = noko_xml_document_unwrap(document);
error_list = rb_ary_new();

ctxt = xmlNewValidCtxt();
Expand Down
3 changes: 2 additions & 1 deletion ext/nokogiri/xml_element_content.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

VALUE cNokogiriXmlElementContent;

const rb_data_type_t element_content_data_type = {
static const rb_data_type_t element_content_data_type = {
.wrap_struct_name = "Nokogiri::XML::ElementContent",
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
};

/*
Expand Down
2 changes: 1 addition & 1 deletion ext/nokogiri/xml_encoding_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static const rb_data_type_t xml_encoding_handler_type = {
.function = {
.dfree = xml_encoding_handler_dealloc,
},
.flags = RUBY_TYPED_FREE_IMMEDIATELY
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
};


Expand Down
2 changes: 1 addition & 1 deletion ext/nokogiri/xml_entity_reference.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ new (int argc, VALUE *argv, VALUE klass)

rb_scan_args(argc, argv, "2*", &document, &name, &rest);

TypedData_Get_Struct(document, xmlDoc, &noko_xml_document_data_type, xml_doc);
xml_doc = noko_xml_document_unwrap(document);

node = xmlNewReference(
xml_doc,
Expand Down
23 changes: 11 additions & 12 deletions ext/nokogiri/xml_namespace.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,20 @@ _xml_namespace_update_references(void *ptr)
#endif

static const rb_data_type_t nokogiri_xml_namespace_type_with_dealloc = {
"Nokogiri/XMLNamespace/WithDealloc",
{0, _xml_namespace_dealloc, 0, _xml_namespace_update_references},
0, 0,
#ifdef RUBY_TYPED_FREE_IMMEDIATELY
RUBY_TYPED_FREE_IMMEDIATELY,
#endif
.wrap_struct_name = "Nokogiri::XML::Namespace#with_dealloc",
.function = {
.dfree = _xml_namespace_dealloc,
.dcompact = _xml_namespace_update_references,
},
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
};

static const rb_data_type_t nokogiri_xml_namespace_type_without_dealloc = {
"Nokogiri/XMLNamespace/WithoutDealloc",
{0, 0, 0, _xml_namespace_update_references},
0, 0,
#ifdef RUBY_TYPED_FREE_IMMEDIATELY
RUBY_TYPED_FREE_IMMEDIATELY,
#endif
.wrap_struct_name = "Nokogiri::XML::Namespace#without_dealloc",
.function = {
.dcompact = _xml_namespace_update_references,
},
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
};

/*
Expand Down
14 changes: 7 additions & 7 deletions ext/nokogiri/xml_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ _xml_node_update_references(void *ptr)
#endif

static const rb_data_type_t nokogiri_node_type = {
"Nokogiri/XMLNode",
{_xml_node_mark, 0, 0, _xml_node_update_references},
0, 0,
#ifdef RUBY_TYPED_FREE_IMMEDIATELY
RUBY_TYPED_FREE_IMMEDIATELY,
#endif
.wrap_struct_name = "Nokogiri::XML::Node",
.function = {
.dmark = _xml_node_mark,
.dcompact = _xml_node_update_references,
},
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
};

static void
Expand Down Expand Up @@ -984,7 +984,7 @@ duplicate_node(int argc, VALUE *argv, VALUE self)
if (n_args < 2) {
new_parent_doc = node->doc;
} else {
TypedData_Get_Struct(r_new_parent_doc, xmlDoc, &noko_xml_document_data_type, new_parent_doc);
new_parent_doc = noko_xml_document_unwrap(r_new_parent_doc);
}

dup = xmlDocCopyNode(node, new_parent_doc, level);
Expand Down
2 changes: 1 addition & 1 deletion ext/nokogiri/xml_node_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static const rb_data_type_t xml_node_set_type = {
.dmark = xml_node_set_mark,
.dfree = xml_node_set_deallocate,
},
.flags = RUBY_TYPED_FREE_IMMEDIATELY
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
};

static void
Expand Down
Loading