CodeSampleX

示例

golang.org/x/net v0.29.0: html.ParseFragment, html.Render

已验证示例 — golang golang.org/x/net v0.29.0: html.ParseFragment, html.Render. contract 在 go 1.26 · linux alpine/x64 · docker 上运行并通过.

sha256:c00e983a99d189e6fe281f344a3a8c5d2263d056275c6f7ad407323150425766

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-27

案例

HOW
目标
verify golang.org/x/net/html.Render and ParseFragment in pkg:golang/golang.org/x/net@v0.29.0
包
符号
  • golang.org/x/net/html.ParseFragment
  • golang.org/x/net/html.Render
创建时间
2026-08-27T13:18:58Z

契约

  1. html.ParseFragment parses HTML fragment string in context node into slice of nodes
  2. html.ParseFragment handles multiple sibling elements and text nodes
  3. html.Render serializes HTML node tree to writer output
  4. html.Render correctly escapes special characters in text nodes
  5. html.ParseFragment and html.Render roundtrip preserve element structure and attributes

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.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.Render and ParseFragment in pkg:golang/golang.org/x/net@v0.29.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/net@v0.29.0
Demonstrate these symbols/APIs:
  - golang.org/x/net/html.ParseFragment
  - golang.org/x/net/html.Render

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:3ef1faa38146a15e1f21189a3e63340b3a8ce79fcfd7e58ee35a2c16ae8625b0","contract":["html.ParseFragment parses HTML fragment string in context node into slice of nodes","html.ParseFragment handles multiple sibling elements and text nodes","html.Render serializes HTML node tree to writer output","html.Render correctly escapes special characters in text nodes","html.ParseFragment and html.Render roundtrip preserve element structure and attributes"],"goal":"verify golang.org/x/net/html.Render and ParseFragment in pkg:golang/golang.org/x/net@v0.29.0","kind":"HOW","packages":["pkg:golang/golang.org/x/net@v0.29.0"],"schemaVersion":1,"symbols":["golang.org/x/net/html.ParseFragment","golang.org/x/net/html.Render"]},"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.29.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/net@v0.29.0","symbols":["golang.org/x/net/html.ParseFragment","golang.org/x/net/html.Render"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.22

require golang.org/x/net v0.29.0
go.sum
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
main.go
package main

