• Stars
    star
    119
  • Rank 297,930 (Top 6 %)
  • Language
    JavaScript
  • Created about 13 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🚫 minimalistic HTML sanitizer for node.js

bleach

Sanitize your HTML the easy way!

bleach

install

npm install bleach

test

vows --spec test/*

example

Basic:

var bleach = require('bleach');

var html = bleach.sanitize(aBunchOfHTML);

console.log(html);

Advanced:

var bleach = require('bleach');

var whitelist = [
  'a',
  'b',
  'i',
  'em',
  'strong'
]

var options = {
  mode: 'white',
  list: whitelist
}

var html = bleach.sanitize(aBunchOfHTML, options);

console.log(html);

usage

bleach.sanitize(html, options)

Runs HTML through sanitizer and returns sanitized HTML as string.

options may contain the following optional attributes:

  • mode may be set to 'white' or 'black'
  • list is an array containing tags to match against

whitemode will remove all tags from html, excluding those in list

blackmode will remove all tags found in list that are found in html

bleach.analyze(html)

Will extract all tags from HTML and return an array of JSON objects. Example return:

[
  {
    full: '<div id="post-119477">',
    name: 'div',
    attr: [
      "id": "post-119477"
    ]
  },
  ...
]

bleach.filter(html, filters)

SEXY FUN TIME

var nyanFilter = function(input){
  return input.replace('cats', 'nyannyannyan');
}

console.log(
  bleach.filter('cats', nyanFilter)
);

// nyannyannyan
var cutFilter = function(input){
  return input.slice(0, 3);
}

console.log(
  bleach.filter('cats', [
    nyanFilter,
    cutFilter
  ])
);

// nyan

You may also define longer filters and include them in the ./node_modules/bleach/filters directory. A sample filter is included to convert YouTube flash embed objects to iDevice-compatible YouTube iframes.

var html = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="420" height="315" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true"><param name="allowscriptaccess" value="always"><param name="src" value="http://www.youtube.com/v/aU079Mdkenw?version=3&amp;hl=en_US"><param name="allowfullscreen" value="true"><embed type="application/x-shockwave-flash" width="420" height="315" src="http://www.youtube.com/v/aU079Mdkenw?version=3&amp;hl=en_US" allowscriptaccess="always" allowfullscreen="true" id="s_media_1_0" name="s_media_1_0"></object>';

console.log(
  bleach.filter(html, 'youtube')
);

// <iframe type="text/html" frameborder="0" scrolling="no" allowfullscreen src="http://youtube.com/embed/aU079Mdkenw"></iframe>

Refer to the filters directory for the template.

disclaimer

This is not a port of the Python bleach library - in fact their implementations are very different.

license

(The MIT License)

Copyright (c) 2011 Cam Pedersen [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

duino

πŸ’‘ Arduino framework for node.js
JavaScript
955
star
2

node-scp

πŸ“¦ remote file copy wrapper for node.js
JavaScript
75
star
3

morse

βž– simple Morse code library for node
JavaScript
61
star
4

bolt

πŸ”Œ Send messages to any node.js process, anywhere on the Internet.
JavaScript
61
star
5

node-timeago

πŸ•˜ Humanized time for node apps
JavaScript
43
star
6

earth

🌍 dat earth
JavaScript
25
star
7

jaccard

Compute the Jaccard Index (similarity) of two sets
JavaScript
25
star
8

zalgo

he comes
24
star
9

node-nasa

A node.js client for NASA's data API
JavaScript
17
star
10

amphibian

pipe an ssh session to your browser
JavaScript
12
star
11

ekg

advanced process analytics for node.js
JavaScript
12
star
12

broomstick

Lightweight streaming and in-memory caching static file middleware for Director
JavaScript
9
star
13

node-minecraft

A node wrapper for communicating with a Minecraft server through the Bukkit JSONAPI plugin
JavaScript
8
star
14

snow

❄️ snowflakes in your terminal
JavaScript
7
star
15

node-hackernews

a nodejs API client for hacker news
6
star
16

npmtop.com

JavaScript
6
star
17

witch

✨ a spooky little language
JavaScript
5
star
18

knife

Parse shitty JSON!
5
star
19

otter

πŸ™ˆ one time pad lisp toy
Common Lisp
5
star
20

ripstop

Prevent pages on iOS Safari from ripping when using -webkit-overflow-scroll
JavaScript
4
star
21

lumos

turn your terminal into a flashlight
JavaScript
4
star
22

lunr

A RESTful full-text indexer, searcher, and recommendation engine
JavaScript
4
star
23

node-poke

easily loop through a network's mdns broadcasts
JavaScript
4
star
24

neural

JavaScript
4
star
25

crypton-paper

TeX
3
star
26

hax

lolwut
JavaScript
3
star
27

jeans

Opinionated config for nodejs
JavaScript
3
star
28

axe

SHOW YOUR CLUSTER WHO'S BOSS
JavaScript
3
star
29

cry

crypto from the command line
JavaScript
3
star
30

node-weekly

A weekly digest of node.js news
JavaScript
3
star
31

sort.js

Just some sorting algorithms
JavaScript
3
star
32

facelock

πŸ”’ lock your mac when you walk away
Go
3
star
33

bolt-monitor

bolt status monitor - track events passing through bolt inside your browser
JavaScript
3
star
34

aurora

artificial pseduo-intelligence for node.js (like A.L.I.C.E.)
JavaScript
2
star
35

spy

Realtime data syncing made easy
2
star
36

abyss

My Onswipe hackathon entry
JavaScript
2
star
37

ghost

decompose and recompose functions in javascript
2
star
38

bolt-logger

log bolt events flying through your system, easy as pie
JavaScript
2
star
39

crate-old

bundle your node.js app dependencies into your package.json with one swift kick
2
star
40

node-roulette

crash your browser with webrtc and websockets in chrome canary!
JavaScript
2
star
41

node-rickroll

too bad I'm a 90's kid
JavaScript
2
star
42

oakboard

A realtime something
JavaScript
2
star
43

node-tiporskip

2
star
44

dotfiles

Vim Script
1
star
45

ruby-derp

adventures in learning ruby!
Ruby
1
star
46

packfi

Social Network
JavaScript
1
star
47

dimple

Distributed in-memory pleasure
JavaScript
1
star
48

nerdshow

A node script that translates and packages Markdown slideshows
JavaScript
1
star
49

docker-crypton

Shell
1
star
50

nope

stupid-simple, opinionated node proxy engine?
1
star
51

mutiny

move a mysql table to a mongodb collection
JavaScript
1
star
52

ecto.github.com

My personal blog
1
star
53

campedersen.com

JavaScript
1
star
54

memify.me

Create memes of your friends' Facebook pictures
JavaScript
1
star
55

zmk-config

1
star
56

nucleus

Distributed event emission over TCP with autodiscovery via mDNS or Redis
JavaScript
1
star
57

bolt-php

PHP bindings for bolt
PHP
1
star
58

terra

a stupid browser game
1
star
59

tron

A multiplayer lightcycles game for your terminal
JavaScript
1
star
60

ecto

1
star
61

emerald

πŸ€ toy language
C
1
star
62

isomer

web worker proxy
JavaScript
1
star
63

node-football

A probabilistic prediction engine using neural networks and NodeJS
1
star
64

presentation

A Node.js Slideshow created with slidedown
1
star
65

really-old-blog

mirror of campedersen.com
1
star
66

lolcache

Stupid simple asynchronous in-memory caching layer for node
1
star