boxes

package
v1.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: GPL-3.0 Imports: 21 Imported by: 0

Documentation

Overview

Package boxes builds the box tree and resolves the CSS subset used by layout.

Index

Constants

View Source
const DefaultMaxBytes = 10 << 20

DefaultMaxBytes limits each external resource to 10 MiB.

Variables

This section is empty.

Functions

func AssignSourcePaths

func AssignSourcePaths(root *Box)

Types

type BorderSide

type BorderSide struct {
	Width float64
	Color [4]float64
	Style string
}

BorderSide describes a border side by width and color. A zero width means no border.

type Box

type Box struct {
	Tag string

	Style           ComputedStyle
	Text            string
	Image           *images.Raster            // resolved <img> only
	SVG             *svg.Document             // <svg> inline vectorial
	Parts           []Part                    // ordered segments (anonymous text or children)
	Link            string                    // Link is the raw href value from <a>.
	Fonts           map[string]*fonts.Face    // document font registry (root only)
	FontVariants    map[string]fonts.Variants // faces by weight and style
	Page            PageConfig                // @page from the first valid rule (root only)
	PageRules       []css.PageRule            // Ordered @page rules for page-context resolution.
	PageFooter      []css.ContentChunk        // @bottom-center (root only)
	PageFooterStyle css.PageFooterStyle       // @bottom-center supported style (root only)
	Meta            Metadata                  // <title>/meta (root only)
	Children        []*Box
	Parent          *Box // direct structural parent in the source tree
	// contains filtered or unexported fields
}

Box stores a node's content, resources, and metadata. Resolved CSS and user-agent values are stored in ComputedStyle.

func Build

func Build(htmlStr string) (*Box, error)

Build constructs a box tree from an HTML fragment using default options.

func BuildWithOptions

func BuildWithOptions(htmlStr string, opts Options) (*Box, []string, error)

BuildWithOptions constructs a box tree and returns warnings for recoverable resource failures.

func (*Box) SourcePath

func (b *Box) SourcePath() []int

SourcePath returns the box's stable child-index path under the document root.

type BoxSizing

type BoxSizing string

BoxSizing selects whether declared dimensions include border and padding.

const (
	BoxSizingContentBox BoxSizing = ""
	BoxSizingBorderBox  BoxSizing = "border-box"
)

type BreakAfterMode

type BreakAfterMode string

BreakAfterMode selects the supported forced page break after a box.

const (
	BreakAfterAuto  BreakAfterMode = ""
	BreakAfterAvoid BreakAfterMode = "avoid"
	BreakAfterPage  BreakAfterMode = "page"
)

type BreakBeforeMode

type BreakBeforeMode string

BreakBeforeMode selects the supported break behavior before a box.

const (
	BreakBeforeAuto  BreakBeforeMode = ""
	BreakBeforeAvoid BreakBeforeMode = "avoid"
	BreakBeforePage  BreakBeforeMode = "page"
)

type BreakInsideMode

type BreakInsideMode string

BreakInsideMode controls whether a box should stay intact when it fits.

const (
	BreakInsideAuto  BreakInsideMode = ""
	BreakInsideAvoid BreakInsideMode = "avoid"
)

type CSSDimension

type CSSDimension struct {
	Kind  DimensionKind
	Value float64
}

CSSDimension stores an authored dimension without resolving its containing block.

func (CSSDimension) IsAuto

func (d CSSDimension) IsAuto() bool

func (CSSDimension) IsLength

func (d CSSDimension) IsLength() bool

func (CSSDimension) IsPercentage

func (d CSSDimension) IsPercentage() bool

type ClearSide

type ClearSide string

ClearSide controls which float exclusions an in-flow box clears.

const (
	ClearNone  ClearSide = ""
	ClearLeft  ClearSide = "left"
	ClearRight ClearSide = "right"
	ClearBoth  ClearSide = "both"
)

type ColumnFillMode added in v1.2.0

type ColumnFillMode string

