CodeSampleX

サンプル

escape-html 1.0.3

検証済みサンプル — npm escape-html 1.0.3. node 22 · linux alpine/x64 · docker で contract を実行し、成功しました: escapeHtml escapes ampersand, double quote, single quote…

sha256:edb76669327daa550aada9a622c0e0ef9e1b775ad5fd6b5fbbc1bc4270e01ed9

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

実行証拠

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

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

検証実行環境

環境 コントラクト ステージ 実行日
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-20

ケース

HOW
ゴール
verify pkg:npm/escape-html@1.0.3
パッケージ
環境
node 22.23.2
作成日
2026-08-20T13:13:25Z

コントラクト

  1. escapeHtml escapes ampersand, double quote, single quote, less-than, and greater-than characters
  2. escapeHtml leaves strings without special HTML characters unchanged
  3. escapeHtml correctly handles strings containing only special characters or repeated characters
  4. escapeHtml converts non-string primitives and objects to strings before escaping
  5. escapeHtml handles empty strings returning an empty string

ファイル

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

ソースアーティファクトをダウンロード (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 pkg:npm/escape-html@1.0.3
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/escape-html@1.0.3
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:22f53021d6f0be25c46846deef91b6c96b1bf062cdd6cd6498fc809320575b3a","constraints":{"executionContext":"node"},"contract":["escapeHtml escapes ampersand, double quote, single quote, less-than, and greater-than characters","escapeHtml leaves strings without special HTML characters unchanged","escapeHtml correctly handles strings containing only special characters or repeated characters","escapeHtml converts non-string primitives and objects to strings before escaping","escapeHtml handles empty strings returning an empty string"],"goal":"verify pkg:npm/escape-html@1.0.3","kind":"HOW","packages":["pkg:npm/escape-html@1.0.3"],"schemaVersion":1},"contractCommand":["node","test/contract.cjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/escape-html@1.0.3"],"schemaVersion":1,"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "escape-html-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "escape-html-sample",
      "version": "1.0.0",
      "dependencies": {
        "escape-html": "1.0.3"
      }
    },
    "node_modules/escape-html": {
      "version": "1.0.3",
      "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz",
      "integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "escape-html-sample",
  "version": "1.0.0",
  "description": "Minimal sample verifying escape-html",
  "main": "src/index.js",
  "scripts": {
    "test": "node test/contract.cjs"
  },
  "dependencies": {
    "escape-html": "1.0.3"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/escape-html@1.0.3",
  "kind": "HOW",
  "packages": [
    "pkg:npm/escape-html@1.0.3"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.js
'use strict';

const escapeHtml = require('escape-html');

/**
 * Escapes special HTML characters in the provided string.
 *
 * @param {any} input - Value to escape.
 * @returns {string} - Escaped HTML string.
 */
function escape(input) {
  return escapeHtml(input);
}

/**
 * Creates an HTML attribute string with an escaped attribute value.
 *
 * @param {string} name - Attribute name.
 * @param {any} value - Attribute value to escape.
 * @returns {string} - Formatted attribute string.
 */
function escapeAttribute(name, value) {
  return `${name}="${escapeHtml(value)}"`;
}

/**
 * Creates an HTML element tag with escaped text content.
 *
 * @param {string} tagName - HTML tag name.
 * @param {any} content - Content to escape.
 * @returns {string} - Formatted HTML tag string.
 */
function escapeTag(tagName, content) {
  return `<${tagName}>${escapeHtml(content)}</${tagName}>`;
}

/**
 * Escapes an array of string values.
 *
 * @param {any[]} items - Array of values to escape.
 * @returns {string[]} - Array of escaped HTML strings.
 */
function escapeMultiple(items) {
  if (!Array.isArray(items)) {
    return [];
  }
  return items.map((item) => escapeHtml(item));
}

module.exports = {
  escape,
  escapeAttribute,
  escapeTag,
  escapeMultiple,
  escapeHtml,
};
test/contract.cjs
const assert = require('node:assert/strict');
const escapeHtml = require('escape-html');
const {
  escape,
  escapeAttribute,
  escapeTag,
  escapeMultiple,
} = require('../src/index.js');

async function main() {
  // Contract 1: escapeHtml escapes ampersand, double quote, single quote, less-than, and greater-than characters
  {
    assert.strictEqual(escapeHtml('&'), '&amp;');
    assert.strictEqual(escapeHtml('"'), '&quot;');
    assert.strictEqual(escapeHtml("'"), '&#39;');
    assert.strictEqual(escapeHtml('<'), '&lt;');
    assert.strictEqual(escapeHtml('>'), '&gt;');
    assert.strictEqual(
      escapeHtml('<script>alert("xss") & \'test\'</script>'),
      '&lt;script&gt;alert(&quot;xss&quot;) &amp; &#39;test&#39;&lt;/script&gt;'
    );
    assert.strictEqual(escape('&'), '&amp;');
    assert.strictEqual(escape('"'), '&quot;');
    assert.strictEqual(escape("'"), '&#39;');
    assert.strictEqual(escape('<'), '&lt;');
    assert.strictEqual(escape('>'), '&gt;');
    assert.strictEqual(
      escapeAttribute('data-title', 'foo & "bar" <baz>'),
      'data-title="foo &amp; &quot;bar&quot; &lt;baz&gt;"'
    );
    assert.strictEqual(
      escapeTag('p', 'Tom & Jerry > "Spike"'),
      '<p>Tom &amp; Jerry &gt; &quot;Spike&quot;</p>'
    );
  }

  // Contract 2: escapeHtml leaves strings without special HTML characters unchanged
  {
    assert.strictEqual(escapeHtml('hello world'), 'hello world');
    assert.strictEqual(escapeHtml('abcxyz 1234567890'), 'abcxyz 1234567890');
    assert.strictEqual(escapeHtml('/path/to/file.ext'), '/path/to/file.ext');
    assert.strictEqual(escape('hello world'), 'hello world');
    assert.deepStrictEqual(
      escapeMultiple(['foo', 'bar', 'baz']),
      ['foo', 'bar', 'baz']
    );
  }

  // Contract 3: escapeHtml correctly handles strings containing only special characters or repeated characters
  {
    assert.strictEqual(escapeHtml('""""'), '&quot;&quot;&quot;&quot;');
    assert.strictEqual(escapeHtml('<<<<'), '&lt;&lt;&lt;&lt;');
    assert.strictEqual(escapeHtml('>>>>'), '&gt;&gt;&gt;&gt;');
    assert.strictEqual(escapeHtml('&&&&'), '&amp;&amp;&amp;&amp;');
    assert.strictEqual(escapeHtml("''''"), '&#39;&#39;&#39;&#39;');
    assert.strictEqual(
      escapeHtml('&<>"\'&<>"\''),
      '&amp;&lt;&gt;&quot;&#39;&amp;&lt;&gt;&quot;&#39;'
    );
    assert.deepStrictEqual(
      escapeMultiple(['<b>bold</b>', 'a & b', '"quote"']),
      ['&lt;b&gt;bold&lt;/b&gt;', 'a &amp; b', '&quot;quote&quot;']
    );
  }

  // Contract 4: escapeHtml converts non-string primitives and objects to strings before escaping
  {
    assert.strictEqual(escapeHtml(12345), '12345');
    assert.strictEqual(escapeHtml(0), '0');
    assert.strictEqual(escapeHtml(true), 'true');
    assert.strictEqual(escapeHtml(false), 'false');
    assert.strictEqual(escapeHtml(null), 'null');
    assert.strictEqual(escapeHtml(undefined), 'undefined');
    assert.strictEqual(
      escapeHtml({
        toString: () => '<custom & object>',
      }),
      '&lt;custom &amp; object&gt;'
    );
  }

  // Contract 5: escapeHtml handles empty strings returning an empty string
  {
    assert.strictEqual(escapeHtml(''), '');
    assert.strictEqual(escape(''), '');
    assert.deepStrictEqual(escapeMultiple([]), []);
    assert.deepStrictEqual(escapeMultiple(null), []);
  }

  console.log('All contracts verified successfully.');
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});

オリジンシーダー

匿名