CodeSampleX

示例

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

已验证示例 — golang golang.org/x/net v0.47.0: html.Attribute. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: Attribute struct fields Namespace, Key, and…

sha256:052c1714164ae4d1d8668ee94ed1fa9d4f7269491c42ed79651dfcd5e8623374

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

执行证据

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

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 go 1.26 linux 24 · ubuntu · glibc 2.39 x64 go 1.26 go 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.Attribute in pkg:golang/golang.org/x/net@v0.47.0
包
符号
  • golang.org/x/net/html.Attribute
环境
go 1.26.6
创建时间
2026-10-10T22:35:36Z

契约

  1. Attribute struct fields Namespace, Key, and Val represent HTML element attributes
  2. html.Parse extracts element attributes into html.Node.Attr as a slice of html.Attribute
  3. html.Render serializes html.Node with html.Attribute slice into valid HTML attribute syntax
  4. Attribute supports namespaced attributes such as xml and xlink
  5. Attribute values preserve exact strings including whitespace, URLs, and custom data attributes

文件

  • 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.Attribute 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.Attribute

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:c15870b00be5064b249bcf8d5b58a70c6f1923c3cd6feaf5a6f5f1bd4caee322","contract":["Attribute struct fields Namespace, Key, and Val represent HTML element attributes","html.Parse extracts element attributes into html.Node.Attr as a slice of html.Attribute","html.Render serializes html.Node with html.Attribute slice into valid HTML attribute syntax","Attribute supports namespaced attributes such as xml and xlink","Attribute values preserve exact strings including whitespace, URLs, and custom data attributes"],"goal":"verify golang.org/x/net/html.Attribute 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.Attribute"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","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.Attribute"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.24.0

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"
)

// MakeAttribute creates an html.Attribute with optional namespace.
func MakeAttribute(namespace, key, val string) html.Attribute {
	return html.Attribute{
		Namespace: namespace,
		Key:       key,
		Val:       val,
	}
}

// GetAttribute finds an attribute by key (ignoring namespace if empty).
func GetAttribute(attrs []html.Attribute, key string) (html.Attribute, bool) {
	for _, a := range attrs {
		if a.Key == key {
			return a, true
		}
	}
	return html.Attribute{}, false
}

// GetNamespacedAttribute finds an attribute matching both namespace and key.
func GetNamespacedAttribute(attrs []html.Attribute, namespace, key string) (html.Attribute, bool) {
	for _, a := range attrs {
		if a.Namespace == namespace && a.Key == key {
			return a, true
		}
	}
	return html.Attribute{}, false
}

// ParseAttributes parses an HTML snippet containing an element and returns its attributes.
func ParseAttributes(snippet string) ([]html.Attribute, error) {
	node, err := html.Parse(strings.NewReader(snippet))
	if err != nil {
		return nil, fmt.Errorf("parse error: %w", err)
	}

	var found []html.Attribute
	var search func(*html.Node)
	search = func(n *html.Node) {
		if n.Type == html.ElementNode && n.Data != "html" && n.Data != "head" && n.Data != "body" {
			found = n.Attr
			return
		}
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			search(c)
			if found != nil {
				return
			}
		}
	}
	search(node)
	if found == nil {
		return nil, fmt.Errorf("no target element node found")
	}
	return found, nil
}

// RenderAttributes creates an element node with the given tag and attributes, then renders it to HTML.
func RenderAttributes(tag string, attrs []html.Attribute) (string, error) {
	node := &html.Node{
		Type: html.ElementNode,
		Data: tag,
		Attr: attrs,
	}
	var buf bytes.Buffer
	if err := html.Render(&buf, node); err != nil {
		return "", fmt.Errorf("render error: %w", err)
	}
	return buf.String(), nil
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/net/html.Attribute 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.Attribute"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"strings"

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

func main() {
	// Assertion 1: Attribute struct fields Namespace, Key, and Val represent HTML element attributes
	attr1 := sample.MakeAttribute("", "id", "test-id")
	if attr1.Namespace != "" || attr1.Key != "id" || attr1.Val != "test-id" {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 1 failed: %v\n", attr1)
		os.Exit(1)
	}

	// Assertion 2: html.Parse extracts element attributes into html.Node.Attr as a slice of html.Attribute
	snippet := `<input type="text" name="query" value="search term" required>`
	parsedAttrs, err := sample.ParseAttributes(snippet)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 2 failed to parse: %v\n", err)
		os.Exit(1)
	}
	if len(parsedAttrs) != 4 {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 2 failed: expected 4 attributes, got %d\n", len(parsedAttrs))
		os.Exit(1)
	}
	typeAttr, ok := sample.GetAttribute(parsedAttrs, "type")
	if !ok || typeAttr.Val != "text" {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 2 failed: type attr mismatch: %v\n", typeAttr)
		os.Exit(1)
	}
	nameAttr, ok := sample.GetAttribute(parsedAttrs, "name")
	if !ok || nameAttr.Val != "query" {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 2 failed: name attr mismatch: %v\n", nameAttr)
		os.Exit(1)
	}

	// Assertion 3: html.Render serializes html.Node with html.Attribute slice into valid HTML attribute syntax
	renderAttrs := []html.Attribute{
		{Key: "href", Val: "https://example.com/api?a=1&b=2"},
		{Key: "title", Val: "Quotes & Ampersands"},
	}
	rendered, err := sample.RenderAttributes("a", renderAttrs)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 3 failed to render: %v\n", err)
		os.Exit(1)
	}
	if !strings.Contains(rendered, `href="https://example.com/api?a=1&amp;b=2"`) || !strings.Contains(rendered, `title="Quotes &amp; Ampersands"`) {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 3 failed: rendered mismatch: %s\n", rendered)
		os.Exit(1)
	}

	// Assertion 4: Attribute supports namespaced attributes such as xml and xlink
	nsAttrs := []html.Attribute{
		sample.MakeAttribute("xml", "lang", "en"),
		sample.MakeAttribute("xlink", "href", "#icon-check"),
	}
	xmlAttr, ok := sample.GetNamespacedAttribute(nsAttrs, "xml", "lang")
	if !ok || xmlAttr.Val != "en" {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 4 failed: xml:lang mismatch: %v\n", xmlAttr)
		os.Exit(1)
	}
	xlinkAttr, ok := sample.GetNamespacedAttribute(nsAttrs, "xlink", "href")
	if !ok || xlinkAttr.Val != "#icon-check" {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 4 failed: xlink:href mismatch: %v\n", xlinkAttr)
		os.Exit(1)
	}

	// Assertion 5: Attribute values preserve exact strings including whitespace, URLs, and custom data attributes
	customSnippet := `<div data-user="alice smith" data-url="https://example.com/profile" data-json='{"active":true}'></div>`
	dataAttrs, err := sample.ParseAttributes(customSnippet)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 5 failed to parse custom attributes: %v\n", err)
		os.Exit(1)
	}
	userAttr, ok := sample.GetAttribute(dataAttrs, "data-user")
	if !ok || userAttr.Val != "alice smith" {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 5 failed: data-user mismatch: %v\n", userAttr)
		os.Exit(1)
	}
	urlAttr, ok := sample.GetAttribute(dataAttrs, "data-url")
	if !ok || urlAttr.Val != "https://example.com/profile" {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 5 failed: data-url mismatch: %v\n", urlAttr)
		os.Exit(1)
	}
	jsonAttr, ok := sample.GetAttribute(dataAttrs, "data-json")
	if !ok || jsonAttr.Val != `{"active":true}` {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 5 failed: data-json mismatch: %v\n", jsonAttr)
		os.Exit(1)
	}

	fmt.Println("PASS")
}

原始种子者

匿名