Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.0.4 / 2022-09-16
==================

* build: support ESM module system

1.0.3 / 2015-09-01
==================

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ The `escapeHtml` function is designed to accept a string input of text and
return an escaped value to interpolate into HTML.

```js
var escapeHtml = require('escape-html')
var escapeHtml = require('escape-html') // CommonJS require
// import escapeHtml from 'escape-html' // ESM import

// example values
var desc = 'I <b>think</b> this is good.'
Expand Down
78 changes: 78 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*!
* escape-html
* Copyright(c) 2012-2013 TJ Holowaychuk
* Copyright(c) 2015 Andreas Lubbe
* Copyright(c) 2015 Tiancheng "Timothy" Gu
* MIT Licensed
*/

/**
* Module variables.
* @private
*/

var matchHtmlRegExp = /["'&<>]/;

/**
* Module exports.
* @public
*/

var oss = escapeHtml;

/**
* Escape special characters in the given string of text.
*
* @param {string} string The string to escape for inserting into HTML
* @return {string}
* @public
*/

function escapeHtml (string) {
var str = '' + string;
var match = matchHtmlRegExp.exec(str);

if (!match) {
return str
}

var escape;
var html = '';
var index = 0;
var lastIndex = 0;

for (index = match.index; index < str.length; index++) {
switch (str.charCodeAt(index)) {
case 34: // "
escape = '&quot;';
break
case 38: // &
escape = '&amp;';
break
case 39: // '
escape = '&#39;';
break
case 60: // <
escape = '&lt;';
break
case 62: // >
escape = '&gt;';
break
default:
continue
}

if (lastIndex !== index) {
html += str.substring(lastIndex, index);
}

lastIndex = index + 1;
html += escape;
}

return lastIndex !== index
? html + str.substring(lastIndex, index)
: html
}

export { oss as default };
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@
"utility"
],
"repository": "component/escape-html",
"exports": {
".": {
"require": "./index.js",
"import": "./index.mjs"
}
},
"devDependencies": {
"benchmark": "2.1.4",
"@rollup/plugin-commonjs": "^22.0.2",
"beautify-benchmark": "0.2.4",
"benchmark": "2.1.4",
"eslint": "5.12.0",
"eslint-config-standard": "12.0.0",
"eslint-plugin-import": "2.14.0",
Expand All @@ -20,7 +27,8 @@
"eslint-plugin-promise": "4.0.1",
"eslint-plugin-standard": "4.0.0",
"mocha": "5.2.0",
"nyc": "13.1.0"
"nyc": "13.1.0",
"rollup": "^2.79.0"
},
"files": [
"HISTORY.md",
Expand All @@ -32,6 +40,7 @@
"bench": "node benchmark/index.js",
"lint": "eslint --plugin markdown --ext js,md .",
"test": "mocha test/",
"test-cov": "nyc --reporter=html --reporter=text npm test"
"test-cov": "nyc --reporter=html --reporter=text npm test",
"roll": "rollup --config rollup.config.mjs"
}
}
11 changes: 11 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from "rollup";
import commonjs from "@rollup/plugin-commonjs";

export default defineConfig({
input: "./index.js",
output: {
file: "index.mjs",
format: "es",
},
plugins: [commonjs()],
});