Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

decomment

Package Overview
Dependencies
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

decomment - npm Package Compare versions

Comparing version 0.5.1 to 0.6.0

test/textSpec.js

20

lib/index.js

@@ -5,10 +5,22 @@ 'use strict';

function main(text, options) {
return parser(text, options, false);
function main(code, options) {
return parser(code, options, {
parse: true // need to parse;
});
}
main.css = function (text, options) {
return parser(text, options, true);
main.text = function (text, options) {
return parser(text, options, {
parse: false, // do not parse;
html: false // treat as plain text;
});
};
main.html = function (html, options) {
return parser(html, options, {
parse: false, // do not parse;
html: true // treat as HTML;
});
};
module.exports = main;

60

lib/parser.js

@@ -6,6 +6,6 @@ 'use strict';

function decomment(text, options, css) {
function decomment(code, options, config) {
if (typeof text !== 'string') {
throw new TypeError("Parameter 'text' must be a string.");
if (typeof code !== 'string') {
throw new TypeError("Parameter 'code' must be a text string.");
}

@@ -18,29 +18,31 @@

var idx = 0, // current index;
s = '', // resulting text;
len = text.length, // text length;
s = '', // resulting code;
len = code.length, // code length;
emptyLine = true, // set while no symbols encountered on the current line;
emptyLetters = '', // empty letters on a new line;
isHtml = false, // set when the input is recognized as HTML;
optTrim = options && options.trim, // 'trim' option;
optSafe = options && options.safe, // 'safe' option.
regEx = []; // regular expression details;
regEx = [], // regular expression details;
isHtml; // set when the input is recognized as HTML;
if (!len) {
return text;
return code;
}
if (!css) {
isHtml = utils.isHtml(text);
if (config.parse) {
isHtml = utils.isHtml(code);
if (!isHtml) {
regEx = utils.parseRegEx(text);
regEx = utils.parseRegEx(code);
}
} else {
isHtml = config.html;
}
do {
if (!isHtml && text[idx] === '/' && idx < len - 1 && (!idx || text[idx - 1] !== '\\')) {
if (text[idx + 1] === '/') {
if (!isHtml && code[idx] === '/' && idx < len - 1 && (!idx || code[idx - 1] !== '\\')) {
if (code[idx + 1] === '/') {
if (utils.indexInRegEx(idx, regEx)) {
continue;
}
var lb = text.indexOf(EOL, idx + 2);
var lb = code.indexOf(EOL, idx + 2);
if (lb < 0) {

@@ -59,13 +61,13 @@ break;

if (text[idx + 1] === '*') {
if (code[idx + 1] === '*') {
if (utils.indexInRegEx(idx, regEx)) {
continue;
}
var end = text.indexOf('*/', idx + 2);
var keep = optSafe && idx < len - 2 && text[idx + 2] === '!';
var end = code.indexOf('*/', idx + 2);
var keep = optSafe && idx < len - 2 && code[idx + 2] === '!';
if (keep) {
if (end >= 0) {
s += text.substr(idx, end - idx + 2);
s += code.substr(idx, end - idx + 2);
} else {
s += text.substr(idx, len - idx);
s += code.substr(idx, len - idx);
}

@@ -80,3 +82,3 @@ }

if (!keep) {
var lb = text.indexOf(EOL, idx + 1);
var lb = code.indexOf(EOL, idx + 1);
if (lb > idx) {

@@ -92,4 +94,4 @@ idx = lb + EOL.length - 1; // last symbol of the line break;

if (isHtml && text[idx] === '<' && idx < len - 3 && text.substr(idx + 1, 3) === '!--') {
var end = text.indexOf('-->', idx + 4);
if (isHtml && code[idx] === '<' && idx < len - 3 && code.substr(idx + 1, 3) === '!--') {
var end = code.indexOf('-->', idx + 4);
if (end < 0) {

@@ -101,3 +103,3 @@ break;

emptyLetters = '';
var lb = text.indexOf(EOL, idx + 1);
var lb = code.indexOf(EOL, idx + 1);
if (lb > idx) {

@@ -111,6 +113,6 @@ idx = lb + EOL.length - 1; // last symbol of the line break;

var symbol = text[idx];
var symbol = code[idx];
var isSpace = symbol === ' ' || symbol === '\t';
if (symbol === '\r' || symbol === '\n') {
if (text.indexOf(EOL, idx) === idx) {
if (code.indexOf(EOL, idx) === idx) {
emptyLine = true;

@@ -132,3 +134,3 @@ }

if (!isHtml && (symbol === '\'' || symbol === '"' || symbol === '`')) {
var closeIdx = text.indexOf(symbol, idx + 1);
var closeIdx = code.indexOf(symbol, idx + 1);
if (utils.indexInRegEx(idx, regEx)) {

@@ -140,3 +142,3 @@ continue;

}
s += text.substr(idx + 1, closeIdx - idx);
s += code.substr(idx + 1, closeIdx - idx);
idx = closeIdx;

@@ -152,5 +154,5 @@ }

startIdx = idx + 1;
endIdx = text.indexOf(EOL, startIdx);
endIdx = code.indexOf(EOL, startIdx);
i = startIdx;
while ((text[i] === ' ' || text[i] === '\t') && ++i < endIdx);
while ((code[i] === ' ' || code[i] === '\t') && ++i < endIdx);
if (i === endIdx) {

@@ -157,0 +159,0 @@ idx = endIdx + EOL.length - 1;

@@ -8,7 +8,9 @@ 'use strict';

var result = [];
// NOTE: Even though we do not need the location details,
// using option `loc` makes `tokenize` perform 40% faster.
esprima.tokenize(code, {loc: true, range: true}, function (node) {
if (node.type === 'RegularExpression') {
result.push({
start: node.range[0],
end: node.range[1]
start: node.range[0] + 1,
end: node.range[1] - 1
});

@@ -15,0 +17,0 @@ }

{
"name": "decomment",
"version": "0.5.1",
"description": "Removes comments from JSON, JavaScript, CSS and HTML.",
"version": "0.6.0",
"description": "Removes comments from JSON, JavaScript, CSS, HTML, etc.",
"main": "lib/index.js",

@@ -29,3 +29,4 @@ "scripts": {

"CSS",
"HTML"
"HTML",
"Text"
],

@@ -32,0 +33,0 @@ "author": {

decomment
=========
Removes comments from JSON, JavaScript, CSS and HTML.
Removes comments from JSON, JavaScript, CSS, HTML, etc.

@@ -31,12 +31,10 @@ [![Build Status](https://travis-ci.org/vitaly-t/decomment.svg?branch=master)](https://travis-ci.org/vitaly-t/decomment)

var text = "var t; // comments";
var code = "var t; // comments";
decomment(text); //=> var t;
decomment(code); //=> var t;
```
Specifically for CSS, call `decomment.css(text, [options])` instead.
## Features
* Removes both single and multi-line comments from JSON, JavaScript and CSS
* Removes both single and multi-line comments from JSON, JavaScript and CSS/Text
* Automatically recognizes HTML and removes all `<!-- comments -->` from it

@@ -59,4 +57,14 @@ * Does not change layout / formatting of the original document

#### decomment(text, [options]) ⇒ String
### decomment(code, [options]) ⇒ String
This method first parses `code` to determine whether it is an HTML (starts with `<`),
and if so, removes all `<!-- comment -->` entries from it, according to `options`.
When `code` is not recognized as HTML, it is assumed to be either JSON or JavaScript.
In this case the code is parsed through [esprima] for ECMAScript 6 compliance, and
to extract details about regular expressions.
If [esprima] fails to validate the code, it will throw a parsing error. When successful,
this method will remove `//` and `/**/` comments according to the `options` (see below).
##### options.trim ⇒ Boolean

@@ -66,8 +74,9 @@ * `false (default)` - do not trim comments

Examples:
Example:
```js
var text = "/* comment */\r\n\r\n var test = 123";
decomment(text); //=> \r\n var test = 123
decomment(text, {trim: true}); //=> var test = 123
var decomment = require('decomment');
var code = "/* comment */\r\n\r\n var test = 123";
decomment(code); //=> \r\n var test = 123
decomment(code, {trim: true}); //=> var test = 123
```

@@ -79,8 +88,9 @@

Examples:
Example:
```js
var text = "/*! special */ js code /* normal */";
decomment(text); //=> js code
decomment(text, {safe: true}); //=> /*! special */ js code
var decomment = require('decomment');
var code = "/*! special */ var a; /* normal */";
decomment(code); //=> var a;
decomment(code, {safe: true}); //=> /*! special */ var a;
```

@@ -90,6 +100,28 @@

#### decomment.css(text, [options]) ⇒ String
### decomment.text(text, [options]) ⇒ String
The same as **decomment**, but specific to CSS.
Unlike the default **decomment**, it instructs the library that `text` is not
a JSON, JavaScript or HTML, rather a plain text that needs no parsing or validation,
only to remove `//` and `/**/` comments from it according to the `options`.
CSS is the most frequent example of where this method is to be used.
Example:
```js
var decomment = require('decomment');
var text = "cssClass{color:Red;}// comments";
decomment.text(text); //=> cssClass{color:Red;}
```
Please note that while comment blocks located inside `''`, `""` or \`\` are not removed,
the same as for JSON or JavaScript, you should not use this method for JSON or JavaScript,
as it can break your regular expressions.
### decomment.html(html, [options]) ⇒ String
Unlike the default **decomment** method, it instructs the library not to parse
or validate the input in any way, rather assume it to be HTML, and remove all
`<!-- comment -->` entries from it according to the `options`.
## License

@@ -96,0 +128,0 @@

@@ -13,3 +13,3 @@ 'use strict';

decomment();
}).toThrow(new TypeError("Parameter 'text' must be a string."));
}).toThrow(new TypeError("Parameter 'code' must be a text string."));
});

@@ -16,0 +16,0 @@ });

@@ -54,2 +54,7 @@ 'use strict';

describe("Explicit HTML call", function () {
it("must process it as HTML always", function () {
expect(decomment.html("text<!-- comment -->")).toBe("text");
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc