sanitisehtml

package module
v0.0.0-...-8623e26 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2023 License: GPL-3.0 Imports: 4 Imported by: 0

README

Go Sanitise HTML

A simple library to sanitise untrusted HTML. Unwraps any element not in the white-list, and the same for attributes of allowed elements.

Usage

go get git.sr.ht/~gc/go-sanitise-html

You can sanitise a HTML string with the following example:

package main

import (
	"fmt"

	"git.sr.ht/~gc/go-sanitise-html"
)

func main() {
	s := sanitisehtml.New(map[string][]string{
		"p":   {},
		"div": {},
		"a":   {"href"},
	})
	safe, err := s.Sanitise("<div><p>Hello <strong>World</strong></p> - <a href=\"https://georgecraggs.co.uk\" target=\"_blank\">Link</a></div>")
	if err != nil {
		fmt.Printf("Error parsing: %s", err)
	}
	fmt.Println(safe)
	// "<div><p>Hello World</p> - <a href="https://georgecraggs.co.uk">Link</a></div>"
}

License

LGPL-3.0-only

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Sanitiser

type Sanitiser struct {
	// contains filtered or unexported fields
}

func New

func New(tags map[string][]string) *Sanitiser

func (*Sanitiser) AttributeAllowed

func (s *Sanitiser) AttributeAllowed(tag string, attribute string) bool

func (*Sanitiser) Sanitise

func (s *Sanitiser) Sanitise(h string) (string, error)

func (*Sanitiser) TagAllowed

func (s *Sanitiser) TagAllowed(tag string) bool

Jump to

Keyboard shortcuts

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