Skip to content

Commit

Permalink
Update mocha version and rewrite tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericnorris committed Feb 1, 2017
1 parent 86e95d2 commit adf53b6
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 123 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules/
coverage.html
coverage/
52 changes: 27 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
{
"name": "striptags",
"description": "PHP strip_tags in Node.js",
"license": "MIT",
"author": "Eric Norris (https://github.com/ericnorris)",
"repository": {
"type": "git",
"url": "https://github.com/ericnorris/striptags.git"
},
"main": "src/striptags.js",
"homepage": "https://github.com/ericnorris/striptags",
"bugs": "https://github.com/ericnorris/striptags/issues",
"version": "2.2.1",
"devDependencies": {
"mocha": "~2.1.0"
},
"keywords": [
"striptags",
"strip_tags",
"html",
"strip",
"tags"
],
"scripts": {
"test": "node ./node_modules/mocha/bin/mocha",
}
"name": "striptags",
"description": "PHP strip_tags in Node.js",
"license": "MIT",
"author": "Eric Norris (https://github.com/ericnorris)",
"repository": {
"type": "git",
"url": "https://github.com/ericnorris/striptags.git"
},
"main": "src/striptags.js",
"homepage": "https://github.com/ericnorris/striptags",
"bugs": "https://github.com/ericnorris/striptags/issues",
"version": "2.2.1",
"devDependencies": {
"istanbul": "^0.4.5",
"mocha": "^3.2.0"
},
"keywords": [
"striptags",
"strip_tags",
"html",
"strip",
"tags"
],
"scripts": {
"test": "mocha",
"coverage": "istanbul cover _mocha -- -R spec"
}
}
8 changes: 6 additions & 2 deletions src/striptags.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,14 @@
if (typeof define === 'function' && define.amd) {
// AMD
define([], striptags);
} else if (typeof module === 'object' && module.exports) {
}

else if (typeof module === 'object' && module.exports) {
// Node
module.exports = striptags;
} else {
}

else {
// Browser
global.striptags = striptags;
}
Expand Down
198 changes: 103 additions & 95 deletions test/striptags-test.js
Original file line number Diff line number Diff line change
@@ -1,140 +1,148 @@
'use strict';
/* global describe, it */

let assert = require('assert');
let fs = require('fs');
let vm = require('vm');
let striptags = require('../');

var assert = require('assert'),
striptags = require('../');

/* global describe, it */
describe('striptags', function() {
it('should not modify plain text', function() {
var text = 'lorem ipsum < a>';
describe('#module', function() {
let path = require.resolve('../');
let src = fs.readFileSync(path);
let script = new vm.Script(src);

assert.equal(striptags(text), text);
});
it('should define a node module', function() {
let module = { exports: {} };

it('should remove simple HTML tags', function() {
var html = '<a href="">lorem <strong>ipsum</strong></a>',
text = 'lorem ipsum';
script.runInNewContext({module});

assert.equal(striptags(html), text);
});
assert.notEqual(module.exports, {});
});

it('should leave HTML tags if specified', function() {
var html = '<strong>lorem ipsum</strong>',
allowedTags = '<strong>';
it('should define an amd module', function() {
let global = {};
let define = function(dependencies, module) {
global.defined = module;
};

assert.equal(striptags(html, allowedTags), html);
});

it('should leave attributes when allowing HTML', function() {
var html = '<a href="https://example.com">lorem ipsum</a>',
allowedTags = '<a>';
define.amd = true;

assert.equal(striptags(html, allowedTags), html);
});
script.runInNewContext({global, define});

it('should leave nested HTML tags if specified', function() {
var html = '<div>lorem <strong>ipsum</strong></div>',
strippedHtml = 'lorem <strong>ipsum</strong>',
allowedTags = '<strong>';
assert.notEqual(global.defined, null);
});

assert.equal(striptags(html, allowedTags), strippedHtml);
});
it('should define a browser global', function() {
let global = {};

it('should leave outer HTML tags if specified', function() {
var html = '<div>lorem <strong>ipsum</strong></div>',
strippedHtml = '<div>lorem ipsum</div>',
allowedTags = '<div>';
script.runInNewContext(global);

assert.equal(striptags(html, allowedTags), strippedHtml);
assert.notEqual(global.striptags, null);
});
});

it('should remove DOCTYPE declaration', function() {
var html = '<!DOCTYPE html> lorem ipsum',
text = ' lorem ipsum';
describe('with no optional parameters', function() {
it('should not strip invalid tags', function() {
let text = 'lorem ipsum < a> < div>';

assert.equal(striptags(html), text);
});
assert.equal(striptags(text), text);
});

it('should remove comments', function() {
var html = '<!-- lorem ipsum --> dolor sit amet',
text = ' dolor sit amet';
it('should remove simple HTML tags', function() {
let html = '<a href="">lorem <strong>ipsum</strong></a>',
text = 'lorem ipsum';

assert.equal(striptags(html), text);
});
assert.equal(striptags(html), text);
});

it('should strip <> within quotes', function() {
var html = '<a href="<script>">lorem ipsum</a>',
strippedHtml = '<a href="script">lorem ipsum</a>',
allowedTags = '<a>';
it('should remove comments', function() {
let html = '<!-- lorem -- ipsum -- --> dolor sit amet',
text = ' dolor sit amet';

assert.equal(striptags(html, allowedTags), strippedHtml);
});
assert.equal(striptags(html), text);
});

it('should strip extra < within tags', function() {
var html = '<div<>>lorem ipsum</div>',
strippedHtml = '<div>lorem ipsum</div>',
allowedTags = '<div>';
it('should strip tags within comments', function() {
let html = '<!-- <strong>lorem ipsum</strong> --> dolor sit',
text = ' dolor sit';

assert.equal(striptags(html), text);
});

assert.equal(striptags(html, allowedTags), strippedHtml);
});

it('should strip tags within comments', function() {
var html = '<!-- <strong>lorem ipsum</strong> --> dolor sit',
text = ' dolor sit';
it('should not fail with nested quotes', function() {
let html = '<article attr="foo \'bar\'">lorem</article> ipsum',
text = 'lorem ipsum';

assert.equal(striptags(html), text);
assert.equal(striptags(html), text);
});
});

it('should strip comment-like tags', function() {
var html = '<! lorem ipsum> dolor sit',
text = ' dolor sit';
describe('#allowed_tags', function() {
it('should parse a string', function() {
let html = '<strong>lorem ipsum</strong>',
allowed_tags = '<strong>';

assert.equal(striptags(html), text);
});
assert.equal(striptags(html, allowed_tags), html);
});

it('should leave normal exclamation points alone', function() {
var text = 'lorem ipsum! dolor sit amet';
it('should take an array', function() {
let html = '<strong>lorem <em>ipsum</em></strong>',
allowed_tags = ['strong', 'em'];

assert.equal(striptags(text), text);
assert.equal(striptags(html, allowed_tags), html);
});
});

it('should allow an array parameter for allowable tags', function() {
var html = '<strong>lorem <em>ipsum</em></strong>',
allowedTags = ['strong', 'em'];
describe('with allowable_tags parameter', function() {
it('should leave attributes when allowing HTML', function() {
let html = '<a href="https://example.com">lorem ipsum</a>',
allowed_tags = '<a>';

assert.equal(striptags(html, allowedTags), html);
});
assert.equal(striptags(html, allowed_tags), html);
});

it('should strip tags when an empty array is provided', function() {
var html = '<article>lorem <a href="#">ipsum</a></article>',
allowedTags = [],
text = 'lorem ipsum';
it('should strip extra < within tags', function() {
let html = '<div<>>lorem ipsum</div>',
text = '<div>lorem ipsum</div>',
allowed_tags = '<div>';

assert.equal(striptags(html, allowedTags), text);
});
assert.equal(striptags(html, allowed_tags), text);
});

it('should not fail with nested quotes', function() {
var html = '<article attr="foo \'bar\'">lorem</article> ipsum',
allowedTags = [],
text = 'lorem ipsum';
it('should strip <> within quotes', function() {
let html = '<a href="<script>">lorem ipsum</a>',
text = '<a href="script">lorem ipsum</a>',
allowed_tags = '<a>';

assert.equal(striptags(html, allowedTags), text);
assert.equal(striptags(html, allowed_tags), text);
});
});

it('should strip the tag\'s properties and attributes', function() {
var html = '<a href="http://google.com" title="foo" data-id="0">Click here</a>',
allowedTags = [],
text = 'Click here';
describe('with tag_replacement parameter', function() {
it('should replace tags with that parameter', function() {
var html = 'Line One<br>Line Two',
allowed_tags = [],
tag_replacement = '\n',
text = 'Line One\nLine Two';

assert.equal(striptags(html, allowedTags), text);
assert.equal(striptags(html, allowed_tags, tag_replacement), text);
});
});

it('should replace with the tagReplacement parameter', function() {
var html = 'Line One<br>Line Two',
allowedTags = [],
tagReplacement = '\n',
text = 'Line One\nLine Two';
describe('#streaming_mode', function() {
it('should strip streamed HTML', function() {
let striptags_stream = striptags.init_streaming_mode();

let part_one = striptags_stream('lorem ipsum <stro');
let part_two = striptags_stream('ng>dolor sit <');
let part_three = striptags_stream(' amet');

assert.equal(striptags(html, allowedTags, tagReplacement), text);
assert.equal(part_one, 'lorem ipsum ');
assert.equal(part_two, 'dolor sit ');
assert.equal(part_three, '< amet');
});
});
});

0 comments on commit adf53b6

Please sign in to comment.