An open API service indexing awesome lists of open source software.

https://github.com/pseudomuto/safe_htmlify

Safely display user-generated HTML on your web page
https://github.com/pseudomuto/safe_htmlify

Last synced: 9 months ago
JSON representation

Safely display user-generated HTML on your web page

Awesome Lists containing this project

README

          

# SafeHtmlify

[![Build Status](https://travis-ci.org/pseudomuto/safe_htmlify.png)](https://travis-ci.org/pseudomuto/safe_htmlify)

A jQuery plugin that strips HTML from strings except in cases where you say otherwise!

The main purpose is to be able to show user-generated HTML on a web page without having to worry about scripts/styles breaking your whole site.

Things to note:

* only allows specifically whitelisted tags and attributes, no other markup
* script tags are never allowed (even if you try to whitelist `script`)

## Installation

* Clone this repo and cd to the working directory
* Run `npm install` to download packages
* Run `lineman build` to build the script
* Copy `dist/js/safe_htmlify.js` to your project

*I intend to make this simpler in the future...*

## Usage

By default all HTML will be removed

var safeHTML = $.safeHtmlify('

Some message here

');
// safeHTML === 'Some message here'

You can whitelist tags by supplying an options hash with `tags`

var safeHTML = $.safeHtmlify('

Some message here

', {
tags: {
p: []
}
});

// safeHTML === '

Some message here

'

You can allow attributes on individual tags in the options hash

var safeHTML = $.safeHtmlify('

Some message here

', {
tags: {
p: ['class']
}
});

// safeHTML === '

Some message here

'

You can globally allow attributes (on any whitelisted tag) by supplying an options hash with `globalAttributes`

var safeHTML = $.safeHtmlify('

Some message here

', {
tags: {
p: []
},
globalAttributes: ['class']
});

// safeHTML === '

Some message here

'

## Contributing

Same as always...fork it, change it, push it, pull it.

### Running Samples

lineman run

Then open `http://localhost:8000`

### Running the tests

For this you'll want to have two terminals available

* In one terminal, run `lineman run` - this will watch for changes and build as necessary
* In another terminal, run `lineman spec` or `lineman spec-ci`