ColumnFillMode selects balanced or sequential multicolumn flow.

const (
	ColumnFillBalance ColumnFillMode = ""
	ColumnFillAuto    ColumnFillMode = "auto"
)

type ComputedStyle

type ComputedStyle struct {
	BodyMargin                               float64 // uniform UA margin for the body element
	Display                                  Display // supported display mode
	FlexWrap                                 FlexWrapMode
	FlexDirection                            FlexDirection
	JustifyContent                           JustifyContent
	Float                                    FloatSide
	Clear                                    ClearSide
	Position                                 Position
	Top, Left, Right, Bottom                 float64
	HasTop, HasRight, HasBottom              bool
	Sizing                                   SizeConstraints
	BoxSizing                                BoxSizing
	GridCols                                 int // columns from grid-template-columns
	GridTracks                               []GridTrack
	GridRowHeights                           []float64 // fixed or auto heights from grid-template-rows
	GridRowFixedZeroTracks                   []bool    // distinguishes 0pt from auto
	GridAutoRowHeights                       []float64 // Cyclic pattern of implicit row heights (0 = auto or 0%).
	GridAutoRowFixedZeroTracks               []bool    // Distinguishes fixed zero-size tracks from auto tracks.
	GridAutoColumnWidths                     []float64 // Cyclic pattern of implicit column widths.
	GridAutoColumnPercentages                []float64 // Cyclic percentages for implicit tracks.
	GridAutoColumnPercentageTracks           []bool    // distinguishes 0% from auto
	GridAutoColumnFixedZeroTracks            []bool    // distinguishes 0pt from auto
	GridAutoColumnFractions                  []float64 // Cyclic fr factors for implicit tracks.
	GridAutoColumnMinimums                   []float64 // Fixed minimums for implicit minmax(..., fr) tracks.
	GridAutoColumnMinimumPercentages         []float64 // Percentage minimums for implicit fr tracks.
	GridAutoColumnAutomaticMinimums          []bool    // Automatic minimums for implicit fr tracks.
	GridAreas                                [][]string
	GridAutoFlowDense                        bool
	GridAutoFlowColumn                       bool
	RowGap, ColumnGap                        float64 // CSS gap in points
	RowGapPercent, ColumnGapPercent          float64 // percentage component of the gap
	ColCount                                 int     // column-count (0 = no columns)
	ColumnWidth                              float64 // column-width in points
	HasColumnWidth                           bool
	ColumnFill                               ColumnFillMode
	ColumnSpanAll                            bool // column-span: all
	ColumnRuleWidth                          float64
	ColumnRuleColor                          [4]float64
	AlignItems                               FlexAlignment // flex-start | center | flex-end | baseline | stretch
	AlignSelf                                FlexAlignment // auto | stretch | flex-start | center | flex-end | baseline
	FlexGrow                                 float64
	FlexBasis                                FlexBasis
	FlexShrink                               float64
	FlexShrinkSet                            bool
	ColSpan                                  int // number of spanned cells (1 = normal)
	RowSpan                                  int
	GridColumnLine                           int    // 1-based start line (negative counts from the end); 0 = auto.
	GridColumnEndLine                        int    // Optional 1-based end line.
	GridRowSpan                              int    // grid rows spanned by grid-row: span N
	GridRowLine                              int    // 1-based grid-row start line; 0 = auto.
	GridRowEndLine                           int    // Optional 1-based end line.
	GridAreaName                             string // Area name specified by grid-area.
	MarginTop, MarginRight                   float64
	MarginBottom, MarginLeft                 float64
	MarginAutoTop, MarginAutoRight           bool
	MarginAutoBottom, MarginAutoLeft         bool
	PaddingTop, PaddingRight                 float64
	PaddingBottom, PaddingLeft               float64
	ImageWidth, ImageHeight                  float64 // absolute CSS dimensions in points
	ImageWidthPct, ImageHeightPct            float64 // percentage CSS dimensions
	SVGWidth, SVGHeight                      float64 // CSS viewport dimensions in pt; 0 = intrinsic.
	SVGWidthPct                              float64 // percentage of the viewport width
	SVGHeightPct                             float64 // percentage of the viewport height
	HasBg                                    bool
	Bg                                       [4]float64 // background-color RGBA 0-1
	BorderWidth                              float64    // px, uniform border
	BorderCollapse                           bool
	TableBorderSpacingX, TableBorderSpacingY float64
	TableLayoutFixed                         bool
	TableColumnSpan                          int
	BorderCornerRadii                        [4]CornerRadius
	Border                                   [4]float64 // border color
	BorderTop, BorderRight                   BorderSide
	BorderBottom, BorderLeft                 BorderSide
	FontSize                                 float64 // pt; 0 = unset (layout default)
	HasColor                                 bool
	Color                                    [4]float64 // RGBA text color, values from 0 to 1
	Align                                    string     // left | center | right | justify
	FontFamily                               string     // first font-family entry
	FontFamilies                             []string   // ordered CSS font-family fallback list
	Bold                                     bool
	Italic                                   bool
	Underline                                bool
	Strike                                   bool
	LineHeight                               float64 // factor (0 = default 1.2)
	LineHeightPt                             float64 // absolute value in points (0 = unset; overrides the factor)
	LetterSpacing                            float64 // extra pt per rune (0 = normal)
	BreakBefore                              BreakBeforeMode
	BreakAfter                               BreakAfterMode
	Overflow                                 OverflowMode
	OverflowX                                OverflowMode // horizontal axis; unset inherits the shorthand
	OverflowY                                OverflowMode // vertical axis; unset inherits the shorthand
	Widows, Orphans                          int          // Minimum lines per fragment (0 = initial value 2).
	BreakInside                              BreakInsideMode
	Opacity                                  float64 // 0-1 (used when HasOpacity is true)
	HasOpacity                               bool
	WhiteSpace                               string // pre | pre-wrap | pre-line | nowrap ("" = normal)
	ListInside                               bool   // list-style-position: inside (default outside)
	ListMarkerShape                          string // circle | square | ""
	VAlign                                   string // sub | super | top | middle | bottom
	Transform                                string // uppercase | lowercase | capitalize ("" = none)
}

