CodeSampleX

Exemple

@xmldom/xmldom 0.8.13: XMLSerializer

Échantillon vérifié pour npm @xmldom/xmldom 0.8.13: XMLSerializer. Le contrat s'est exécuté sur node 22 · linux alpine/x64 · docker et a réussi.

sha256:0d23ff0511e419682567775db4692666b7ad3ebf770108957c97f0f47be7c87e

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
2
Clés de signature qui l’ont compilé
1
Environnement déclaré linux 24 · ubuntu · glibc 2.39 x64 npm

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-23
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-07

Cas

HOW
Objectif
verify @xmldom/xmldom.XMLSerializer in pkg:npm/%40xmldom/xmldom@0.8.13
Paquets
Symboles
  • @xmldom/xmldom.XMLSerializer
Créé
2026-08-23T14:04:49Z

Contrat

  1. XMLSerializer instance serializes a DOM Document node with elements, attributes, and text into a well-formed XML string
  2. XMLSerializer serializes individual DOM Element and DocumentFragment nodes into XML string representations
  3. XMLSerializer serializes Comment, CDATASection, and Text nodes accurately preserving their content
  4. XMLSerializer correctly serializes XML namespaces and escapes special characters in attributes and text
  5. XMLSerializer correctly formats self-closing empty XML elements

Fichiers

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.mjs
  • test/contract.mjs

Télécharger l’artefact source (tar.gz)

Code source

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 @xmldom/xmldom.XMLSerializer in pkg:npm/%40xmldom/xmldom@0.8.13
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40xmldom/xmldom@0.8.13
Demonstrate these symbols/APIs:
  - @xmldom/xmldom.XMLSerializer

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:b96c754bc31126b2c577b04536bc727931689998d9e9ab9415544499fd746513","contract":["XMLSerializer instance serializes a DOM Document node with elements, attributes, and text into a well-formed XML string","XMLSerializer serializes individual DOM Element and DocumentFragment nodes into XML string representations","XMLSerializer serializes Comment, CDATASection, and Text nodes accurately preserving their content","XMLSerializer correctly serializes XML namespaces and escapes special characters in attributes and text","XMLSerializer correctly formats self-closing empty XML elements"],"goal":"verify @xmldom/xmldom.XMLSerializer in pkg:npm/%40xmldom/xmldom@0.8.13","kind":"HOW","packages":["pkg:npm/%40xmldom/xmldom@0.8.13"],"schemaVersion":1,"symbols":["@xmldom/xmldom.XMLSerializer"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/%40xmldom/xmldom@0.8.13"],"schemaVersion":1,"subject":"pkg:npm/%40xmldom/xmldom@0.8.13","symbols":["@xmldom/xmldom.XMLSerializer"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-xmldom-xmlserializer",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-xmldom-xmlserializer",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "@xmldom/xmldom": "0.8.13"
      }
    },
    "node_modules/@xmldom/xmldom": {
      "version": "0.8.13",
      "resolved": "https://registry.npmjs.org/@xmldom/xmldom/-/xmldom-0.8.13.tgz",
      "integrity": "sha512-KRYzxepc14G/CEpEGc3Yn+JKaAeT63smlDr+vjB8jRfgTBBI9wRj/nkQEO+ucV8p8I9bfKLWp37uHgFrbntPvw==",
      "license": "MIT",
      "engines": {
        "node": ">=10.0.0"
      }
    }
  }
}
package.json
{
  "name": "sample-xmldom-xmlserializer",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "@xmldom/xmldom": "0.8.13"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify @xmldom/xmldom.XMLSerializer in pkg:npm/%40xmldom/xmldom@0.8.13",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40xmldom/xmldom@0.8.13"
  ],
  "symbols": [
    "@xmldom/xmldom.XMLSerializer"
  ]
}
src/index.mjs
import { XMLSerializer, DOMParser } from '@xmldom/xmldom';

export function createSerializer() {
  return new XMLSerializer();
}

export function serializeNode(node) {
  const serializer = new XMLSerializer();
  return serializer.serializeToString(node);
}

