Package org.jsoup.nodes
Class Attributes
java.lang.Object
org.jsoup.nodes.Attributes
The attributes of an Element.
During parsing, attributes in with the same name in an element are deduplicated, according to the configured parser's
attribute case-sensitive setting. It is possible to have duplicate attributes subsequently if
add(String, String) vs put(String, String) is used.
Attribute name and value comparisons are generally case sensitive. By default for HTML, attribute names are normalized to lower-case on parsing. That means you should use lower-case strings when referring to attributes by name.
- Author:
- Jonathan Hedley, jonathan@hedley.net
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds a new attribute.voidaddAll(Attributes incoming) Add all the attributes from the incoming set to this set.asList()Get the attributes as a List, for iteration.@Nullable AttributeGet an Attribute by key.clone()dataset()Retrieves a filtered view of attributes that are HTML5 custom data attributes; that is, attributes with keys starting withdata-.intdeduplicate(ParseSettings settings) Internal method.booleanChecks if these attributes are equal to another set of attributes, by comparing the two sets.Get an attribute value by key.getIgnoreCase(String key) Get an attribute's value by case-insensitive keybooleanCheck if these attributes contain an attribute with a value for this key.booleanCheck if these attributes contain an attribute with a value for this key.inthashCode()Calculates the hashcode of these attributes, by iterating all attributes and summing their hashcodes.booleanTests if these attributes contain an attribute with this key.booleanhasKeyIgnoreCase(String key) Tests if these attributes contain an attribute with this key.html()Get the HTML representation of these attributes.booleanisEmpty()Test if this Attributes list is empty.iterator()voidInternal method.Set a new boolean attribute.Set a new attribute, or replace an existing one by key.Set a new attribute, or replace an existing one by key.voidRemove an attribute by key.voidremoveIgnoreCase(String key) Remove an attribute by key.intsize()Get the number of attributes in this set, excluding any internal-only attributes (e.g. user data).sourceRange(String key) Get the source ranges (start to end position) in the original input source from which this attribute's name and value were parsed.sourceRange(String key, Range.AttributeRange range) Set the source ranges (start to end position) from which this attribute's name and value were parsed.toString()@Nullable ObjectGet an arbitrary user-data object by key.Set an arbitrary user-data object by key.Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
dataPrefix
- See Also:
-
-
Constructor Details
-
Attributes
public Attributes()
-
-
Method Details
-
get
Get an attribute value by key.- Parameters:
key- the (case-sensitive) attribute key- Returns:
- the attribute value if set; or empty string if not set (or a boolean attribute).
- See Also:
-
attribute
Get an Attribute by key. The Attribute will remain connected to these Attributes, so changes made viaAttribute.setKey(String),Attribute.setValue(String)etc will cascade back to these Attributes and their owning Element.- Parameters:
key- the (case-sensitive) attribute key- Returns:
- the Attribute for this key, or null if not present.
- Since:
- 1.17.2
-
getIgnoreCase
Get an attribute's value by case-insensitive key- Parameters:
key- the attribute name- Returns:
- the first matching attribute value if set; or empty string if not set (ora boolean attribute).
-
add
Adds a new attribute. Will produce duplicates if the key already exists.- See Also:
-
put
Set a new attribute, or replace an existing one by key.- Parameters:
key- case sensitive attribute key (not null)value- attribute value (which can be null, to set a true boolean attribute)- Returns:
- these attributes, for chaining
-
userData
Get an arbitrary user-data object by key.- Parameters:
key- case-sensitive key to the object.- Returns:
- the object associated to this key, or
nullif not found. - Since:
- 1.17.1
- See Also:
-
userData
Set an arbitrary user-data object by key. Will be treated as an internal attribute, so will not be emitted in HTML.- Parameters:
key- case-sensitive keyvalue- object value. Providing anullvalue has the effect of removing the key from the userData map.- Returns:
- these attributes
- Since:
- 1.17.1
- See Also:
-
put
Set a new boolean attribute. Removes the attribute if the value is false.- Parameters:
key- case insensitive attribute keyvalue- attribute value- Returns:
- these attributes, for chaining
-
put
Set a new attribute, or replace an existing one by key.- Parameters:
attribute- attribute with case-sensitive key- Returns:
- these attributes, for chaining
-
remove
Remove an attribute by key. Case sensitive.- Parameters:
key- attribute key to remove
-
removeIgnoreCase
Remove an attribute by key. Case insensitive.- Parameters:
key- attribute key to remove
-
hasKey
Tests if these attributes contain an attribute with this key.- Parameters:
key- case-sensitive key to check for- Returns:
- true if key exists, false otherwise
-
hasKeyIgnoreCase
Tests if these attributes contain an attribute with this key.- Parameters:
key- key to check for- Returns:
- true if key exists, false otherwise
-
hasDeclaredValueForKey
Check if these attributes contain an attribute with a value for this key.- Parameters:
key- key to check for- Returns:
- true if key exists, and it has a value
-
hasDeclaredValueForKeyIgnoreCase
Check if these attributes contain an attribute with a value for this key.- Parameters:
key- case-insensitive key to check for- Returns:
- true if key exists, and it has a value
-
size
Get the number of attributes in this set, excluding any internal-only attributes (e.g. user data).Internal attributes are excluded from the
html(),asList(), anditerator()methods.- Returns:
- size
-
isEmpty
Test if this Attributes list is empty.This does not include internal attributes, such as user data.
-
addAll
Add all the attributes from the incoming set to this set.- Parameters:
incoming- attributes to add to these attributes.
-
sourceRange
Get the source ranges (start to end position) in the original input source from which this attribute's name and value were parsed.Position tracking must be enabled prior to parsing the content.
- Parameters:
key- the attribute name- Returns:
- the ranges for the attribute's name and value, or
untrackedif the attribute does not exist or its range was not tracked. - Since:
- 1.17.1
- See Also:
-
sourceRange
Set the source ranges (start to end position) from which this attribute's name and value were parsed.- Parameters:
key- the attribute namerange- the range for the attribute's name and value- Returns:
- these attributes, for chaining
- Since:
- 1.18.2
-
iterator
-
asList
Get the attributes as a List, for iteration.- Returns:
- a view of the attributes as an unmodifiable List.
-
dataset
Retrieves a filtered view of attributes that are HTML5 custom data attributes; that is, attributes with keys starting withdata-.- Returns:
- map of custom data attributes.
-
html
Get the HTML representation of these attributes.- Returns:
- HTML
-
toString
-
equals
Checks if these attributes are equal to another set of attributes, by comparing the two sets. Note that the order of the attributes does not impact this equality (as per the Map interface equals()). -
hashCode
Calculates the hashcode of these attributes, by iterating all attributes and summing their hashcodes. -
clone
-
normalize
Internal method. Lowercases all (non-internal) keys. -
deduplicate
Internal method. Removes duplicate attribute by name. Settings for case sensitivity of key names.- Parameters:
settings- case sensitivity- Returns:
- number of removed dupes
-