ComputedStyle stores the resolved CSS and user-agent values used for layout and painting. The Box tree stores the document's content, resources, and metadata.

type CornerRadius

type CornerRadius struct {
	X, Y RadiusLength
}

CornerRadius contains the horizontal and vertical radii of a corner.

type DimensionKind

type DimensionKind uint8

DimensionKind identifies whether the declared CSS size is auto, absolute or relative.

const (
	DimensionAuto DimensionKind = iota
	DimensionLength
	DimensionPercentage
)

type Display

type Display string

Display is the supported computed display mode.

const (
	DisplayNone             Display = "none"
	DisplayBlock            Display = "block"
	DisplayInline           Display = "inline"
	DisplayInlineBlock      Display = "inline-block"
	DisplayFlex             Display = "flex"
	DisplayGrid             Display = "grid"
	DisplayTable            Display = "table"
	DisplayInlineTable      Display = "inline-table"
	DisplayTableRowGroup    Display = "table-row-group"
	DisplayTableHeaderGroup Display = "table-header-group"
	DisplayTableFooterGroup Display = "table-footer-group"
	DisplayTableRow         Display = "table-row"
	DisplayTableColumnGroup Display = "table-column-group"
	DisplayTableColumn      Display = "table-column"
	DisplayTableCell        Display = "table-cell"
	DisplayTableCaption     Display = "table-caption"
)

type FlexAlignment

type FlexAlignment string

FlexAlignment is shared by align-items and align-self.

const (
	AlignUnset     FlexAlignment = ""
	AlignStretch   FlexAlignment = "stretch"
	AlignAuto      FlexAlignment = "auto"
	AlignCenter    FlexAlignment = "center"
	AlignFlexStart FlexAlignment = "flex-start"
	AlignFlexEnd   FlexAlignment = "flex-end"
	AlignBaseline  FlexAlignment = "baseline"
)

