Class: AtPage

AtPage()

new AtPage()

Source:

Methods

addBorderVars(border, list, item)

Adds CSS custom properties (variables) for page borders to the rule block.

Adds CSS custom properties (variables) for page borders to the rule block.

Parameters:
Name Type Description
border Object

An object with string values for top, right, bottom, and left.

list Object

The list of declarations.

item Object

Reference node for insertion.

Source:

addDimensions(width, height, orientation, list, item)

Adds CSS custom properties for page width and height based on orientation.

Adds CSS custom properties for page width and height based on orientation.

Parameters:
Name Type Description
width Object

Width value (e.g., {value: 210, unit: "mm"}).

height Object

Height value (same structure as width).

orientation string

Either 'portrait' or 'landscape'.

list Object

Declaration list to which variables are added.

item Object

Reference node.

Source:

addMarginVars(margin, list, item)

Adds CSS custom properties (variables) for page margins to the rule block.

Adds CSS custom properties (variables) for page margins to the rule block.

Parameters:
Name Type Description
margin Object

An object with top, right, bottom, and left values.

list Object

The list of declarations (typically from a Block AST node).

item Object

Reference item used for insertion position.

Source:

addMarginaliaContent(page, list, item, sheet)

Generates the content-only display rules for marginalia.

Generates the content-only display rules for marginalia. Adds display: none or display: block for margin content depending on whether content: none is used.

Parameters:
Name Type Description
page Object

Page object with marginalia blocks.

list Object

Rule list.

item Object

Rule being built.

sheet Object

Stylesheet to which rules are inserted.

Source:

addMarginaliaStyles(page, list, item, sheet)

Adds marginalia rules (styles) for specified page regions (e.g., top-left, right-middle).

Adds marginalia rules (styles) for specified page regions (e.g., top-left, right-middle). Handles:

  • Content detection
  • Vertical alignment conversion
  • max-width/max-height additions
Parameters:
Name Type Description
page Object

The page object containing marginalia blocks.

list Object

Rule list to append to.

item Object

The current rule or rule block.

sheet Object

The stylesheet to insert rules into.

Source:

addNotesStyles(notes, page, list, item, sheet)

Appends CSS rules for footnotes, sidenotes, or other types of page notes.

Appends CSS rules for footnotes, sidenotes, or other types of page notes.

Each note rule targets a .pagedjs_<type>_content class inside the given page selector.

Parameters:
Name Type Description
notes Object

Object where each key is a note type (e.g. "footnote") and value is a Block node.

page Object

The page object.

list Object

The CSS rule list to append new note rules to.

item Object

Not used here, but may be for future insertion reference.

sheet Object

The stylesheet object (not used in this function).

Source:

addPaddingVars(padding, list, item)

Adds CSS custom properties (variables) for page padding to the rule block.

Adds CSS custom properties (variables) for page padding to the rule block.

Parameters:
Name Type Description
padding Object

An object with top, right, bottom, and left values.

list Object

The list of declarations.

item Object

Reference node for insertion.

Source:

addPageAttributes(page, start, pages)

Adds page-specific classes based on dataset attributes.

Adds page-specific classes based on dataset attributes.

Parameters:
Name Type Description
page object

The page object.

start HTMLElement

The element marking the start of the page.

pages Array

The array of all pages.

Source:

addPageClasses(pages, ast, sheet)

Adds dynamically generated page classes (rules) to the stylesheet.

Adds dynamically generated page classes (rules) to the stylesheet. These are based on parsed @page rules with selectors like:

  • * (default)
  • :left, :right, :first, :blank
  • :nth(...)
  • Named pages (e.g., @page chapter)

Ensures each rule is only added once.

Parameters:
Name Type Description
pages Object

A dictionary of parsed @page definitions.

ast Object

The stylesheet AST (typically from csstree).

sheet Object

The stylesheet object that supports insertRule().

Source:

afterPageLayout(page, contents, breakToken, chunker)

Hook called after a page has been laid out.

Hook called after a page has been laid out.

Parameters:
Name Type Description
page object

The page object.

contents HTMLElement

The content container.

breakToken object

The break token for the current page.

chunker object

The chunking utility.

Source:

afterTreeWalk(ast, sheet)

Finalizes processing after the CSS AST tree has been walked.

Finalizes processing after the CSS AST tree has been walked. Applies page-level classes and, if a default @page rule (*) is marked as dirty (i.e., changed), it updates root-level CSS variables and emits size and page-related metadata.

Parameters:
Name Type Description
ast Object

The full CSS AST (typically from csstree) representing the stylesheet.

sheet Object

