CodeSampleX

サンプル

@xmldom/xmldom 0.8.13: XMLSerializer

検証済みサンプル — npm @xmldom/xmldom 0.8.13: XMLSerializer. node 22 · linux alpine/x64 · docker で contract を実行し、成功しました: XMLSerializer instance serializes a DOM…

sha256:0d23ff0511e419682567775db4692666b7ad3ebf770108957c97f0f47be7c87e

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
2
ビルドした署名鍵
1
宣言された環境 linux 24 · ubuntu · glibc 2.39 x64 npm

検証実行環境

環境 コントラクト ステージ 実行日
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

ケース

HOW
ゴール
verify @xmldom/xmldom.XMLSerializer in pkg:npm/%40xmldom/xmldom@0.8.13
パッケージ
シンボル
  • @xmldom/xmldom.XMLSerializer
作成日
2026-08-23T14:04:49Z

コントラクト

  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

ファイル

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

ソースアーティファクトをダウンロード (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 @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');
}

オリジンシーダー

匿名