Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Revert "pushed the fix for #794 to the ruby land."

This reverts commit 96f91eb.
  • Loading branch information...
commit b4f3f47c8aff9c471d31951fb1eecfef195d6b4e 1 parent 788f2d3
John Shahid authored
8 ext/java/nokogiri/XmlComment.java
View
@@ -33,13 +33,11 @@
package nokogiri;
import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
-import nokogiri.internals.NokogiriHelpers;
import nokogiri.internals.SaveContextVisitor;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.anno.JRubyClass;
-import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.w3c.dom.Comment;
@@ -85,12 +83,6 @@ protected void init(ThreadContext context, IRubyObject[] args) {
setNode(context, node);
}
}
-
- @Override
- @JRubyMethod(name = {"content", "to_str", "text", "inner_text"})
- public IRubyObject content(ThreadContext context) {
- return NokogiriHelpers.nodeToString(context, node, (XmlDocument) document(context));
- }
@Override
public boolean isComment() { return true; }
43 ext/java/nokogiri/XmlNode.java
View
@@ -807,9 +807,48 @@ private boolean isErrorIncreased(RubyArray baseErrors, RubyArray createdErrors)
return diff_in_length > 0;
}
- @JRubyMethod(name = {"content", "text", "inner_text", "to_str"})
+ @JRubyMethod(name = {"content", "text", "inner_text"})
public IRubyObject content(ThreadContext context) {
- return context.runtime.newString();
+ if (!node.hasChildNodes() && node.getNodeValue() == null &&
+ (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE))
+ return context.nil;
+ String textContent;
+ if (this instanceof XmlDocument) {
+ Node node = ((Document)this.node).getDocumentElement();
+ if (node == null) {
+ textContent = "";
+ } else {
+ Node documentElement = ((Document)this.node).getDocumentElement();
+ StringBuffer buffer = new StringBuffer();
+ getTextContentRecursively(context, buffer, documentElement);
+ textContent = buffer.toString();
+ }
+ } else {
+ StringBuffer buffer = new StringBuffer();
+ getTextContentRecursively(context, buffer, node);
+ textContent = buffer.toString();
+ }
+ NokogiriHelpers.convertEncodingByNKFIfNecessary(context.getRuntime(), (XmlDocument)document(context), textContent);
+ return stringOrNil(context.getRuntime(), textContent);
+ }
+
+ private void getTextContentRecursively(ThreadContext context, StringBuffer buffer, Node currentNode) {
+ String textContent = currentNode.getNodeValue();
+ if (textContent != null && NokogiriHelpers.shouldDecode(currentNode))
+ textContent = NokogiriHelpers.decodeJavaString(textContent);
+ if (textContent != null)
+ buffer.append(textContent);
+ NodeList children = currentNode.getChildNodes();
+ for (int i = 0; i < children.getLength(); i++) {
+ Node child = children.item(i);
+ if (hasTextContent(child))
+ getTextContentRecursively(context, buffer, child);
+ }
+ }
+
+ private boolean hasTextContent(Node child) {
+ return child.getNodeType() != Node.COMMENT_NODE &&
+ child.getNodeType() != Node.PROCESSING_INSTRUCTION_NODE;
}
@JRubyMethod
8 ext/java/nokogiri/XmlText.java
View
@@ -34,13 +34,11 @@
import static nokogiri.internals.NokogiriHelpers.getCachedNodeOrCreate;
import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
-import nokogiri.internals.NokogiriHelpers;
import nokogiri.internals.SaveContextVisitor;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.anno.JRubyClass;
-import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.w3c.dom.Document;
@@ -84,12 +82,6 @@ protected void init(ThreadContext context, IRubyObject[] args) {
}
@Override
- @JRubyMethod(name = {"content", "text", "inner_text", "to_str"})
- public IRubyObject content(ThreadContext context) {
- return NokogiriHelpers.nodeToString(context, node, (XmlDocument) document(context));
- }
-
- @Override
protected IRubyObject getNodeName(ThreadContext context) {
if (name == null) name = context.getRuntime().newString("text");
return name;
9 ext/java/nokogiri/internals/NokogiriHelpers.java
View
@@ -693,15 +693,6 @@ private static String ignoreInvalidEncoding(Ruby runtime, IRubyObject encoding)
else return guessEncoding();
}
- public static IRubyObject nodeToString(ThreadContext context, Node node, XmlDocument doc) {
- String textContent = node.getNodeValue();
- if (textContent != null && shouldDecode(node))
- textContent = decodeJavaString(textContent);
- if (textContent != null)
- convertEncodingByNKFIfNecessary(context.getRuntime(), doc, textContent);
- return stringOrNil(context.getRuntime(), textContent);
- }
-
public static String adjustSystemIdIfNecessary(String currentDir, String scriptFileName, String baseURI, String systemId) {
if (systemId == null) return systemId;
File file = new File(systemId);
12 lib/nokogiri/xml/node.rb
View
@@ -169,15 +169,6 @@ def xpath *paths
end
end
- if Nokogiri.jruby?
- def content
- nodes = xpath('.//text()')
- nodes.map(&:content).join
- end
- end
- alias :text :content
- alias :inner_text :content
- alias :to_str :content
###
# call-seq: css *rules, [namespace-bindings, custom-pseudo-class]
#
@@ -432,10 +423,13 @@ def swap node_or_tags
alias :get_attribute :[]
alias :attr :[]
alias :set_attribute :[]=
+ alias :text :content
+ alias :inner_text :content
alias :has_attribute? :key?
alias :name :node_name
alias :name= :node_name=
alias :type :node_type
+ alias :to_str :text
alias :clone :dup
alias :elements :element_children
Please sign in to comment.
Something went wrong with that request. Please try again.