striwptags v3.1.1
A fork from (https://travis-ci.org/ericnorris/striptags) to strip WP tags.
An implementation of PHP's strip_tags in Node.js.
Features
- Fast
- Zero dependencies
- 100% test code coverage
- No unsafe regular expressions
Installing
npm install stripWPtagsBasic Usage
stripWPtags(html, allowed_tags, tag_replacement);Example
var stripWPtags = require('stripWPtags');
var html =
'[a href="https://example.com"]' +
'lorem ipsum [strong]dolor[/strong] [em]sit[/em] amet' +
'[/a]';
stripWPtags(html);
stripWPtags(html, '[strong]');
stripWPtags(html, ['a']);
stripWPtags(html, [], '\n');Outputs:
'lorem ipsum dolor sit amet'lorem ipsum [strong]dolor[/strong] sit amet''[a href="https://example.com"]lorem ipsum dolor sit amet[/a]'lorem ipsum
dolor
sit ametStreaming Mode
stripWPtags can also operate in streaming mode. Simply call init_streaming_mode to get back a function that accepts HTML and outputs stripped HTML. State is saved between calls so that partial HTML can be safely passed in.
let stream_function = stripWPtags.init_streaming_mode(
allowed_tags,
tag_replacement
);
let partial_text = stream_function(partial_html);
let more_text = stream_function(more_html);Check out test/stripWPtags-test.js for a concrete example.
Tests
You can run tests (powered by mocha) locally via:
npm testGenerate test coverage (powered by istanbul) via :
npm run coverageDoesn't use regular expressions
stripWPtags does not use any regular expressions for stripping HTML tags.
Regular expressions are not capable of preventing all possible scripting attacks (see this). Here is a great StackOverflow answer regarding how strip_tags (when used without specifying allowableTags) is not vulnerable to scripting attacks.
8 years ago