tagQuery() provides a
jQuery inspired interface for querying and modifying
tag() (and tagList()) objects.
Value
A class with methods that are described below. This class can't be
used directly inside other tag() or a renderTags() context, but
underlying HTML tags may be extracted via $allTags() or
$selectedTags().
Altered Tag structure
For performance reasons, the input tag structure to tagQuery() will be
altered into a consistently expected shape.
Some alterations include:
tags flattening their
$childrenfields into a singlelist()tags relocating any attribute
html_dependency() to be located in$children`tagList()-like structures relocating any attribute html dependency to be a entry in its list structure.
While the resulting tag shape has possibly changed,
tagQuery()'s' resulting tags will still render
to the same HTML value (ex: renderTags()) and
HTML dependencies (ex: findDependencies()).
Vignette
To get started with using tagQuery(), visit
https://rstudio.github.io/htmltools/articles/tagQuery.html.
Methods
Unless otherwise stated, tagQuery() methods accept a character
vector as input.
Query methods
Query methods identify particular subsets of the root tag using CSS selectors (or R functions).
Children
$find(cssSelector): Get the descendants of each selected tag, filtered by acssSelector.$children(cssSelector = NULL): Get the direct children of each selected tag, optionally filtered by acssSelector.
Siblings
siblings(cssSelector = NULL): Get the siblings of each selected tag, optionally filtered by acssSelector.
Parents
$parent(cssSelector = NULL): Get the parent of each selected tag, optionally filtered by acssSelector.$parents(cssSelector = NULL): Get the ancestors of each selected tag, optionally filtered by acssSelector.$closest(cssSelector = NULL): For each selected tag, get the closest ancestor tag (including itself) satisfying acssSelector. IfcssSelector = NULL, it is equivalent to calling$selectedTags().
Modify methods
Unlike query methods, modify methods modify the tagQuery() object.
Attributes
$addClass(class): Adds class(es) to each selected tag.$removeClass(class): Removes class(es) to each selected tag.$toggleClass(class): Adds class(es) that don't already exist and removes class(es) that do already exist (for each selected tag).$hasClass(class): Does each selected tag have all the provided class(es)?$addAttrs(...): Add a set of attributes to each selected tag.$removeAttrs(attrs): Remove a set of attributes from each selected tag.$hasAttrs(attr): Do each selected tags have all of the attributes?
Children
$append(...): For each selected tag, insert...after any existing children.$prepend(...): For each selected tag, insert...before any existing children.
Replace methods
$replaceWith(...): Replace all selected tags with...in the root tag and clear the selection.$remove(...): Remove all selected tags from the root tag and clear the current selection.$empty(): Remove any children of each selected tag. Use this method before calling$append(...)to replace the children of each selected tag, with other content.
Extract HTML tags
$allTags(): Return the (possibly modified) roottags.$selectedTags(): Return atagList()of the currently selected tags.
Examples
tagQ <- tagQuery(div(a()))
tagQ$find("a")$addClass("foo")
#> `$allTags()`:
#> <div>
#> <a class="foo"></a>
#> </div>
#>
#> `$selectedTags()`:
#> [[1]]
#> <a class="foo"></a>
#>
tagQ
#> `$allTags()`:
#> <div>
#> <a class="foo"></a>
#> </div>
#>
#> `$selectedTags()`: `$allTags()`
# To learn more, visit https://rstudio.github.io/htmltools/articles/tagQuery.html
