Code Monkey home page Code Monkey logo

htmlmatch's Introduction

htmlmatch

Library used to match elements and attributes inside HTML. The patterns are plain HTML with only those elements and attributes that should be compared. That is, elements and attributes can be omitted if they don't need to match. The HTML elements must match exactly in both order and structure, but attributes are not sensitive to order.

Motivation

Make it easy to write tests that verifies the presence of of essential HTML elements and attributes. Use it to verify htmx attributes and build confidence that the application keeps working, without comprehensive browser testing tools such as chromedp.

Text matching

Whitespace are trimmed by default before matching text. It is also possible to match verbatim by prepending the pattern text by verbatim:. Substring matching is supported by prepending the pattern with substring:.

HTML parsing

https://pkg.go.dev/golang.org/x/net/html can be used to parse HTML, but the results can be confusing since it follows complicated rules for HTML5. This library provides a primitive alternative ParseVerbatim to make it easy to parse patterns without surprises.

Example

func ExampleContainsTree() {
        full := MustParseVerbatim(`<html>
                <body>
                        <div hx-test="/foo" hx-target="#mytarget">Click here</div>
                        <div>lorem ipsum</div>
                        <div>lorem <span id="mytarget">ipsum</span></div>
                </body>
                </html>`)
        pattern := MustParseVerbatim(`
                <div hx-target="#mytarget"></div>
                <span id="mytarget" />`)
        fmt.Println(ContainsTree(full, pattern))
        // Output: true
}

htmlmatch's People

Contributors

larschri avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.