Class: Trenni::Sanitize::Filter
- Inherits:
-
Object
- Object
- Trenni::Sanitize::Filter
- Defined in:
- lib/trenni/sanitize/filter.rb
Overview
Provides a high level interface for parsing markup.
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
-
#current ⇒ Object
readonly
The current node being parsed.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
Class Method Summary collapse
Instance Method Summary collapse
- #attribute(key, value) ⇒ Object
- #cdata(string) ⇒ Object
- #close_tag(name, offset = nil) ⇒ Object
- #comment(string) ⇒ Object
- #doctype(string) ⇒ Object
- #filter(tag) ⇒ Object
-
#initialize(output, entities) ⇒ Filter
constructor
A new instance of Filter.
- #instruction(string) ⇒ Object
- #open_tag_begin(name, offset) ⇒ Object
- #open_tag_end(self_closing) ⇒ Object
- #parse!(input) ⇒ Object
- #parse_begin ⇒ Object
- #parse_end ⇒ Object
- #text(string) ⇒ Object
- #top ⇒ Object
Constructor Details
#initialize(output, entities) ⇒ Filter
Returns a new instance of Filter.
75 76 77 78 79 80 81 82 83 84 85 86 |
# 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 |
Instance Attribute Details
#current ⇒ Object (readonly)
The current node being parsed.
91 92 93 |
# File 'lib/trenni/sanitize/filter.rb', line 91 def current @current end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
88 89 90 |
# File 'lib/trenni/sanitize/filter.rb', line 88 def output @output end |
#stack ⇒ Object (readonly)
Returns the value of attribute stack.
93 94 95 |
# File 'lib/trenni/sanitize/filter.rb', line 93 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
124 125 126 |
# File 'lib/trenni/sanitize/filter.rb', line 124 def attribute(key, value) @current.tag.attributes[key] = value end |
#cdata(string) ⇒ Object
169 170 171 |
# File 'lib/trenni/sanitize/filter.rb', line 169 def cdata(string) @output << string unless current.skip? CDATA end |
#close_tag(name, offset = nil) ⇒ Object
143 144 145 146 147 148 149 150 151 |
# 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) ⇒ Object
161 162 163 |
# File 'lib/trenni/sanitize/filter.rb', line 161 def comment(string) @output << string unless current.skip? COMMENT end |
#doctype(string) ⇒ Object
157 158 159 |
# File 'lib/trenni/sanitize/filter.rb', line 157 def doctype(string) @output << string unless current.skip? DOCTYPE end |
#filter(tag) ⇒ Object
153 154 155 |
# File 'lib/trenni/sanitize/filter.rb', line 153 def filter(tag) return tag end |
#instruction(string) ⇒ Object
165 166 167 |
# File 'lib/trenni/sanitize/filter.rb', line 165 def instruction(string) @output << string unless current.skip? INSTRUCTION end |
#open_tag_begin(name, offset) ⇒ Object
118 119 120 121 122 |
# 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) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# 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) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# 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 ⇒ Object
109 110 |
# File 'lib/trenni/sanitize/filter.rb', line 109 def parse_begin end |
#parse_end ⇒ Object
112 113 114 115 116 |
# File 'lib/trenni/sanitize/filter.rb', line 112 def parse_end while @stack.size > 1 close_tag(@stack.last.name) end end |
#text(string) ⇒ Object
173 174 175 |
# File 'lib/trenni/sanitize/filter.rb', line 173 def text(string) Markup.append(@output, string) unless current.skip? TEXT end |
#top ⇒ Object
95 96 97 |
# File 'lib/trenni/sanitize/filter.rb', line 95 def top @stack.last || @top end |