The current stylesheet being processed (contextual information, optional).

Source:

beforePageLayout(page, contents, breakToken, chunker)

Hook called before a page is laid out.

Hook called before a page is laid out.

Parameters:
Name Type Description
page object

The page object.

contents HTMLElement

The content container.

breakToken object

The token where layout breaks.

chunker object

The chunking utility.

Source:

createBlock(declarations) → {object}

Creates a CSSTree Block node from an array of declarations.

Creates a CSSTree Block node from an array of declarations.

Parameters:
Name Type Description
declarations Array

Array of CSSTree declaration nodes.

Source:
Returns:

A CSSTree block node.

Type
object

createCalculatedDimension(property, items, important, operatoropt) → {object}

Creates a CSS calc() declaration from multiple dimensions.

Creates a CSS calc() declaration from multiple dimensions.

Parameters:
Name Type Attributes Default Description
property string

The CSS property name.

items Array

Array of {value, unit} objects.

important boolean

Whether the declaration is !important.

operator string <optional>
'+'

Math operator (e.g. '+', '-', etc.).

Source:
Returns:

A CSSTree declaration node.

Type
object

createDeclaration(property, value, important) → {object}

Creates a CSS declaration for a property with a simple identifier value.

Creates a CSS declaration for a property with a simple identifier value.

Parameters:
Name Type Description
property string

The CSS property name.

value string

The CSS value.

important boolean

Whether the declaration is !important.

Source:
Returns:

A CSSTree declaration node.

Type
object

createDimension(property, cssValue, important) → {object}

Creates a CSS dimension-based declaration.

Creates a CSS dimension-based declaration.

Parameters:
Name Type Description
property string

The CSS property.

cssValue object

Object with value and unit keys.

important boolean

Whether the declaration is !important.

Source:
Returns:

A CSSTree declaration node.

Type
object

createPage(page, ruleList, sheet) → {Object}

Creates a CSS rule for a page, applying margin, padding, border, and dimension variables.

Creates a CSS rule for a page, applying margin, padding, border, and dimension variables. Also adds marginalia and notes if present.

Parameters:
Name Type Description
page Object

The page object containing properties like width, margin, etc.

ruleList Object

The list of AST rules to which new rules may be appended.

sheet Object

The stylesheet object used to insert rules.

Source:
Returns:

A CSS rule representing the page.

Type
Object

createRule(selectors, block) → {object}

Creates a CSSTree Rule node from selectors and a block.

Creates a CSSTree Rule node from selectors and a block.

Parameters:
Name Type Description
selectors csstree.List

List of selector nodes.

block object | Array

A block node or array of declarations.

Source:
Returns:

A CSSTree rule node.

Type
object

createVariable(property, value) → {object}

Creates a raw CSS variable declaration.

Creates a raw CSS variable declaration.

Parameters:
Name Type Description
property string

The variable name.

value string

The raw CSS value.

Source:
Returns:

A CSSTree declaration node.

Type
object

finalizePage(fragment, page, breakToken, chunker)

Final adjustments to the page after layout including margin styling.

Final adjustments to the page after layout including margin styling.

Parameters:
Name Type Description
fragment HTMLElement

The document fragment.

page object

The page object.

breakToken object

The break token.

chunker object

The chunker object.

Source:

getBorders(declaration) → {Object}

Parses border-related declarations (border, border-top, etc.) and expands them into an object representing each side.

Parses border-related declarations (border, border-top, etc.) and expands them into an object representing each side.

This is used to apply page-level borders on generated content (e.g. .pagedjs_area).

Parameters:
Name Type Description
declaration Object

A declaration node with a prop and value.

Source:
Returns:

An object with top, right, bottom, and left properties.

Type
Object

getMargins(declaration) → {Object}

Parses a shorthand or longhand margin declaration and expands it into individual top, right, bottom, and left sides.

Parses a shorthand or longhand margin declaration and expands it into individual top, right, bottom, and left sides.

Supports values like:

  • margin: 10px
  • margin: 10px 20px
  • margin: 10px 20px 30px
  • margin: 10px 20px 30px 40px
Parameters:
Name Type Description
declaration Object

The AST node representing the margin declaration.

Source:
Returns:

An object with top, right, bottom, and left properties.

Type
Object

getNth(nth) → {object}

Parses an nth selector string (e.g., "2n+1") into its components.

Parses an nth selector string (e.g., "2n+1") into its components.

Parameters:
Name Type Description
nth string

The nth selector string.

Source:
Returns:

Parsed nth object in An+B format.

Type
object

getNthSelector(ast) → {string|undefined}

Extracts the argument of an :nth pseudo-class selector, if present.

