CodeSampleX

示例

golang.org/x/net v0.0.0-20180724234803-3673e40ba225: html.ParseFragment

已验证示例 — golang golang.org/x/net v0.0.0-20180724234803-3673e40ba225: html.ParseFragment. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过.

sha256:af6b6efd44d9ee4f7e73db903974f3a3489cc020cc76bda91895a34ade8b90a2

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

案例

HOW
目标
verify golang.org/x/net/html.ParseFragment in pkg:golang/golang.org/x/net@v0.0.0-20180724234803-3673e40ba225
包
符号
  • golang.org/x/net/html.ParseFragment
创建时间
2026-10-17T20:22:09Z

契约

  1. html.ParseFragment with nil context parses a fragment into a root html element containing head and body
  2. html.ParseFragment with a body element context parses child elements directly without outer html/body wrappers
  3. html.ParseFragment with a table element context wraps tr elements inside an implicit tbody element
  4. html.ParseFragment with a select element context parses option elements and preserves their attributes

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • 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.ParseFragment in pkg:golang/golang.org/x/net@v0.0.0-20180724234803-3673e40ba225
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/net@v0.0.0-20180724234803-3673e40ba225
Demonstrate these symbols/APIs:
  - golang.org/x/net/html.ParseFragment

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:607ab9340b0f02210af40088d7da392d19ab82aa01a413d77c7f8f39055bea31","contract":["html.ParseFragment with nil context parses a fragment into a root html element containing head and body","html.ParseFragment with a body element context parses child elements directly without outer html/body wrappers","html.ParseFragment with a table element context wraps tr elements inside an implicit tbody element","html.ParseFragment with a select element context parses option elements and preserves their attributes"],"goal":"verify golang.org/x/net/html.ParseFragment in pkg:golang/golang.org/x/net@v0.0.0-20180724234803-3673e40ba225","kind":"HOW","packages":["pkg:golang/golang.org/x/net@v0.0.0-20180724234803-3673e40ba225"],"schemaVersion":1,"symbols":["golang.org/x/net/html.ParseFragment"]},"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-20180724234803-3673e40ba225"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/net@v0.0.0-20180724234803-3673e40ba225","symbols":["golang.org/x/net/html.ParseFragment"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.21

require golang.org/x/net v0.0.0-20180724234803-3673e40ba225
go.sum
golang.org/x/net v0.0.0-20180724234803-3673e40ba225 h1:kNX+jCowfMYzvlSvJu5pQWEmyWFrBXJ3PBy10xKMXK8=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/net/html.ParseFragment in pkg:golang/golang.org/x/net@v0.0.0-20180724234803-3673e40ba225",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/net@v0.0.0-20180724234803-3673e40ba225"
  ],
  "symbols": [
    "golang.org/x/net/html.ParseFragment"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"strings"

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

func fail(msg string, args ...any) {
	fmt.Fprintf(os.Stderr, msg+"\n", args...)
	os.Exit(1)
}

func main() {
	// 1. ParseFragment with nil context parses into a root html element containing head and body.
	nodesNil, err := html.ParseFragment(strings.NewReader("<p>hello <b>world</b></p>"), nil)
	if err != nil {
		fail("ParseFragment with nil context failed: %v", err)
	}
	if len(nodesNil) != 1 || nodesNil[0].Type != html.ElementNode || nodesNil[0].Data != "html" {
		fail("expected single root html element node for nil context, got %v", nodesNil)
	}
	rootHTML := nodesNil[0]
	if rootHTML.FirstChild == nil || rootHTML.FirstChild.Data != "head" {
		fail("expected first child of html root to be head")
	}
	if rootHTML.LastChild == nil || rootHTML.LastChild.Data != "body" {
		fail("expected last child of html root to be body")
	}
	bodyChild := rootHTML.LastChild.FirstChild
	if bodyChild == nil || bodyChild.Type != html.ElementNode || bodyChild.Data != "p" {
		fail("expected p element inside body, got %v", bodyChild)
	}

	// 2. ParseFragment with a body element context parses child elements directly without html/body wrappers.
	bodyCtx := &html.Node{
		Type:     html.ElementNode,
		DataAtom: atom.Body,
		Data:     "body",
	}
	nodesBody, err := html.ParseFragment(strings.NewReader("<div>alpha</div><span>beta</span>"), bodyCtx)
	if err != nil {
		fail("ParseFragment with body context failed: %v", err)
	}
	if len(nodesBody) != 2 {
		fail("expected 2 nodes with body context, got %d", len(nodesBody))
	}
	if nodesBody[0].Type != html.ElementNode || nodesBody[0].Data != "div" || nodesBody[0].FirstChild == nil || nodesBody[0].FirstChild.Data != "alpha" {
		fail("unexpected first node in body context: %+v", nodesBody[0])
	}
	if nodesBody[1].Type != html.ElementNode || nodesBody[1].Data != "span" || nodesBody[1].FirstChild == nil || nodesBody[1].FirstChild.Data != "beta" {
		fail("unexpected second node in body context: %+v", nodesBody[1])
	}

	// 3. ParseFragment with a table element context wraps tr elements inside an implicit tbody element.
	tableCtx := &html.Node{
		Type:     html.ElementNode,
		DataAtom: atom.Table,
		Data:     "table",
	}
	nodesTable, err := html.ParseFragment(strings.NewReader("<tr><td>cell1</td><td>cell2</td></tr>"), tableCtx)
	if err != nil {
		fail("ParseFragment with table context failed: %v", err)
	}
	if len(nodesTable) != 1 || nodesTable[0].Type != html.ElementNode || nodesTable[0].Data != "tbody" {
		fail("expected single implicit tbody element in table context, got %v", nodesTable)
	}
	trNode := nodesTable[0].FirstChild
	if trNode == nil || trNode.Type != html.ElementNode || trNode.Data != "tr" {
		fail("expected tr element inside tbody, got %v", trNode)
	}
	if trNode.FirstChild == nil || trNode.FirstChild.Data != "td" || trNode.LastChild == nil || trNode.LastChild.Data != "td" || trNode.FirstChild == trNode.LastChild {
		fail("expected two td children inside tr")
	}

	// 4. ParseFragment with a select element context parses option elements and preserves their attributes.
	selectCtx := &html.Node{
		Type:     html.ElementNode,
		DataAtom: atom.Select,
		Data:     "select",
	}
	nodesSelect, err := html.ParseFragment(strings.NewReader(`<option value="one" selected>First</option><option value="two">Second</option>`), selectCtx)
	if err != nil {
		fail("ParseFragment with select context failed: %v", err)
	}
	if len(nodesSelect) != 2 {
		fail("expected 2 option nodes in select context, got %d", len(nodesSelect))
	}
	if nodesSelect[0].Data != "option" || len(nodesSelect[0].Attr) != 2 || nodesSelect[0].Attr[0].Key != "value" || nodesSelect[0].Attr[0].Val != "one" {
		fail("unexpected first option node or attributes: %+v", nodesSelect[0])
	}
	if nodesSelect[0].FirstChild == nil || nodesSelect[0].FirstChild.Type != html.TextNode || nodesSelect[0].FirstChild.Data != "First" {
		fail("unexpected first option text child")
	}
	if nodesSelect[1].Data != "option" || len(nodesSelect[1].Attr) != 1 || nodesSelect[1].Attr[0].Key != "value" || nodesSelect[1].Attr[0].Val != "two" {
		fail("unexpected second option node or attributes: %+v", nodesSelect[1])
	}
}

原始种子者

匿名