NativeElixirPdfUtilities.HtmlToPdf.CssParser (native_elixir_pdf_utilities v0.5.1)

View Source

Strict CSS parser for the native HTML-to-PDF renderer.

The parser accepts the document-oriented selector subset used by the style cascade: element, class, id, element.class, descendant, child, and comma groups. Simple @page rules are accepted outside the style cascade so the renderer can use page size and margin defaults. Declarations are kept as normalized property/value pairs so the style layer can validate values against the renderer's supported property set.

Summary

Functions

Extracts renderer page defaults from simple @page rules.

Parses a CSS stylesheet into strict renderer rules.

Parses a CSS declaration block into normalized property/value pairs.

Parses a CSS declaration block and returns source-location details on failure.

Parses a CSS stylesheet and returns source-location details when parsing fails.

Types

declaration()

@type declaration() :: {String.t(), String.t()} | {String.t(), String.t(), :important}

page_option()

@type page_option() ::
  {:page_size, :a4 | :letter | {number(), number()}}
  | {:margin, String.t() | number()}

rule()

@type rule() :: %{
  selectors: [selector()],
  declarations: [declaration()],
  order: non_neg_integer()
}

selector()

@type selector() :: %{
  parts: [selector_part()],
  specificity: {non_neg_integer(), non_neg_integer(), non_neg_integer()}
}

selector_part()

@type selector_part() :: %{
  tag: String.t() | nil,
  id: String.t() | nil,
  classes: [String.t()],
  pseudo_classes: [
    :first_child | :last_child | :root | {:nth_child, pos_integer()}
  ],
  combinator: nil | :descendant | :child
}

stylesheet()

@type stylesheet() :: [rule()]

Functions

page_options(css)

@spec page_options(String.t()) :: {:ok, [page_option()]} | {:error, :invalid_css}

Extracts renderer page defaults from simple @page rules.

Supported declarations are single-value margin lengths and common size values such as A4, A4 landscape, letter, and letter landscape. Unsupported page declarations are ignored so normal CSS parsing remains strict for the supported style cascade.

parse(css)

@spec parse(String.t()) :: {:ok, stylesheet()} | {:error, :invalid_css}

Parses a CSS stylesheet into strict renderer rules.

parse_declarations(css)

@spec parse_declarations(String.t()) ::
  {:ok, [declaration()]} | {:error, :invalid_css}

Parses a CSS declaration block into normalized property/value pairs.

This is used for both stylesheet blocks and inline style attributes.

parse_declarations_detailed(css)

@spec parse_declarations_detailed(String.t()) ::
  {:ok, [declaration()]} | {:error, {:invalid_css, map()}}

Parses a CSS declaration block and returns source-location details on failure.

parse_detailed(css)

@spec parse_detailed(String.t()) ::
  {:ok, stylesheet()} | {:error, {:invalid_css, map()}}

Parses a CSS stylesheet and returns source-location details when parsing fails.