Skip to content

Commit

Permalink
prefactor: clean up Text.new implementations
Browse files Browse the repository at this point in the history
(cherry picked from commit 6325ed6)
  • Loading branch information
flavorjones committed Jul 5, 2023
1 parent e6eb81b commit e6c7768
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
6 changes: 3 additions & 3 deletions ext/java/nokogiri/XmlText.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ public class XmlText extends XmlNode
}

content = args[0];
IRubyObject xNode = args[1];
IRubyObject rbDocument = args[1];

if (!(xNode instanceof XmlDocument)) {
if (!(rbDocument 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();
Document document = asXmlNode(context, rbDocument).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.
Node node = document.createTextNode(rubyStringToString(content));
Expand Down
34 changes: 17 additions & 17 deletions ext/nokogiri/xml_text.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ VALUE cNokogiriXmlText ;
* Create a new Text element on the +document+ with +content+
*/
static VALUE
new (int argc, VALUE *argv, VALUE klass)
rb_xml_text_s_new(int argc, VALUE *argv, VALUE klass)
{
xmlDocPtr doc;
xmlNodePtr node;
VALUE string;
VALUE document;
VALUE rest;
xmlDocPtr c_document;
xmlNodePtr c_node;
VALUE rb_string;
VALUE rb_document;
VALUE rb_rest;
VALUE rb_node;

rb_scan_args(argc, argv, "2*", &string, &document, &rest);
rb_scan_args(argc, argv, "2*", &rb_string, &rb_document, &rb_rest);

if (rb_obj_is_kind_of(document, cNokogiriXmlDocument)) {
doc = noko_xml_document_unwrap(document);
} else {
if (!rb_obj_is_kind_of(rb_document, cNokogiriXmlDocument)) {
xmlNodePtr deprecated_node_type_arg;
// TODO: deprecate allowing Node
NOKO_WARN_DEPRECATION("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.");
Noko_Node_Get_Struct(document, xmlNode, deprecated_node_type_arg);
doc = deprecated_node_type_arg->doc;
Noko_Node_Get_Struct(rb_document, xmlNode, deprecated_node_type_arg);
c_document = deprecated_node_type_arg->doc;
} else {
c_document = noko_xml_document_unwrap(rb_document);
}

node = xmlNewText((xmlChar *)StringValueCStr(string));
node->doc = doc;
c_node = xmlNewText((xmlChar *)StringValueCStr(rb_string));
c_node->doc = c_document;

noko_xml_document_pin_node(node);
noko_xml_document_pin_node(c_node);

rb_node = noko_xml_node_wrap(klass, node) ;
rb_node = noko_xml_node_wrap(klass, c_node) ;
rb_obj_call_init(rb_node, argc, argv);

if (rb_block_given_p()) { rb_yield(rb_node); }
Expand All @@ -52,5 +52,5 @@ noko_init_xml_text(void)
*/
cNokogiriXmlText = rb_define_class_under(mNokogiriXml, "Text", cNokogiriXmlCharacterData);

rb_define_singleton_method(cNokogiriXmlText, "new", new, -1);
rb_define_singleton_method(cNokogiriXmlText, "new", rb_xml_text_s_new, -1);
}

0 comments on commit e6c7768

Please sign in to comment.