builder

package
v0.5.3 Latest Latest
Warning

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

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

Documentation

Overview

Package builder is a module used to write tests for ProseMirror. ProseMirror is a well-behaved rich semantic content editor based on contentEditable, with support for collaborative editing and custom document schemas.

This module provides helpers for building ProseMirror documents for tests. It's main file exports a basic schema with list support, and a number of functions, whose name mostly follows the corresponding HTML tag, to create nodes and marks in this schema. The prosemirror-test-builder/dist/build module exports a function that you can use to create such helpers for your own schema.

Node builder functions optionally take an attribute object as their first argument, followed by zero or more child nodes, and return a node with those attributes and children. Children should be either strings (for text nodes), existing nodes, or the result of calling a mark builder. For leaf nodes, you may also pass the builder function itself, without calling it. Mark builder functions work similarly, but return an object representing a set of nodes rather than a single node.

These builders help specifying and retrieving positions in the documents that you created (to avoid needing to count tokens when writing tests). Inside of strings passed as child nodes, angle-brackets <name> syntax can be used to place a tag called name at that position. The angle-bracketed part will not appear in the result node, but is stored in the node's tag property, which is an object mapping tag names to position integers. A string which is only a tag or set of tags may appear everywhere, even in places where text nodes aren't allowed.

So if you've imported doc and p from this module, the expression doc(p("foo<a>")) will return a document containing a single paragraph, and its .tag.a will hold the number 4 (the position at the end of the paragraph).

Index

Constants

This section is empty.

Variables

View Source
var (
	// Schema is the test schema itself.
	Schema = out["schema"].(*model.Schema)
	// Doc is a builder for top type of Schema (the document).
	Doc = out["doc"].(NodeBuilder)
	// P is a builder for paragraph nodes.
	P = out["p"].(NodeBuilder)
	// Blockquote is a builder for blockquote nodes.
	Blockquote = out["blockquote"].(NodeBuilder)
	// Pre is a builder for code block nodes.
	Pre = out["pre"].(NodeBuilder)
	// H1 is a builder for heading block nodes with the level attribute defaulting to 1.
	H1 = out["h1"].(NodeBuilder)
	// H2 is a builder for heading block nodes with the level attribute defaulting to 2.
	H2 = out["h2"].(NodeBuilder)
	// H3 is a builder for heading block nodes with the level attribute defaulting to 3.
	H3 = out["h3"].(NodeBuilder)
	// Li is a builder for list item nodes.
	Li = out["li"].(NodeBuilder)
	// Ul is a builder for bullet list nodes.
	Ul = out["ul"].(NodeBuilder)
	// Ol is a builder for ordered list nodes.
	Ol = out["ol"].(NodeBuilder)
	// Br is a builder for hard break nodes.
	Br = out["br"].(NodeBuilder)
	// Img is a builder for image nodes, with the src attribute defaulting to "img.png".
	Img = out["img"].(NodeBuilder)
	// Hr is a builder for horizontal rule nodes.
	Hr = out["hr"].(NodeBuilder)
	// A is a builder for link marks.
	A = out["a"].(MarkBuilder)
	// Em is a builder for em marks.
	Em = out["em"].(MarkBuilder)
	// Strong is a builder for strong marks.
	Strong = out["strong"].(MarkBuilder)
	// Code is a builder for code marks.
	Code = out["code"].(MarkBuilder)
)

Functions

func Builders

func Builders(schema *model.Schema, names map[string]Spec) map[string]interface{}

Builders can be called with a schema and an optional object of renamed/configured builders to create a object of builders for a custom schema. It will return an object with a schema property and one builder for each node and mark in the schema. The second argument can be used to add custom builders—if given, it should be an object mapping names to attribute objects, which may contain a nodeType or markType property to specify which node or mark the builder by this name should create.

Types

type MarkBuilder

type MarkBuilder func(args ...interface{}) Result

MarkBuilder functions work similarly to NodeBuilder, but return an object representing a set of nodes rather than a single node.

type NodeBuilder

type NodeBuilder func(args ...interface{}) NodeWithTag

NodeBuilder functions optionally take an attribute object as their first argument, followed by zero or more child nodes, and return a node with those attributes and children. Children should be either strings (for text nodes), existing nodes, or the result of calling a mark builder. For leaf nodes, you may also pass the builder function itself, without calling it.

type NodeWithTag

type NodeWithTag struct {
	*model.Node
	Tag map[string]int
}

NodeWithTag is what is returned by a NodeBuilder.

type Result

type Result struct {
	Nodes []*model.Node
	Flat  []*model.Node
	Tag   map[string]int
}

Result is what is returned by a MarkBuilder.

type Spec

type Spec map[string]interface{}

Spec can be used to add custom builders—if given, it should be an object mapping names to attribute objects, which may contain a nodeType or markType property to specify which node or mark the builder by this name should create.

Jump to

Keyboard shortcuts

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