CodeSampleX

サンプル

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

検証済みサンプル — golang golang.org/x/net v0.41.0: html.ElementNode. go 1.26 · linux debian/x64 · docker で contract を実行し、成功しました: html.ElementNode constant is…

sha256:bebe914e9b765a5c7dc78eee1f082304e47cae3e2a400ea26161ea4e79655d76

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 linux 24 · ubuntu · glibc 2.39 x64 go

検証実行環境

環境 コントラクト ステージ 実行日
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-14

ケース

HOW
ゴール
verify golang.org/x/net/html.ElementNode in pkg:golang/golang.org/x/net@v0.41.0
パッケージ
シンボル
  • golang.org/x/net/html.ElementNode
作成日
2026-10-14T17:47:09Z

コントラクト

  1. html.ElementNode constant is distinct from TextNode, DocumentNode, CommentNode, and DoctypeNode
  2. html.Parse parses HTML tags into nodes with Type == html.ElementNode, Data equal to the tag name, and populated Attr slice
  3. Tree traversal filtering on Type == html.ElementNode collects only element nodes and excludes document, doctype, comment, and text nodes
  4. Manually constructed html.Node values with Type == html.ElementNode render valid HTML elements and attributes via html.Render
  5. html.ParseFragment with an html.ElementNode context parses HTML snippets into child ElementNode nodes

ファイル

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

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/net@v0.41.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:658101b1f21e6dda0eb5c997b6360d2963762d98f95f67178840a00a9f869c3e","contract":["html.ElementNode constant is distinct from TextNode, DocumentNode, CommentNode, and DoctypeNode","html.Parse parses HTML tags into nodes with Type == html.ElementNode, Data equal to the tag name, and populated Attr slice","Tree traversal filtering on Type == html.ElementNode collects only element nodes and excludes document, doctype, comment, and text nodes","Manually constructed html.Node values with Type == html.ElementNode render valid HTML elements and attributes via html.Render","html.ParseFragment with an html.ElementNode context parses HTML snippets into child ElementNode nodes"],"goal":"verify golang.org/x/net/html.ElementNode in pkg:golang/golang.org/x/net@v0.41.0","kind":"HOW","packages":["pkg:golang/golang.org/x/net@v0.41.0"],"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.41.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/net@v0.41.0","symbols":["golang.org/x/net/html.ElementNode"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.24.0

require golang.org/x/net v0.41.0
go.sum
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
sample.go
package sample

import (
	"bytes"
	"strings"

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

// ParseDocument parses raw HTML markup into an html.Node tree.
func ParseDocument(markup string) (*html.Node, error) {
	return html.Parse(strings.NewReader(markup))
}

// CollectElements traverses the node tree and returns all nodes with Type == html.ElementNode.
// If tagFilter is non-empty, only elements whose Data matches tagFilter (case-insensitively) are returned.
func CollectElements(root *html.Node, tagFilter string) []*html.Node {
	var out []*html.Node
	var walk func(*html.Node)
	walk = func(n *html.Node) {
		if n == nil {
			return
		}
		if n.Type == html.ElementNode {
			if tagFilter == "" || strings.EqualFold(n.Data, tagFilter) {
				out = append(out, n)
			}
		}
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			walk(c)
		}
	}
	walk(root)
	return out
}

// NewElement constructs an html.Node with Type html.ElementNode, optional attributes, and optional text child.
func NewElement(tag string, tagAtom atom.Atom, attrs []html.Attribute, text string) *html.Node {
	el := &html.Node{
		Type:     html.ElementNode,
		Data:     tag,
		DataAtom: tagAtom,
		Attr:     attrs,
	}
	if text != "" {
		el.AppendChild(&html.Node{
			Type: html.TextNode,
			Data: text,
		})
	}
	return el
}

// RenderHTML renders an html.Node subtree to a string.
func RenderHTML(n *html.Node) (string, error) {
	var buf bytes.Buffer
	if err := html.Render(&buf, n); err != nil {
		return "", err
	}
	return buf.String(), nil
}