import (
	"bytes"
	"fmt"
	"log"
	"strings"

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

func main() {
	// Parse an HTML fragment in the context of a body element
	fragmentHTML := `<div class="card"><h1 class="title">Item</h1><p>Description &amp; details</p></div>`
	contextNode := &html.Node{
		Type:     html.ElementNode,
		DataAtom: atom.Body,
		Data:     "body",
	}

	nodes, err := html.ParseFragment(strings.NewReader(fragmentHTML), contextNode)
	if err != nil {
		log.Fatalf("ParseFragment failed: %v", err)
	}

	fmt.Printf("Parsed %d top-level node(s)\n", len(nodes))

	// Render the parsed node tree back to an HTML string
	var buf bytes.Buffer
	for _, n := range nodes {
		if err := html.Render(&buf, n); err != nil {
			log.Fatalf("Render failed: %v", err)
		}
	}

	renderedHTML := buf.String()
	fmt.Printf("Rendered HTML:\n%s\n", renderedHTML)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/net/html.Render and ParseFragment in pkg:golang/golang.org/x/net@v0.29.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/net@v0.29.0"
  ],
  "symbols": [
    "golang.org/x/net/html.ParseFragment",
    "golang.org/x/net/html.Render"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"os"
	"strings"

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

func main() {
	bodyContext := &html.Node{
		Type:     html.ElementNode,
		DataAtom: atom.Body,
		Data:     "body",
	}

	// 1. html.ParseFragment parses HTML fragment string in context node into slice of nodes
	{
		input := `<div id="box" class="highlight"><p>Hello World</p></div>`
		nodes, err := html.ParseFragment(strings.NewReader(input), bodyContext)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: ParseFragment returned error: %v\n", err)
			os.Exit(1)
		}
		if len(nodes) != 1 {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: expected 1 node, got %d\n", len(nodes))
			os.Exit(1)
		}
		div := nodes[0]
		if div.Type != html.ElementNode || div.Data != "div" {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: expected ElementNode 'div', got type=%v, data=%q\n", div.Type, div.Data)
			os.Exit(1)
		}
		var idVal, classVal string
		for _, attr := range div.Attr {
			if attr.Key == "id" {
				idVal = attr.Val
			}
			if attr.Key == "class" {
				classVal = attr.Val
			}
		}
		if idVal != "box" || classVal != "highlight" {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: expected id='box' and class='highlight', got id=%q, class=%q\n", idVal, classVal)
			os.Exit(1)
		}
		p := div.FirstChild
		if p == nil || p.Type != html.ElementNode || p.Data != "p" {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: expected child ElementNode 'p', got %+v\n", p)
			os.Exit(1)
		}
		if p.FirstChild == nil || p.FirstChild.Type != html.TextNode || p.FirstChild.Data != "Hello World" {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: expected text child 'Hello World', got %+v\n", p.FirstChild)
			os.Exit(1)
		}
	}

	// 2. html.ParseFragment handles multiple sibling elements and text nodes
	{
		input := `<span>First</span> and <span>Second</span>`
		nodes, err := html.ParseFragment(strings.NewReader(input), bodyContext)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: ParseFragment error: %v\n", err)
			os.Exit(1)
		}
		if len(nodes) != 3 {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: expected 3 nodes, got %d\n", len(nodes))
			os.Exit(1)
		}
		if nodes[0].Type != html.ElementNode || nodes[0].Data != "span" {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: expected node 0 to be span element\n")
			os.Exit(1)
		}
		if nodes[1].Type != html.TextNode || nodes[1].Data != " and " {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: expected node 1 to be text ' and ', got %q\n", nodes[1].Data)
			os.Exit(1)
		}
		if nodes[2].Type != html.ElementNode || nodes[2].Data != "span" {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: expected node 2 to be span element\n")
			os.Exit(1)
		}
	}

	// 3. html.Render serializes HTML node tree to writer output
	{
		aNode := &html.Node{
			Type:     html.ElementNode,
			DataAtom: atom.A,
			Data:     "a",
			Attr: []html.Attribute{
				{Key: "href", Val: "https://example.com"},
			},
		}
		textNode := &html.Node{
			Type: html.TextNode,
			Data: "Link",
		}
		aNode.AppendChild(textNode)

		var buf bytes.Buffer
		if err := html.Render(&buf, aNode); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: Render error: %v\n", err)
			os.Exit(1)
		}
		expected := `<a href="https://example.com">Link</a>`
		if buf.String() != expected {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: expected %q, got %q\n", expected, buf.String())
			os.Exit(1)
		}
	}

	// 4. html.Render correctly escapes special characters in text nodes
	{
		pNode := &html.Node{
			Type:     html.ElementNode,
			DataAtom: atom.P,
			Data:     "p",
		}
		textNode := &html.Node{
			Type: html.TextNode,
			Data: `5 < 10 & 10 > 5`,
		}
		pNode.AppendChild(textNode)

		var buf bytes.Buffer
		if err := html.Render(&buf, pNode); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: Render error: %v\n", err)
			os.Exit(1)
		}
		expected := `<p>5 &lt; 10 &amp; 10 &gt; 5</p>`
		if buf.String() != expected {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: expected %q, got %q\n", expected, buf.String())
			os.Exit(1)
		}
	}

	// 5. html.ParseFragment and html.Render roundtrip preserve element structure and attributes
	{
		originalHTML := `<section id="sec1"><ul class="list"><li>Item 1</li><li>Item 2</li></ul></section>`
		nodes, err := html.ParseFragment(strings.NewReader(originalHTML), bodyContext)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: ParseFragment error: %v\n", err)
			os.Exit(1)
		}
		var buf bytes.Buffer
		for _, n := range nodes {
			if err := html.Render(&buf, n); err != nil {
				fmt.Fprintf(os.Stderr, "assertion 5 failed: Render error: %v\n", err)
				os.Exit(1)
			}
		}
		if buf.String() != originalHTML {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: roundtrip mismatch: expected %q, got %q\n", originalHTML, buf.String())
			os.Exit(1)
		}
	}

	fmt.Println("All contract assertions passed.")
}

原始种子者

匿名