Class: RbbCode::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/rbbcode/schema.rb

Instance Method Summary collapse

Constructor Details

#initializeSchema

Returns a new instance of Schema.



158
159
160
161
162
163
164
165
# File 'lib/rbbcode/schema.rb', line 158

def initialize
	@allowed_tags = DEFAULT_ALLOWED_TAGS.dup
	@forbidden_descent = {}
	@required_parents = {}
	@child_requirements = {}
	@no_text = []
	use_defaults
end

Instance Method Details

#allow_descent(ancestor, descendant) ⇒ Object

:nodoc:



108
109
110
111
112
# File 'lib/rbbcode/schema.rb', line 108

def allow_descent(ancestor, descendant) #:nodoc:
	if @forbidden_descent.has_key?(descendant.to_s) and @forbidden_descent[descendant.to_s].include?(ancestor.to_s)
		@forbidden_descent[descendant.to_s].delete(ancestor.to_s)
	end
end

#allow_tag(*tag_names) ⇒ Object Also known as: allow_tags



114
115
116
117
118
119
120
# File 'lib/rbbcode/schema.rb', line 114

def allow_tag(*tag_names)
	tag_names.each do |tag_name|
		unless @allowed_tags.include?(tag_name.to_s)
			@allowed_tags << tag_name.to_s
		end
	end
end

#allow_text(tag_name) ⇒ Object



122
123
124
# File 'lib/rbbcode/schema.rb', line 122

def allow_text(tag_name)
	@no_text.delete(tag_name.to_s)
end

#block_level?(tag_name) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/rbbcode/schema.rb', line 126

def block_level?(tag_name)
	DEFAULT_BLOCK_LEVEL_ELEMENTS.include?(tag_name.to_s)
end

#clearObject



132
133
134
135
136
137
# File 'lib/rbbcode/schema.rb', line 132

def clear
	@allowed_tags = []
	@forbidden_descent = {}
	@required_parents = {}
	@no_text = []
end

#forbid_children_except(parent, children) ⇒ Object



139
140
141
# File 'lib/rbbcode/schema.rb', line 139

def forbid_children_except(parent, children)
	@child_requirements[parent.to_s] = children.collect { |c| c.to_s }
end

#forbid_descent(ancestor, descendant) ⇒ Object

:nodoc:



143
144
145
146
147
148
# File 'lib/rbbcode/schema.rb', line 143

def forbid_descent(ancestor, descendant) #:nodoc:
	@forbidden_descent[descendant.to_s] ||= []
	unless @forbidden_descent[descendant.to_s].include?(ancestor.to_s)
		@forbidden_descent[descendant.to_s] << ancestor.to_s
	end
end

#forbid_tag(name) ⇒ Object



150
151
152
# File 'lib/rbbcode/schema.rb', line 150

def forbid_tag(name)
	@allowed_tags.delete(name.to_s)
end

#forbid_text(tag_name) ⇒ Object



154
155
156
# File 'lib/rbbcode/schema.rb', line 154

def forbid_text(tag_name)
	@no_text << tag_name.to_s unless @no_text.include?(tag_name.to_s)
end

#line_break_tag_nameObject



167
168
169
# File 'lib/rbbcode/schema.rb', line 167

def line_break_tag_name
	'br'
end

#paragraph_tag_nameObject



171
172
173
# File 'lib/rbbcode/schema.rb', line 171

def paragraph_tag_name
	'p'
end

#require_parents(parents, child) ⇒ Object

:nodoc:



175
176
177
178
179
180
181
182
# File 'lib/rbbcode/schema.rb', line 175

def require_parents(parents, child) #:nodoc:
	@required_parents[child.to_s] = parents.collect { |p| p.to_s }
	parents.each do |parent|
		if @forbidden_descent.has_key?(child.to_s)
			@forbidden_descent[child.to_s].delete(parent)
		end
	end
end

#tag(name) ⇒ Object



184
185
186
# File 'lib/rbbcode/schema.rb', line 184

def tag(name)
	SchemaTag.new(self, name)
end

#tag_valid_in_context?(tag_name, ancestors) ⇒ Boolean

Returns:

  • (Boolean)


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rbbcode/schema.rb', line 188

def tag_valid_in_context?(tag_name, ancestors)
	return false unless @allowed_tags.include?(tag_name.to_s)
	if @required_parents.has_key?(tag_name.to_s) and !@required_parents[tag_name.to_s].include?(ancestors[0].to_s)
		return false
	end
	if @child_requirements.has_key?(ancestors[0].to_s) and !@child_requirements[ancestors[0].to_s].include?(tag_name.to_s)
		return false
	end
	if @forbidden_descent.has_key?(tag_name.to_s)
		@forbidden_descent[tag_name.to_s].each do |forbidden_ancestor|
			return false if ancestors.include?(forbidden_ancestor)
		end
	end
	return true
end

#textObject



204
205
206
# File 'lib/rbbcode/schema.rb', line 204

def text
	SchemaText.new(self)
end

#text_valid_in_context?(*ancestors) ⇒ Boolean

Returns:

  • (Boolean)


208
209
210
211
212
213
# File 'lib/rbbcode/schema.rb', line 208

def text_valid_in_context?(*ancestors)
	if @no_text.include?(ancestors[0].to_s)
		return false
	end
	return true
end

#unrequire_parent(parent, child) ⇒ Object



215
216
217
# File 'lib/rbbcode/schema.rb', line 215

def unrequire_parent(parent, child)
	@required_parents.delete(child.to_s)
end

#use_defaultsObject



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/rbbcode/schema.rb', line 219

def use_defaults
	tag('br').must_be_empty
	tag('p').may_not_be_nested
	tag('b').may_not_be_nested
	tag('i').may_not_be_nested
	tag('u').may_not_be_nested
	tag('url').may_not_be_nested
	tag('img').may_not_be_nested
	tag('code').may_not_be_nested
	tag('p').may_not_be_nested
	tag('*').must_be_child_of('list')
	tag('list').may_not_descend_from('p')
	tag('list').may_only_be_parent_of('*')
	tag('list').may_not_contain_text
end