Skip to Content
PureHTMLdocs

Basics

Fundamental concepts of PureHTML selectors, type casts, defaults, and transformer pipelines.

Basic CSS Selector

The simplest PureHTML configuration specifies a CSS selector to extract the inner text of an element:

selector: .foo

Extracting Numbers & Type Casts

By default, DOM text extractions yield string values. When you need a numeric value, add transform: number:

selector: '#price'
transform: number

Declaring the Type (type: number / type: boolean)

As a concise alternative to adding a transformer, you can specify type: number or type: boolean. The cast runs automatically after any other transformation:

selector: '#price'
type: number

Default Values (default)

When a selector matches nothing, the extracted value is null. You can provide a fallback value with default. The default value is returned whenever the selector does not match in the document:

type: object
properties:
title: '#title'
subtitle:
  selector: '#subtitle'
  default: No subtitle

Fallback Selectors (selector: [...])

Websites frequently redesign layouts, run A/B experiments, or rename CSS classes. PureHTML allows selector to be an array of CSS selector strings. The parser evaluates them in order and extracts data from the first matching selector:

selector:
- .flash-deal
- .sale-price
- .regular-price
transform: trim

Selector Objects & Fixture References

You can also write selectors as objects with sampleHTMLs. This allows test fixture files to be linked alongside individual DOM variations:

selector:
- selector: .promo-price
  sampleHTMLs:
    - fixtures/promo.html
- selector: .standard-price
  sampleHTMLs:
    - fixtures/standard.html
transform: trim

Trimming Text (trim)

Web pages frequently contain excess whitespace, indentation, or newline characters around text. Use trim to clean them up:

selector: '#product-title'
transform: trim

Combining Multiple Transformers

Transformers can be composed as an array pipeline. The result of each transformer is passed as the input to the next:

selector: 'span#product-price'
transform: [trim, number]

Next, see the Transformers reference for a comprehensive breakdown of all available transformers.

On this page