CodeSampleX

Пример

golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e: html.CommentNode

Проверенный пример — golang golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e: html.CommentNode. Контракт выполнен на go 1.26 · linux debian/x64 · docker и…

sha256:4b5f2b599cde069e191e08a2c2ea6e261720fcef539b18c18d3499c24c5fa3d6

Эта сеть предлагает одно: образец, который собирается. Она запустила его в песочнице и сохранила подписанную квитанцию. Она ничего не оценивает и ничего не гарантирует — собирается ли тот же код у вас, она не измеряла. Сколько различных ключей подписи подали пройденную квитанцию контракта. Один — только автор; больше одного — значит, кто-то ещё тоже собрал. Ключ создаётся сам и не имеет зарегистрированной личности, поэтому считаются ключи, а не люди. 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-21

Кейс

HOW
Цель
verify golang.org/x/net/html.CommentNode in pkg:golang/golang.org/x/net@v0.0.0-20200324143707-d3edc9973b7e
Пакеты
Символы
  • golang.org/x/net/html.CommentNode
Создан
2026-10-21T11:11:27Z

Контракт

  1. html.Parse extracts nodes of Type html.CommentNode with preserved comment text from HTML input
  2. html.Render serializes an html.Node of Type html.CommentNode into HTML comment markup <!--...-->
  3. html.CommentNode nodes can be programmatically constructed and appended to an HTML DOM tree

Файлы

  • PROMPT.md
  • comment.go
  • 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.CommentNode in pkg:golang/golang.org/x/net@v0.0.0-20200324143707-d3edc9973b7e
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/net@v0.0.0-20200324143707-d3edc9973b7e
Demonstrate these symbols/APIs:
  - golang.org/x/net/html.CommentNode

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.
comment.go
package htmlcomment

import (
	"bytes"
	"strings"

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

// FindComments parses an HTML fragment or document and returns all comment strings from html.CommentNode nodes.
func FindComments(rawHTML string) ([]string, error) {
	doc, err := html.Parse(strings.NewReader(rawHTML))
	if err != nil {
		return nil, err
	}

	var results []string
	var visit func(*html.Node)
	visit = func(n *html.Node) {
		if n.Type == html.CommentNode {
			results = append(results, n.Data)
		}
		for child := n.FirstChild; child != nil; child = child.NextSibling {
			visit(child)
		}
	}
	visit(doc)
	return results, nil
}

// NewComment constructs a new html.Node with Type set to html.CommentNode.
func NewComment(text string) *html.Node {
	return &html.Node{
		Type: html.CommentNode,
		Data: text,
	}
}

// RenderHTML serializes an html.Node subtree into HTML markup.
func RenderHTML(n *html.Node) (string, error) {
	var buf bytes.Buffer
	if err := html.Render(&buf, n); err != nil {
		return "", err
	}
	return buf.String(), nil
}
csx.json
{"case":{"caseId":"case:sha256:092b02015f0c1b08d50a55188543681c14ab87e16f55165c7e51932ae210e1e6","contract":["html.Parse extracts nodes of Type html.CommentNode with preserved comment text from HTML input","html.Render serializes an html.Node of Type html.CommentNode into HTML comment markup \u003c!--...--\u003e","html.CommentNode nodes can be programmatically constructed and appended to an HTML DOM tree"],"goal":"verify golang.org/x/net/html.CommentNode in pkg:golang/golang.org/x/net@v0.0.0-20200324143707-d3edc9973b7e","kind":"HOW","packages":["pkg:golang/golang.org/x/net@v0.0.0-20200324143707-d3edc9973b7e"],"schemaVersion":1,"symbols":["golang.org/x/net/html.CommentNode"]},"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-20200324143707-d3edc9973b7e"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/net@v0.0.0-20200324143707-d3edc9973b7e","symbols":["golang.org/x/net/html.CommentNode"],"verifierAdapter":"golang@1"}
go.mod
module example.com/html-commentnode

go 1.22.0

require golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e
go.sum
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/net/html.CommentNode in pkg:golang/golang.org/x/net@v0.0.0-20200324143707-d3edc9973b7e",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/net@v0.0.0-20200324143707-d3edc9973b7e"
  ],
  "symbols": [
    "golang.org/x/net/html.CommentNode"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"strings"

	htmlcomment "example.com/html-commentnode"
	"golang.org/x/net/html"
)

func main() {
	// 1. Verify html.CommentNode is a distinct NodeType constant.
	if html.CommentNode == html.ErrorNode ||
		html.CommentNode == html.TextNode ||
		html.CommentNode == html.DocumentNode ||
		html.CommentNode == html.ElementNode ||
		html.CommentNode == html.DoctypeNode {
		fmt.Fprintln(os.Stderr, "FAIL: html.CommentNode collides with another html.NodeType")
		os.Exit(1)
	}

	// 2. Verify html.Parse produces nodes with Type == html.CommentNode and preserves comment Data.
	sampleHTML := `<!DOCTYPE html><html><head><!-- header comment --></head><body><div><!-- body comment --><span>text</span><!----></div></body></html>`
	comments, err := htmlcomment.FindComments(sampleHTML)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: FindComments returned error: %v\n", err)
		os.Exit(1)
	}
	expected := []string{" header comment ", " body comment ", ""}
	if len(comments) != len(expected) {
		fmt.Fprintf(os.Stderr, "FAIL: expected %d comments, got %d (%v)\n", len(expected), len(comments), comments)
		os.Exit(1)
	}
	for i, want := range expected {
		if comments[i] != want {
			fmt.Fprintf(os.Stderr, "FAIL: comment[%d] = %q, want %q\n", i, comments[i], want)
			os.Exit(1)
		}
	}

	// 3. Verify constructing an html.CommentNode and rendering it with html.Render outputs <!--...-->.
	commentNode := htmlcomment.NewComment("standalone comment")
	if commentNode.Type != html.CommentNode {
		fmt.Fprintf(os.Stderr, "FAIL: NewComment Type = %v, want %v\n", commentNode.Type, html.CommentNode)
		os.Exit(1)
	}
	rendered, err := htmlcomment.RenderHTML(commentNode)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: RenderHTML failed: %v\n", err)
		os.Exit(1)
	}
	if rendered != "<!--standalone comment-->" {
		fmt.Fprintf(os.Stderr, "FAIL: rendered comment = %q, want %q\n", rendered, "<!--standalone comment-->")
		os.Exit(1)
	}

	// 4. Verify inserting an html.CommentNode into a parsed DOM tree and serializing the document.
	doc, err := html.Parse(strings.NewReader(`<html><head></head><body><p>hello</p></body></html>`))
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: html.Parse failed: %v\n", err)
		os.Exit(1)
	}
	doc.AppendChild(htmlcomment.NewComment("trailing doc comment"))
	fullOut, err := htmlcomment.RenderHTML(doc)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: RenderHTML(doc) failed: %v\n", err)
		os.Exit(1)
	}
	if !strings.HasSuffix(fullOut, "<!--trailing doc comment-->") {
		fmt.Fprintf(os.Stderr, "FAIL: expected trailing comment in rendered output, got: %s\n", fullOut)
		os.Exit(1)
	}

	fmt.Println("PASS")
}

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

аноним