// ParseElementFragment parses an HTML snippet within the given context ElementNode.
func ParseElementFragment(contextEl *html.Node, snippet string) ([]*html.Node, error) {
	return html.ParseFragment(strings.NewReader(snippet), contextEl)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/net/html.ElementNode in pkg:golang/golang.org/x/net@v0.41.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/net@v0.41.0"
  ],
  "symbols": [
    "golang.org/x/net/html.ElementNode"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"strings"

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

func main() {
	// 1. Verify html.ElementNode constant distinguishes element nodes from other html.NodeType values
	if html.ElementNode == html.TextNode || html.ElementNode == html.DocumentNode || html.ElementNode == html.CommentNode || html.ElementNode == html.DoctypeNode {
		fmt.Fprintln(os.Stderr, "assertion 1 failed: html.ElementNode is not distinct from other NodeType constants")
		os.Exit(1)
	}

	// 2. Verify html.Parse creates html.ElementNode nodes for HTML tags with matching Data and attributes
	markup := `<!DOCTYPE html><html><head><title>Sample</title></head><body><!--comment--><article id="post-1" class="entry"><h1>Heading</h1><p>Paragraph text</p></article></body></html>`
	doc, err := sample.ParseDocument(markup)
	if err != nil {
		fmt.Fprintf(os.Stderr, "assertion 2 failed: ParseDocument error: %v\n", err)
		os.Exit(1)
	}
	articles := sample.CollectElements(doc, "article")
	if len(articles) != 1 || articles[0].Type != html.ElementNode || articles[0].Data != "article" {
		fmt.Fprintf(os.Stderr, "assertion 2 failed: expected 1 article ElementNode, got %d\n", len(articles))
		os.Exit(1)
	}
	var idAttr, classAttr string
	for _, a := range articles[0].Attr {
		switch a.Key {
		case "id":
			idAttr = a.Val
		case "class":
			classAttr = a.Val
		}
	}
	if idAttr != "post-1" || classAttr != "entry" {
		fmt.Fprintf(os.Stderr, "assertion 2 failed: unexpected attributes id=%q class=%q\n", idAttr, classAttr)
		os.Exit(1)
	}

	// 3. Verify tree filtering with Type == html.ElementNode excludes document, doctype, comment, and text nodes
	allElements := sample.CollectElements(doc, "")
	var tags []string
	for _, el := range allElements {
		if el.Type != html.ElementNode {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: non-ElementNode returned: %v\n", el.Type)
			os.Exit(1)
		}
		tags = append(tags, el.Data)
	}
	expectedTags := "html,head,title,body,article,h1,p"
	if strings.Join(tags, ",") != expectedTags {
		fmt.Fprintf(os.Stderr, "assertion 3 failed: expected tags %q, got %q\n", expectedTags, strings.Join(tags, ","))
		os.Exit(1)
	}

	// 4. Verify constructing html.ElementNode nodes manually and serializing them with html.Render
	nav := sample.NewElement("nav", atom.Nav, []html.Attribute{{Key: "aria-label", Val: "main"}}, "")
	link := sample.NewElement("a", atom.A, []html.Attribute{{Key: "href", Val: "https://example.com"}}, "Home")
	nav.AppendChild(link)

	rendered, err := sample.RenderHTML(nav)
	if err != nil {
		fmt.Fprintf(os.Stderr, "assertion 4 failed: RenderHTML error: %v\n", err)
		os.Exit(1)
	}
	expectedHTML := `<nav aria-label="main"><a href="https://example.com">Home</a></nav>`
	if rendered != expectedHTML {
		fmt.Fprintf(os.Stderr, "assertion 4 failed: expected %q, got %q\n", expectedHTML, rendered)
		os.Exit(1)
	}

	// 5. Verify html.ParseFragment with an html.ElementNode context returns parsed ElementNode children
	ctxNode := sample.NewElement("ul", atom.Ul, nil, "")
	fragNodes, err := sample.ParseElementFragment(ctxNode, `<li data-idx="1">First</li><li data-idx="2">Second</li>`)
	if err != nil {
		fmt.Fprintf(os.Stderr, "assertion 5 failed: ParseElementFragment error: %v\n", err)
		os.Exit(1)
	}
	if len(fragNodes) != 2 {
		fmt.Fprintf(os.Stderr, "assertion 5 failed: expected 2 fragment nodes, got %d\n", len(fragNodes))
		os.Exit(1)
	}
	for i, fn := range fragNodes {
		if fn.Type != html.ElementNode || fn.Data != "li" {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: node %d type=%v data=%q\n", i, fn.Type, fn.Data)
			os.Exit(1)
		}
	}

	fmt.Println("contract ok")
}

オリジンシーダー

匿名