export function parseAndSerialize(xmlString, mimeType = 'text/xml') {
  const parser = new DOMParser();
  const doc = parser.parseFromString(xmlString, mimeType);
  const serializer = new XMLSerializer();
  return serializer.serializeToString(doc);
}
test/contract.mjs
import assert from 'node:assert/strict';
import { DOMParser, XMLSerializer } from '@xmldom/xmldom';
import {
  createSerializer,
  serializeNode,
  parseAndSerialize
} from '../src/index.mjs';

const parser = new DOMParser();
const serializer = createSerializer();

// Assertion 1: XMLSerializer instance serializes a DOM Document node with elements, attributes, and text into a well-formed XML string
{
  const xml = '<catalog><book id="bk101"><title>XML Developers Guide</title><price>44.95</price></book></catalog>';
  const doc = parser.parseFromString(xml, 'text/xml');
  const serialized = serializer.serializeToString(doc);
  assert.equal(serialized, xml, 'Document serialization should match original XML');
  assert.equal(parseAndSerialize(xml), xml, 'parseAndSerialize should produce identical output');
}

// Assertion 2: XMLSerializer serializes individual DOM Element and DocumentFragment nodes into XML string representations
{
  const xml = '<root><header>Header Text</header><content>Body Text</content></root>';
  const doc = parser.parseFromString(xml, 'text/xml');

  const headerElem = doc.getElementsByTagName('header')[0];
  const headerXml = serializeNode(headerElem);
  assert.equal(headerXml, '<header>Header Text</header>', 'Element serialization should produce element XML');

  const docFrag = doc.createDocumentFragment();
  const fragChild = doc.createElement('fragment-item');
  fragChild.setAttribute('status', 'active');
  fragChild.textContent = 'Fragment Content';
  docFrag.appendChild(fragChild);

  const fragXml = serializer.serializeToString(docFrag);
  assert.equal(fragXml, '<fragment-item status="active">Fragment Content</fragment-item>', 'DocumentFragment serialization should serialize child elements');
}

// Assertion 3: XMLSerializer serializes Comment, CDATASection, and Text nodes accurately preserving their content
{
  const xml = '<container><!-- A valid comment --><![CDATA[<raw-data>&value</raw-data>]]><info>Sample text</info></container>';
  const doc = parser.parseFromString(xml, 'text/xml');
  const serialized = serializer.serializeToString(doc);

  assert.ok(serialized.includes('<!-- A valid comment -->'), 'Comment should be serialized correctly');
  assert.ok(serialized.includes('<![CDATA[<raw-data>&value</raw-data>]]>'), 'CDATASection should be serialized correctly');
  assert.ok(serialized.includes('<info>Sample text</info>'), 'Text node in element should be serialized correctly');
}

// Assertion 4: XMLSerializer correctly serializes XML namespaces and escapes special characters in attributes and text
{
  const xml = '<ns:root xmlns:ns="https://example.com/ns" attr="A &amp; B &quot;quoted&quot;"><ns:node>&lt;unescaped-tag&gt; &amp; symbol</ns:node></ns:root>';
  const doc = parser.parseFromString(xml, 'text/xml');
  const serialized = serializer.serializeToString(doc);

  assert.ok(serialized.includes('xmlns:ns="https://example.com/ns"'), 'Namespace definition should be preserved');
  assert.ok(serialized.includes('attr="A &amp; B &quot;quoted&quot;"'), 'Attribute special characters should be escaped');
  assert.ok(serialized.includes('&lt;unescaped-tag&gt; &amp; symbol'), 'Text special characters should be escaped');
}

// Assertion 5: XMLSerializer correctly formats self-closing empty XML elements
{
  const xml = '<root><empty/><item flag="true"/><nested><emptyLeaf/></nested></root>';
  const doc = parser.parseFromString(xml, 'text/xml');
  const serialized = serializer.serializeToString(doc);

  assert.equal(serialized, xml, 'Empty elements should be serialized as self-closing tags');
}

Seeder d'origine

anonyme