type FlexBasis

type FlexBasis struct {
	Kind  FlexBasisKind
	Value float64
}

FlexBasis preserves the authored flex-basis until the container size is known.

type FlexBasisKind

type FlexBasisKind uint8

FlexBasisKind describes how a Flex item's main-size basis is determined.

const (
	FlexBasisAuto FlexBasisKind = iota
	FlexBasisLength
	FlexBasisPercentage
	FlexBasisContent
)

type FlexDirection

type FlexDirection string

FlexDirection is the main axis of a Flex container.

const (
	FlexDirectionRow    FlexDirection = ""
	FlexDirectionColumn FlexDirection = "column"
)

type FlexWrapMode

type FlexWrapMode string

FlexWrapMode controls whether flex items may form additional lines.

const (
	FlexNoWrap FlexWrapMode = ""
	FlexWrap   FlexWrapMode = "wrap"
)

type FloatSide

type FloatSide string

FloatSide identifies which side receives a floated box.

const (
	FloatNone  FloatSide = ""
	FloatLeft  FloatSide = "left"
	FloatRight FloatSide = "right"
)

type GridTrack

type GridTrack struct {
	Fixed      float64 // fixed width in points
	Percent    float64 // percentage of the available width
	Fraction   float64 // unidad fr
	Min        float64 // fixed minimum for minmax(..., fr)
	MinPercent float64 // percentage minimum for minmax(..., fr)
	MinAuto    bool    // automatic minimum for fr
}

GridTrack describes a fixed or fractional grid track.

type JustifyContent

type JustifyContent string

JustifyContent distributes flex items along the main axis.

const (
	JustifyFlexStart    JustifyContent = ""
	JustifyCenter       JustifyContent = "center"
	JustifyFlexEnd      JustifyContent = "flex-end"
	JustifySpaceBetween JustifyContent = "space-between"
)

type Metadata

type Metadata struct {
	Title       string
	Author      string
	Description string
}

Metadata contains title, author, and description values from <head>.

type Options

type Options struct {
	Base     string
	Fetch    func(url string) ([]byte, error)
	MaxBytes int64
}

Options configures Build with a base URL, an optional fetch function, and a byte limit.

type OverflowMode

type OverflowMode string

OverflowMode preserves whether overflow is inherited or explicitly set.

const (
	OverflowUnset   OverflowMode = ""
	OverflowVisible OverflowMode = "visible"
	OverflowHidden  OverflowMode = "hidden"
)

type PageConfig

type PageConfig struct {
	Width, Height float64
	Margin        float64
	HasMargin     bool
	Margins       css.PageMargins
	Background    [4]float64
	HasBackground bool
	Rotation      int
	Bleed         float64
	HasBleed      bool
	Marks         css.PageMarks
	HasMarks      bool
}

PageConfig stores the @page size and margins in points. Zero values mean no rule was set.

type Part

type Part struct {
	Text            string
	Child           *Box
	ListMarkerShape string
}

loadFonts registers @font-face rules from style elements (local file or data URI). Part is an ordered segment of anonymous text or a child element.

type Position

type Position string

Position is the computed CSS positioning mode. Its zero value represents static flow.

const (
	PositionStatic   Position = ""
	PositionRelative Position = "relative"
	PositionAbsolute Position = "absolute"
	PositionFixed    Position = "fixed"
)

type RadiusLength

type RadiusLength struct {
	Value   float64
	Percent bool
}

RadiusLength stores an absolute or percentage CSS radius until the box size is known.

type SizeConstraints

type SizeConstraints struct {
	Width, Height        CSSDimension
	MinWidth, MaxWidth   CSSDimension
	MinHeight, MaxHeight CSSDimension
}

SizeConstraints keeps authored dimensions and axis limits together until layout resolves them.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL