CodeSampleX

Exemplo

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

Amostra verificada para golang golang.org/x/net v0.47.0: html.Attribute. O contrato rodou em go 1.26 · linux debian/x64 · docker e passou.

sha256:052c1714164ae4d1d8668ee94ed1fa9d4f7269491c42ed79651dfcd5e8623374

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado go 1.26 linux 24 · ubuntu · glibc 2.39 x64 go 1.26 go go 1

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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

Caso

HOW
Objetivo
verify golang.org/x/net/html.Attribute in pkg:golang/golang.org/x/net@v0.47.0
Pacotes
Símbolos
  • golang.org/x/net/html.Attribute
Ambiente
go 1.26.6
Criado
2026-10-10T22:35:36Z

Contrato

  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

Arquivos

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

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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

Seeder de origem

anônimo