XRB::Sanitize GuidesGetting Started

Getting Started

This guide explains how to get started with the XRB::Sanitize gem.

Installation

Add the gem to your project:

$ bundle add xrb-sanitize

Core Concepts

Extracting Text

You can extract text using something similar to the following parser delegate:

class Text < XRB::Sanitize::Filter
	def filter(node)
		# Skip any nodes that aren't text:
		node.skip!(TAG)
	end
	
	def doctype(string)
		# Ignore doctype.
	end
	
	def instruction(string)
		# Ignore processing instructions.
	end
end

text = Text.parse("<p>Hello World</p>").output
# => "Hello World"

Extracting Safe Markup

Here is a simple filter that only allows a limited set of tags:

class Fragment < XRB::Sanitize::Filter
	STANDARD_ATTRIBUTES = ['class'].freeze
	
	ALLOWED_TAGS = {
		'em' => [],
		'strong' => [],
		'p' => [],
		'img' => ['src', 'alt', 'width', 'height'],
		'a' => ['href']
	}.freeze
	
	def filter(node)
		if attributes = ALLOWED_TAGS[node.name]
			node.tag.attributes.slice!(*attributes)
		else
			# Skip the tag, and all contents
			node.skip!(ALL)
		end
	end
	
	def doctype(string)
	end
	
	def instruction(string)
	end
end