Extracts the argument of an :nth pseudo-class selector, if present. For example, in @page :nth(3n) {}, this returns "3n".

Parameters:
Name Type Description
ast Object

The AST node for the @page rule.

Source:
Returns:

The :nth selector argument if found, otherwise undefined.

Type
string | undefined

getPaddings(declaration) → {Object}

Parses a shorthand or longhand padding declaration and expands it into top, right, bottom, and left properties.

Parses a shorthand or longhand padding declaration and expands it into top, right, bottom, and left properties.

Supports values like:

  • padding: 10px
  • padding: 10px 20px
  • padding: 10px 20px 30px
  • padding: 10px 20px 30px 40px
Parameters:
Name Type Description
declaration Object

The AST node representing the padding declaration.

Source:
Returns:

An object with top, right, bottom, and left properties.

Type
Object

getPsuedoSelector(ast) → {string|undefined}

Extracts a pseudo-class selector from the @page prelude.

Extracts a pseudo-class selector from the @page prelude. Looks for values like :left, :right, :first, etc., and returns the name. Skips :nth pseudo-classes (handled separately).

Parameters:
Name Type Description
ast Object

The AST node for the @page rule.

Source:
Returns:

The pseudo-class name if found, otherwise undefined.

Type
string | undefined

getStartElement(content, breakToken) → {HTMLElement|undefined}

Determines the start element for content on a new page.

Determines the start element for content on a new page.

Parameters:
Name Type Description
content HTMLElement

The content container.

breakToken object

The token representing where the break occurred.

Source:
Returns:

The starting element.

Type
HTMLElement | undefined

getTypeSelector(ast) → {string|undefined}

Extracts the type selector (page name) from the @page rule prelude.

Extracts the type selector (page name) from the @page rule prelude. For example, in @page myPage {}, this returns "myPage".

Parameters:
Name Type Description
ast Object

The AST node for the @page rule (should contain a prelude).

Source:
Returns:

The type selector name if found, otherwise undefined.

Type
string | undefined

onAtPage(node, item, list)

Processes a CSS @page rule node and integrates it into the internal pages model.

Processes a CSS @page rule node and integrates it into the internal pages model. Handles merging of existing page data, extracting selectors, marginalia, size, bleed, marks, margins, padding, and borders. Also removes the processed item from the rule list.

Parameters:
Name Type Description
node Object

The AST node representing the @page rule.

item Object

The list item in the AST that contains the rule (used for removal).

list Object

The parent list of rules, typically from the CSS AST (csstree.List).

Source:

replaceDeclarations(ast) → {Object}

Extracts and removes relevant declarations from the @page block such as:

  • size
  • bleed
  • marks
  • margin / margin-*
  • padding / padding-*
  • border / border-*

Converts them into structured objects for internal processing.

Extracts and removes relevant declarations from the @page block such as:

  • size
  • bleed
  • marks
  • margin / margin-*
  • padding / padding-*
  • border / border-*

Converts them into structured objects for internal processing.

Parameters:
Name Type Description
ast Object

The AST node for the @page rule.

Source:
Returns:

A parsed object containing size, bleed, marks, margin, padding, and border properties.

Type
Object

replaceMarginalia(ast) → {Object}

Extracts and removes @margin-* style at-rules from the block of a @page rule.

Extracts and removes @margin-* style at-rules from the block of a @page rule. These are stored in a dictionary keyed by their normalized region names.

Parameters:
Name Type Description
ast Object

The AST node for the @page rule.

Source:
Returns:

A dictionary of marginalia region names to their blocks.

Type
Object

replaceNotes(ast) → {Object}

Extracts and removes @footnote at-rules from the block of a @page rule.

Extracts and removes @footnote at-rules from the block of a @page rule. Returns a dictionary of extracted footnote blocks.

Parameters:
Name Type Description
ast Object

The AST node for the @page rule.

Source:
Returns:

A dictionary of note names (currently only footnote) to their blocks.

Type
Object

selectorsForPage(page) → {csstree.List}

Builds a list of CSS selectors for a given page.

Builds a list of CSS selectors for a given page.

Parameters:
Name Type Description
page object

The page object.

Source:
Returns:

A list of CSS selector nodes.

Type
csstree.List

selectorsForPageMargin(page, margin) → {csstree.List}

Builds CSS selectors for a specific margin area of a page.

Builds CSS selectors for a specific margin area of a page.

Parameters:
Name Type Description
page object

The page object.

margin string

The margin position (e.g. "top", "bottom").

Source:
Returns:

A list of CSS selector nodes for the margin.

Type
csstree.List