Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/djthorpe/go-dom
Document Object Model (DOM) implementation for backend and frontend.
https://github.com/djthorpe/go-dom
dom golang js wasm
Last synced: about 1 month ago
JSON representation
Document Object Model (DOM) implementation for backend and frontend.
- Host: GitHub
- URL: https://github.com/djthorpe/go-dom
- Owner: djthorpe
- License: apache-2.0
- Created: 2021-08-12T12:31:11.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-10-11T21:19:26.000Z (over 1 year ago)
- Last Synced: 2024-12-11T14:23:05.395Z (about 2 months ago)
- Topics: dom, golang, js, wasm
- Language: Go
- Homepage:
- Size: 48.8 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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](https://tinygo.org/) 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:```go
package mainimport (
. "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:
```bash
git clone [email protected]: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:
```bash
git clone [email protected]:djthorpe/go-dom.git
cd go-dom
GOOS=js GOARCH=wasm go build -o build/helloworld.wasm ./cmd/wasm/helloworld
```See the instructions [here](https://github.com/golang/go/wiki/WebAssembly) for running
WASM within a web browser. There is some helper code which allows you to run it
from the command line:```bash
git clone [email protected]: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`](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:```bash
git clone [email protected]:djthorpe/go-dom.git
cd go-dom
make test && make jstest
```Testing in a WASM environment uses [wasmbrowsertest](https://github.com/agnivade/wasmbrowsertest). Please see the documentation for that package for more information
on testing in the WASM environment and browser support.