dom

package module
v0.0.0-...-6f140b9 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2021 License: Apache-2.0 Imports: 2 Imported by: 0

README

go-dom

Experimental! Please note this repository contains code which depends on experimental features of the Go language.

Implements the document object model (DOM) for Go - which is runnable both in a native go environment or via a web browser when compiling for WASM target.

Presently go version 1.17 has been tested. Tinygo should eventually be supported in order to facilitate smaller binary sizes.

Hello, World

In order to access the current HTML document, access the window object. You can dump the current document contents from the root element, or set the title of the document:

package main

import (
  . "github.com/djthorpe/go-dom/pkg/dom"
)

func main() {
  window := GetWindow()
  window.Document().SetTitle("Hello, World!")
  fmt.Println(window.Document().DocumentElement().OuterHTML())
}

To compile and run this example natively, run:

git clone git@github.com:djthorpe/go-dom.git
cd go-dom
go run ./cmd/wasm/helloworld

To run this within a web browser, you'll need to compile it to WASM instead:

git clone git@github.com:djthorpe/go-dom.git
cd go-dom
GOOS=js GOARCH=wasm go build -o build/helloworld.wasm ./cmd/wasm/helloworld

See the instructions here for running WASM within a web browser. There is some helper code which allows you to run it from the command line:

git clone git@github.com:djthorpe/go-dom.git
cd go-dom
make httpserver cmd/wasm/helloworld
cd build && ./httpserver -port :9090 

Then you can simply view the page at http://localhost:9090/helloworld.html and check the console log to see the output.

Running Tests

You should run tests both in the native go environment and in the WASM environment. For the latter, Google Chrome needs to be installed (other browsers may work but have not been tested). The commands for testing are:

git clone git@github.com:djthorpe/go-dom.git
cd go-dom
make test && make jstest

Testing in a WASM environment uses wasmbrowsertest. Please see the documentation for that package for more information on testing in the WASM environment and browser support.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attr

type Attr interface {
	Node

	// Properties
	OwnerElement() Element
	Name() string
	Value() string
	SetValue(string)
}

type Comment

type Comment interface {
	Node

	// Properties
	Data() string
	Length() int
}

type Document

type Document interface {
	Node

	// Properties
	Body() Element
	//CharacterSet() string
	//ContentType() string
	Doctype() DocumentType
	DocumentElement() Element

	// Methods
	CreateElement(string) Element
	CreateAttribute(string) Attr
	CreateComment(string) Comment
	CreateTextNode(string) Text
}

Document implements https://developer.mozilla.org/en-US/docs/Web/API/Document

type DocumentType

type DocumentType interface {
	Node

	// Properties
	Name() string
	PublicId() string
	SystemId() string
}

Document implements https://developer.mozilla.org/en-US/docs/Web/API/DocumentType

type Element

type Element interface {
	Node

	// Properties
	InnerHTML() string
	OuterHTML() string
	TagName() string
	Attributes() []Attr

	// Methods
	//RemoveAttrbute(string)
	//RemoveAttributeNode(Attr) Attr
	SetAttribute(string, string) Attr
	//SetAttributeNode(Attr) Attr
	//GetAttribute(string) string
	//GetAttributeNames() []string
	//GetAttributeNode(string) Attr
	//HasAttribute(string) bool
	HasAttributes() bool
}

type Error

type Error uint
const (
	ErrSuccess Error = iota
	ErrBadParameter
	ErrDuplicateEntry
	ErrUnexpectedResponse
	ErrNotFound
	ErrNotModified
	ErrInternalAppError
	ErrNotImplemented
)

func (Error) Error

func (e Error) Error() string

func (Error) With

func (e Error) With(args ...interface{}) error

type Node

type Node interface {
	// Properties
	ChildNodes() []Node
	Contains(Node) bool
	Equals(Node) bool
	FirstChild() Node
	HasChildNodes() bool
	IsConnected() bool
	LastChild() Node
	NextSibling() Node
	NodeName() string
	NodeType() NodeType
	OwnerDocument() Document
	ParentElement() Element
	ParentNode() Node
	PreviousSibling() Node
	TextContent() string

	// Methods
	AppendChild(Node) Node
	CloneNode(bool) Node
	InsertBefore(Node, Node) Node
	RemoveChild(Node)
	ReplaceChild(Node, Node)
}

Node implements https://developer.mozilla.org/en-US/docs/Web/API/Node

type NodeType

type NodeType int
const (
	UNKNOWN_NODE NodeType = iota
	ELEMENT_NODE
	ATTRIBUTE_NODE
	TEXT_NODE
	CDATA_SECTION_NODE
	ENTITY_REFERENCE_NODE
	ENTITY_NODE
	PROCESSING_INSTRUCTION_NODE
	COMMENT_NODE
	DOCUMENT_NODE
	DOCUMENT_TYPE_NODE
	DOCUMENT_FRAGMENT_NODE
	NOTATION_NODE
)

func (NodeType) String

func (t NodeType) String() string

type Text

type Text interface {
	Node

	// Properties
	Data() string
	Length() int
}

type Window

type Window interface {
	// Properties
	Document() Document

	// Methods
	Write(io.Writer, Node) (int, error)
	Read(io.Reader, string) (Document, error)
}

Directories

Path Synopsis
cmd
pkg
dom

Jump to

Keyboard shortcuts

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