Skip to content

Commit

Permalink
Fix for formatted output along with the default format option change,…
Browse files Browse the repository at this point in the history
  • Loading branch information
yokolet committed Mar 9, 2011
1 parent 4337005 commit 3dce889
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 42 deletions.
1 change: 0 additions & 1 deletion ext/java/nokogiri/XmlAttr.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import nokogiri.internals.SaveContext;

import org.jruby.Ruby;
import org.jruby.RubyBoolean;
import org.jruby.RubyClass;
import org.jruby.RubyString;
import org.jruby.anno.JRubyClass;
Expand Down
10 changes: 0 additions & 10 deletions ext/java/nokogiri/XmlNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -491,25 +491,15 @@ protected void saveNodeListContent(ThreadContext context, XmlNodeSet list, SaveC
protected void saveNodeListContent(ThreadContext context, RubyArray array, SaveContext ctx) {
int length = array.getLength();

boolean formatIndentation = ctx.format() && ctx.indentString()!=null;

for(int i = 0; i < length; i++) {
Object item = array.get(i);
if (item instanceof XmlNode) {
XmlNode cur = (XmlNode) item;

// if(formatIndentation &&
// (cur.isElement() || cur.isComment() || cur.isProcessingInstruction())) {
// ctx.append(ctx.getCurrentIndentString());
// }

cur.saveContent(context, ctx);
} else if (item instanceof XmlNamespace) {
XmlNamespace cur = (XmlNamespace)item;
cur.saveContent(context, ctx);
}

// if(ctx.format()) ctx.append("\n");
}
}

Expand Down
35 changes: 6 additions & 29 deletions ext/java/nokogiri/internals/SaveContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public class SaveContext {
private final ThreadContext context;
private final RubyClass elementDescription;
private StringBuffer buffer;
private int options;
private int level=0;
private String encoding, indentString;
private boolean format, noDecl, noEmpty, noXhtml, xhtml, asXml, asHtml;
Expand All @@ -80,7 +79,6 @@ public class SaveContext {
public SaveContext(ThreadContext context, int options, String indentString, String encoding) {
this.context = context;
this.elementDescription = (RubyClass)getNokogiriClass(context.getRuntime(), "Nokogiri::HTML::ElementDescription");
this.options = options;
this.encoding = encoding;
this.indentString = indentString;
this.buffer = new StringBuffer();
Expand Down Expand Up @@ -117,18 +115,13 @@ public void appendQuoted(StringBuffer sb) {
this.append("\"");
}

public void emptyTag(String name) {
emptyTagStart(name);
emptyTagEnd(name);
}

public void emptyTagStart(String name) {
openTagInlineStart(name);
}

public void emptyTagEnd(String name) {
if (asHtml) {
if (isEmpty(name) && noEmpty()) {
if (isEmpty(name) && noEmpty) {
append(">");
} else {
openTagInlineEnd();
Expand All @@ -141,11 +134,6 @@ public void emptyTagEnd(String name) {
}
}

public void openTag(String name) {
openTagStart(name);
openTagEnd();
}

public void openTagStart(String name) {
maybeBreak();
indent();
Expand Down Expand Up @@ -206,7 +194,9 @@ public void maybeIndent() {
}

public void indent() {
if (format) append(getCurrentIndentString());
// format option doesn't work, so changed to see indentString is given or not
String spaces = getCurrentIndentString();
if (spaces != null) append(spaces);
}

public boolean endsInWhitespace() {
Expand All @@ -232,14 +222,13 @@ public void decreaseLevel() {
if(this.level > 0) this.level--;
}

public String encoding() { return this.encoding; }

public boolean format() { return this.format; }

public String getCurrentIndentString() {
if (indentString == null || indentString.length() == 0) return null;
StringBuffer res = new StringBuffer();
for(int i = 0; i < this.level; i++) {
res.append(this.indentString());
res.append(indentString);
}
return res.toString();
}
Expand All @@ -250,18 +239,8 @@ public void increaseLevel() {
if(this.level >= 0) this.level++;
}

public String indentString() { return this.indentString; }

public boolean noDecl() { return this.noDecl; }

public boolean noEmpty() { return this.noEmpty; }

public boolean noXhtml() { return this.noXhtml; }

public void setFormat(boolean format) { this.format = format; }

public void setLevel(int level) { this.level = level; }

@Override
public String toString() { return this.buffer.toString(); }

Expand All @@ -272,8 +251,6 @@ public RubyString toRubyString(Ruby runtime) {
return RubyString.newString(runtime, bytes);
}

public boolean Xhtml() { return this.xhtml; }

/**
* Looks up the HTML ElementDescription and tests if it is an
* empty element.
Expand Down
8 changes: 6 additions & 2 deletions lib/nokogiri/xml/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ def == other
def serialize *args, &block
options = args.first.is_a?(Hash) ? args.shift : {
:encoding => args[0],
:save_with => args[1] || SaveOptions::FORMAT
:save_with => args[1]
}

encoding = options[:encoding] || document.encoding
Expand Down Expand Up @@ -791,7 +791,11 @@ def to_xhtml options = {}
def write_to io, *options
options = options.first.is_a?(Hash) ? options.shift : {}
encoding = options[:encoding] || options[0]
save_options = options[:save_with] || options[1] || SaveOptions::FORMAT
if Nokogiri.jruby?
save_options = options[:save_with] || options[1]
else
save_options = options[:save_with] || options[1] || SaveOptions::FORMAT
end
indent_text = options[:indent_text] || ' '
indent_times = options[:indent] || 2

Expand Down

0 comments on commit 3dce889

Please sign in to comment.