CodeSampleX

샘플

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

검증된 샘플 — golang golang.org/x/net v0.47.0: html.ElementNode. go 1.26 · linux debian/x64 · docker에서 contract를 실행해 통과했습니다: html.ElementNode constant…

sha256:0070ea328201b279308155700a260a23bcd7ffb861dc419dad7b6224f54d6e69

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. MIT-0

실행 증거

선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.

증거 기준
서명된 컨트랙트 통과
검증 영수증
1
빌드한 서명 키
1
선언된 환경 go 1.26 linux 24 · ubuntu · glibc 2.39 amd64 go 1.26 go 1.26 go 1

검증 실행 환경

환경 컨트랙트 단계 실행일
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-10

케이스

HOW
목표
verify golang.org/x/net/html.ElementNode in pkg:golang/golang.org/x/net@v0.47.0
패키지
심벌
  • golang.org/x/net/html.ElementNode
환경
go 1.26.6
생성일
2026-10-10T18:50:57Z

컨트랙트

  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

파일

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

소스 아티팩트 내려받기 (tar.gz)

소스

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

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/net@v0.47.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:a349bd01b19b14ebd72477dd3f412b7dfaf0548ce13346f99744f95eaa3d618c","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.47.0","kind":"HOW","packages":["pkg:golang/golang.org/x/net@v0.47.0"],"schemaVersion":1,"symbols":["golang.org/x/net/html.ElementNode"]},"contractCommand":["go","run","./test"],"environment":{"arch":"amd64","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.47.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/net@v0.47.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.47.0
go.sum
golang.org/x/net v0.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY=
golang.org/x/net v0.47.0/go.mod h1:/jNxtkgq5yWUGYkaZGqo27cfGZ1c5Nen03aYrrKpVRU=
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.47.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/net@v0.47.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")
}

오리진 시더

익명