engine

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package engine is the platform-independent core of the WebAssembly playground. It wraps the htmlpolicy API in a JSON-friendly interface so the js/wasm glue in the parent playground package stays a thin, logic-free layer. Everything here is ordinary Go, fully covered by tests on every platform.

Index

Constants

This section is empty.

Variables

View Source
var Verbs = []Verb{
	{"strip-tag", "SELECTOR[,...]", "remove element and all content"},
	{"comment-out-tag", "SELECTOR[,...]", "wrap element in an HTML comment"},
	{"placeholder-tag", "SELECTOR[,...]", "replace element with a [removed] label"},
	{"demote-tag", "SELECTOR[,...]", "convert element to <div>/<span>"},
	{"allow-tag", "SELECTOR[,...]", "keep element (attributes filtered separately)"},
	{"unwrap-tag", "SELECTOR[,...]", "remove the element's tags but keep children"},
	{"strip-attr", "SELECTOR ATTR[,...]", "strip named attributes"},
	{"allow-attr", "SELECTOR ATTR[,...]", "allow only named attributes"},
	{"defang-attr", "SELECTOR ATTR[,...]", "rename attributes to an inert prefix"},
	{"allow-scheme", "SELECTOR ATTR SCHEME[,...]", "allow URLs with listed schemes"},
	{"strip-scheme", "SELECTOR ATTR SCHEME[,...]", "strip URLs with listed schemes"},
	{"defang-scheme", "SELECTOR ATTR SCHEME[,...]", "neutralize URLs when scheme matches"},
	{"allow-content-type", "SELECTOR ATTR TYPE[,...]", "allow data: URIs with listed MIME types"},
	{"strip-content-type", "SELECTOR ATTR TYPE[,...]", "strip data: URIs with listed MIME types"},
	{"defang-content-type", "SELECTOR ATTR TYPE[,...]", "rename attr when data: MIME type matches"},
	{"css-strip", "PROP[,...]", "strip CSS properties"},
	{"css-allow", "PROP[,...]", "allow CSS properties"},
	{"css-defang", "PROP[,...]", "defang CSS properties (prefix name)"},
	{"css-strip-value", "PATTERN", "strip properties matching a value pattern"},
	{"css-defang-value", "PATTERN", "defang properties matching a value pattern"},
	{"css-strip-at", "NAME[,...]", "strip CSS at-rules"},
	{"css-allow-at", "NAME[,...]", "allow CSS at-rules"},
	{"css-defang-at", "NAME[,...]", "defang CSS at-rules (prefix name)"},
	{"css-strip-scheme", "TARGET SCHEME[,...]", "strip CSS url() values by scheme"},
	{"css-allow-scheme", "TARGET SCHEME[,...]", "allow CSS url() values by scheme"},
	{"css-defang-scheme", "TARGET SCHEME[,...]", "defang CSS url() values by scheme"},
	{"css-strip-content-type", "TARGET TYPE[,...]", "strip CSS url() data: URIs by MIME type"},
	{"css-allow-content-type", "TARGET TYPE[,...]", "allow CSS url() data: URIs by MIME type"},
	{"css-defang-content-type", "TARGET TYPE[,...]", "defang CSS url() data: URIs by MIME type"},
	{"css-strip-pseudo", "NAME[,...]", "strip selectors using a pseudo-class/element"},
	{"css-allow-pseudo", "NAME[,...]", "allow pseudo-classes/elements"},
	{"css-strip-media", "FEATURE[:VALUE][,...]", "strip @media blocks by media feature"},
	{"css-allow-media", "FEATURE[:VALUE][,...]", "allow @media blocks by media feature"},
	{"strip-comments", "", "remove all HTML comments"},
	{"prefix", "NAME", "set the prefix for defang/comment-out actions"},
	{"placeholder-label", "TEXT", "set the placeholder-tag label text"},
	{"include", "NAME", "include another policy (builtin:NAME for presets)"},
}

Verbs lists every policy verb and directive, in the order they appear in the policy language reference. TestVerbsParse asserts that a sample line for each entry parses, so this table cannot drift from the real grammar.

Functions

func PresetsJSON

func PresetsJSON() string

PresetsJSON returns the built-in presets as JSON, for the js/wasm glue.

func RunJSON

func RunJSON(policyText, input, mode string, verbose bool) string

RunJSON is Run with the Result marshaled to JSON, for the js/wasm glue.

func SyntaxJSON

func SyntaxJSON() string

SyntaxJSON returns the policy language metadata as JSON, for the js/wasm glue (playground autocompletion).

Types

type Presets

type Presets struct {
	Names    []string          `json:"names"`
	Policies map[string]string `json:"policies"`
}

Presets lists the built-in policy presets for the preset picker.

type Result

type Result struct {
	// Output is the sanitized content. Empty when ParseError or
	// ApplyError is set.
	Output string `json:"output"`
	// Modified reports whether policy rules changed the content (not
	// whether the output bytes differ from the input).
	Modified bool `json:"modified"`
	// ParseError is the policy parse error, if any. When set, no other
	// field except LintWarnings (empty) is meaningful.
	ParseError string `json:"parseError,omitempty"`
	// ApplyError is the error from applying the policy, if any.
	// LintWarnings and CompiledPolicy are still populated so the UI can
	// show live policy feedback even when the input is broken.
	ApplyError string `json:"applyError,omitempty"`
	// LintWarnings are the policy lint findings (never null in JSON).
	LintWarnings []Warning `json:"lintWarnings"`
	// Log is the verbose action log, empty unless verbose was requested.
	Log string `json:"log"`
	// CompiledPolicy is the fully-inlined compiled policy (Policy.String).
	CompiledPolicy string `json:"compiledPolicy"`
}

Result is the JSON-serializable outcome of one playground run.

func Run

func Run(policyText, input, mode string, verbose bool) Result

Run parses policyText, lints it, and applies it to input in the given mode ("fragment", "document", "css", or "inline-css"). A panic anywhere in the pipeline degrades to an ApplyError instead of propagating, so a library bug cannot kill the long-lived wasm instance.

type Verb

type Verb struct {
	Name string `json:"name"`
	Args string `json:"args"`
	Doc  string `json:"doc"`
}

Verb describes one policy verb or directive for the playground's autocompletion. Args is a human-readable argument hint (uppercase placeholders, mirroring the package godoc); Doc is a one-line description shown next to the suggestion.

type Warning

type Warning struct {
	Line    int    `json:"line"`
	Message string `json:"message"`
}

Warning is a policy lint warning.

Jump to

Keyboard shortcuts

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