CodeSampleX

Exemplo

golang.org/x/net v0.61.0: html.ElementNode

Amostra verificada para golang golang.org/x/net v0.61.0: html.ElementNode. O contrato rodou em go 1.26 · linux debian/x64 · docker e passou.

sha256:ac259edf22ed6ed2e94f1bce4f5bbaa40742f6f7c52ac8c11f5c36e91409ba4d

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado go 1.26 linux 24 · ubuntu · glibc 2.39 x64 go 1.26 go 1.26 go 1

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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-11

Caso

HOW
Objetivo
verify golang.org/x/net/html.ElementNode in pkg:golang/golang.org/x/net@v0.61.0
Pacotes
Símbolos
  • golang.org/x/net/html.ElementNode
Ambiente
go 1.26.6
Criado
2026-10-11T07:38:38Z

Contrato

  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

Arquivos

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

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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.61.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/net@v0.61.0
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:d328a708f378979320a290d61371ebd87e64a46f4e49c1c7de01cb80652a1f7a","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.61.0","kind":"HOW","packages":["pkg:golang/golang.org/x/net@v0.61.0"],"schemaVersion":1,"symbols":["golang.org/x/net/html.ElementNode"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","compiler":"go","compilerVersion":"1.26.6","distro":"ubuntu","ecosystem":"golang","language":"go","languageVersion":"1.26.6","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","packageManagerVersion":"1.26.6","runtime":"go","runtimeVersion":"1.26.6","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/golang.org/x/net@v0.61.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/net@v0.61.0","symbols":["golang.org/x/net/html.ElementNode"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require golang.org/x/net v0.61.0
go.sum
golang.org/x/net v0.61.0 h1:RR+6j/BTBCrggOLugxa+66R8FvoN6MHlzDlZIbN9YFw=
golang.org/x/net v0.61.0/go.mod h1:WgDCOTH8iwtB66T4/GfI94AHo/hhdb7Hah9NDseQfiI=
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.61.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/net@v0.61.0"
  ],
  "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")
}

Seeder de origem

anônimo