Documentation
¶
Index ¶
- Variables
- func GeneratedTemplate(f func(GeneratedComponentInput) error) templ.Component
- func GetBuilder() (sb strings.Builder)
- func GetDevModeTextFileName(templFileName string) string
- func ReleaseBuffer(w io.Writer) (err error)
- func RenderAttributes(ctx context.Context, w io.Writer, scriptWriter io.Writer, elementName string, ...) (err error)
- func SanitizeAttributeName(name string) string
- func SanitizeStyleAttributeValues(values ...any) (string, error)
- func ScriptContentInsideRegexp[T any](v T, errs ...error) (string, error)
- func ScriptContentInsideStringLiteral[T any](v T, errs ...error) (string, error)
- func ScriptContentInsideTemplateLiteral[T any](v T, errs ...error) (string, error)
- func ScriptContentOutsideStringLiteral[T any](v T, errs ...error) (string, error)
- func WriteString(w io.Writer, index int, s string) (err error)
- type AttributeExpression
- func (e AttributeExpression[T]) Resolve(ctx context.Context) (string, error)
- func (e AttributeExpression[T]) ResolveAnimationValue(ctx context.Context) (string, error)
- func (e AttributeExpression[T]) ResolveDynamic(ctx context.Context, scriptWriter io.Writer, elementName, name string) (string, error)
- func (e AttributeExpression[T]) ResolveSrcset(ctx context.Context) (string, error)
- func (e AttributeExpression[T]) ResolveURL(ctx context.Context) (string, error)
- type Buffer
- type GeneratedComponentInput
- type SrcdocExpression
- type StringLoader
Constants ¶
This section is empty.
Variables ¶
var DefaultBufferSize = 4 * 1024 // 4KB
DefaultBufferSize is the default size of buffers. It is set to 4KB by default, which is the same as the default buffer size of bufio.Writer.
var ErrInvalidStyleAttributeFunctionSignature = errors.New("invalid function signature, should be in the form func() (string, error)")
var TemplUnsupportedStyleAttributeValue = "zTemplUnsupportedStyleAttributeValue:Invalid;"
TemplUnsupportedStyleAttributeValue is the default value returned for unsupported types.
Functions ¶
func GeneratedTemplate ¶
func GeneratedTemplate(f func(GeneratedComponentInput) error) templ.Component
GeneratedTemplate is used to avoid generated code needing to import the `context` and `io` packages.
func GetDevModeTextFileName ¶
func ReleaseBuffer ¶
ReleaseBuffer flushes the buffer and returns it to the pool.
func RenderAttributes ¶ added in v0.3.1100
func RenderAttributes(ctx context.Context, w io.Writer, scriptWriter io.Writer, elementName string, attributes templ.Attributer) (err error)
RenderAttributes renders spread attributes on the named element. Attribute names that would change the structure of the element are replaced with templ.FailedSanitizationAttributeName. Attribute values are escaped and sanitized in the same way as attributes with constant names, except that event handler values that are not a templ.ComponentScript are replaced with templ.FailedSanitizationJS. The functions of templ.ComponentScript values are rendered to scriptWriter, which generated code writes before the opening tag of the element, so that the functions are defined before the element calls them.
func SanitizeAttributeName ¶ added in v0.3.1100
SanitizeAttributeName returns name if it can be written as an HTML attribute name, or templ.FailedSanitizationAttributeName if it can not.
func SanitizeStyleAttributeValues ¶
SanitizeStyleAttributeValues renders a style attribute value. The supported types are: - string - templ.SafeCSS - map[string]string - map[string]templ.SafeCSSProperty - templ.KeyValue[string, string] - A map of key/values where the key is the CSS property name and the value is the CSS property value. - templ.KeyValue[string, templ.SafeCSSProperty] - A map of key/values where the key is the CSS property name and the value is the CSS property value. - templ.KeyValue[string, bool] - The bool determines whether the value should be included. - templ.KeyValue[templ.SafeCSS, bool] - The bool determines whether the value should be included. - func() (anyOfTheAboveTypes) - func() (anyOfTheAboveTypes, error) - []anyOfTheAboveTypes
In the above, templ.SafeCSS and templ.SafeCSSProperty are types that are used to indicate that the value is safe to render as CSS without sanitization. All other types are sanitized before rendering.
If an error is returned by any function, or a non-nil error is included in the input, the error is returned.
func ScriptContentInsideRegexp ¶ added in v0.3.1100
ScriptContentInsideRegexp escapes v for the pattern of a JavaScript regular expression literal, so that it matches itself, and can not end the regular expression.
func ScriptContentInsideTemplateLiteral ¶ added in v0.3.1100
Types ¶
type AttributeExpression ¶ added in v0.3.1100
type AttributeExpression[T attributeValue] struct {
Value T
Err error
}
AttributeExpression is the result of an attribute expression. Generated code creates one with NewAttributeExpression, so that an expression that returns a value and an error, e.g. { getURL() }, can be resolved by a method that also takes a context.
func NewAttributeExpression ¶ added in v0.3.1100
func NewAttributeExpression[T attributeValue](v T, errs ...error) AttributeExpression[T]
NewAttributeExpression returns the result of an attribute expression.
func (AttributeExpression[T]) Resolve ¶ added in v0.3.1100
func (e AttributeExpression[T]) Resolve(ctx context.Context) (string, error)
Resolve converts the value to an HTML-escaped string. A templ.ComponentScript is converted to its function call.
func (AttributeExpression[T]) ResolveAnimationValue ¶ added in v0.3.1100
func (e AttributeExpression[T]) ResolveAnimationValue(ctx context.Context) (string, error)
ResolveAnimationValue converts the value of an SVG animation attribute, such as the to attribute of a set element, to an HTML-escaped string. SVG animations can set the href of their parent element, so each item in the semicolon separated list is sanitized with templ.URL. A templ.SafeURL is not sanitized.
func (AttributeExpression[T]) ResolveDynamic ¶ added in v0.3.1100
func (e AttributeExpression[T]) ResolveDynamic(ctx context.Context, scriptWriter io.Writer, elementName, name string) (string, error)
ResolveDynamic converts the value of an attribute with a name that is only known at render time to an HTML-escaped string. The value is sanitized based on the name, in the same way as the value of a spread attribute. If the value is a templ.ComponentScript, its function is rendered to scriptWriter, which generated code writes before the opening tag of the element, so that the function is defined before the element calls it.
func (AttributeExpression[T]) ResolveSrcset ¶ added in v0.3.1100
func (e AttributeExpression[T]) ResolveSrcset(ctx context.Context) (string, error)
ResolveSrcset converts the value of a srcset attribute, which holds a comma separated list of image candidates, to an HTML-escaped string. The URL of each candidate is sanitized with templ.URL. A templ.SafeURL is not sanitized.
func (AttributeExpression[T]) ResolveURL ¶ added in v0.3.1100
func (e AttributeExpression[T]) ResolveURL(ctx context.Context) (string, error)
ResolveURL converts the value of an attribute that contains a URL to an HTML-escaped string. A templ.SafeURL is not sanitized. All other values are sanitized with templ.URL.
type Buffer ¶
Buffer is a wrapper around bufio.Writer that enables flushing and closing of the underlying writer.
func GetBuffer ¶
GetBuffer creates and returns a new buffer if the writer is not already a buffer, or returns the existing buffer if it is.
func (*Buffer) Close ¶
Close closes the buffer and the underlying io.Writer if it implements io.Closer.
func (*Buffer) Flush ¶
Flush writes any buffered data to the underlying io.Writer and calls the Flush method of the underlying http.Flusher if it implements it.
type GeneratedComponentInput ¶
GeneratedComponentInput is used to avoid generated code needing to import the `context` and `io` packages.
type SrcdocExpression ¶ added in v0.3.1100
SrcdocExpression is the result of the expression of an iframe srcdoc attribute. Unlike AttributeExpression, the value can be a templ.Component.
func NewSrcdocExpression ¶ added in v0.3.1100
func NewSrcdocExpression[T any](v T, errs ...error) SrcdocExpression[T]
NewSrcdocExpression returns the result of the expression of an iframe srcdoc attribute.
func (SrcdocExpression[T]) Resolve ¶ added in v0.3.1100
func (e SrcdocExpression[T]) Resolve(ctx context.Context) (string, error)
Resolve converts the value to an HTML-escaped string. The browser parses the value as an HTML document, so a templ.Component is rendered as HTML, while strings, booleans, and numbers are escaped as text. Other types return an error.
type StringLoader ¶
type StringLoader struct {
// contains filtered or unexported fields
}
func NewStringLoader ¶
func NewStringLoader(devModeWatchRootPath string) (sl *StringLoader)