CodeSampleX

Beispiel

golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e: html.ElementNode

Verifiziertes Beispiel für golang golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e: html.ElementNode. Der Vertrag lief auf go 1.26 · linux debian/x64 …

sha256:dacab5fd7faa4781d416730f4591d44429ea064c1931cd1f7cb769fcc0a7957d

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
1
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung linux 24 · ubuntu · glibc 2.39 x64 go

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-10-21

Fall

HOW
Ziel
verify golang.org/x/net/html.ElementNode in pkg:golang/golang.org/x/net@v0.0.0-20200324143707-d3edc9973b7e
Pakete
Symbole
  • golang.org/x/net/html.ElementNode
Erstellt
2026-10-21T08:22:00Z

Contract

  1. html.ElementNode constant distinguishes element nodes from text, document, and comment nodes
  2. html.Parse parses HTML markup into a tree where elements have Type html.ElementNode and Data equal to tag name
  3. Constructing an html.ElementNode with attributes and children renders valid HTML tags via html.Render
  4. Tree traversal inspects Type == html.ElementNode to filter element tags and extract attributes
  5. html.ParseFragment parses snippet in element context and returns nodes with Type html.ElementNode

Dateien

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • sample.go
  • spec.json
  • test/contract.go

Quellartefakt herunterladen (tar.gz)

Quelltext

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify golang.org/x/net/html.ElementNode in pkg:golang/golang.org/x/net@v0.0.0-20200324143707-d3edc9973b7e
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/net@v0.0.0-20200324143707-d3edc9973b7e
Demonstrate these symbols/APIs:
  - golang.org/x/net/html.ElementNode

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:7d032e3f93c77990c6fe576319af8624c125cafced107f789eb0af314e46a68e","contract":["html.ElementNode constant distinguishes element nodes from text, document, and comment nodes","html.Parse parses HTML markup into a tree where elements have Type html.ElementNode and Data equal to tag name","Constructing an html.ElementNode with attributes and children renders valid HTML tags via html.Render","Tree traversal inspects Type == html.ElementNode to filter element tags and extract attributes","html.ParseFragment parses snippet in element context and returns nodes with Type html.ElementNode"],"goal":"verify golang.org/x/net/html.ElementNode in pkg:golang/golang.org/x/net@v0.0.0-20200324143707-d3edc9973b7e","kind":"HOW","packages":["pkg:golang/golang.org/x/net@v0.0.0-20200324143707-d3edc9973b7e"],"schemaVersion":1,"symbols":["golang.org/x/net/html.ElementNode"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/golang.org/x/net@v0.0.0-20200324143707-d3edc9973b7e"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/net@v0.0.0-20200324143707-d3edc9973b7e","symbols":["golang.org/x/net/html.ElementNode"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.21

require golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e
go.sum
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
sample.go
package sample

import (
	"bytes"
	"fmt"
	"strings"

	"golang.org/x/net/html"
	"golang.org/x/net/html/atom"
)

// FindElementsByTag traverses the node tree recursively and returns all nodes with Type == html.ElementNode and matching tag name.
func FindElementsByTag(root *html.Node, tag string) []*html.Node {
	var results []*html.Node
	var traverse func(*html.Node)
	traverse = func(n *html.Node) {
		if n == nil {
			return
		}
		if n.Type == html.ElementNode && strings.EqualFold(n.Data, tag) {
			results = append(results, n)
		}
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			traverse(c)
		}
	}
	traverse(root)
	return results
}

// FindFirstElement traverses the node tree and returns the first node with Type == html.ElementNode.
func FindFirstElement(root *html.Node) (*html.Node, bool) {
	var found *html.Node
	var traverse func(*html.Node)
	traverse = func(n *html.Node) {
		if found != nil || n == nil {
			return
		}
		if n.Type == html.ElementNode {
			found = n
			return
		}
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			traverse(c)
		}
	}
	traverse(root)
	return found, found != nil
}

// CreateElement creates a new html.ElementNode with the given tag, attributes, and text child.
func CreateElement(tag string, dataAtom atom.Atom, attrs []html.Attribute, textContent string) *html.Node {
	elem := &html.Node{
		Type:     html.ElementNode,
		Data:     tag,
		DataAtom: dataAtom,
		Attr:     attrs,
	}
	if textContent != "" {
		elem.AppendChild(&html.Node{
			Type: html.TextNode,
			Data: textContent,
		})
	}
	return elem
}

// RenderNode serializes an html.Node tree into an HTML string.
func RenderNode(n *html.Node) (string, error) {
	var buf bytes.Buffer
	if err := html.Render(&buf, n); err != nil {
		return "", fmt.Errorf("failed to render HTML node: %w", err)
	}
	return buf.String(), nil
}

// ParseHTML parses an HTML string and returns the root document node.
func ParseHTML(htmlStr string) (*html.Node, error) {
	doc, err := html.Parse(strings.NewReader(htmlStr))
	if err != nil {
		return nil, fmt.Errorf("failed to parse HTML: %w", err)
	}
	return doc, nil
}

