Constructor
new AttributeList(attrs)
- Source:
Parameters:
Name | Type | Description |
---|---|---|
attrs |
AttributeArgument | void | attributes. |
Extends
- Array
Methods
(static) from(attrs)
- Source:
Return a new AttributeList from an array or object of attributes.
Examples
Return an array of attributes from a regular object.
AttributeList.from({ dataFoo: 'bar' }) // returns AttributeList [{ name: 'data-foo', value: 'bar' }]
Return a normalized array of attributes from an impure array of attributes.
AttributeList.from([{ name: 'data-foo', value: true, foo: 'bar' }]) // returns AttributeList [{ name: 'data-foo', value: 'true' }]
Parameters:
Name | Type | Description |
---|---|---|
attrs |
Array.<(AttributePartial|AttributePlain)> | Array or object of attributes. |
add(name, …valueopt)
- Source:
Add an attribute or attributes to the current AttributeList, returning whether the attribute or attributes were added.
Examples
Add an empty "id" attribute.
attrs.add('id')
Add an "id" attribute with a value of "bar".
attrs.add({ id: 'bar' })
attrs.add([{ name: 'id', value: 'bar' }])
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
AttributeArgument | RegExp | string | Attribute to remove. |
|
value |
Array.<string> |
<optional> <repeatable> |
Value of the attribute to remove. |
clone(…attrs)
- Source:
Return a new clone of the current AttributeList while conditionally applying additional attributes.
Examples
attrs.clone()
Clone the current attribute and add an "id" attribute with a value of "bar".
attrs.clone({ name: 'id', value: 'bar' })
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
attrs |
Array.<(AttributeArgument|RegExp|string)> |
<repeatable> |
Additional attributes to be added to the new AttributeList. |
contains(name)
- Source:
Return whether an attribute or attributes exists in the current AttributeList.
Examples
Return whether there is an "id" attribute.
attrs.contains('id')
attrs.contains({ id: 'bar' })
Return whether there is an "id" attribute with a value of "bar".
attrs.contains([{ name: 'id': value: 'bar' }])
Parameters:
Name | Type | Description |
---|---|---|
name |
AttributeArgument | RegExp | string | Attribute to check. |
get(name) → {false|null|string}
- Source:
If the attribute exists with a value then a String is returned. If the attribute exists with no value then null
is returned. If the attribute does not exist then false
is returned.
Example
Return the value of "id" or `false`.
// <div>this element has no "id" attribute</div>
attrs.get('id') // returns false
// <div id>this element has an "id" attribute with no value</div>
attrs.get('id') // returns null
// <div id="">this element has an "id" attribute with a value</div>
attrs.get('id') // returns ''
Parameters:
Name | Type | Description |
---|---|---|
name |
AttributeArgument | RegExp | string | Name or attribute object being accessed. |
Returns:
- Type
- false | null | string
indexOf(name, …valueopt)
- Source:
Return the position of an attribute by name or attribute object in the current AttributeList.
Examples
Return the index of "id".
attrs.indexOf('id')
Return the index of /d$/.
attrs.indexOf(/d$/i)
Return the index of "foo" with a value of "bar".
attrs.indexOf({ foo: 'bar' })
Return the index of "ariaLabel" or "aria-label" matching /^open/.
attrs.indexOf({ ariaLabel: /^open/ })
Return the index of an attribute whose name matches `/^foo/`.
attrs.indexOf([{ name: /^foo/ })
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
AttributeArgument | RegExp | string | Attribute to locate. |
|
value |
Array.<(RegExp|string)> |
<optional> <repeatable> |
Value of the attribute to locate. |
remove(name, …valueopt)
- Source:
Remove an attribute or attributes from the current AttributeList, returning whether any attribute was removed.
Examples
Remove the "id" attribute.
attrs.remove('id')
Remove the "id" attribute when it has a value of "bar".
attrs.remove('id', 'bar')
attrs.remove({ id: 'bar' })
attrs.remove([{ name: 'id', value: 'bar' }])
Remove the "id" and "class" attributes.
attrs.remove(['id', 'class'])
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
AttributeArgument | RegExp | string | Attribute to remove. |
|
value |
Array.<string> |
<optional> <repeatable> |
Value of the attribute being removed. |
toggle(attr, …valueopt, forceopt)
- Source:
Toggle an attribute or attributes from the current AttributeList, returning whether any attribute was added.
Examples
Toggle the "id" attribute.
attrs.toggle('id')
Toggle the "id" attribute with a value of "bar".
attrs.toggle('id', 'bar')
attrs.toggle({ id: 'bar' })
attrs.toggle([{ name: 'id', value: 'bar' }])
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
attr |
AttributeArgument | RegExp | string | Attribute to toggle. |
|
value |
Array.<{0: boolean}> | Array.<{0: string}> | Array.<{0: string, 1: boolean}> |
<optional> <repeatable> |
Value of the attribute being toggled when the first argument is not an object, or attributes should be exclusively added (true) or removed (false). |
force |
boolean |
<optional> |
Whether attributes should be exclusively added (true) or removed (false). |
toJSON() → {AttributePlain}
- Source:
Return the current AttributeList as a stringifiable Object.
Example
attrs.toJSON() // returns { class: 'foo', dataFoo: 'bar' } when <x class="foo" data-foo: "bar" />
Returns:
- Type
- AttributePlain
toString()
- Source:
Return the current AttributeList as a String.
Example
attrs.toString() // returns 'class="foo" data-foo="bar"'