class Trenni::Sanitize::Filter

Provides a high level interface for parsing markup.

Constants

ALL
CDATA
COMMENT
CONTENT
DOCTYPE
INSTRUCTION
Node
TAG
TEXT

Attributes

current[R]

The current node being parsed.

output[R]
stack[R]

Public Class Methods

new(output, entities) click to toggle source
# File lib/trenni/sanitize/filter.rb, line 75
def initialize(output, entities)
        @output = output
        
        @entities = entities
        
        @current = nil
        @stack = []
        
        @current = @top = Node.new(nil, nil, 0)
        
        @skip = nil
end
parse(input, output = nil, entities = Trenni::Entities::HTML5) click to toggle source
# File lib/trenni/sanitize/filter.rb, line 40
def self.parse(input, output = nil, entities = Trenni::Entities::HTML5)
        # This allows us to handle passing in a string:
        input = Trenni::Buffer(input)
        
        output ||= MarkupString.new.force_encoding(input.encoding)
        
        delegate = self.new(output, entities)
        
        delegate.parse!(input)
        
        return delegate
end

Public Instance Methods

attribute(key, value) click to toggle source
# File lib/trenni/sanitize/filter.rb, line 124
def attribute(key, value)
        @current.tag.attributes[key] = value
end
cdata(string) click to toggle source
# File lib/trenni/sanitize/filter.rb, line 169
def cdata(string)
        @output << string unless current.skip? CDATA
end
close_tag(name, offset = nil) click to toggle source
# File lib/trenni/sanitize/filter.rb, line 143
def close_tag(name, offset = nil)
        while node = @stack.pop
                node.tag.write_closing_tag(@output) unless node.skip? TAG
                
                break if node.name == name
        end
        
        @current = self.top
end
comment(string) click to toggle source
# File lib/trenni/sanitize/filter.rb, line 161
def comment(string)
        @output << string unless current.skip? COMMENT
end
doctype(string) click to toggle source
# File lib/trenni/sanitize/filter.rb, line 157
def doctype(string)
        @output << string unless current.skip? DOCTYPE
end
filter(tag) click to toggle source
# File lib/trenni/sanitize/filter.rb, line 153
def filter(tag)
        return tag
end
instruction(string) click to toggle source
# File lib/trenni/sanitize/filter.rb, line 165
def instruction(string)
        @output << string unless current.skip? INSTRUCTION
end
open_tag_begin(name, offset) click to toggle source
# File lib/trenni/sanitize/filter.rb, line 118
def open_tag_begin(name, offset)
        tag = Tag.new(name, false, {})
        
        @current = Node.new(name, tag, current.skip)
end
open_tag_end(self_closing) click to toggle source
# File lib/trenni/sanitize/filter.rb, line 128
def open_tag_end(self_closing)
        if self_closing
                @current.tag.closed = true
        else
                @stack << @current
        end
        
        filter(@current)
        
        @current.tag.write_opening_tag(@output) unless @current.skip? TAG
        
        # If the tag was self-closing, it's no longer current at this point, we are back in the context of the parent tag.
        @current = self.top if self_closing
end
parse!(input) click to toggle source
# File lib/trenni/sanitize/filter.rb, line 99
def parse!(input)
        parse_begin
        
        Trenni::Parsers.parse_markup(input, self, @entities)
        
        parse_end
        
        return self
end
parse_begin() click to toggle source
# File lib/trenni/sanitize/filter.rb, line 109
def parse_begin
end
parse_end() click to toggle source
# File lib/trenni/sanitize/filter.rb, line 112
def parse_end
        while @stack.size > 1
                close_tag(@stack.last.name)
        end
end
text(string) click to toggle source
# File lib/trenni/sanitize/filter.rb, line 173
def text(string)
        Markup.append(@output, string) unless current.skip? TEXT
end
top() click to toggle source
# File lib/trenni/sanitize/filter.rb, line 95
def top
        @stack.last || @top
end