html_limits: Resource limits for parsing and extraction

View source: R/limits.R

html_limitsR Documentation

Resource limits for parsing and extraction

Description

Every parse and extraction runs under explicit limits, so that untrusted HTML cannot exhaust memory or time. Limits are per call, not global options: pass the result to the limits argument of html_parse() and the extraction functions. Exceeding one raises a zuhtml_limit_error (see zuhtml-conditions) after all native memory is released.

Usage

html_limits(
  max_input = 16 * 1024^2,
  max_memory = 512 * 1024^2,
  max_depth = 512,
  max_nodes = 4e+06,
  max_errors = 100,
  max_table_cells = 1e+06,
  max_selector_length = 16 * 1024
)

Arguments

max_input

Largest input, in bytes of UTF-8 after decoding.

max_memory

Largest native memory the parser may hold at once, in bytes.

max_depth

Deepest nesting of open elements. Parsing time grows with the square of nesting depth, so this bounds it; it is enforced while parsing, not afterwards.

max_nodes

Most nodes a document may have.

max_errors

Most parse problems kept for html_problems(). Parsing continues past it; only the record is truncated.

max_table_cells

Most cells a table may expand to, spans included.

max_selector_length

Longest CSS selector, in bytes.

Details

The memory limit is the real guard; the input limit is a cheap check before parsing starts. Parsing and conversion together need about 26 bytes of native memory per input byte on ordinary markup, and 40 to 90 on markup dense with small elements, tables or formatting, so a large enough page reaches max_memory before max_input or max_nodes: a zuhtml_limit_error, not a crash. At the defaults, 16 MiB of ordinary markup (about 1.5 million nodes, 450 MB) parses.

Value

An object of class zuhtml_limits: a named list of the limits, as doubles.

See Also

Other parsing: html_fragment(), html_info(), html_parse(), html_problems(), zuhtml_info()

Examples

html_limits()

# A tighter nesting limit for a service parsing untrusted pages:
lim <- html_limits(max_depth = 128)
try(html_parse(strrep("<div>", 200), limits = lim))

zuhtml documentation built on Oct. 6, 2026, 5:06 p.m.