diff --git a/build.gradle b/build.gradle index ee6ac15..07be04a 100644 --- a/build.gradle +++ b/build.gradle @@ -1,48 +1,49 @@ buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:2.0.0' } } apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion '23.0.3' // was 23.0.2 compileOptions { sourceCompatibility JavaVersion.VERSION_1_7 targetCompatibility JavaVersion.VERSION_1_7 } sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } } lintOptions { abortOnError false } repositories { mavenCentral() mavenLocal() } dependencies { compile 'com.android.support:support-v4:23.2.1' compile 'com.android.support:appcompat-v7:23.2.1' compile 'com.melnykov:floatingactionbutton:1.1.0' + compile 'org.jsoup:jsoup:1.7.3' } } \ No newline at end of file diff --git a/src/org/jsoup/Connection.java b/src/org/jsoup/Connection.java deleted file mode 100644 index 44307a6..0000000 --- a/src/org/jsoup/Connection.java +++ /dev/null @@ -1,572 +0,0 @@ -package org.jsoup; - -import org.jsoup.nodes.Document; -import org.jsoup.parser.Parser; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.Collection; -import java.util.Map; - -/** - * A Connection provides a convenient interface to fetch content from the web, and parse them into Documents. - *
- * To get a new Connection, use {@link org.jsoup.Jsoup#connect(String)}. Connections contain {@link Connection.Request} - * and {@link Connection.Response} objects. The request objects are reusable as prototype requests. - * - * Request configuration can be made using either the shortcut methods in Connection (e.g. {@link #userAgent(String)}), - * or by methods in the Connection.Request object directly. All request configuration must be made before the request is - * executed. - */ -public interface Connection { - - /** - * GET and POST http methods. - */ - public enum Method { - GET(false), POST(true), PUT(true), DELETE(false), PATCH(true); - - private final boolean hasBody; - - private Method(boolean hasBody) { - this.hasBody = hasBody; - } - - /** - * Check if this HTTP method has/needs a request body - * @return if body needed - */ - public final boolean hasBody() { - return hasBody; - } - } - - /** - * Set the request URL to fetch. The protocol must be HTTP or HTTPS. - * @param url URL to connect to - * @return this Connection, for chaining - */ - public Connection url(URL url); - - /** - * Set the request URL to fetch. The protocol must be HTTP or HTTPS. - * @param url URL to connect to - * @return this Connection, for chaining - */ - public Connection url(String url); - - /** - * Set the request user-agent header. - * @param userAgent user-agent to use - * @return this Connection, for chaining - */ - public Connection userAgent(String userAgent); - - /** - * Set the request timeouts (connect and read). If a timeout occurs, an IOException will be thrown. The default - * timeout is 3 seconds (3000 millis). A timeout of zero is treated as an infinite timeout. - * @param millis number of milliseconds (thousandths of a second) before timing out connects or reads. - * @return this Connection, for chaining - */ - public Connection timeout(int millis); - - /** - * Set the maximum bytes to read from the (uncompressed) connection into the body, before the connection is closed, - * and the input truncated. The default maximum is 1MB. A max size of zero is treated as an infinite amount (bounded - * only by your patience and the memory available on your machine). - * @param bytes number of bytes to read from the input before truncating - * @return this Connection, for chaining - */ - public Connection maxBodySize(int bytes); - - /** - * Set the request referrer (aka "referer") header. - * @param referrer referrer to use - * @return this Connection, for chaining - */ - public Connection referrer(String referrer); - - /** - * Configures the connection to (not) follow server redirects. By default this is true. - * @param followRedirects true if server redirects should be followed. - * @return this Connection, for chaining - */ - public Connection followRedirects(boolean followRedirects); - - /** - * Set the request method to use, GET or POST. Default is GET. - * @param method HTTP request method - * @return this Connection, for chaining - */ - public Connection method(Method method); - - /** - * Configures the connection to not throw exceptions when a HTTP error occurs. (4xx - 5xx, e.g. 404 or 500). By - * default this is false; an IOException is thrown if an error is encountered. If set to true, the - * response is populated with the error body, and the status message will reflect the error. - * @param ignoreHttpErrors - false (default) if HTTP errors should be ignored. - * @return this Connection, for chaining - */ - public Connection ignoreHttpErrors(boolean ignoreHttpErrors); - - /** - * Ignore the document's Content-Type when parsing the response. By default this is false, an unrecognised - * content-type will cause an IOException to be thrown. (This is to prevent producing garbage by attempting to parse - * a JPEG binary image, for example.) Set to true to force a parse attempt regardless of content type. - * @param ignoreContentType set to true if you would like the content type ignored on parsing the response into a - * Document. - * @return this Connection, for chaining - */ - public Connection ignoreContentType(boolean ignoreContentType); - - /** - * Add a request data parameter. Request parameters are sent in the request query string for GETs, and in the - * request body for POSTs. A request may have multiple values of the same name. - * @param key data key - * @param value data value - * @return this Connection, for chaining - */ - public Connection data(String key, String value); - - /** - * Add an input stream as a request data paramater. For GETs, has no effect, but for POSTS this will upload the - * input stream. - * @param key data key (form item name) - * @param filename the name of the file to present to the remove server. Typically just the name, not path, - * component. - * @param inputStream the input stream to upload, that you probably obtained from a {@link java.io.FileInputStream}. - * You must close the InputStream in a {@code finally} block. - * @return this Connections, for chaining - */ - public Connection data(String key, String filename, InputStream inputStream); - - /** - * Adds all of the supplied data to the request data parameters - * @param data collection of data parameters - * @return this Connection, for chaining - */ - public Connection data(Collection.data("name",
- * "jsoup", "language", "Java", "language", "English"); creates a query string like:
- * ?name=jsoup&language=Java&language=English
- * @param keyvals a set of key value pairs.
- * @return this Connection, for chaining
- */
- public Connection data(String... keyvals);
-
- /**
- * Set a request header.
- * @param name header name
- * @param value header value
- * @return this Connection, for chaining
- * @see org.jsoup.Connection.Request#headers()
- */
- public Connection header(String name, String value);
-
- /**
- * Set a cookie to be sent in the request.
- * @param name name of cookie
- * @param value value of cookie
- * @return this Connection, for chaining
- */
- public Connection cookie(String name, String value);
-
- /**
- * Adds each of the supplied cookies to the request.
- * @param cookies map of cookie name -> value pairs
- * @return this Connection, for chaining
- */
- public Connection cookies(Map- * Use examples: - *
Document doc = Jsoup.connect("http://example.com").userAgent("Mozilla").data("name", "jsoup").get();Document doc = Jsoup.connect("http://example.com").cookie("auth", "token").post();
- The encoding character set is determined by the content-type header or http-equiv meta tag, or falls back to {@code UTF-8}.
-
- @param url URL to fetch (with a GET). The protocol must be {@code http} or {@code https}.
- @param timeoutMillis Connection and read timeout, in milliseconds. If exceeded, IOException is thrown.
- @return The parsed HTML.
-
- @throws java.net.MalformedURLException if the request URL is not a HTTP or HTTPS URL, or is otherwise malformed
- @throws HttpStatusException if the response is not OK and HTTP response errors are not ignored
- @throws UnsupportedMimeTypeException if the response mime type is not supported and those errors are not ignored
- @throws java.net.SocketTimeoutException if the connection times out
- @throws IOException if a connection or read error occurs
-
- @see #connect(String)
- */
- public static Document parse(URL url, int timeoutMillis) throws IOException {
- Connection con = HttpConnection.connect(url);
- con.timeout(timeoutMillis);
- return con.get();
- }
-
- /**
- Get safe HTML from untrusted input HTML, by parsing input HTML and filtering it through a white-list of permitted
- tags and attributes.
-
- @param bodyHtml input untrusted HTML (body fragment)
- @param baseUri URL to resolve relative URLs against
- @param whitelist white-list of permitted HTML elements
- @return safe HTML (body fragment)
-
- @see Cleaner#clean(Document)
- */
- public static String clean(String bodyHtml, String baseUri, Whitelist whitelist) {
- Document dirty = parseBodyFragment(bodyHtml, baseUri);
- Cleaner cleaner = new Cleaner(whitelist);
- Document clean = cleaner.clean(dirty);
- return clean.body().html();
- }
-
- /**
- Get safe HTML from untrusted input HTML, by parsing input HTML and filtering it through a white-list of permitted
- tags and attributes.
-
- @param bodyHtml input untrusted HTML (body fragment)
- @param whitelist white-list of permitted HTML elements
- @return safe HTML (body fragment)
-
- @see Cleaner#clean(Document)
- */
- public static String clean(String bodyHtml, Whitelist whitelist) {
- return clean(bodyHtml, "", whitelist);
- }
-
- /**
- * Get safe HTML from untrusted input HTML, by parsing input HTML and filtering it through a white-list of
- * permitted
- * tags and attributes.
- *
- * @param bodyHtml input untrusted HTML (body fragment)
- * @param baseUri URL to resolve relative URLs against
- * @param whitelist white-list of permitted HTML elements
- * @param outputSettings document output settings; use to control pretty-printing and entity escape modes
- * @return safe HTML (body fragment)
- * @see Cleaner#clean(Document)
- */
- public static String clean(String bodyHtml, String baseUri, Whitelist whitelist, Document.OutputSettings outputSettings) {
- Document dirty = parseBodyFragment(bodyHtml, baseUri);
- Cleaner cleaner = new Cleaner(whitelist);
- Document clean = cleaner.clean(dirty);
- clean.outputSettings(outputSettings);
- return clean.body().html();
- }
-
- /**
- Test if the input HTML has only tags and attributes allowed by the Whitelist. Useful for form validation. The input HTML should
- still be run through the cleaner to set up enforced attributes, and to tidy the output.
- @param bodyHtml HTML to test
- @param whitelist whitelist to test against
- @return true if no tags or attributes were removed; false otherwise
- @see #clean(String, org.jsoup.safety.Whitelist)
- */
- public static boolean isValid(String bodyHtml, Whitelist whitelist) {
- Document dirty = parseBodyFragment(bodyHtml, "");
- Cleaner cleaner = new Cleaner(whitelist);
- return cleaner.isValid(dirty);
- }
-
-}
diff --git a/src/org/jsoup/UnsupportedMimeTypeException.java b/src/org/jsoup/UnsupportedMimeTypeException.java
deleted file mode 100644
index 124bc97..0000000
--- a/src/org/jsoup/UnsupportedMimeTypeException.java
+++ /dev/null
@@ -1,30 +0,0 @@
-package org.jsoup;
-
-import java.io.IOException;
-
-/**
- * Signals that a HTTP response returned a mime type that is not supported.
- */
-public class UnsupportedMimeTypeException extends IOException {
- private String mimeType;
- private String url;
-
- public UnsupportedMimeTypeException(String message, String mimeType, String url) {
- super(message);
- this.mimeType = mimeType;
- this.url = url;
- }
-
- public String getMimeType() {
- return mimeType;
- }
-
- public String getUrl() {
- return url;
- }
-
- @Override
- public String toString() {
- return super.toString() + ". Mimetype=" + mimeType + ", URL="+url;
- }
-}
diff --git a/src/org/jsoup/helper/DataUtil.java b/src/org/jsoup/helper/DataUtil.java
deleted file mode 100644
index 95d1200..0000000
--- a/src/org/jsoup/helper/DataUtil.java
+++ /dev/null
@@ -1,224 +0,0 @@
-package org.jsoup.helper;
-
-import org.jsoup.nodes.Document;
-import org.jsoup.nodes.Element;
-import org.jsoup.parser.Parser;
-
-import java.io.*;
-import java.nio.ByteBuffer;
-import java.nio.charset.Charset;
-import java.nio.charset.IllegalCharsetNameException;
-import java.util.Random;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-import java.util.Locale;
-
-/**
- * Internal static utilities for handling data.
- *
- */
-public final class DataUtil {
- private static final Pattern charsetPattern = Pattern.compile("(?i)\\bcharset=\\s*(?:\"|')?([^\\s,;\"']*)");
- static final String defaultCharset = "UTF-8"; // used if not found in header or meta charset
- private static final int bufferSize = 0x20000; // ~130K.
- private static final int UNICODE_BOM = 0xFEFF;
- private static final char[] mimeBoundaryChars =
- "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
- static final int boundaryLength = 32;
-
- private DataUtil() {}
-
- /**
- * Loads a file to a Document.
- * @param in file to load
- * @param charsetName character set of input
- * @param baseUri base URI of document, to resolve relative links against
- * @return Document
- * @throws IOException on IO error
- */
- public static Document load(File in, String charsetName, String baseUri) throws IOException {
- ByteBuffer byteData = readFileToByteBuffer(in);
- return parseByteData(byteData, charsetName, baseUri, Parser.htmlParser());
- }
-
- /**
- * Parses a Document from an input steam.
- * @param in input stream to parse. You will need to close it.
- * @param charsetName character set of input
- * @param baseUri base URI of document, to resolve relative links against
- * @return Document
- * @throws IOException on IO error
- */
- public static Document load(InputStream in, String charsetName, String baseUri) throws IOException {
- ByteBuffer byteData = readToByteBuffer(in);
- return parseByteData(byteData, charsetName, baseUri, Parser.htmlParser());
- }
-
- /**
- * Parses a Document from an input steam, using the provided Parser.
- * @param in input stream to parse. You will need to close it.
- * @param charsetName character set of input
- * @param baseUri base URI of document, to resolve relative links against
- * @param parser alternate {@link Parser#xmlParser() parser} to use.
- * @return Document
- * @throws IOException on IO error
- */
- public static Document load(InputStream in, String charsetName, String baseUri, Parser parser) throws IOException {
- ByteBuffer byteData = readToByteBuffer(in);
- return parseByteData(byteData, charsetName, baseUri, parser);
- }
-
- /**
- * Writes the input stream to the output stream. Doesn't close them.
- * @param in input stream to read from
- * @param out output stream to write to
- * @throws IOException on IO error
- */
- static void crossStreams(final InputStream in, final OutputStream out) throws IOException {
- final byte[] buffer = new byte[bufferSize];
- int len;
- while ((len = in.read(buffer)) != -1) {
- out.write(buffer, 0, len);
- }
- }
-
- // reads bytes first into a buffer, then decodes with the appropriate charset. done this way to support
- // switching the chartset midstream when a meta http-equiv tag defines the charset.
- // todo - this is getting gnarly. needs a rewrite.
- static Document parseByteData(ByteBuffer byteData, String charsetName, String baseUri, Parser parser) {
- String docData;
- Document doc = null;
- if (charsetName == null) { // determine from meta. safe parse as UTF-8
- // look for or HTML5
- docData = Charset.forName(defaultCharset).decode(byteData).toString();
- doc = parser.parseInput(docData, baseUri);
- Element meta = doc.select("meta[http-equiv=content-type], meta[charset]").first();
- if (meta != null) { // if not found, will keep utf-8 as best attempt
- String foundCharset;
- if (meta.hasAttr("http-equiv")) {
- foundCharset = getCharsetFromContentType(meta.attr("content"));
- if (foundCharset == null && meta.hasAttr("charset")) {
- try {
- if (Charset.isSupported(meta.attr("charset"))) {
- foundCharset = meta.attr("charset");
- }
- } catch (IllegalCharsetNameException e) {
- foundCharset = null;
- }
- }
- } else {
- foundCharset = meta.attr("charset");
- }
-
- if (foundCharset != null && foundCharset.length() != 0 && !foundCharset.equals(defaultCharset)) { // need to re-decode
- foundCharset = foundCharset.trim().replaceAll("[\"']", "");
- charsetName = foundCharset;
- byteData.rewind();
- docData = Charset.forName(foundCharset).decode(byteData).toString();
- doc = null;
- }
- }
- } else { // specified by content type header (or by user on file load)
- Validate.notEmpty(charsetName, "Must set charset arg to character set of file to parse. Set to null to attempt to detect from HTML");
- docData = Charset.forName(charsetName).decode(byteData).toString();
- }
- // UTF-8 BOM indicator. takes precedence over everything else. rarely used. re-decodes incase above decoded incorrectly
- if (docData.length() > 0 && docData.charAt(0) == UNICODE_BOM) {
- byteData.rewind();
- docData = Charset.forName(defaultCharset).decode(byteData).toString();
- docData = docData.substring(1);
- charsetName = defaultCharset;
- doc = null;
- }
- if (doc == null) {
- doc = parser.parseInput(docData, baseUri);
- doc.outputSettings().charset(charsetName);
- }
- return doc;
- }
-
- /**
- * Read the input stream into a byte buffer.
- * @param inStream the input stream to read from
- * @param maxSize the maximum size in bytes to read from the stream. Set to 0 to be unlimited.
- * @return the filled byte buffer
- * @throws IOException if an exception occurs whilst reading from the input stream.
- */
- static ByteBuffer readToByteBuffer(InputStream inStream, int maxSize) throws IOException {
- Validate.isTrue(maxSize >= 0, "maxSize must be 0 (unlimited) or larger");
- final boolean capped = maxSize > 0;
- byte[] buffer = new byte[bufferSize];
- ByteArrayOutputStream outStream = new ByteArrayOutputStream(bufferSize);
- int read;
- int remaining = maxSize;
-
- while (true) {
- read = inStream.read(buffer);
- if (read == -1) break;
- if (capped) {
- if (read > remaining) {
- outStream.write(buffer, 0, remaining);
- break;
- }
- remaining -= read;
- }
- outStream.write(buffer, 0, read);
- }
- ByteBuffer byteData = ByteBuffer.wrap(outStream.toByteArray());
- return byteData;
- }
-
- static ByteBuffer readToByteBuffer(InputStream inStream) throws IOException {
- return readToByteBuffer(inStream, 0);
- }
-
- static ByteBuffer readFileToByteBuffer(File file) throws IOException {
- RandomAccessFile randomAccessFile = null;
- try {
- randomAccessFile = new RandomAccessFile(file, "r");
- byte[] bytes = new byte[(int) randomAccessFile.length()];
- randomAccessFile.readFully(bytes);
- return ByteBuffer.wrap(bytes);
- } finally {
- if (randomAccessFile != null)
- randomAccessFile.close();
- }
- }
-
- /**
- * Parse out a charset from a content type header. If the charset is not supported, returns null (so the default
- * will kick in.)
- * @param contentType e.g. "text/html; charset=EUC-JP"
- * @return "EUC-JP", or null if not found. Charset is trimmed and uppercased.
- */
- static String getCharsetFromContentType(String contentType) {
- if (contentType == null) return null;
- Matcher m = charsetPattern.matcher(contentType);
- if (m.find()) {
- String charset = m.group(1).trim();
- charset = charset.replace("charset=", "");
- if (charset.length() == 0) return null;
- try {
- if (Charset.isSupported(charset)) return charset;
- charset = charset.toUpperCase(Locale.ENGLISH);
- if (Charset.isSupported(charset)) return charset;
- } catch (IllegalCharsetNameException e) {
- // if our advanced charset matching fails.... we just take the default
- return null;
- }
- }
- return null;
- }
-
- /**
- * Creates a random string, suitable for use as a mime boundary
- */
- static String mimeBoundary() {
- final StringBuilder mime = new StringBuilder(boundaryLength);
- final Random rand = new Random();
- for (int i = 0; i < boundaryLength; i++) {
- mime.append(mimeBoundaryChars[rand.nextInt(mimeBoundaryChars.length)]);
- }
- return mime.toString();
- }
-}
diff --git a/src/org/jsoup/helper/DescendableLinkedList.java b/src/org/jsoup/helper/DescendableLinkedList.java
deleted file mode 100644
index 01bed86..0000000
--- a/src/org/jsoup/helper/DescendableLinkedList.java
+++ /dev/null
@@ -1,87 +0,0 @@
-package org.jsoup.helper;
-
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.ListIterator;
-
-/**
- * Provides a descending iterator and other 1.6 methods to allow support on the 1.5 JRE.
- * @param
- * The default escape mode is
- * Where possible (when parsing from a URL or File), the document's output charset is automatically set to the
- * input charset. Otherwise, it defaults to UTF-8.
- * @return the document's current charset.
- */
- public Charset charset() {
- return charset;
- }
-
- /**
- * Update the document's output charset.
- * @param charset the new charset to use.
- * @return the document's output settings, for chaining
- */
- public OutputSettings charset(Charset charset) {
- // todo: this should probably update the doc's meta charset
- this.charset = charset;
- charsetEncoder = charset.newEncoder();
- return this;
- }
-
- /**
- * Update the document's output charset.
- * @param charset the new charset (by name) to use.
- * @return the document's output settings, for chaining
- */
- public OutputSettings charset(String charset) {
- charset(Charset.forName(charset));
- return this;
- }
-
- CharsetEncoder encoder() {
- return charsetEncoder;
- }
-
- /**
- * Get the document's current output syntax.
- * @return current syntax
- */
- public Syntax syntax() {
- return syntax;
- }
-
- /**
- * Set the document's output syntax. Either {@code html}, with empty tags and boolean attributes (etc), or
- * {@code xml}, with self-closing tags.
- * @param syntax serialization syntax
- * @return the document's output settings, for chaining
- */
- public OutputSettings syntax(Syntax syntax) {
- this.syntax = syntax;
- return this;
- }
-
- /**
- * Get if pretty printing is enabled. Default is true. If disabled, the HTML output methods will not re-format
- * the output, and the output will generally look like the input.
- * @return if pretty printing is enabled.
- */
- public boolean prettyPrint() {
- return prettyPrint;
- }
-
- /**
- * Enable or disable pretty printing.
- * @param pretty new pretty print setting
- * @return this, for chaining
- */
- public OutputSettings prettyPrint(boolean pretty) {
- prettyPrint = pretty;
- return this;
- }
-
- /**
- * Get if outline mode is enabled. Default is false. If enabled, the HTML output methods will consider
- * all tags as block.
- * @return if outline mode is enabled.
- */
- public boolean outline() {
- return outline;
- }
-
- /**
- * Enable or disable HTML outline mode.
- * @param outlineMode new outline setting
- * @return this, for chaining
- */
- public OutputSettings outline(boolean outlineMode) {
- outline = outlineMode;
- return this;
- }
-
- /**
- * Get the current tag indent amount, used when pretty printing.
- * @return the current indent amount
- */
- public int indentAmount() {
- return indentAmount;
- }
-
- /**
- * Set the indent amount for pretty printing
- * @param indentAmount number of spaces to use for indenting each level. Must be >= 0.
- * @return this, for chaining
- */
- public OutputSettings indentAmount(int indentAmount) {
- Validate.isTrue(indentAmount >= 0);
- this.indentAmount = indentAmount;
- return this;
- }
-
- @Override
- public OutputSettings clone() {
- OutputSettings clone;
- try {
- clone = (OutputSettings) super.clone();
- } catch (CloneNotSupportedException e) {
- throw new RuntimeException(e);
- }
- clone.charset(charset.name()); // new charset and charset encoder
- clone.escapeMode = Entities.EscapeMode.valueOf(escapeMode.name());
- // indentAmount, prettyPrint are primitives so object.clone() will handle
- return clone;
- }
- }
-
- /**
- * Get the document's current output settings.
- * @return the document's current output settings.
- */
- public OutputSettings outputSettings() {
- return outputSettings;
- }
-
- /**
- * Set the document's output settings.
- * @param outputSettings new output settings.
- * @return this document, for chaining.
- */
- public Document outputSettings(OutputSettings outputSettings) {
- Validate.notNull(outputSettings);
- this.outputSettings = outputSettings;
- return this;
- }
-
- public enum QuirksMode {
- noQuirks, quirks, limitedQuirks;
- }
-
- public QuirksMode quirksMode() {
- return quirksMode;
- }
-
- public Document quirksMode(QuirksMode quirksMode) {
- this.quirksMode = quirksMode;
- return this;
- }
-
- @Override
- public boolean equals(Object o) {
- return super.equals(o);
- }
-}
-
diff --git a/src/org/jsoup/nodes/DocumentType.java b/src/org/jsoup/nodes/DocumentType.java
deleted file mode 100644
index 9b7ead4..0000000
--- a/src/org/jsoup/nodes/DocumentType.java
+++ /dev/null
@@ -1,59 +0,0 @@
-package org.jsoup.nodes;
-
-import org.jsoup.helper.StringUtil;
-import org.jsoup.nodes.Document.OutputSettings.*;
-
-/**
- * A {@code } node.
- */
-public class DocumentType extends Node {
- private static final String NAME = "name";
- private static final String PUBLIC_ID = "publicId";
- private static final String SYSTEM_ID = "systemId";
- // todo: quirk mode from publicId and systemId
-
- /**
- * Create a new doctype element.
- * @param name the doctype's name
- * @param publicId the doctype's public ID
- * @param systemId the doctype's system ID
- * @param baseUri the doctype's base URI
- */
- public DocumentType(String name, String publicId, String systemId, String baseUri) {
- super(baseUri);
-
- attr(NAME, name);
- attr(PUBLIC_ID, publicId);
- attr(SYSTEM_ID, systemId);
- }
-
- @Override
- public String nodeName() {
- return "#doctype";
- }
-
- @Override
- void outerHtmlHead(StringBuilder accum, int depth, Document.OutputSettings out) {
- if (out.syntax() == Syntax.html && !has(PUBLIC_ID) && !has(SYSTEM_ID)) {
- // looks like a html5 doctype, go lowercase for aesthetics
- accum.append("');
- }
-
- @Override
- void outerHtmlTail(StringBuilder accum, int depth, Document.OutputSettings out) {
- }
-
- private boolean has(final String attribute) {
- return !StringUtil.isBlank(attr(attribute));
- }
-}
diff --git a/src/org/jsoup/nodes/Element.java b/src/org/jsoup/nodes/Element.java
deleted file mode 100644
index 3ea9780..0000000
--- a/src/org/jsoup/nodes/Element.java
+++ /dev/null
@@ -1,1174 +0,0 @@
-package org.jsoup.nodes;
-
-import org.jsoup.helper.StringUtil;
-import org.jsoup.helper.Validate;
-import org.jsoup.parser.Parser;
-import org.jsoup.parser.Tag;
-import org.jsoup.select.*;
-
-import java.util.*;
-import java.util.regex.Pattern;
-import java.util.regex.PatternSyntaxException;
-
-/**
- * A HTML element consists of a tag name, attributes, and child nodes (including text nodes and
- * other elements).
- *
- * From an Element, you can extract data, traverse the node graph, and manipulate the HTML.
- *
- * @author Jonathan Hedley, jonathan@hedley.net
- */
-public class Element extends Node {
- private Tag tag;
- /**
- * Create a new, standalone Element. (Standalone in that is has no parent.)
- *
- * @param tag tag of this element
- * @param baseUri the base URI
- * @param attributes initial attributes
- * @see #appendChild(Node)
- * @see #appendElement(String)
- */
- public Element(Tag tag, String baseUri, Attributes attributes) {
- super(baseUri, attributes);
-
- Validate.notNull(tag);
- this.tag = tag;
- }
-
- /**
- * Create a new Element from a tag and a base URI.
- *
- * @param tag element tag
- * @param baseUri the base URI of this element. It is acceptable for the base URI to be an empty
- * string, but not null.
- * @see Tag#valueOf(String)
- */
- public Element(Tag tag, String baseUri) {
- this(tag, baseUri, new Attributes());
- }
-
- @Override
- public String nodeName() {
- return tag.getName();
- }
-
- /**
- * Get the name of the tag for this element. E.g. {@code div}
- *
- * @return the tag name
- */
- public String tagName() {
- return tag.getName();
- }
-
- /**
- * Change the tag of this element. For example, convert a {@code } to a {@code == false}).
- *
- * @return true if block, false if not (and thus inline)
- */
- public boolean isBlock() {
- return tag.isBlock();
- }
-
- /**
- * Get the {@code id} attribute of this element.
- *
- * @return The id attribute, if present, or an empty string if not.
- */
- public String id() {
- String id = attr("id");
- return id == null ? "" : id;
- }
-
- /**
- * Set an attribute value on this element. If this element already has an attribute with the
- * key, its value is updated; otherwise, a new attribute is added.
- *
- * @return this element
- */
- public Element attr(String attributeKey, String attributeValue) {
- super.attr(attributeKey, attributeValue);
- return this;
- }
-
- /**
- * Get this element's HTML5 custom data attributes. Each attribute in the element that has a key
- * starting with "data-" is included the dataset.
- *
- * E.g., the element {@code
- * This map is a filtered view of the element's attribute map. Changes to one map (add, remove, update) are reflected
- * in the other map.
- *
- * You can find elements that have data attributes using the {@code [^data-]} attribute key prefix selector.
- * @return a map of {@code key=value} custom data attributes.
- */
- public Map One Two Three
- * Note that this finds the first matching ID, starting with this element. If you search down from a different
- * starting point, it is possible to find a different element by ID. For unique element by ID within a Document,
- * use {@link Document#getElementById(String)}
- * @param id The ID to search for.
- * @return The first matching element by ID, starting with this element, or null if none found.
- */
- public Element getElementById(String id) {
- Validate.notEmpty(id);
-
- Elements elements = Collector.collect(new Evaluator.Id(id), this);
- if (elements.size() > 0)
- return elements.get(0);
- else
- return null;
- }
-
- /**
- * Find elements that have this class, including or under this element. Case insensitive.
- *
- * Elements can have multiple classes (e.g. {@code
- * For example, given HTML {@code Hello there now!
- * For example, given HTML {@code Hello there now! }, would return
- * {@code is remainder
- if (wrapChildren.size() > 0) {
- for (int i = 0; i < wrapChildren.size(); i++) {
- Node remainder = wrapChildren.get(i);
- remainder.parentNode.removeChild(remainder);
- wrap.appendChild(remainder);
- }
- }
- return this;
- }
-
- /**
- * Removes this node from the DOM, and moves its children up into the node's parent. This has the effect of dropping
- * the node but keeping its children.
- *
- * The cloned node may be adopted into another Document or node structure using {@link Element#appendChild(Node)}.
- * @return stand-alone cloned node
- */
- @Override
- public Node clone() {
- Node thisClone = doClone(null); // splits for orphan
-
- // Queue up nodes that need their children cloned (BFS).
- LinkedList )
- tag = new Tag(tagName);
- tag.isBlock = false;
- tag.canContainBlock = true;
- }
- }
- return tag;
- }
-
- /**
- * Gets if this is a block tag.
- *
- * @return if block tag
- */
- public boolean isBlock() {
- return isBlock;
- }
-
- /**
- * Gets if this tag should be formatted as a block (or as inline)
- *
- * @return if should be formatted as block or inline
- */
- public boolean formatAsBlock() {
- return formatAsBlock;
- }
-
- /**
- * Gets if this tag can contain block tags.
- *
- * @return if tag can contain block tags
- */
- public boolean canContainBlock() {
- return canContainBlock;
- }
-
- /**
- * Gets if this tag is an inline tag.
- *
- * @return if this tag is an inline tag.
- */
- public boolean isInline() {
- return !isBlock;
- }
-
- /**
- * Gets if this tag is a data only tag.
- *
- * @return if this tag is a data only tag
- */
- public boolean isData() {
- return !canContainInline && !isEmpty();
- }
-
- /**
- * Get if this is an empty tag
- *
- * @return if this is an empty tag
- */
- public boolean isEmpty() {
- return empty;
- }
-
- /**
- * Get if this tag is self closing.
- *
- * @return if this tag should be output as self closing.
- */
- public boolean isSelfClosing() {
- return empty || selfClosing;
- }
-
- /**
- * Get if this is a pre-defined tag, or was auto created on parsing.
- *
- * @return if a known tag
- */
- public boolean isKnownTag() {
- return tags.containsKey(tagName);
- }
-
- /**
- * Check if this tagname is a known tag.
- *
- * @param tagName name of tag
- * @return if known HTML tag
- */
- public static boolean isKnownTag(String tagName) {
- return tags.containsKey(tagName);
- }
-
- /**
- * Get if this tag should preserve whitespace within child text nodes.
- *
- * @return if preserve whitepace
- */
- public boolean preserveWhitespace() {
- return preserveWhitespace;
- }
-
- /**
- * Get if this tag represents a control associated with a form. E.g. input, textarea, output
- * @return if associated with a form
- */
- public boolean isFormListed() {
- return formList;
- }
-
- /**
- * Get if this tag represents an element that should be submitted with a form. E.g. input, option
- * @return if submittable with a form
- */
- public boolean isFormSubmittable() {
- return formSubmit;
- }
-
- Tag setSelfClosing() {
- selfClosing = true;
- return this;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof Tag)) return false;
-
- Tag tag = (Tag) o;
-
- if (!tagName.equals(tag.tagName)) return false;
- if (canContainBlock != tag.canContainBlock) return false;
- if (canContainInline != tag.canContainInline) return false;
- if (empty != tag.empty) return false;
- if (formatAsBlock != tag.formatAsBlock) return false;
- if (isBlock != tag.isBlock) return false;
- if (preserveWhitespace != tag.preserveWhitespace) return false;
- if (selfClosing != tag.selfClosing) return false;
- if (formList != tag.formList) return false;
- if (formSubmit != tag.formSubmit) return false;
-
- return true;
- }
-
- @Override
- public int hashCode() {
- int result = tagName.hashCode();
- result = 31 * result + (isBlock ? 1 : 0);
- result = 31 * result + (formatAsBlock ? 1 : 0);
- result = 31 * result + (canContainBlock ? 1 : 0);
- result = 31 * result + (canContainInline ? 1 : 0);
- result = 31 * result + (empty ? 1 : 0);
- result = 31 * result + (selfClosing ? 1 : 0);
- result = 31 * result + (preserveWhitespace ? 1 : 0);
- result = 31 * result + (formList ? 1 : 0);
- result = 31 * result + (formSubmit ? 1 : 0);
- return result;
- }
-
- @Override
- public String toString() {
- return tagName;
- }
-
- // internal static initialisers:
- // prepped from http://www.w3.org/TR/REC-html40/sgml/dtd.html and other sources
- private static final String[] blockTags = {
- "html", "head", "body", "frameset", "script", "noscript", "style", "meta", "link", "title", "frame",
- "noframes", "section", "nav", "aside", "hgroup", "header", "footer", "p", "h1", "h2", "h3", "h4", "h5", "h6",
- "ul", "ol", "pre", "div", "blockquote", "hr", "address", "figure", "figcaption", "form", "fieldset", "ins",
- "del", "s", "dl", "dt", "dd", "li", "table", "caption", "thead", "tfoot", "tbody", "colgroup", "col", "tr", "th",
- "td", "video", "audio", "canvas", "details", "menu", "plaintext", "template", "article", "main",
- "svg", "math"
- };
- private static final String[] inlineTags = {
- "object", "base", "font", "tt", "i", "b", "u", "big", "small", "em", "strong", "dfn", "code", "samp", "kbd",
- "var", "cite", "abbr", "time", "acronym", "mark", "ruby", "rt", "rp", "a", "img", "br", "wbr", "map", "q",
- "sub", "sup", "bdo", "iframe", "embed", "span", "input", "select", "textarea", "label", "button", "optgroup",
- "option", "legend", "datalist", "keygen", "output", "progress", "meter", "area", "param", "source", "track",
- "summary", "command", "device", "area", "basefont", "bgsound", "menuitem", "param", "source", "track",
- "data", "bdi"
- };
- private static final String[] emptyTags = {
- "meta", "link", "base", "frame", "img", "br", "wbr", "embed", "hr", "input", "keygen", "col", "command",
- "device", "area", "basefont", "bgsound", "menuitem", "param", "source", "track"
- };
- private static final String[] formatAsInlineTags = {
- "title", "a", "p", "h1", "h2", "h3", "h4", "h5", "h6", "pre", "address", "li", "th", "td", "script", "style",
- "ins", "del", "s"
- };
- private static final String[] preserveWhitespaceTags = {
- "pre", "plaintext", "title", "textarea"
- // script is not here as it is a data node, which always preserve whitespace
- };
- // todo: I think we just need submit tags, and can scrub listed
- private static final String[] formListedTags = {
- "button", "fieldset", "input", "keygen", "object", "output", "select", "textarea"
- };
- private static final String[] formSubmitTags = {
- "input", "keygen", "object", "select", "textarea"
- };
-
- static {
- // creates
- for (String tagName : blockTags) {
- Tag tag = new Tag(tagName);
- register(tag);
- }
- for (String tagName : inlineTags) {
- Tag tag = new Tag(tagName);
- tag.isBlock = false;
- tag.canContainBlock = false;
- tag.formatAsBlock = false;
- register(tag);
- }
-
- // mods:
- for (String tagName : emptyTags) {
- Tag tag = tags.get(tagName);
- Validate.notNull(tag);
- tag.canContainBlock = false;
- tag.canContainInline = false;
- tag.empty = true;
- }
-
- for (String tagName : formatAsInlineTags) {
- Tag tag = tags.get(tagName);
- Validate.notNull(tag);
- tag.formatAsBlock = false;
- }
-
- for (String tagName : preserveWhitespaceTags) {
- Tag tag = tags.get(tagName);
- Validate.notNull(tag);
- tag.preserveWhitespace = true;
- }
-
- for (String tagName : formListedTags) {
- Tag tag = tags.get(tagName);
- Validate.notNull(tag);
- tag.formList = true;
- }
-
- for (String tagName : formSubmitTags) {
- Tag tag = tags.get(tagName);
- Validate.notNull(tag);
- tag.formSubmit = true;
- }
- }
-
- private static void register(Tag tag) {
- tags.put(tag.tagName, tag);
- }
-}
diff --git a/src/org/jsoup/parser/Token.java b/src/org/jsoup/parser/Token.java
deleted file mode 100644
index b888f78..0000000
--- a/src/org/jsoup/parser/Token.java
+++ /dev/null
@@ -1,275 +0,0 @@
-package org.jsoup.parser;
-
-import org.jsoup.helper.Validate;
-import org.jsoup.nodes.Attribute;
-import org.jsoup.nodes.Attributes;
-
-/**
- * Parse tokens for the Tokeniser.
- */
-abstract class Token {
- TokenType type;
-
- private Token() {
- }
-
- String tokenType() {
- return this.getClass().getSimpleName();
- }
-
- static class Doctype extends Token {
- final StringBuilder name = new StringBuilder();
- final StringBuilder publicIdentifier = new StringBuilder();
- final StringBuilder systemIdentifier = new StringBuilder();
- boolean forceQuirks = false;
-
- Doctype() {
- type = TokenType.Doctype;
- }
-
- String getName() {
- return name.toString();
- }
-
- String getPublicIdentifier() {
- return publicIdentifier.toString();
- }
-
- public String getSystemIdentifier() {
- return systemIdentifier.toString();
- }
-
- public boolean isForceQuirks() {
- return forceQuirks;
- }
- }
-
- static abstract class Tag extends Token {
- protected String tagName;
- private String pendingAttributeName; // attribute names are generally caught in one hop, not accumulated
- private StringBuilder pendingAttributeValue; // but values are accumulated, from e.g. & in hrefs
-
- boolean selfClosing = false;
- Attributes attributes; // start tags get attributes on construction. End tags get attributes on first new attribute (but only for parser convenience, not used).
-
- void newAttribute() {
- if (attributes == null)
- attributes = new Attributes();
-
- if (pendingAttributeName != null) {
- Attribute attribute;
- if (pendingAttributeValue == null)
- attribute = new Attribute(pendingAttributeName, "");
- else
- attribute = new Attribute(pendingAttributeName, pendingAttributeValue.toString());
- attributes.put(attribute);
- }
- pendingAttributeName = null;
- if (pendingAttributeValue != null)
- pendingAttributeValue.delete(0, pendingAttributeValue.length());
- }
-
- void finaliseTag() {
- // finalises for emit
- if (pendingAttributeName != null) {
- // todo: check if attribute name exists; if so, drop and error
- newAttribute();
- }
- }
-
- String name() {
- Validate.isFalse(tagName == null || tagName.length() == 0);
- return tagName;
- }
-
- Tag name(String name) {
- tagName = name;
- return this;
- }
-
- boolean isSelfClosing() {
- return selfClosing;
- }
-
- @SuppressWarnings({"TypeMayBeWeakened"})
- Attributes getAttributes() {
- return attributes;
- }
-
- // these appenders are rarely hit in not null state-- caused by null chars.
- void appendTagName(String append) {
- tagName = tagName == null ? append : tagName.concat(append);
- }
-
- void appendTagName(char append) {
- appendTagName(String.valueOf(append));
- }
-
- void appendAttributeName(String append) {
- pendingAttributeName = pendingAttributeName == null ? append : pendingAttributeName.concat(append);
- }
-
- void appendAttributeName(char append) {
- appendAttributeName(String.valueOf(append));
- }
-
- void appendAttributeValue(String append) {
- ensureAttributeValue();
- pendingAttributeValue.append(append);
- }
-
- void appendAttributeValue(char append) {
- ensureAttributeValue();
- pendingAttributeValue.append(append);
- }
-
- void appendAttributeValue(char[] append) {
- ensureAttributeValue();
- pendingAttributeValue.append(append);
- }
-
- private final void ensureAttributeValue() {
- if (pendingAttributeValue == null)
- pendingAttributeValue = new StringBuilder();
- }
- }
-
- static class StartTag extends Tag {
- StartTag() {
- super();
- attributes = new Attributes();
- type = TokenType.StartTag;
- }
-
- StartTag(String name) {
- this();
- this.tagName = name;
- }
-
- StartTag(String name, Attributes attributes) {
- this();
- this.tagName = name;
- this.attributes = attributes;
- }
-
- @Override
- public String toString() {
- if (attributes != null && attributes.size() > 0)
- return "<" + name() + " " + attributes.toString() + ">";
- else
- return "<" + name() + ">";
- }
- }
-
- static class EndTag extends Tag{
- EndTag() {
- super();
- type = TokenType.EndTag;
- }
-
- EndTag(String name) {
- this();
- this.tagName = name;
- }
-
- @Override
- public String toString() {
- return "" + name() + ">";
- }
- }
-
- static class Comment extends Token {
- final StringBuilder data = new StringBuilder();
- boolean bogus = false;
-
- Comment() {
- type = TokenType.Comment;
- }
-
- String getData() {
- return data.toString();
- }
-
- @Override
- public String toString() {
- return "";
- }
- }
-
- static class Character extends Token {
- private final String data;
-
- Character(String data) {
- type = TokenType.Character;
- this.data = data;
- }
-
- String getData() {
- return data;
- }
-
- @Override
- public String toString() {
- return getData();
- }
- }
-
- static class EOF extends Token {
- EOF() {
- type = Token.TokenType.EOF;
- }
- }
-
- boolean isDoctype() {
- return type == TokenType.Doctype;
- }
-
- Doctype asDoctype() {
- return (Doctype) this;
- }
-
- boolean isStartTag() {
- return type == TokenType.StartTag;
- }
-
- StartTag asStartTag() {
- return (StartTag) this;
- }
-
- boolean isEndTag() {
- return type == TokenType.EndTag;
- }
-
- EndTag asEndTag() {
- return (EndTag) this;
- }
-
- boolean isComment() {
- return type == TokenType.Comment;
- }
-
- Comment asComment() {
- return (Comment) this;
- }
-
- boolean isCharacter() {
- return type == TokenType.Character;
- }
-
- Character asCharacter() {
- return (Character) this;
- }
-
- boolean isEOF() {
- return type == TokenType.EOF;
- }
-
- enum TokenType {
- Doctype,
- StartTag,
- EndTag,
- Comment,
- Character,
- EOF
- }
-}
diff --git a/src/org/jsoup/parser/TokenQueue.java b/src/org/jsoup/parser/TokenQueue.java
deleted file mode 100644
index bc45472..0000000
--- a/src/org/jsoup/parser/TokenQueue.java
+++ /dev/null
@@ -1,397 +0,0 @@
-package org.jsoup.parser;
-
-import org.jsoup.helper.StringUtil;
-import org.jsoup.helper.Validate;
-
-/**
- * A character queue with parsing helpers.
- *
- * @author Jonathan Hedley
- */
-public class TokenQueue {
- private String queue;
- private int pos = 0;
-
- private static final char ESC = '\\'; // escape char for chomp balanced.
-
- /**
- Create a new TokenQueue.
- @param data string of data to back queue.
- */
- public TokenQueue(String data) {
- Validate.notNull(data);
- queue = data;
- }
-
- /**
- * Is the queue empty?
- * @return true if no data left in queue.
- */
- public boolean isEmpty() {
- return remainingLength() == 0;
- }
-
- private int remainingLength() {
- return queue.length() - pos;
- }
-
- /**
- * Retrieves but does not remove the first character from the queue.
- * @return First character, or 0 if empty.
- */
- public char peek() {
- return isEmpty() ? 0 : queue.charAt(pos);
- }
-
- /**
- Add a character to the start of the queue (will be the next character retrieved).
- @param c character to add
- */
- public void addFirst(Character c) {
- addFirst(c.toString());
- }
-
- /**
- Add a string to the start of the queue.
- @param seq string to add.
- */
- public void addFirst(String seq) {
- // not very performant, but an edge case
- queue = seq + queue.substring(pos);
- pos = 0;
- }
-
- /**
- * Tests if the next characters on the queue match the sequence. Case insensitive.
- * @param seq String to check queue for.
- * @return true if the next characters match.
- */
- public boolean matches(String seq) {
- return queue.regionMatches(true, pos, seq, 0, seq.length());
- }
-
- /**
- * Case sensitive match test.
- * @param seq string to case sensitively check for
- * @return true if matched, false if not
- */
- public boolean matchesCS(String seq) {
- return queue.startsWith(seq, pos);
- }
-
-
- /**
- Tests if the next characters match any of the sequences. Case insensitive.
- @param seq list of strings to case insensitively check for
- @return true of any matched, false if none did
- */
- public boolean matchesAny(String... seq) {
- for (String s : seq) {
- if (matches(s))
- return true;
- }
- return false;
- }
-
- public boolean matchesAny(char... seq) {
- if (isEmpty())
- return false;
-
- for (char c: seq) {
- if (queue.charAt(pos) == c)
- return true;
- }
- return false;
- }
-
- public boolean matchesStartTag() {
- // micro opt for matching "
- Case insensitive.
- * @param seq sequence to remove from head of queue.
- */
- public void consume(String seq) {
- if (!matches(seq))
- throw new IllegalStateException("Queue did not match expected sequence");
- int len = seq.length();
- if (len > remainingLength())
- throw new IllegalStateException("Queue not long enough to consume sequence");
-
- pos += len;
- }
-
- /**
- * Pulls a string off the queue, up to but exclusive of the match sequence, or to the queue running out.
- * @param seq String to end on (and not include in return, but leave on queue). Case sensitive.
- * @return The matched data consumed from queue.
- */
- public String consumeTo(String seq) {
- int offset = queue.indexOf(seq, pos);
- if (offset != -1) {
- String consumed = queue.substring(pos, offset);
- pos += consumed.length();
- return consumed;
- } else {
- return remainder();
- }
- }
-
- public String consumeToIgnoreCase(String seq) {
- int start = pos;
- String first = seq.substring(0, 1);
- boolean canScan = first.toLowerCase().equals(first.toUpperCase()); // if first is not cased, use index of
- while (!isEmpty()) {
- if (matches(seq))
- break;
-
- if (canScan) {
- int skip = queue.indexOf(first, pos) - pos;
- if (skip == 0) // this char is the skip char, but not match, so force advance of pos
- pos++;
- else if (skip < 0) // no chance of finding, grab to end
- pos = queue.length();
- else
- pos += skip;
- }
- else
- pos++;
- }
-
- String data = queue.substring(start, pos);
- return data;
- }
-
- /**
- Consumes to the first sequence provided, or to the end of the queue. Leaves the terminator on the queue.
- @param seq any number of terminators to consume to. Case insensitive.
- @return consumed string
- */
- // todo: method name. not good that consumeTo cares for case, and consume to any doesn't. And the only use for this
- // is is a case sensitive time...
- public String consumeToAny(String... seq) {
- int start = pos;
- while (!isEmpty() && !matchesAny(seq)) {
- pos++;
- }
-
- String data = queue.substring(start, pos);
- return data;
- }
-
- /**
- * Pulls a string off the queue (like consumeTo), and then pulls off the matched string (but does not return it).
- *
- * If the queue runs out of characters before finding the seq, will return as much as it can (and queue will go
- * isEmpty() == true).
- * @param seq String to match up to, and not include in return, and to pull off queue. Case sensitive.
- * @return Data matched from queue.
- */
- public String chompTo(String seq) {
- String data = consumeTo(seq);
- matchChomp(seq);
- return data;
- }
-
- public String chompToIgnoreCase(String seq) {
- String data = consumeToIgnoreCase(seq); // case insensitive scan
- matchChomp(seq);
- return data;
- }
-
- /**
- * Pulls a balanced string off the queue. E.g. if queue is "(one (two) three) four", (,) will return "one (two) three",
- * and leave " four" on the queue. Unbalanced openers and closers can be escaped (with \). Those escapes will be left
- * in the returned string, which is suitable for regexes (where we need to preserve the escape), but unsuitable for
- * contains text strings; use unescape for that.
- * @param open opener
- * @param close closer
- * @return data matched from the queue
- */
- public String chompBalanced(char open, char close) {
- int start = -1;
- int end = -1;
- int depth = 0;
- char last = 0;
-
- do {
- if (isEmpty()) break;
- Character c = consume();
- if (last == 0 || last != ESC) {
- if (c.equals(open)) {
- depth++;
- if (start == -1)
- start = pos;
- }
- else if (c.equals(close))
- depth--;
- }
-
- if (depth > 0 && last != 0)
- end = pos; // don't include the outer match pair in the return
- last = c;
- } while (depth > 0);
- return (end >= 0) ? queue.substring(start, end) : "";
- }
-
- /**
- * Unescaped a \ escaped string.
- * @param in backslash escaped string
- * @return unescaped string
- */
- public static String unescape(String in) {
- StringBuilder out = new StringBuilder();
- char last = 0;
- for (char c : in.toCharArray()) {
- if (c == ESC) {
- if (last != 0 && last == ESC)
- out.append(c);
- }
- else
- out.append(c);
- last = c;
- }
- return out.toString();
- }
-
- /**
- * Pulls the next run of whitespace characters of the queue.
- * @return Whether consuming whitespace or not
- */
- public boolean consumeWhitespace() {
- boolean seen = false;
- while (matchesWhitespace()) {
- pos++;
- seen = true;
- }
- return seen;
- }
-
- /**
- * Retrieves the next run of word type (letter or digit) off the queue.
- * @return String of word characters from queue, or empty string if none.
- */
- public String consumeWord() {
- int start = pos;
- while (matchesWord())
- pos++;
- return queue.substring(start, pos);
- }
-
- /**
- * Consume an tag name off the queue (word or :, _, -)
- *
- * @return tag name
- */
- public String consumeTagName() {
- int start = pos;
- while (!isEmpty() && (matchesWord() || matchesAny(':', '_', '-')))
- pos++;
-
- return queue.substring(start, pos);
- }
-
- /**
- * Consume a CSS element selector (tag name, but | instead of : for namespaces, to not conflict with :pseudo selects).
- *
- * @return tag name
- */
- public String consumeElementSelector() {
- int start = pos;
- while (!isEmpty() && (matchesWord() || matchesAny('|', '_', '-')))
- pos++;
-
- return queue.substring(start, pos);
- }
-
- /**
- Consume a CSS identifier (ID or class) off the queue (letter, digit, -, _)
- http://www.w3.org/TR/CSS2/syndata.html#value-def-identifier
- @return identifier
- */
- public String consumeCssIdentifier() {
- int start = pos;
- while (!isEmpty() && (matchesWord() || matchesAny('-', '_')))
- pos++;
-
- return queue.substring(start, pos);
- }
-
- /**
- Consume an attribute key off the queue (letter, digit, -, _, :")
- @return attribute key
- */
- public String consumeAttributeKey() {
- int start = pos;
- while (!isEmpty() && (matchesWord() || matchesAny('-', '_', ':')))
- pos++;
-
- return queue.substring(start, pos);
- }
-
- /**
- Consume and return whatever is left on the queue.
- @return remained of queue.
- */
- public String remainder() {
- final String remainder = queue.substring(pos, queue.length());
- pos = queue.length();
- return remainder;
- }
-
- @Override
- public String toString() {
- return queue.substring(pos);
- }
-}
diff --git a/src/org/jsoup/parser/Tokeniser.java b/src/org/jsoup/parser/Tokeniser.java
deleted file mode 100644
index 6335a6a..0000000
--- a/src/org/jsoup/parser/Tokeniser.java
+++ /dev/null
@@ -1,248 +0,0 @@
-package org.jsoup.parser;
-
-import org.jsoup.helper.Validate;
-import org.jsoup.nodes.Entities;
-
-
-/**
- * Readers the input stream into tokens.
- */
-class Tokeniser {
- static final char replacementChar = '\uFFFD'; // replaces null character
-
- private CharacterReader reader; // html input
- private ParseErrorList errors; // errors found while tokenising
-
- private TokeniserState state = TokeniserState.Data; // current tokenisation state
- private Token emitPending; // the token we are about to emit on next read
- private boolean isEmitPending = false;
- private StringBuilder charBuffer = new StringBuilder(); // buffers characters to output as one token
- StringBuilder dataBuffer; // buffers data looking for
-
- Token.Tag tagPending; // tag we are building up
- Token.Doctype doctypePending; // doctype building up
- Token.Comment commentPending; // comment building up
- private Token.StartTag lastStartTag; // the last start tag emitted, to test appropriate end tag
- private boolean selfClosingFlagAcknowledged = true;
-
- Tokeniser(CharacterReader reader, ParseErrorList errors) {
- this.reader = reader;
- this.errors = errors;
- }
-
- Token read() {
- if (!selfClosingFlagAcknowledged) {
- error("Self closing flag not acknowledged");
- selfClosingFlagAcknowledged = true;
- }
-
- while (!isEmitPending)
- state.read(this, reader);
-
- // if emit is pending, a non-character token was found: return any chars in buffer, and leave token for next read:
- if (charBuffer.length() > 0) {
- String str = charBuffer.toString();
- charBuffer.delete(0, charBuffer.length());
- return new Token.Character(str);
- } else {
- isEmitPending = false;
- return emitPending;
- }
- }
-
- void emit(Token token) {
- Validate.isFalse(isEmitPending, "There is an unread token pending!");
-
- emitPending = token;
- isEmitPending = true;
-
- if (token.type == Token.TokenType.StartTag) {
- Token.StartTag startTag = (Token.StartTag) token;
- lastStartTag = startTag;
- if (startTag.selfClosing)
- selfClosingFlagAcknowledged = false;
- } else if (token.type == Token.TokenType.EndTag) {
- Token.EndTag endTag = (Token.EndTag) token;
- if (endTag.attributes != null)
- error("Attributes incorrectly present on end tag");
- }
- }
-
- void emit(String str) {
- // buffer strings up until last string token found, to emit only one token for a run of character refs etc.
- // does not set isEmitPending; read checks that
- charBuffer.append(str);
- }
-
- void emit(char[] chars) {
- charBuffer.append(chars);
- }
-
- void emit(char c) {
- charBuffer.append(c);
- }
-
- TokeniserState getState() {
- return state;
- }
-
- void transition(TokeniserState state) {
- this.state = state;
- }
-
- void advanceTransition(TokeniserState state) {
- reader.advance();
- this.state = state;
- }
-
- void acknowledgeSelfClosingFlag() {
- selfClosingFlagAcknowledged = true;
- }
-
- char[] consumeCharacterReference(Character additionalAllowedCharacter, boolean inAttribute) {
- if (reader.isEmpty())
- return null;
- if (additionalAllowedCharacter != null && additionalAllowedCharacter == reader.current())
- return null;
- if (reader.matchesAny('\t', '\n', '\r', '\f', ' ', '<', '&'))
- return null;
-
- reader.mark();
- if (reader.matchConsume("#")) { // numbered
- boolean isHexMode = reader.matchConsumeIgnoreCase("X");
- String numRef = isHexMode ? reader.consumeHexSequence() : reader.consumeDigitSequence();
- if (numRef.length() == 0) { // didn't match anything
- characterReferenceError("numeric reference with no numerals");
- reader.rewindToMark();
- return null;
- }
- if (!reader.matchConsume(";"))
- characterReferenceError("missing semicolon"); // missing semi
- int charval = -1;
- try {
- int base = isHexMode ? 16 : 10;
- charval = Integer.valueOf(numRef, base);
- } catch (NumberFormatException e) {
- } // skip
- if (charval == -1 || (charval >= 0xD800 && charval <= 0xDFFF) || charval > 0x10FFFF) {
- characterReferenceError("character outside of valid range");
- return new char[]{replacementChar};
- } else {
- // todo: implement number replacement table
- // todo: check for extra illegal unicode points as parse errors
- return Character.toChars(charval);
- }
- } else { // named
- // get as many letters as possible, and look for matching entities.
- String nameRef = reader.consumeLetterThenDigitSequence();
- boolean looksLegit = reader.matches(';');
- // found if a base named entity without a ;, or an extended entity with the ;.
- boolean found = (Entities.isBaseNamedEntity(nameRef) || (Entities.isNamedEntity(nameRef) && looksLegit));
-
- if (!found) {
- reader.rewindToMark();
- if (looksLegit) // named with semicolon
- characterReferenceError(String.format("invalid named referenece '%s'", nameRef));
- return null;
- }
- if (inAttribute && (reader.matchesLetter() || reader.matchesDigit() || reader.matchesAny('=', '-', '_'))) {
- // don't want that to match
- reader.rewindToMark();
- return null;
- }
- if (!reader.matchConsume(";"))
- characterReferenceError("missing semicolon"); // missing semi
- return new char[]{Entities.getCharacterByName(nameRef)};
- }
- }
-
- Token.Tag createTagPending(boolean start) {
- tagPending = start ? new Token.StartTag() : new Token.EndTag();
- return tagPending;
- }
-
- void emitTagPending() {
- tagPending.finaliseTag();
- emit(tagPending);
- }
-
- void createCommentPending() {
- commentPending = new Token.Comment();
- }
-
- void emitCommentPending() {
- emit(commentPending);
- }
-
- void createDoctypePending() {
- doctypePending = new Token.Doctype();
- }
-
- void emitDoctypePending() {
- emit(doctypePending);
- }
-
- void createTempBuffer() {
- dataBuffer = new StringBuilder();
- }
-
- boolean isAppropriateEndTagToken() {
- if (lastStartTag == null)
- return false;
- return tagPending.tagName.equals(lastStartTag.tagName);
- }
-
- String appropriateEndTagName() {
- if (lastStartTag == null)
- return null;
- return lastStartTag.tagName;
- }
-
- void error(TokeniserState state) {
- if (errors.canAddError())
- errors.add(new ParseError(reader.pos(), "Unexpected character '%s' in input state [%s]", reader.current(), state));
- }
-
- void eofError(TokeniserState state) {
- if (errors.canAddError())
- errors.add(new ParseError(reader.pos(), "Unexpectedly reached end of file (EOF) in input state [%s]", state));
- }
-
- private void characterReferenceError(String message) {
- if (errors.canAddError())
- errors.add(new ParseError(reader.pos(), "Invalid character reference: %s", message));
- }
-
- private void error(String errorMsg) {
- if (errors.canAddError())
- errors.add(new ParseError(reader.pos(), errorMsg));
- }
-
- boolean currentNodeInHtmlNS() {
- // todo: implement namespaces correctly
- return true;
- // Element currentNode = currentNode();
- // return currentNode != null && currentNode.namespace().equals("HTML");
- }
-
- /**
- * Utility method to consume reader and unescape entities found within.
- * @param inAttribute
- * @return unescaped string from reader
- */
- String unescapeEntities(boolean inAttribute) {
- StringBuilder builder = new StringBuilder();
- while (!reader.isEmpty()) {
- builder.append(reader.consumeTo('&'));
- if (reader.matches('&')) {
- reader.consume();
- char[] c = consumeCharacterReference(null, inAttribute);
- if (c == null || c.length==0)
- builder.append('&');
- else
- builder.append(c);
- }
- }
- return builder.toString();
- }
-}
diff --git a/src/org/jsoup/parser/TokeniserState.java b/src/org/jsoup/parser/TokeniserState.java
deleted file mode 100644
index 786e84a..0000000
--- a/src/org/jsoup/parser/TokeniserState.java
+++ /dev/null
@@ -1,1725 +0,0 @@
-package org.jsoup.parser;
-
-/**
- * States and transition activations for the Tokeniser.
- */
-enum TokeniserState {
- Data {
- // in data state, gather characters until a character reference or tag is found
- void read(Tokeniser t, CharacterReader r) {
- switch (r.current()) {
- case '&':
- t.advanceTransition(CharacterReferenceInData);
- break;
- case '<':
- t.advanceTransition(TagOpen);
- break;
- case nullChar:
- t.error(this); // NOT replacement character (oddly?)
- t.emit(r.consume());
- break;
- case eof:
- t.emit(new Token.EOF());
- break;
- default:
- String data = r.consumeToAny('&', '<', nullChar);
- t.emit(data);
- break;
- }
- }
- },
- CharacterReferenceInData {
- // from & in data
- void read(Tokeniser t, CharacterReader r) {
- char[] c = t.consumeCharacterReference(null, false);
- if (c == null)
- t.emit('&');
- else
- t.emit(c);
- t.transition(Data);
- }
- },
- Rcdata {
- /// handles data in title, textarea etc
- void read(Tokeniser t, CharacterReader r) {
- switch (r.current()) {
- case '&':
- t.advanceTransition(CharacterReferenceInRcdata);
- break;
- case '<':
- t.advanceTransition(RcdataLessthanSign);
- break;
- case nullChar:
- t.error(this);
- r.advance();
- t.emit(replacementChar);
- break;
- case eof:
- t.emit(new Token.EOF());
- break;
- default:
- String data = r.consumeToAny('&', '<', nullChar);
- t.emit(data);
- break;
- }
- }
- },
- CharacterReferenceInRcdata {
- void read(Tokeniser t, CharacterReader r) {
- char[] c = t.consumeCharacterReference(null, false);
- if (c == null)
- t.emit('&');
- else
- t.emit(c);
- t.transition(Rcdata);
- }
- },
- Rawtext {
- void read(Tokeniser t, CharacterReader r) {
- switch (r.current()) {
- case '<':
- t.advanceTransition(RawtextLessthanSign);
- break;
- case nullChar:
- t.error(this);
- r.advance();
- t.emit(replacementChar);
- break;
- case eof:
- t.emit(new Token.EOF());
- break;
- default:
- String data = r.consumeToAny('<', nullChar);
- t.emit(data);
- break;
- }
- }
- },
- ScriptData {
- void read(Tokeniser t, CharacterReader r) {
- switch (r.current()) {
- case '<':
- t.advanceTransition(ScriptDataLessthanSign);
- break;
- case nullChar:
- t.error(this);
- r.advance();
- t.emit(replacementChar);
- break;
- case eof:
- t.emit(new Token.EOF());
- break;
- default:
- String data = r.consumeToAny('<', nullChar);
- t.emit(data);
- break;
- }
- }
- },
- PLAINTEXT {
- void read(Tokeniser t, CharacterReader r) {
- switch (r.current()) {
- case nullChar:
- t.error(this);
- r.advance();
- t.emit(replacementChar);
- break;
- case eof:
- t.emit(new Token.EOF());
- break;
- default:
- String data = r.consumeTo(nullChar);
- t.emit(data);
- break;
- }
- }
- },
- TagOpen {
- // from < in data
- void read(Tokeniser t, CharacterReader r) {
- switch (r.current()) {
- case '!':
- t.advanceTransition(MarkupDeclarationOpen);
- break;
- case '/':
- t.advanceTransition(EndTagOpen);
- break;
- case '?':
- t.advanceTransition(BogusComment);
- break;
- default:
- if (r.matchesLetter()) {
- t.createTagPending(true);
- t.transition(TagName);
- } else {
- t.error(this);
- t.emit('<'); // char that got us here
- t.transition(Data);
- }
- break;
- }
- }
- },
- EndTagOpen {
- void read(Tokeniser t, CharacterReader r) {
- if (r.isEmpty()) {
- t.eofError(this);
- t.emit("");
- t.transition(Data);
- } else if (r.matchesLetter()) {
- t.createTagPending(false);
- t.transition(TagName);
- } else if (r.matches('>')) {
- t.error(this);
- t.advanceTransition(Data);
- } else {
- t.error(this);
- t.advanceTransition(BogusComment);
- }
- }
- },
- TagName {
- // from < or in data, will have start or end tag pending
- void read(Tokeniser t, CharacterReader r) {
- // previous TagOpen state did NOT consume, will have a letter char in current
- String tagName = r.consumeToAny('\t', '\n', '\r', '\f', ' ', '/', '>', nullChar).toLowerCase();
- t.tagPending.appendTagName(tagName);
-
- switch (r.consume()) {
- case '\t':
- case '\n':
- case '\r':
- case '\f':
- case ' ':
- t.transition(BeforeAttributeName);
- break;
- case '/':
- t.transition(SelfClosingStartTag);
- break;
- case '>':
- t.emitTagPending();
- t.transition(Data);
- break;
- case nullChar: // replacement
- t.tagPending.appendTagName(replacementStr);
- break;
- case eof: // should emit pending tag?
- t.eofError(this);
- t.transition(Data);
- // no default, as covered with above consumeToAny
- }
- }
- },
- RcdataLessthanSign {
- // from < in rcdata
- void read(Tokeniser t, CharacterReader r) {
- if (r.matches('/')) {
- t.createTempBuffer();
- t.advanceTransition(RCDATAEndTagOpen);
- } else if (r.matchesLetter() && t.appropriateEndTagName() != null && !r.containsIgnoreCase("" + t.appropriateEndTagName())) {
- // diverge from spec: got a start tag, but there's no appropriate end tag (), so rather than
- // consuming to EOF; break out here
- t.tagPending = new Token.EndTag(t.appropriateEndTagName());
- t.emitTagPending();
- r.unconsume(); // undo "<"
- t.transition(Data);
- } else {
- t.emit("<");
- t.transition(Rcdata);
- }
- }
- },
- RCDATAEndTagOpen {
- void read(Tokeniser t, CharacterReader r) {
- if (r.matchesLetter()) {
- t.createTagPending(false);
- t.tagPending.appendTagName(Character.toLowerCase(r.current()));
- t.dataBuffer.append(Character.toLowerCase(r.current()));
- t.advanceTransition(RCDATAEndTagName);
- } else {
- t.emit("");
- t.transition(Rcdata);
- }
- }
- },
- RCDATAEndTagName {
- void read(Tokeniser t, CharacterReader r) {
- if (r.matchesLetter()) {
- String name = r.consumeLetterSequence();
- t.tagPending.appendTagName(name.toLowerCase());
- t.dataBuffer.append(name);
- return;
- }
-
- char c = r.consume();
- switch (c) {
- case '\t':
- case '\n':
- case '\r':
- case '\f':
- case ' ':
- if (t.isAppropriateEndTagToken())
- t.transition(BeforeAttributeName);
- else
- anythingElse(t, r);
- break;
- case '/':
- if (t.isAppropriateEndTagToken())
- t.transition(SelfClosingStartTag);
- else
- anythingElse(t, r);
- break;
- case '>':
- if (t.isAppropriateEndTagToken()) {
- t.emitTagPending();
- t.transition(Data);
- }
- else
- anythingElse(t, r);
- break;
- default:
- anythingElse(t, r);
- }
- }
-
- private void anythingElse(Tokeniser t, CharacterReader r) {
- t.emit("" + t.dataBuffer.toString());
- r.unconsume();
- t.transition(Rcdata);
- }
- },
- RawtextLessthanSign {
- void read(Tokeniser t, CharacterReader r) {
- if (r.matches('/')) {
- t.createTempBuffer();
- t.advanceTransition(RawtextEndTagOpen);
- } else {
- t.emit('<');
- t.transition(Rawtext);
- }
- }
- },
- RawtextEndTagOpen {
- void read(Tokeniser t, CharacterReader r) {
- if (r.matchesLetter()) {
- t.createTagPending(false);
- t.transition(RawtextEndTagName);
- } else {
- t.emit("");
- t.transition(Rawtext);
- }
- }
- },
- RawtextEndTagName {
- void read(Tokeniser t, CharacterReader r) {
- handleDataEndTag(t, r, Rawtext);
- }
- },
- ScriptDataLessthanSign {
- void read(Tokeniser t, CharacterReader r) {
- switch (r.consume()) {
- case '/':
- t.createTempBuffer();
- t.transition(ScriptDataEndTagOpen);
- break;
- case '!':
- t.emit("':
- t.emit(c);
- t.transition(ScriptData);
- break;
- case nullChar:
- t.error(this);
- t.emit(replacementChar);
- t.transition(ScriptDataEscaped);
- break;
- default:
- t.emit(c);
- t.transition(ScriptDataEscaped);
- }
- }
- },
- ScriptDataEscapedLessthanSign {
- void read(Tokeniser t, CharacterReader r) {
- if (r.matchesLetter()) {
- t.createTempBuffer();
- t.dataBuffer.append(Character.toLowerCase(r.current()));
- t.emit("<" + r.current());
- t.advanceTransition(ScriptDataDoubleEscapeStart);
- } else if (r.matches('/')) {
- t.createTempBuffer();
- t.advanceTransition(ScriptDataEscapedEndTagOpen);
- } else {
- t.emit('<');
- t.transition(ScriptDataEscaped);
- }
- }
- },
- ScriptDataEscapedEndTagOpen {
- void read(Tokeniser t, CharacterReader r) {
- if (r.matchesLetter()) {
- t.createTagPending(false);
- t.tagPending.appendTagName(Character.toLowerCase(r.current()));
- t.dataBuffer.append(r.current());
- t.advanceTransition(ScriptDataEscapedEndTagName);
- } else {
- t.emit("");
- t.transition(ScriptDataEscaped);
- }
- }
- },
- ScriptDataEscapedEndTagName {
- void read(Tokeniser t, CharacterReader r) {
- handleDataEndTag(t, r, ScriptDataEscaped);
- }
- },
- ScriptDataDoubleEscapeStart {
- void read(Tokeniser t, CharacterReader r) {
- handleDataDoubleEscapeTag(t, r, ScriptDataDoubleEscaped, ScriptDataEscaped);
- }
- },
- ScriptDataDoubleEscaped {
- void read(Tokeniser t, CharacterReader r) {
- char c = r.current();
- switch (c) {
- case '-':
- t.emit(c);
- t.advanceTransition(ScriptDataDoubleEscapedDash);
- break;
- case '<':
- t.emit(c);
- t.advanceTransition(ScriptDataDoubleEscapedLessthanSign);
- break;
- case nullChar:
- t.error(this);
- r.advance();
- t.emit(replacementChar);
- break;
- case eof:
- t.eofError(this);
- t.transition(Data);
- break;
- default:
- String data = r.consumeToAny('-', '<', nullChar);
- t.emit(data);
- }
- }
- },
- ScriptDataDoubleEscapedDash {
- void read(Tokeniser t, CharacterReader r) {
- char c = r.consume();
- switch (c) {
- case '-':
- t.emit(c);
- t.transition(ScriptDataDoubleEscapedDashDash);
- break;
- case '<':
- t.emit(c);
- t.transition(ScriptDataDoubleEscapedLessthanSign);
- break;
- case nullChar:
- t.error(this);
- t.emit(replacementChar);
- t.transition(ScriptDataDoubleEscaped);
- break;
- case eof:
- t.eofError(this);
- t.transition(Data);
- break;
- default:
- t.emit(c);
- t.transition(ScriptDataDoubleEscaped);
- }
- }
- },
- ScriptDataDoubleEscapedDashDash {
- void read(Tokeniser t, CharacterReader r) {
- char c = r.consume();
- switch (c) {
- case '-':
- t.emit(c);
- break;
- case '<':
- t.emit(c);
- t.transition(ScriptDataDoubleEscapedLessthanSign);
- break;
- case '>':
- t.emit(c);
- t.transition(ScriptData);
- break;
- case nullChar:
- t.error(this);
- t.emit(replacementChar);
- t.transition(ScriptDataDoubleEscaped);
- break;
- case eof:
- t.eofError(this);
- t.transition(Data);
- break;
- default:
- t.emit(c);
- t.transition(ScriptDataDoubleEscaped);
- }
- }
- },
- ScriptDataDoubleEscapedLessthanSign {
- void read(Tokeniser t, CharacterReader r) {
- if (r.matches('/')) {
- t.emit('/');
- t.createTempBuffer();
- t.advanceTransition(ScriptDataDoubleEscapeEnd);
- } else {
- t.transition(ScriptDataDoubleEscaped);
- }
- }
- },
- ScriptDataDoubleEscapeEnd {
- void read(Tokeniser t, CharacterReader r) {
- handleDataDoubleEscapeTag(t,r, ScriptDataEscaped, ScriptDataDoubleEscaped);
- }
- },
- BeforeAttributeName {
- // from tagname Usage example: {@code Document xmlDoc = Jsoup.parse(html, baseUrl, Parser.xmlParser());}
- * Note that it is possible to get repeats if the matched elements contain both parent elements and their own
- * children, as the Element.text() method returns the combined text of a parent and all its children.
- * @return string of all text: unescaped and no HTML.
- * @see Element#text()
- */
- public String text() {
- StringBuilder sb = new StringBuilder();
- for (Element element : contents) {
- if (sb.length() != 0)
- sb.append(" ");
- sb.append(element.text());
- }
- return sb.toString();
- }
-
- public boolean hasText() {
- for (Element element: contents) {
- if (element.hasText())
- return true;
- }
- return false;
- }
-
- /**
- * Get the combined inner HTML of all matched elements.
- * @return string of all element's inner HTML.
- * @see #text()
- * @see #outerHtml()
- */
- public String html() {
- StringBuilder sb = new StringBuilder();
- for (Element element : contents) {
- if (sb.length() != 0)
- sb.append("\n");
- sb.append(element.html());
- }
- return sb.toString();
- }
-
- /**
- * Get the combined outer HTML of all matched elements.
- * @return string of all element's outer HTML.
- * @see #text()
- * @see #html()
- */
- public String outerHtml() {
- StringBuilder sb = new StringBuilder();
- for (Element element : contents) {
- if (sb.length() != 0)
- sb.append("\n");
- sb.append(element.outerHtml());
- }
- return sb.toString();
- }
-
- /**
- * Get the combined outer HTML of all matched elements. Alias of {@link #outerHtml()}.
- * @return string of all element's outer HTML.
- * @see #text()
- * @see #html()
- */
- @Override
- public String toString() {
- return outerHtml();
- }
-
- /**
- * Update the tag name of each matched element. For example, to change each {@code } to a {@code }, do
- * {@code doc.select("i").tagName("em");}
- * @param tagName the new tag name
- * @return this, for chaining
- * @see Element#tagName(String)
- */
- public Elements tagName(String tagName) {
- for (Element element : contents) {
- element.tagName(tagName);
- }
- return this;
- }
-
- /**
- * Set the inner HTML of each matched element.
- * @param html HTML to parse and set into each matched element.
- * @return this, for chaining
- * @see Element#html(String)
- */
- public Elements html(String html) {
- for (Element element : contents) {
- element.html(html);
- }
- return this;
- }
-
- /**
- * Add the supplied HTML to the start of each matched element's inner HTML.
- * @param html HTML to add inside each element, before the existing HTML
- * @return this, for chaining
- * @see Element#prepend(String)
- */
- public Elements prepend(String html) {
- for (Element element : contents) {
- element.prepend(html);
- }
- return this;
- }
-
- /**
- * Add the supplied HTML to the end of each matched element's inner HTML.
- * @param html HTML to add inside each element, after the existing HTML
- * @return this, for chaining
- * @see Element#append(String)
- */
- public Elements append(String html) {
- for (Element element : contents) {
- element.append(html);
- }
- return this;
- }
-
- /**
- * Insert the supplied HTML before each matched element's outer HTML.
- * @param html HTML to insert before each element
- * @return this, for chaining
- * @see Element#before(String)
- */
- public Elements before(String html) {
- for (Element element : contents) {
- element.before(html);
- }
- return this;
- }
-
- /**
- * Insert the supplied HTML after each matched element's outer HTML.
- * @param html HTML to insert after each element
- * @return this, for chaining
- * @see Element#after(String)
- */
- public Elements after(String html) {
- for (Element element : contents) {
- element.after(html);
- }
- return this;
- }
-
- /**
- Wrap the supplied HTML around each matched elements. For example, with HTML
- {@code This is Jsoup This is jsoup
- * E.g. HTML: {@code Hello there now
- * E.g. HTML: {@code Hello there
- * Note that this method should not be used to clean user-submitted HTML; rather, use {@link org.jsoup.safety.Cleaner} to clean HTML.
- * @return this, for chaining
- * @see Element#empty()
- * @see #empty()
- */
- public Elements remove() {
- for (Element element : contents) {
- element.remove();
- }
- return this;
- }
-
- // filters
-
- /**
- * Find matching elements within this element list.
- * @param query A {@link Selector} query
- * @return the filtered list of elements, or an empty list if none match.
- */
- public Elements select(String query) {
- return Selector.select(query, this);
- }
-
- /**
- * Remove elements from this list that match the {@link Selector} query.
- *
- * E.g. HTML: {@code
- * @param query the selector query whose results should be removed from these elements
- * @return a new elements list that contains only the filtered results
- */
- public Elements not(String query) {
- Elements out = Selector.select(query, this);
- return Selector.filterOut(this, out);
- }
-
- /**
- * Get the nth matched element as an Elements object.
- *
- * See also {@link #get(int)} to retrieve an Element.
- * @param index the (zero-based) index of the element in the list to retain
- * @return Elements containing only the specified element, or, if that element did not exist, an empty list.
- */
- public Elements eq(int index) {
- return contents.size() > index ? new Elements(get(index)) : new Elements();
- }
-
- /**
- * Test if any of the matched elements match the supplied query.
- * @param query A selector
- * @return true if at least one element in the list matches the query.
- */
- public boolean is(String query) {
- Elements children = select(query);
- return !children.isEmpty();
- }
-
- /**
- * Get all of the parents and ancestor elements of the matched elements.
- * @return all of the parents and ancestor elements of the matched elements
- */
- public Elements parents() {
- HashSetbase, which provides a limited set of named HTML
- * entities and escapes other characters as numbered entities for maximum compatibility; or extended,
- * which uses the complete set of HTML named entities.
- * base.
- * @return the document's current escape mode
- */
- public Entities.EscapeMode escapeMode() {
- return escapeMode;
- }
-
- /**
- * Set the document's escape mode, which determines how characters are escaped when the output character set
- * does not support a given character:- using either a named or a numbered escape.
- * @param escapeMode the new escape mode to use
- * @return the document's output settings, for chaining
- */
- public OutputSettings escapeMode(Entities.EscapeMode escapeMode) {
- this.escapeMode = escapeMode;
- return this;
- }
-
- /**
- * Get the document's current output charset, which is used to control which characters are escaped when
- * generating HTML (via the html() methods), and which are kept intact.
- *
Four
- *
- */
- public List
]}
, " Four"]}
- *
- *
- * See the query syntax documentation in {@link org.jsoup.select.Selector}.
- *
- * @param cssQuery a {@link Selector} CSS-like query
- * @return elements that match the query (empty if none match)
- * @see org.jsoup.select.Selector
- */
- public Elements select(String cssQuery) {
- return Selector.select(cssQuery, this);
- }
-
- /**
- * Add a node child node to this element.
- *
- * @param child node to add.
- * @return this element, so that you can add more child nodes or elements.
- */
- public Element appendChild(Node child) {
- Validate.notNull(child);
-
- addChildren(child);
- return this;
- }
-
- /**
- * Add a node to the start of this element's children.
- *
- * @param child node to add.
- * @return this element, so that you can add more child nodes or elements.
- */
- public Element prependChild(Node child) {
- Validate.notNull(child);
-
- addChildren(0, child);
- return this;
- }
-
-
- /**
- * Inserts the given child nodes into this element at the specified index. Current nodes will be shifted to the
- * right. The inserted nodes will be moved from their current parent. To prevent moving, copy the nodes first.
- *
- * @param index 0-based index to insert children at. Specify {@code 0} to insert at the start, {@code -1} at the
- * end
- * @param children child nodes to insert
- * @return this element, for chaining.
- */
- public Element insertChildren(int index, Collection extends Node> children) {
- Validate.notNull(children, "Children collection to be inserted must not be null.");
- int currentSize = childNodeSize();
- if (index < 0) index += currentSize +1; // roll around
- Validate.isTrue(index >= 0 && index <= currentSize, "Insert position out of bounds.");
-
- ArrayList<div class="header gray"> returns, "header gray")
- * @return The literal class attribute, or empty string if no class attribute set.
- */
- public String className() {
- return attr("class").trim();
- }
-
- /**
- * Get all of the element's class names. E.g. on element {@code in html,
in xml
- }
- else
- accum.append(">");
- }
-
- void outerHtmlTail(StringBuilder accum, int depth, Document.OutputSettings out) {
- if (!(childNodes.isEmpty() && tag.isSelfClosing())) {
- if (out.prettyPrint() && (!childNodes.isEmpty() && (
- tag.formatAsBlock() || (out.outline() && (childNodes.size()>1 || (childNodes.size()==1 && !(childNodes.get(0) instanceof TextNode))))
- )))
- indent(accum, depth, out);
- accum.append("").append(tagName()).append(">");
- }
- }
-
- /**
- * Retrieves the element's inner HTML. E.g. on a {@code
abs,
- * which is a shortcut to the {@link #absUrl} method.
- * E.g.:
- * @param attributeKey The attribute key.
- * @return The attribute, or empty string if not present (to avoid nulls).
- * @see #attributes()
- * @see #hasAttr(String)
- * @see #absUrl(String)
- */
- public String attr(String attributeKey) {
- Validate.notNull(attributeKey);
-
- if (attributes.hasKey(attributeKey))
- return attributes.get(attributeKey);
- else if (attributeKey.toLowerCase().startsWith("abs:"))
- return absUrl(attributeKey.substring("abs:".length()));
- else return "";
- }
-
- /**
- * Get all of the element's attributes.
- * @return attributes (which implements iterable, in same order as presented in original HTML).
- */
- public Attributes attributes() {
- return attributes;
- }
-
- /**
- * Set an attribute (key=value). If the attribute already exists, it is replaced.
- * @param attributeKey The attribute key.
- * @param attributeValue The attribute value.
- * @return this (for chaining)
- */
- public Node attr(String attributeKey, String attributeValue) {
- attributes.put(attributeKey, attributeValue);
- return this;
- }
-
- /**
- * Test if this element has an attribute.
- * @param attributeKey The attribute key to check.
- * @return true if the attribute exists, false if not.
- */
- public boolean hasAttr(String attributeKey) {
- Validate.notNull(attributeKey);
-
- if (attributeKey.startsWith("abs:")) {
- String key = attributeKey.substring("abs:".length());
- if (attributes.hasKey(key) && !absUrl(key).equals(""))
- return true;
- }
- return attributes.hasKey(attributeKey);
- }
-
- /**
- * Remove an attribute from this element.
- * @param attributeKey The attribute to remove.
- * @return this (for chaining)
- */
- public Node removeAttr(String attributeKey) {
- Validate.notNull(attributeKey);
- attributes.remove(attributeKey);
- return this;
- }
-
- /**
- Get the base URI of this node.
- @return base URI
- */
- public String baseUri() {
- return baseUri;
- }
-
- /**
- Update the base URI of this node and all of its descendants.
- @param baseUri base URI to set
- */
- public void setBaseUri(final String baseUri) {
- Validate.notNull(baseUri);
-
- traverse(new NodeVisitor() {
- public void head(Node node, int depth) {
- node.baseUri = baseUri;
- }
-
- public void tail(Node node, int depth) {
- }
- });
- }
-
- /**
- * Get an absolute URL from a URL attribute that may be relative (i.e. an String url = a.attr("abs:href");<a href> or
- * <img src>).
- *
- * E.g.: String absUrl = linkEl.absUrl("href");
- *
- * If the attribute value is already absolute (i.e. it starts with a protocol, like
- * http:// or https:// etc), and it successfully parses as a URL, the attribute is
- * returned directly. Otherwise, it is treated as a URL relative to the element's {@link #baseUri}, and made
- * absolute using that.
- *
- * As an alternate, you can use the {@link #attr} method with the abs: prefix, e.g.:
- * String absUrl = linkEl.attr("abs:href");
- *
- * @param attributeKey The attribute key
- * @return An absolute URL if one could be made, or an empty string (not null) if the attribute was missing or
- * could not be made successfully into a URL.
- * @see #attr
- * @see java.net.URL#URL(java.net.URL, String)
- */
- public String absUrl(String attributeKey) {
- Validate.notEmpty(attributeKey);
-
- String relUrl = attr(attributeKey);
- if (!hasAttr(attributeKey)) {
- return ""; // nothing to make absolute with
- } else {
- URL base;
- try {
- try {
- base = new URL(baseUri);
- } catch (MalformedURLException e) {
- // the base is unsuitable, but the attribute may be abs on its own, so try that
- URL abs = new URL(relUrl);
- return abs.toExternalForm();
- }
- // workaround: java resolves '//path/file + ?foo' to '//path/?foo', not '//path/file?foo' as desired
- if (relUrl.startsWith("?"))
- relUrl = base.getPath() + relUrl;
- URL abs = new URL(base, relUrl);
- return abs.toExternalForm();
- } catch (MalformedURLException e) {
- return "";
- }
- }
- }
-
- /**
- Get a child node by its 0-based index.
- @param index index of child node
- @return the child node at this index. Throws a {@code IndexOutOfBoundsException} if the index is out of bounds.
- */
- public Node childNode(int index) {
- return childNodes.get(index);
- }
-
- /**
- Get this node's children. Presented as an unmodifiable list: new children can not be added, but the child nodes
- themselves can be manipulated.
- @return list of children. If no children, returns an empty list.
- */
- public List
- * {@code
- * Calling {@code element.unwrap()} on the {@code span} element will result in the html:
- * {@code
- * and the {@code "Two "} {@link TextNode} being returned.
- * @return the first child of this node, after the node has been unwrapped. Null if the node had no children.
- * @see #remove()
- * @see #wrap(String)
- */
- public Node unwrap() {
- Validate.notNull(parentNode);
-
- int index = siblingIndex;
- Node firstChild = childNodes.size() > 0 ? childNodes.get(0) : null;
- parentNode.addChildren(index, this.childNodesAsArray());
- this.remove();
-
- return firstChild;
- }
-
- private Element getDeepChild(Element el) {
- List, unless in svg
- else
- tb.insert(startTag);
- } else if (name.equals("isindex")) {
- // how much do we care about the early 90s?
- tb.error(this);
- if (tb.getFormElement() != null)
- return false;
-
- tb.tokeniser.acknowledgeSelfClosingFlag();
- tb.process(new Token.StartTag("form"));
- if (startTag.attributes.hasKey("action")) {
- Element form = tb.getFormElement();
- form.attr("action", startTag.attributes.get("action"));
- }
- tb.process(new Token.StartTag("hr"));
- tb.process(new Token.StartTag("label"));
- // hope you like english.
- String prompt = startTag.attributes.hasKey("prompt") ?
- startTag.attributes.get("prompt") :
- "This is a searchable index. Enter search keywords: ";
-
- tb.process(new Token.Character(prompt));
-
- // input
- Attributes inputAttribs = new Attributes();
- for (Attribute attr : startTag.attributes) {
- if (!StringUtil.in(attr.getKey(), Constants.InBodyStartInputAttribs))
- inputAttribs.put(attr);
- }
- inputAttribs.put("name", "isindex");
- tb.process(new Token.StartTag("input", inputAttribs));
- tb.process(new Token.EndTag("label"));
- tb.process(new Token.StartTag("hr"));
- tb.process(new Token.EndTag("form"));
- } else if (name.equals("textarea")) {
- tb.insert(startTag);
- // todo: If the next token is a U+000A LINE FEED (LF) character token, then ignore that token and move on to the next one. (Newlines at the start of textarea elements are ignored as an authoring convenience.)
- tb.tokeniser.transition(TokeniserState.Rcdata);
- tb.markInsertionMode();
- tb.framesetOk(false);
- tb.transition(Text);
- } else if (name.equals("xmp")) {
- if (tb.inButtonScope("p")) {
- tb.process(new Token.EndTag("p"));
- }
- tb.reconstructFormattingElements();
- tb.framesetOk(false);
- handleRawtext(startTag, tb);
- } else if (name.equals("iframe")) {
- tb.framesetOk(false);
- handleRawtext(startTag, tb);
- } else if (name.equals("noembed")) {
- // also handle noscript if script enabled
- handleRawtext(startTag, tb);
- } else if (name.equals("select")) {
- tb.reconstructFormattingElements();
- tb.insert(startTag);
- tb.framesetOk(false);
-
- HtmlTreeBuilderState state = tb.state();
- if (state.equals(InTable) || state.equals(InCaption) || state.equals(InTableBody) || state.equals(InRow) || state.equals(InCell))
- tb.transition(InSelectInTable);
- else
- tb.transition(InSelect);
- } else if (StringUtil.in(name, Constants.InBodyStartOptions)) {
- if (tb.currentElement().nodeName().equals("option"))
- tb.process(new Token.EndTag("option"));
- tb.reconstructFormattingElements();
- tb.insert(startTag);
- } else if (StringUtil.in(name, Constants.InBodyStartRuby)) {
- if (tb.inScope("ruby")) {
- tb.generateImpliedEndTags();
- if (!tb.currentElement().nodeName().equals("ruby")) {
- tb.error(this);
- tb.popStackToBefore("ruby"); // i.e. close up to but not include name
- }
- tb.insert(startTag);
- }
- } else if (name.equals("math")) {
- tb.reconstructFormattingElements();
- // todo: handle A start tag whose tag name is "math" (i.e. foreign, mathml)
- tb.insert(startTag);
- tb.tokeniser.acknowledgeSelfClosingFlag();
- } else if (name.equals("svg")) {
- tb.reconstructFormattingElements();
- // todo: handle A start tag whose tag name is "svg" (xlink, svg)
- tb.insert(startTag);
- tb.tokeniser.acknowledgeSelfClosingFlag();
- } else if (StringUtil.in(name, Constants.InBodyStartDrop)) {
- tb.error(this);
- return false;
- } else {
- tb.reconstructFormattingElements();
- tb.insert(startTag);
- }
- break;
-
- case EndTag:
- Token.EndTag endTag = t.asEndTag();
- name = endTag.name();
- if (name.equals("body")) {
- if (!tb.inScope("body")) {
- tb.error(this);
- return false;
- } else {
- // todo: error if stack contains something not dd, dt, li, optgroup, option, p, rp, rt, tbody, td, tfoot, th, thead, tr, body, html
- tb.transition(AfterBody);
- }
- } else if (name.equals("html")) {
- boolean notIgnored = tb.process(new Token.EndTag("body"));
- if (notIgnored)
- return tb.process(endTag);
- } else if (StringUtil.in(name, Constants.InBodyEndClosers)) {
- if (!tb.inScope(name)) {
- // nothing to close
- tb.error(this);
- return false;
- } else {
- tb.generateImpliedEndTags();
- if (!tb.currentElement().nodeName().equals(name))
- tb.error(this);
- tb.popStackToClose(name);
- }
- } else if (name.equals("form")) {
- Element currentForm = tb.getFormElement();
- tb.setFormElement(null);
- if (currentForm == null || !tb.inScope(name)) {
- tb.error(this);
- return false;
- } else {
- tb.generateImpliedEndTags();
- if (!tb.currentElement().nodeName().equals(name))
- tb.error(this);
- // remove currentForm from stack. will shift anything under up.
- tb.removeFromStack(currentForm);
- }
- } else if (name.equals("p")) {
- if (!tb.inButtonScope(name)) {
- tb.error(this);
- tb.process(new Token.StartTag(name)); // if no p to close, creates an empty
- return tb.process(endTag);
- } else {
- tb.generateImpliedEndTags(name);
- if (!tb.currentElement().nodeName().equals(name))
- tb.error(this);
- tb.popStackToClose(name);
- }
- } else if (name.equals("li")) {
- if (!tb.inListItemScope(name)) {
- tb.error(this);
- return false;
- } else {
- tb.generateImpliedEndTags(name);
- if (!tb.currentElement().nodeName().equals(name))
- tb.error(this);
- tb.popStackToClose(name);
- }
- } else if (StringUtil.in(name, Constants.DdDt)) {
- if (!tb.inScope(name)) {
- tb.error(this);
- return false;
- } else {
- tb.generateImpliedEndTags(name);
- if (!tb.currentElement().nodeName().equals(name))
- tb.error(this);
- tb.popStackToClose(name);
- }
- } else if (StringUtil.in(name, Constants.Headings)) {
- if (!tb.inScope(Constants.Headings)) {
- tb.error(this);
- return false;
- } else {
- tb.generateImpliedEndTags(name);
- if (!tb.currentElement().nodeName().equals(name))
- tb.error(this);
- tb.popStackToClose(Constants.Headings);
- }
- } else if (name.equals("sarcasm")) {
- // *sigh*
- return anyOtherEndTag(t, tb);
- } else if (StringUtil.in(name, Constants.InBodyEndAdoptionFormatters)) {
- // Adoption Agency Algorithm.
- OUTER:
- for (int i = 0; i < 8; i++) {
- Element formatEl = tb.getActiveFormattingElement(name);
- if (formatEl == null)
- return anyOtherEndTag(t, tb);
- else if (!tb.onStack(formatEl)) {
- tb.error(this);
- tb.removeFromActiveFormattingElements(formatEl);
- return true;
- } else if (!tb.inScope(formatEl.nodeName())) {
- tb.error(this);
- return false;
- } else if (tb.currentElement() != formatEl)
- tb.error(this);
-
- Element furthestBlock = null;
- Element commonAncestor = null;
- boolean seenFormattingElement = false;
- LinkedList
and processing fake end tag
- return false;
- }
- tb.generateImpliedEndTags();
- if (!tb.currentElement().nodeName().equals(name))
- tb.error(this);
- tb.popStackToClose(name);
- tb.clearFormattingElementsToLastMarker();
- tb.transition(InRow);
- } else if (StringUtil.in(name, "body", "caption", "col", "colgroup", "html")) {
- tb.error(this);
- return false;
- } else if (StringUtil.in(name, "table", "tbody", "tfoot", "thead", "tr")) {
- if (!tb.inTableScope(name)) {
- tb.error(this);
- return false;
- }
- closeCell(tb);
- return tb.process(t);
- } else {
- return anythingElse(t, tb);
- }
- } else if (t.isStartTag() &&
- StringUtil.in(t.asStartTag().name(),
- "caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr")) {
- if (!(tb.inTableScope("td") || tb.inTableScope("th"))) {
- tb.error(this);
- return false;
- }
- closeCell(tb);
- return tb.process(t);
- } else {
- return anythingElse(t, tb);
- }
- return true;
- }
-
- private boolean anythingElse(Token t, HtmlTreeBuilder tb) {
- return tb.process(t, InBody);
- }
-
- private void closeCell(HtmlTreeBuilder tb) {
- if (tb.inTableScope("td"))
- tb.process(new Token.EndTag("td"));
- else
- tb.process(new Token.EndTag("th")); // only here if th or td in scope
- }
- },
- InSelect {
- boolean process(Token t, HtmlTreeBuilder tb) {
- switch (t.type) {
- case Character:
- Token.Character c = t.asCharacter();
- if (c.getData().equals(nullString)) {
- tb.error(this);
- return false;
- } else {
- tb.insert(c);
- }
- break;
- case Comment:
- tb.insert(t.asComment());
- break;
- case Doctype:
- tb.error(this);
- return false;
- case StartTag:
- Token.StartTag start = t.asStartTag();
- String name = start.name();
- if (name.equals("html"))
- return tb.process(start, InBody);
- else if (name.equals("option")) {
- tb.process(new Token.EndTag("option"));
- tb.insert(start);
- } else if (name.equals("optgroup")) {
- if (tb.currentElement().nodeName().equals("option"))
- tb.process(new Token.EndTag("option"));
- else if (tb.currentElement().nodeName().equals("optgroup"))
- tb.process(new Token.EndTag("optgroup"));
- tb.insert(start);
- } else if (name.equals("select")) {
- tb.error(this);
- return tb.process(new Token.EndTag("select"));
- } else if (StringUtil.in(name, "input", "keygen", "textarea")) {
- tb.error(this);
- if (!tb.inSelectScope("select"))
- return false; // frag
- tb.process(new Token.EndTag("select"));
- return tb.process(start);
- } else if (name.equals("script")) {
- return tb.process(t, InHead);
- } else {
- return anythingElse(t, tb);
- }
- break;
- case EndTag:
- Token.EndTag end = t.asEndTag();
- name = end.name();
- if (name.equals("optgroup")) {
- if (tb.currentElement().nodeName().equals("option") && tb.aboveOnStack(tb.currentElement()) != null && tb.aboveOnStack(tb.currentElement()).nodeName().equals("optgroup"))
- tb.process(new Token.EndTag("option"));
- if (tb.currentElement().nodeName().equals("optgroup"))
- tb.pop();
- else
- tb.error(this);
- } else if (name.equals("option")) {
- if (tb.currentElement().nodeName().equals("option"))
- tb.pop();
- else
- tb.error(this);
- } else if (name.equals("select")) {
- if (!tb.inSelectScope(name)) {
- tb.error(this);
- return false;
- } else {
- tb.popStackToClose(name);
- tb.resetInsertionMode();
- }
- } else
- return anythingElse(t, tb);
- break;
- case EOF:
- if (!tb.currentElement().nodeName().equals("html"))
- tb.error(this);
- break;
- default:
- return anythingElse(t, tb);
- }
- return true;
- }
-
- private boolean anythingElse(Token t, HtmlTreeBuilder tb) {
- tb.error(this);
- return false;
- }
- },
- InSelectInTable {
- boolean process(Token t, HtmlTreeBuilder tb) {
- if (t.isStartTag() && StringUtil.in(t.asStartTag().name(), "caption", "table", "tbody", "tfoot", "thead", "tr", "td", "th")) {
- tb.error(this);
- tb.process(new Token.EndTag("select"));
- return tb.process(t);
- } else if (t.isEndTag() && StringUtil.in(t.asEndTag().name(), "caption", "table", "tbody", "tfoot", "thead", "tr", "td", "th")) {
- tb.error(this);
- if (tb.inTableScope(t.asEndTag().name())) {
- tb.process(new Token.EndTag("select"));
- return (tb.process(t));
- } else
- return false;
- } else {
- return tb.process(t, InSelect);
- }
- }
- },
- AfterBody {
- boolean process(Token t, HtmlTreeBuilder tb) {
- if (isWhitespace(t)) {
- return tb.process(t, InBody);
- } else if (t.isComment()) {
- tb.insert(t.asComment()); // into html node
- } else if (t.isDoctype()) {
- tb.error(this);
- return false;
- } else if (t.isStartTag() && t.asStartTag().name().equals("html")) {
- return tb.process(t, InBody);
- } else if (t.isEndTag() && t.asEndTag().name().equals("html")) {
- if (tb.isFragmentParsing()) {
- tb.error(this);
- return false;
- } else {
- tb.transition(AfterAfterBody);
- }
- } else if (t.isEOF()) {
- // chillax! we're done
- } else {
- tb.error(this);
- tb.transition(InBody);
- return tb.process(t);
- }
- return true;
- }
- },
- InFrameset {
- boolean process(Token t, HtmlTreeBuilder tb) {
- if (isWhitespace(t)) {
- tb.insert(t.asCharacter());
- } else if (t.isComment()) {
- tb.insert(t.asComment());
- } else if (t.isDoctype()) {
- tb.error(this);
- return false;
- } else if (t.isStartTag()) {
- Token.StartTag start = t.asStartTag();
- String name = start.name();
- if (name.equals("html")) {
- return tb.process(start, InBody);
- } else if (name.equals("frameset")) {
- tb.insert(start);
- } else if (name.equals("frame")) {
- tb.insertEmpty(start);
- } else if (name.equals("noframes")) {
- return tb.process(start, InHead);
- } else {
- tb.error(this);
- return false;
- }
- } else if (t.isEndTag() && t.asEndTag().name().equals("frameset")) {
- if (tb.currentElement().nodeName().equals("html")) { // frag
- tb.error(this);
- return false;
- } else {
- tb.pop();
- if (!tb.isFragmentParsing() && !tb.currentElement().nodeName().equals("frameset")) {
- tb.transition(AfterFrameset);
- }
- }
- } else if (t.isEOF()) {
- if (!tb.currentElement().nodeName().equals("html")) {
- tb.error(this);
- return true;
- }
- } else {
- tb.error(this);
- return false;
- }
- return true;
- }
- },
- AfterFrameset {
- boolean process(Token t, HtmlTreeBuilder tb) {
- if (isWhitespace(t)) {
- tb.insert(t.asCharacter());
- } else if (t.isComment()) {
- tb.insert(t.asComment());
- } else if (t.isDoctype()) {
- tb.error(this);
- return false;
- } else if (t.isStartTag() && t.asStartTag().name().equals("html")) {
- return tb.process(t, InBody);
- } else if (t.isEndTag() && t.asEndTag().name().equals("html")) {
- tb.transition(AfterAfterFrameset);
- } else if (t.isStartTag() && t.asStartTag().name().equals("noframes")) {
- return tb.process(t, InHead);
- } else if (t.isEOF()) {
- // cool your heels, we're complete
- } else {
- tb.error(this);
- return false;
- }
- return true;
- }
- },
- AfterAfterBody {
- boolean process(Token t, HtmlTreeBuilder tb) {
- if (t.isComment()) {
- tb.insert(t.asComment());
- } else if (t.isDoctype() || isWhitespace(t) || (t.isStartTag() && t.asStartTag().name().equals("html"))) {
- return tb.process(t, InBody);
- } else if (t.isEOF()) {
- // nice work chuck
- } else {
- tb.error(this);
- tb.transition(InBody);
- return tb.process(t);
- }
- return true;
- }
- },
- AfterAfterFrameset {
- boolean process(Token t, HtmlTreeBuilder tb) {
- if (t.isComment()) {
- tb.insert(t.asComment());
- } else if (t.isDoctype() || isWhitespace(t) || (t.isStartTag() && t.asStartTag().name().equals("html"))) {
- return tb.process(t, InBody);
- } else if (t.isEOF()) {
- // nice work chuck
- } else if (t.isStartTag() && t.asStartTag().name().equals("noframes")) {
- return tb.process(t, InHead);
- } else {
- tb.error(this);
- return false;
- }
- return true;
- }
- },
- ForeignContent {
- boolean process(Token t, HtmlTreeBuilder tb) {
- return true;
- // todo: implement. Also; how do we get here?
- }
- };
-
- private static String nullString = String.valueOf('\u0000');
-
- abstract boolean process(Token t, HtmlTreeBuilder tb);
-
- private static boolean isWhitespace(Token t) {
- if (t.isCharacter()) {
- String data = t.asCharacter().getData();
- // todo: this checks more than spec - "\t", "\n", "\f", "\r", " "
- for (int i = 0; i < data.length(); i++) {
- char c = data.charAt(i);
- if (!StringUtil.isWhitespace(c))
- return false;
- }
- return true;
- }
- return false;
- }
-
- private static void handleRcData(Token.StartTag startTag, HtmlTreeBuilder tb) {
- tb.insert(startTag);
- tb.tokeniser.transition(TokeniserState.Rcdata);
- tb.markInsertionMode();
- tb.transition(Text);
- }
-
- private static void handleRawtext(Token.StartTag startTag, HtmlTreeBuilder tb) {
- tb.insert(startTag);
- tb.tokeniser.transition(TokeniserState.Rawtext);
- tb.markInsertionMode();
- tb.transition(Text);
- }
-
- // lists of tags to search through. A little harder to read here, but causes less GC than dynamic varargs.
- // was contributing around 10% of parse GC load.
- private static final class Constants {
- private static final String[] InBodyStartToHead = new String[]{"base", "basefont", "bgsound", "command", "link", "meta", "noframes", "script", "style", "title"};
- private static final String[] InBodyStartPClosers = new String[]{"address", "article", "aside", "blockquote", "center", "details", "dir", "div", "dl",
- "fieldset", "figcaption", "figure", "footer", "header", "hgroup", "menu", "nav", "ol",
- "p", "section", "summary", "ul"};
- private static final String[] Headings = new String[]{"h1", "h2", "h3", "h4", "h5", "h6"};
- private static final String[] InBodyStartPreListing = new String[]{"pre", "listing"};
- private static final String[] InBodyStartLiBreakers = new String[]{"address", "div", "p"};
- private static final String[] DdDt = new String[]{"dd", "dt"};
- private static final String[] Formatters = new String[]{"b", "big", "code", "em", "font", "i", "s", "small", "strike", "strong", "tt", "u"};
- private static final String[] InBodyStartApplets = new String[]{"applet", "marquee", "object"};
- private static final String[] InBodyStartEmptyFormatters = new String[]{"area", "br", "embed", "img", "keygen", "wbr"};
- private static final String[] InBodyStartMedia = new String[]{"param", "source", "track"};
- private static final String[] InBodyStartInputAttribs = new String[]{"name", "action", "prompt"};
- private static final String[] InBodyStartOptions = new String[]{"optgroup", "option"};
- private static final String[] InBodyStartRuby = new String[]{"rp", "rt"};
- private static final String[] InBodyStartDrop = new String[]{"caption", "col", "colgroup", "frame", "head", "tbody", "td", "tfoot", "th", "thead", "tr"};
- private static final String[] InBodyEndClosers = new String[]{"address", "article", "aside", "blockquote", "button", "center", "details", "dir", "div",
- "dl", "fieldset", "figcaption", "figure", "footer", "header", "hgroup", "listing", "menu",
- "nav", "ol", "pre", "section", "summary", "ul"};
- private static final String[] InBodyEndAdoptionFormatters = new String[]{"a", "b", "big", "code", "em", "font", "i", "nobr", "s", "small", "strike", "strong", "tt", "u"};
- private static final String[] InBodyEndTableFosters = new String[]{"table", "tbody", "tfoot", "thead", "tr"};
- }
-}
diff --git a/src/org/jsoup/parser/ParseError.java b/src/org/jsoup/parser/ParseError.java
deleted file mode 100644
index dfa0900..0000000
--- a/src/org/jsoup/parser/ParseError.java
+++ /dev/null
@@ -1,40 +0,0 @@
-package org.jsoup.parser;
-
-/**
- * A Parse Error records an error in the input HTML that occurs in either the tokenisation or the tree building phase.
- */
-public class ParseError {
- private int pos;
- private String errorMsg;
-
- ParseError(int pos, String errorMsg) {
- this.pos = pos;
- this.errorMsg = errorMsg;
- }
-
- ParseError(int pos, String errorFormat, Object... args) {
- this.errorMsg = String.format(errorFormat, args);
- this.pos = pos;
- }
-
- /**
- * Retrieve the error message.
- * @return the error message.
- */
- public String getErrorMessage() {
- return errorMsg;
- }
-
- /**
- * Retrieves the offset of the error.
- * @return error offset within input
- */
- public int getPosition() {
- return pos;
- }
-
- @Override
- public String toString() {
- return pos + ": " + errorMsg;
- }
-}
diff --git a/src/org/jsoup/parser/ParseErrorList.java b/src/org/jsoup/parser/ParseErrorList.java
deleted file mode 100644
index 3824ffb..0000000
--- a/src/org/jsoup/parser/ParseErrorList.java
+++ /dev/null
@@ -1,34 +0,0 @@
-package org.jsoup.parser;
-
-import java.util.ArrayList;
-
-/**
- * A container for ParseErrors.
- *
- * @author Jonathan Hedley
- */
-class ParseErrorList extends ArrayList body are used.
- @param dirtyDocument Untrusted base document to clean.
- @return cleaned document.
- */
- public Document clean(Document dirtyDocument) {
- Validate.notNull(dirtyDocument);
-
- Document clean = Document.createShell(dirtyDocument.baseUri());
- if (dirtyDocument.body() != null) // frameset documents won't have a body. the clean doc will have empty body.
- copySafeNodes(dirtyDocument.body(), clean.body());
-
- return clean;
- }
-
- /**
- Determines if the input document is valid, against the whitelist. It is considered valid if all the tags and attributes
- in the input HTML are allowed by the whitelist.
-
- This method can be used as a validator for user input forms. An invalid document will still be cleaned successfully
- using the {@link #clean(Document)} document. If using as a validator, it is recommended to still clean the document
- to ensure enforced attributes are set correctly, and that the output is tidied.
- @param dirtyDocument document to test
- @return true if no tags or attributes need to be removed; false if they do
- */
- public boolean isValid(Document dirtyDocument) {
- Validate.notNull(dirtyDocument);
-
- Document clean = Document.createShell(dirtyDocument.baseUri());
- int numDiscarded = copySafeNodes(dirtyDocument.body(), clean.body());
- return numDiscarded == 0;
- }
-
- /**
- Iterates the input and copies trusted nodes (tags, attributes, text) into the destination.
- */
- private final class CleaningVisitor implements NodeVisitor {
- private int numDiscarded = 0;
- private final Element root;
- private Element destination; // current element to append nodes to
-
- private CleaningVisitor(Element root, Element destination) {
- this.root = root;
- this.destination = destination;
- }
-
- public void head(Node source, int depth) {
- if (source instanceof Element) {
- Element sourceEl = (Element) source;
-
- if (whitelist.isSafeTag(sourceEl.tagName())) { // safe, clone and copy safe attrs
- ElementMeta meta = createSafeElement(sourceEl);
- Element destChild = meta.el;
- destination.appendChild(destChild);
-
- numDiscarded += meta.numAttribsDiscarded;
- destination = destChild;
- } else if (source != root) { // not a safe tag, so don't add. don't count root against discarded.
- numDiscarded++;
- }
- } else if (source instanceof TextNode) {
- TextNode sourceText = (TextNode) source;
- TextNode destText = new TextNode(sourceText.getWholeText(), source.baseUri());
- destination.appendChild(destText);
- } else if (source instanceof DataNode && whitelist.isSafeTag(source.parent().nodeName())) {
- DataNode sourceData = (DataNode) source;
- DataNode destData = new DataNode(sourceData.getWholeData(), source.baseUri());
- destination.appendChild(destData);
- } else { // else, we don't care about comments, xml proc instructions, etc
- numDiscarded++;
- }
- }
-
- public void tail(Node source, int depth) {
- if (source instanceof Element && whitelist.isSafeTag(source.nodeName())) {
- destination = destination.parent(); // would have descended, so pop destination stack
- }
- }
- }
-
- private int copySafeNodes(Element source, Element dest) {
- CleaningVisitor cleaningVisitor = new CleaningVisitor(source, dest);
- NodeTraversor traversor = new NodeTraversor(cleaningVisitor);
- traversor.traverse(source);
- return cleaningVisitor.numDiscarded;
- }
-
- private ElementMeta createSafeElement(Element sourceEl) {
- String sourceTag = sourceEl.tagName();
- Attributes destAttrs = new Attributes();
- Element dest = new Element(Tag.valueOf(sourceTag), sourceEl.baseUri(), destAttrs);
- int numDiscarded = 0;
-
- Attributes sourceAttrs = sourceEl.attributes();
- for (Attribute sourceAttr : sourceAttrs) {
- if (whitelist.isSafeAttribute(sourceTag, sourceEl, sourceAttr))
- destAttrs.put(sourceAttr);
- else
- numDiscarded++;
- }
- Attributes enforcedAttrs = whitelist.getEnforcedAttributes(sourceTag);
- destAttrs.addAll(enforcedAttrs);
-
- return new ElementMeta(dest, numDiscarded);
- }
-
- private static class ElementMeta {
- Element el;
- int numAttribsDiscarded;
-
- ElementMeta(Element el, int numAttribsDiscarded) {
- this.el = el;
- this.numAttribsDiscarded = numAttribsDiscarded;
- }
- }
-
-}
diff --git a/src/org/jsoup/safety/Whitelist.java b/src/org/jsoup/safety/Whitelist.java
deleted file mode 100644
index c144520..0000000
--- a/src/org/jsoup/safety/Whitelist.java
+++ /dev/null
@@ -1,615 +0,0 @@
-package org.jsoup.safety;
-
-/*
- Thank you to Ryan Grove (wonko.com) for the Ruby HTML cleaner http://github.com/rgrove/sanitize/, which inspired
- this whitelist configuration, and the initial defaults.
- */
-
-import org.jsoup.helper.Validate;
-import org.jsoup.nodes.Attribute;
-import org.jsoup.nodes.Attributes;
-import org.jsoup.nodes.Element;
-
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-
-/**
- Whitelists define what HTML (elements and attributes) to allow through the cleaner. Everything else is removed.
-
- Start with one of the defaults:
-
-
-
- If you need to allow more through (please be careful!), tweak a base whitelist with:
-
-
-
- You can remove any setting from an existing whitelist with:
-
-
-
- The cleaner and these whitelists assume that you want to clean a body fragment of HTML (to add user
- supplied HTML into a templated page), and not to clean a full HTML document. If the latter is the case, either wrap the
- document HTML around the cleaned body HTML, or create a whitelist that allows html and head
- elements as appropriate.
-
- If you are going to extend a whitelist, please be very careful. Make sure you understand what attributes may lead to
- XSS attack vectors. URL attributes are particularly vulnerable and require careful validation. See
- http://ha.ckers.org/xss.html for some XSS attack examples.
-
- @author Jonathan Hedley
- */
-public class Whitelist {
- private Setb, em, i, strong, u. All other HTML (tags and
- attributes) will be removed.
-
- @return whitelist
- */
- public static Whitelist simpleText() {
- return new Whitelist()
- .addTags("b", "em", "i", "strong", "u")
- ;
- }
-
- /**
- This whitelist allows a fuller range of text nodes: a, b, blockquote, br, cite, code, dd, dl, dt, em, i, li,
- ol, p, pre, q, small, span, strike, strong, sub, sup, u, ul, and appropriate attributes.
-
- Links (a elements) can point to http, https, ftp, mailto, and have an enforced
- rel=nofollow attribute.
-
- Does not allow images.
-
- @return whitelist
- */
- public static Whitelist basic() {
- return new Whitelist()
- .addTags(
- "a", "b", "blockquote", "br", "cite", "code", "dd", "dl", "dt", "em",
- "i", "li", "ol", "p", "pre", "q", "small", "span", "strike", "strong", "sub",
- "sup", "u", "ul")
-
- .addAttributes("a", "href")
- .addAttributes("blockquote", "cite")
- .addAttributes("q", "cite")
-
- .addProtocols("a", "href", "ftp", "http", "https", "mailto")
- .addProtocols("blockquote", "cite", "http", "https")
- .addProtocols("cite", "cite", "http", "https")
-
- .addEnforcedAttribute("a", "rel", "nofollow")
- ;
-
- }
-
- /**
- This whitelist allows the same text tags as {@link #basic}, and also allows img tags, with appropriate
- attributes, with src pointing to http or https.
-
- @return whitelist
- */
- public static Whitelist basicWithImages() {
- return basic()
- .addTags("img")
- .addAttributes("img", "align", "alt", "height", "src", "title", "width")
- .addProtocols("img", "src", "http", "https")
- ;
- }
-
- /**
- This whitelist allows a full range of text and structural body HTML: a, b, blockquote, br, caption, cite,
- code, col, colgroup, dd, div, dl, dt, em, h1, h2, h3, h4, h5, h6, i, img, li, ol, p, pre, q, small, span, strike, strong, sub,
- sup, table, tbody, td, tfoot, th, thead, tr, u, ul
-
- Links do not have an enforced rel=nofollow attribute, but you can add that if desired.
-
- @return whitelist
- */
- public static Whitelist relaxed() {
- return new Whitelist()
- .addTags(
- "a", "b", "blockquote", "br", "caption", "cite", "code", "col",
- "colgroup", "dd", "div", "dl", "dt", "em", "h1", "h2", "h3", "h4", "h5", "h6",
- "i", "img", "li", "ol", "p", "pre", "q", "small", "span", "strike", "strong",
- "sub", "sup", "table", "tbody", "td", "tfoot", "th", "thead", "tr", "u",
- "ul")
-
- .addAttributes("a", "href", "title")
- .addAttributes("blockquote", "cite")
- .addAttributes("col", "span", "width")
- .addAttributes("colgroup", "span", "width")
- .addAttributes("img", "align", "alt", "height", "src", "title", "width")
- .addAttributes("ol", "start", "type")
- .addAttributes("q", "cite")
- .addAttributes("table", "summary", "width")
- .addAttributes("td", "abbr", "axis", "colspan", "rowspan", "width")
- .addAttributes(
- "th", "abbr", "axis", "colspan", "rowspan", "scope",
- "width")
- .addAttributes("ul", "type")
-
- .addProtocols("a", "href", "ftp", "http", "https", "mailto")
- .addProtocols("blockquote", "cite", "http", "https")
- .addProtocols("cite", "cite", "http", "https")
- .addProtocols("img", "src", "http", "https")
- .addProtocols("q", "cite", "http", "https")
- ;
- }
-
- /**
- Create a new, empty whitelist. Generally it will be better to start with a default prepared whitelist instead.
-
- @see #basic()
- @see #basicWithImages()
- @see #simpleText()
- @see #relaxed()
- */
- public Whitelist() {
- tagNames = new HashSetaddAttributes("a", "href", "class") allows href and class attributes
- on a tags.
-
- To make an attribute valid for all tags, use the pseudo tag :all, e.g.
- addAttributes(":all", "class").
-
- @param tag The tag the attributes are for. The tag will be added to the allowed tag list if necessary.
- @param keys List of valid attributes for the tag
- @return this (for chaining)
- */
- public Whitelist addAttributes(String tag, String... keys) {
- Validate.notEmpty(tag);
- Validate.notNull(keys);
- Validate.isTrue(keys.length > 0, "No attributes supplied.");
-
- TagName tagName = TagName.valueOf(tag);
- if (!tagNames.contains(tagName))
- tagNames.add(tagName);
- SetremoveAttributes("a", "href", "class") disallows href and class
- attributes on a tags.
-
- To make an attribute invalid for all tags, use the pseudo tag :all, e.g.
- removeAttributes(":all", "class").
-
- @param tag The tag the attributes are for.
- @param keys List of invalid attributes for the tag
- @return this (for chaining)
- */
- public Whitelist removeAttributes(String tag, String... keys) {
- Validate.notEmpty(tag);
- Validate.notNull(keys);
- Validate.isTrue(keys.length > 0, "No attributes supplied.");
-
- TagName tagName = TagName.valueOf(tag);
- SetaddEnforcedAttribute("a", "rel", "nofollow") will make all a tags output as
- <a href="..." rel="nofollow">
-
- @param tag The tag the enforced attribute is for. The tag will be added to the allowed tag list if necessary.
- @param key The attribute key
- @param value The enforced attribute value
- @return this (for chaining)
- */
- public Whitelist addEnforcedAttribute(String tag, String key, String value) {
- Validate.notEmpty(tag);
- Validate.notEmpty(key);
- Validate.notEmpty(value);
-
- TagName tagName = TagName.valueOf(tag);
- if (!tagNames.contains(tagName))
- tagNames.add(tagName);
- AttributeKey attrKey = AttributeKey.valueOf(key);
- AttributeValue attrVal = AttributeValue.valueOf(value);
-
- if (enforcedAttributes.containsKey(tagName)) {
- enforcedAttributes.get(tagName).put(attrKey, attrVal);
- } else {
- MapaddProtocols("a", "href", "ftp", "http", "https")
-
- To allow a link to an in-page URL anchor (i.e. <a href="#anchor">, add a #:
- E.g.: addProtocols("a", "href", "#")
-
- @param tag Tag the URL protocol is for
- @param key Attribute key
- @param protocols List of valid protocols
- @return this, for chaining
- */
- public Whitelist addProtocols(String tag, String key, String... protocols) {
- Validate.notEmpty(tag);
- Validate.notEmpty(key);
- Validate.notNull(protocols);
-
- TagName tagName = TagName.valueOf(tag);
- AttributeKey attrKey = AttributeKey.valueOf(key);
- MapremoveProtocols("a", "href", "ftp")
-
- @param tag Tag the URL protocol is for
- @param key Attribute key
- @param protocols List of invalid protocols
- @return this, for chaining
- */
- public Whitelist removeProtocols(String tag, String key, String... protocols) {
- Validate.notEmpty(tag);
- Validate.notEmpty(key);
- Validate.notNull(protocols);
-
- TagName tagName = TagName.valueOf(tag);
- AttributeKey attrKey = AttributeKey.valueOf(key);
-
- if(this.protocols.containsKey(tagName)) {
- Mapdoc.select("b").wrap("<i></i>");
- becomes {@code
- * {@code doc.select("font").unwrap();}
- * HTML = {@code
- * doc.select("p").empty();
- * HTML = {@code
- * doc.select("p").remove();
- * HTML = {@code
- * Elements divs = doc.select("div").not("#logo");
- * Result: {@code divs: [null if contents is empty.
- */
- public Element first() {
- return contents.isEmpty() ? null : contents.get(0);
- }
-
- /**
- Get the last matched element.
- @return The last matched element, or null if contents is empty.
- */
- public Element last() {
- return contents.isEmpty() ? null : contents.get(contents.size() - 1);
- }
-
- /**
- * Perform a depth-first traversal on each of the selected elements.
- * @param nodeVisitor the visitor callbacks to perform on each node
- * @return this, for chaining
- */
- public Elements traverse(NodeVisitor nodeVisitor) {
- Validate.notNull(nodeVisitor);
- NodeTraversor traversor = new NodeTraversor(nodeVisitor);
- for (Element el: contents) {
- traversor.traverse(el);
- }
- return this;
- }
-
- /**
- * Get the {@link FormElement} forms from the selected elements, if any.
- * @return a list of {@link FormElement}s pulled from the matched elements. The list will be empty if the elements contain
- * no forms.
- */
- public List