// ParseFragment parses an HTML snippet in the context of a context element node.
func ParseFragment(contextElem *html.Node, htmlSnippet string) ([]*html.Node, error) {
	nodes, err := html.ParseFragment(strings.NewReader(htmlSnippet), contextElem)
	if err != nil {
		return nil, fmt.Errorf("failed to parse HTML fragment: %w", err)
	}
	return nodes, nil
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/net/html.ElementNode in pkg:golang/golang.org/x/net@v0.0.0-20200324143707-d3edc9973b7e",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/net@v0.0.0-20200324143707-d3edc9973b7e"
  ],
  "symbols": [
    "golang.org/x/net/html.ElementNode"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"strings"

	"golang.org/x/net/html"
	"golang.org/x/net/html/atom"
	"sample"
)

func main() {
	// Assertion 1: html.ElementNode constant distinguishes element nodes from text, document, and comment nodes
	if html.ElementNode == html.TextNode || html.ElementNode == html.DocumentNode || html.ElementNode == html.CommentNode {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 1 failed: html.ElementNode NodeType is not unique\n")
		os.Exit(1)
	}
	sampleNode := &html.Node{Type: html.ElementNode, Data: "div"}
	if sampleNode.Type != html.ElementNode {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 1 failed: node Type does not match html.ElementNode\n")
		os.Exit(1)
	}

	// Assertion 2: html.Parse parses HTML markup into a tree where elements have Type html.ElementNode and Data equal to tag name
	htmlInput := `<!DOCTYPE html><html><head><title>Test Page</title></head><body><div id="main" class="box"><p>Hello World</p><button disabled>Click</button></div></body></html>`
	doc, err := sample.ParseHTML(htmlInput)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 2 failed to parse HTML: %v\n", err)
		os.Exit(1)
	}

	pNodes := sample.FindElementsByTag(doc, "p")
	if len(pNodes) != 1 {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 2 failed: expected 1 p element, got %d\n", len(pNodes))
		os.Exit(1)
	}
	if pNodes[0].Type != html.ElementNode || pNodes[0].Data != "p" {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 2 failed: p node type or data mismatch: type=%v, data=%v\n", pNodes[0].Type, pNodes[0].Data)
		os.Exit(1)
	}

	buttonNodes := sample.FindElementsByTag(doc, "button")
	if len(buttonNodes) != 1 {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 2 failed: expected 1 button element, got %d\n", len(buttonNodes))
		os.Exit(1)
	}
	if buttonNodes[0].Type != html.ElementNode || buttonNodes[0].Data != "button" {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 2 failed: button node type or data mismatch: type=%v, data=%v\n", buttonNodes[0].Type, buttonNodes[0].Data)
		os.Exit(1)
	}

	// Assertion 3: Constructing an html.ElementNode with attributes and children renders valid HTML tags via html.Render
	cardElem := sample.CreateElement("section", atom.Section, []html.Attribute{
		{Key: "class", Val: "card-container"},
		{Key: "data-role", Val: "widget"},
	}, "")
	headerElem := sample.CreateElement("h2", atom.H2, nil, "Card Title")
	bodyElem := sample.CreateElement("div", atom.Div, []html.Attribute{{Key: "class", Val: "card-body"}}, "Card description text.")
	cardElem.AppendChild(headerElem)
	cardElem.AppendChild(bodyElem)

	rendered, err := sample.RenderNode(cardElem)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 3 failed to render element node: %v\n", err)
		os.Exit(1)
	}
	expectedSubstrings := []string{
		`<section class="card-container" data-role="widget">`,
		`<h2>Card Title</h2>`,
		`<div class="card-body">Card description text.</div>`,
		`</section>`,
	}
	for _, sub := range expectedSubstrings {
		if !strings.Contains(rendered, sub) {
			fmt.Fprintf(os.Stderr, "FAIL: Assertion 3 failed: rendered HTML missing expected substring %q: got %s\n", sub, rendered)
			os.Exit(1)
		}
	}

	// Assertion 4: Tree traversal inspects Type == html.ElementNode to filter element tags and extract attributes
	divNodes := sample.FindElementsByTag(doc, "div")
	if len(divNodes) != 1 {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 4 failed: expected 1 div element, got %d\n", len(divNodes))
		os.Exit(1)
	}
	divElem := divNodes[0]
	if divElem.Type != html.ElementNode || divElem.Data != "div" {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 4 failed: divElem type or tag mismatch\n")
		os.Exit(1)
	}
	var classVal string
	for _, attr := range divElem.Attr {
		if attr.Key == "class" {
			classVal = attr.Val
		}
	}
	if classVal != "box" {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 4 failed: expected class 'box', got %q\n", classVal)
		os.Exit(1)
	}

	// Assertion 5: html.ParseFragment parses snippet in element context and returns nodes with Type html.ElementNode
	contextNode := &html.Node{
		Type:     html.ElementNode,
		Data:     "div",
		DataAtom: atom.Div,
	}
	fragmentNodes, err := sample.ParseFragment(contextNode, `<span class="badge">Badge 1</span><span class="badge">Badge 2</span>`)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 5 failed to parse fragment: %v\n", err)
		os.Exit(1)
	}
	if len(fragmentNodes) != 2 {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 5 failed: expected 2 fragment nodes, got %d\n", len(fragmentNodes))
		os.Exit(1)
	}
	for i, fn := range fragmentNodes {
		if fn.Type != html.ElementNode || fn.Data != "span" {
			fmt.Fprintf(os.Stderr, "FAIL: Assertion 5 failed: fragment node %d is not ElementNode span: type=%v, data=%v\n", i, fn.Type, fn.Data)
			os.Exit(1)
		}
	}

	fmt.Println("PASS: all golang.org/x/net/html.ElementNode contract assertions passed")
}

Ursprungs-Seeder

anonym