CodeSampleX

Пример

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

Проверенный пример — golang golang.org/x/net v0.41.0: html.DoctypeNode. Контракт выполнен на go 1.26 · linux debian/x64 · docker и пройден.

sha256:7e16967c9200dc5a63aea0839b5ac34d24f46997152ee689a0c9e6e73dd0d63f

Эта сеть предлагает одно: образец, который собирается. Она запустила его в песочнице и сохранила подписанную квитанцию. Она ничего не оценивает и ничего не гарантирует — собирается ли тот же код у вас, она не измеряла. Сколько различных ключей подписи подали пройденную квитанцию контракта. Один — только автор; больше одного — значит, кто-то ещё тоже собрал. Ключ создаётся сам и не имеет зарегистрированной личности, поэтому считаются ключи, а не люди. 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.DoctypeNode in pkg:golang/golang.org/x/net@v0.41.0
Пакеты
Символы
  • golang.org/x/net/html.DoctypeNode
Создан
2026-10-14T21:48:34Z

Контракт

  1. html.DoctypeNode is a NodeType constant
  2. html.DoctypeNode is distinct from other NodeType values
  3. html.Parse parses DOCTYPE declaration as an html.DoctypeNode
  4. html.Parse extracts system and public attributes on html.DoctypeNode
  5. html.Render outputs valid DOCTYPE markup for an html.DoctypeNode

Файлы

  • PROMPT.md
  • csx.json
  • doctype.go
  • 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.DoctypeNode 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.DoctypeNode

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:f9bad2ef5e0e8c8c557cde1ef0c184fea7902b805199caaf8bf15f138324e5b8","contract":["html.DoctypeNode is a NodeType constant","html.DoctypeNode is distinct from other NodeType values","html.Parse parses DOCTYPE declaration as an html.DoctypeNode","html.Parse extracts system and public attributes on html.DoctypeNode","html.Render outputs valid DOCTYPE markup for an html.DoctypeNode"],"goal":"verify golang.org/x/net/html.DoctypeNode 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.DoctypeNode"]},"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.DoctypeNode"],"verifierAdapter":"golang@1"}
doctype.go
package sample

import (
	"bytes"

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

// FindDoctype traverses the direct children of the given document node
// and returns the first node with Type == html.DoctypeNode, or nil if none is found.
func FindDoctype(doc *html.Node) *html.Node {
	if doc == nil {
		return nil
	}
	for c := doc.FirstChild; c != nil; c = c.NextSibling {
		if c.Type == html.DoctypeNode {
			return c
		}
	}
	return nil
}

// IsDoctype returns true if the node is non-nil and its Type is html.DoctypeNode.
func IsDoctype(n *html.Node) bool {
	return n != nil && n.Type == html.DoctypeNode
}

// NewDoctype constructs a new html.Node representing a DOCTYPE declaration.
func NewDoctype(name, publicID, systemID string) *html.Node {
	n := &html.Node{
		Type: html.DoctypeNode,
		Data: name,
	}
	if publicID != "" {
		n.Attr = append(n.Attr, html.Attribute{Key: "public", Val: publicID})
	}
	if systemID != "" {
		n.Attr = append(n.Attr, html.Attribute{Key: "system", Val: systemID})
	}
	return n
}

// RenderDoctype serializes a DoctypeNode into its HTML string representation.
func RenderDoctype(n *html.Node) (string, error) {
	var buf bytes.Buffer
	if err := html.Render(&buf, n); err != nil {
		return "", err
	}
	return buf.String(), nil
}
go.mod
module sample

go 1.23.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=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/net/html.DoctypeNode 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.DoctypeNode"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"strings"

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

func main() {
	// Assertion 1: html.DoctypeNode is a NodeType constant
	var nodeType html.NodeType = html.DoctypeNode
	if nodeType != html.DoctypeNode {
		fmt.Fprintf(os.Stderr, "expected nodeType to match html.DoctypeNode\n")
		os.Exit(1)
	}

	// Assertion 2: html.DoctypeNode is distinct from other NodeType values
	if html.DoctypeNode == html.ErrorNode ||
		html.DoctypeNode == html.TextNode ||
		html.DoctypeNode == html.DocumentNode ||
		html.DoctypeNode == html.ElementNode ||
		html.DoctypeNode == html.CommentNode ||
		html.DoctypeNode == html.RawNode {
		fmt.Fprintf(os.Stderr, "expected html.DoctypeNode to be distinct from other NodeType values\n")
		os.Exit(1)
	}

	// Assertion 3: html.Parse parses DOCTYPE declaration as an html.DoctypeNode
	htmlDoc := "<!DOCTYPE html><html><head></head><body><p>Hello</p></body></html>"
	doc, err := html.Parse(strings.NewReader(htmlDoc))
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to parse HTML: %v\n", err)
		os.Exit(1)
	}
	if doc.Type != html.DocumentNode {
		fmt.Fprintf(os.Stderr, "expected root node to be DocumentNode, got %v\n", doc.Type)
		os.Exit(1)
	}

	dtNode := sample.FindDoctype(doc)
	if dtNode == nil {
		fmt.Fprintf(os.Stderr, "expected doctype node not found\n")
		os.Exit(1)
	}
	if dtNode.Type != html.DoctypeNode {
		fmt.Fprintf(os.Stderr, "expected doctype node to have Type html.DoctypeNode, got %v\n", dtNode.Type)
		os.Exit(1)
	}
	if dtNode.Data != "html" {
		fmt.Fprintf(os.Stderr, "expected doctype node data to be 'html', got %q\n", dtNode.Data)
		os.Exit(1)
	}
	if !sample.IsDoctype(dtNode) {
		fmt.Fprintf(os.Stderr, "expected sample.IsDoctype to return true\n")
		os.Exit(1)
	}

	// Assertion 4: html.Parse extracts system and public attributes on html.DoctypeNode
	htmlWithSystem := `<!DOCTYPE html SYSTEM "about:legacy-compat"><html><body></body></html>`
	docWithSystem, err := html.Parse(strings.NewReader(htmlWithSystem))
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to parse HTML with system doctype: %v\n", err)
		os.Exit(1)
	}
	dtSystem := sample.FindDoctype(docWithSystem)
	if dtSystem == nil || dtSystem.Type != html.DoctypeNode {
		fmt.Fprintf(os.Stderr, "expected DoctypeNode in docWithSystem\n")
		os.Exit(1)
	}
	foundSystem := false
	for _, attr := range dtSystem.Attr {
		if attr.Key == "system" && attr.Val == "about:legacy-compat" {
			foundSystem = true
		}
	}
	if !foundSystem {
		fmt.Fprintf(os.Stderr, "expected system attribute on DoctypeNode\n")
		os.Exit(1)
	}

	// Assertion 5: html.Render outputs valid DOCTYPE markup for an html.DoctypeNode
	manualDoctype := sample.NewDoctype("html", "", "")
	rendered, err := sample.RenderDoctype(manualDoctype)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to render DoctypeNode: %v\n", err)
		os.Exit(1)
	}
	if rendered != "<!DOCTYPE html>" {
		fmt.Fprintf(os.Stderr, "expected '<!DOCTYPE html>', got %q\n", rendered)
		os.Exit(1)
	}

	manualWithSystem := sample.NewDoctype("html", "", "about:legacy-compat")
	renderedSystem, err := sample.RenderDoctype(manualWithSystem)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to render DoctypeNode with system: %v\n", err)
		os.Exit(1)
	}
	if renderedSystem != `<!DOCTYPE html SYSTEM "about:legacy-compat">` {
		fmt.Fprintf(os.Stderr, "expected '<!DOCTYPE html SYSTEM \"about:legacy-compat\">', got %q\n", renderedSystem)
		os.Exit(1)
	}

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

Исходный сидер

аноним