Skip to content

Commit

Permalink
Minor tweaks to LeafNode
Browse files Browse the repository at this point in the history
  • Loading branch information
jhy committed Aug 12, 2024
1 parent fd55ee8 commit 4690661
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/jsoup/nodes/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Comment extends LeafNode {
@param data The contents of the comment
*/
public Comment(String data) {
value = data;
super(data);
}

@Override public String nodeName() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jsoup/nodes/DataNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class DataNode extends LeafNode {
@param data data contents
*/
public DataNode(String data) {
value = data;
super(data);
}

@Override public String nodeName() {
Expand Down
55 changes: 28 additions & 27 deletions src/main/java/org/jsoup/nodes/DocumentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jsoup.internal.StringUtil;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document.OutputSettings.Syntax;
import org.jspecify.annotations.Nullable;

import java.io.IOException;

Expand All @@ -13,10 +14,10 @@ public class DocumentType extends LeafNode {
// todo needs a bit of a chunky cleanup. this level of detail isn't needed
public static final String PUBLIC_KEY = "PUBLIC";
public static final String SYSTEM_KEY = "SYSTEM";
private static final String NAME = "name";
private static final String PUB_SYS_KEY = "pubSysKey"; // PUBLIC or SYSTEM
private static final String PUBLIC_ID = "publicId";
private static final String SYSTEM_ID = "systemId";
private static final String Name = "#doctype";
private static final String PubSysKey = "pubSysKey"; // PUBLIC or SYSTEM
private static final String PublicId = "publicId";
private static final String SystemId = "systemId";
// todo: quirk mode from publicId and systemId

/**
Expand All @@ -26,54 +27,54 @@ public class DocumentType extends LeafNode {
* @param systemId the doctype's system ID
*/
public DocumentType(String name, String publicId, String systemId) {
Validate.notNull(name);
super(name);
Validate.notNull(publicId);
Validate.notNull(systemId);
attr(NAME, name);
attr(PUBLIC_ID, publicId);
attr(SYSTEM_ID, systemId);
attr(Name, name);
attr(PublicId, publicId);
attr(SystemId, systemId);
updatePubSyskey();
}

public void setPubSysKey(String value) {
public void setPubSysKey(@Nullable String value) {
if (value != null)
attr(PUB_SYS_KEY, value);
attr(PubSysKey, value);
}

private void updatePubSyskey() {
if (has(PUBLIC_ID)) {
attr(PUB_SYS_KEY, PUBLIC_KEY);
} else if (has(SYSTEM_ID))
attr(PUB_SYS_KEY, SYSTEM_KEY);
if (has(PublicId)) {
attr(PubSysKey, PUBLIC_KEY);
} else if (has(SystemId))
attr(PubSysKey, SYSTEM_KEY);
}

/**
* Get this doctype's name (when set, or empty string)
* @return doctype name
*/
public String name() {
return attr(NAME);
return attr(Name);
}

/**
* Get this doctype's Public ID (when set, or empty string)
* @return doctype Public ID
*/
public String publicId() {
return attr(PUBLIC_ID);
return attr(PublicId);
}

/**
* Get this doctype's System ID (when set, or empty string)
* @return doctype System ID
*/
public String systemId() {
return attr(SYSTEM_ID);
return attr(SystemId);
}

@Override
public String nodeName() {
return "#doctype";
return Name;
}

@Override
Expand All @@ -82,20 +83,20 @@ void outerHtmlHead(Appendable accum, int depth, Document.OutputSettings out) thr
if (siblingIndex > 0 && out.prettyPrint())
accum.append('\n');

if (out.syntax() == Syntax.html && !has(PUBLIC_ID) && !has(SYSTEM_ID)) {
if (out.syntax() == Syntax.html && !has(PublicId) && !has(SystemId)) {
// looks like a html5 doctype, go lowercase for aesthetics
accum.append("<!doctype");
} else {
accum.append("<!DOCTYPE");
}
if (has(NAME))
accum.append(" ").append(attr(NAME));
if (has(PUB_SYS_KEY))
accum.append(" ").append(attr(PUB_SYS_KEY));
if (has(PUBLIC_ID))
accum.append(" \"").append(attr(PUBLIC_ID)).append('"');
if (has(SYSTEM_ID))
accum.append(" \"").append(attr(SYSTEM_ID)).append('"');
if (has(Name))
accum.append(" ").append(attr(Name));
if (has(PubSysKey))
accum.append(" ").append(attr(PubSysKey));
if (has(PublicId))
accum.append(" \"").append(attr(PublicId)).append('"');
if (has(SystemId))
accum.append(" \"").append(attr(SystemId)).append('"');
accum.append('>');
}

Expand Down
20 changes: 15 additions & 5 deletions src/main/java/org/jsoup/nodes/LeafNode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jsoup.nodes;

import org.jsoup.helper.Validate;

import java.util.List;

/**
Expand All @@ -8,7 +10,16 @@
public abstract class LeafNode extends Node {
Object value; // either a string value, or an attribute map (in the rare case multiple attributes are set)

protected final boolean hasAttributes() {
public LeafNode() {
value = "";
}

protected LeafNode(String coreValue) {
Validate.notNull(coreValue);
value = coreValue;
}

@Override protected final boolean hasAttributes() {
return value instanceof Attributes;
}

Expand All @@ -19,12 +30,11 @@ public final Attributes attributes() {
}

private void ensureAttributes() {
if (!hasAttributes()) {
Object coreValue = value;
if (!hasAttributes()) { // then value is String coreValue
String coreValue = (String) value;
Attributes attributes = new Attributes();
value = attributes;
if (coreValue != null)
attributes.put(nodeName(), (String) coreValue);
attributes.put(nodeName(), coreValue);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jsoup/nodes/TextNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Create a new TextNode representing the supplied (unencoded) text).
@see #createFromEncoded(String)
*/
public TextNode(String text) {
value = text;
super(text);
}

@Override public String nodeName() {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/jsoup/nodes/XmlDeclaration.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public class XmlDeclaration extends LeafNode {
* @param isProcessingInstruction is processing instruction
*/
public XmlDeclaration(String name, boolean isProcessingInstruction) {
Validate.notNull(name);
value = name;
super(name);
this.isProcessingInstruction = isProcessingInstruction;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jsoup/parser/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static void reset(StringBuilder sb) {

static final class Doctype extends Token {
final StringBuilder name = new StringBuilder();
String pubSysKey = null;
@Nullable String pubSysKey = null;
final StringBuilder publicIdentifier = new StringBuilder();
final StringBuilder systemIdentifier = new StringBuilder();
boolean forceQuirks = false;
Expand All @@ -86,7 +86,7 @@ String getName() {
return name.toString();
}

String getPubSysKey() {
@Nullable String getPubSysKey() {
return pubSysKey;
}

Expand Down

0 comments on commit 4690661

Please sign in to comment.