Class: Trenni::Sanitize::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/trenni/sanitize/filter.rb

Overview

Provides a high level interface for parsing markup.

Direct Known Subclasses

Fragment, Text

Defined Under Namespace

Classes: Node

Constant Summary collapse

TAG =
1
DOCTYPE =
2
COMMENT =
4
INSTRUCTION =
8
CDATA =
16
TEXT =
32
CONTENT =
DOCTYPE | COMMENT | INSTRUCTION | CDATA | TEXT
ALL =
TAG | CONTENT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, entities) ⇒ Filter

Returns a new instance of Filter.



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/trenni/sanitize/filter.rb', line 71

def initialize(output, entities)
	@output = output
	
	@entities = entities
	
	@current = nil
	@stack = []
	
	@current = @top = Node.new(nil, nil, 0)
	
	@skip = nil
end

Instance Attribute Details

#currentObject (readonly)

The current node being parsed.



87
88
89
# File 'lib/trenni/sanitize/filter.rb', line 87

def current
  @current
end

#outputObject (readonly)

Returns the value of attribute output.



84
85
86
# File 'lib/trenni/sanitize/filter.rb', line 84

def output
  @output
end

#stackObject (readonly)

Returns the value of attribute stack.



89
90
91
# File 'lib/trenni/sanitize/filter.rb', line 89

def stack
  @stack
end

Class Method Details

.parse(input, output = nil, entities = Trenni::Entities::HTML5) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# 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

Instance Method Details

#attribute(key, value) ⇒ Object



120
121
122
# File 'lib/trenni/sanitize/filter.rb', line 120

def attribute(key, value)
	@current.tag.attributes[key] = value
end

#cdata(string) ⇒ Object



165
166
167
# File 'lib/trenni/sanitize/filter.rb', line 165

def cdata(string)
	@output << string unless current.skip? CDATA
end

#close_tag(name, offset = nil) ⇒ Object



139
140
141
142
143
144
145
146
147
# File 'lib/trenni/sanitize/filter.rb', line 139

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) ⇒ Object



157
158
159
# File 'lib/trenni/sanitize/filter.rb', line 157

def comment(string)
	@output << string unless current.skip? COMMENT
end

#doctype(string) ⇒ Object



153
154
155
# File 'lib/trenni/sanitize/filter.rb', line 153

def doctype(string)
	@output << string unless current.skip? DOCTYPE
end

#filter(tag) ⇒ Object



149
150
151
# File 'lib/trenni/sanitize/filter.rb', line 149

def filter(tag)
	return tag
end

#instruction(string) ⇒ Object



161
162
163
# File 'lib/trenni/sanitize/filter.rb', line 161

def instruction(string)
	@output << string unless current.skip? INSTRUCTION
end

#open_tag_begin(name, offset) ⇒ Object



114
115
116
117
118
# File 'lib/trenni/sanitize/filter.rb', line 114

def open_tag_begin(name, offset)
	tag = Tag.new(name, false, {})
	
	@current = Node.new(name, tag, current.skip)
end

#open_tag_end(self_closing) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/trenni/sanitize/filter.rb', line 124

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) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/trenni/sanitize/filter.rb', line 95

def parse!(input)
	parse_begin
	
	Trenni::Parsers.parse_markup(input, self, @entities)
	
	parse_end
	
	return self
end

#parse_beginObject



105
106
# File 'lib/trenni/sanitize/filter.rb', line 105

def parse_begin
end

#parse_endObject



108
109
110
111
112
# File 'lib/trenni/sanitize/filter.rb', line 108

def parse_end
	while @stack.size > 1
		close_tag(@stack.last.name)
	end
end

#text(string) ⇒ Object



169
170
171
# File 'lib/trenni/sanitize/filter.rb', line 169

def text(string)
	Markup.append(@output, string) unless current.skip? TEXT
end

#topObject



91
92
93
# File 'lib/trenni/sanitize/filter.rb', line 91

def top
	@stack.last || @top
end