From faf06075d995115f2c49c0a53114994de9fe3d53 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Fri, 15 Jan 2021 08:04:27 -0500 Subject: [PATCH] fix(jruby): call Document#initialize when creating via .parse This was noticed while debugging some Loofah behavior that relied on overriding `#initialize` to decorate nodes. Related to https://github.com/flavorjones/loofah/issues/88 --- ext/java/nokogiri/internals/HtmlDomParserContext.java | 3 +++ ext/java/nokogiri/internals/XmlDomParserContext.java | 2 ++ 2 files changed, 5 insertions(+) diff --git a/ext/java/nokogiri/internals/HtmlDomParserContext.java b/ext/java/nokogiri/internals/HtmlDomParserContext.java index 71a2bacdefd..69c1d47d66c 100644 --- a/ext/java/nokogiri/internals/HtmlDomParserContext.java +++ b/ext/java/nokogiri/internals/HtmlDomParserContext.java @@ -52,6 +52,7 @@ import org.jruby.Ruby; import org.jruby.RubyClass; import org.jruby.runtime.ThreadContext; +import org.jruby.runtime.Helpers; import org.jruby.runtime.builtin.IRubyObject; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; @@ -133,6 +134,8 @@ public XmlDocument parse(ThreadContext context, RubyClass klass, IRubyObject url protected XmlDocument wrapDocument(ThreadContext context, RubyClass klass, Document document) { HtmlDocument htmlDocument = new HtmlDocument(context.runtime, klass, document); htmlDocument.setDocumentNode(context.runtime, document); + Helpers.invoke(context, htmlDocument, "initialize"); + if (ruby_encoding.isNil()) { // ruby_encoding might have detected by HtmlDocument::EncodingReader if (detected_encoding != null && !detected_encoding.isNil()) { diff --git a/ext/java/nokogiri/internals/XmlDomParserContext.java b/ext/java/nokogiri/internals/XmlDomParserContext.java index d400faae0c8..d993047fc77 100644 --- a/ext/java/nokogiri/internals/XmlDomParserContext.java +++ b/ext/java/nokogiri/internals/XmlDomParserContext.java @@ -46,6 +46,7 @@ import org.jruby.RubyFixnum; import org.jruby.exceptions.RaiseException; import org.jruby.runtime.ThreadContext; +import org.jruby.runtime.Helpers; import org.jruby.runtime.builtin.IRubyObject; import org.w3c.dom.Document; import org.w3c.dom.Node; @@ -213,6 +214,7 @@ private XmlDocument getInterruptedOrNewXmlDocument(ThreadContext context, RubyCl */ protected XmlDocument wrapDocument(ThreadContext context, RubyClass klass, Document doc) { XmlDocument xmlDocument = new XmlDocument(context.runtime, klass, doc); + Helpers.invoke(context, xmlDocument, "initialize"); xmlDocument.setEncoding(ruby_encoding); if (options.dtdLoad) {