Class: Arrow::HTMLTag

Inherits:
HTMLToken show all
Defined in:
lib/arrow/htmltokenizer.rb

Overview

Class for tokens output by Arrow::HTMLTokenizer for the tags in an HTML document.

Constant Summary collapse

AttributePattern =

The pattern for matching tag attribute key-value pairs

%r{
	\s*([-A-Za-z:]+)
	(?:\s*=\s*(
		"(?:[^"]|\\.)*" |		# Match strings quoted with "
		'(?:[^']|\\.)*' |		# Match strings quoted with '
		\S+						# Match non-whitespace
	))?
}mx

Instance Attribute Summary collapse

Attributes inherited from HTMLToken

#raw

Instance Method Summary collapse

Methods inherited from HTMLToken

#css_class

Methods inherited from Object

deprecate_class_method, deprecate_method, inherited

Constructor Details

#initialize(raw) ⇒ HTMLTag

Create a new HTMLTag from the specified raw source.



216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/arrow/htmltokenizer.rb', line 216

def initialize( raw )
	unless (( match = /<\s*(\/)?(\w+)\s*([^>]*)>/.match(raw) ))
		raise ArgumentError,
			"Malformed HTMLTag: %p" % raw
	end

	@endtag = !match[1].nil?
	@tagname = match[2]
	@rawattrs = match[3] || ''
	@attrs = nil

	super
end

Instance Attribute Details

#tagnameObject (readonly)

The name of the tag



236
237
238
# File 'lib/arrow/htmltokenizer.rb', line 236

def tagname
  @tagname
end

Instance Method Details

#[](name) ⇒ Object

Return the tag attribute with the specified name (if it exists).



269
270
271
# File 'lib/arrow/htmltokenizer.rb', line 269

def []( name )
	self.attrs[ name.gsub(/-/, '_').downcase.to_sym ]
end

#attrsObject

Return the Hash of tag attributes belonging to this token.



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/arrow/htmltokenizer.rb', line 243

def attrs
	unless @attrs
		@attrs = {}
		@rawattrs.scan( AttributePattern ) {|name,value|
			ns = nil
			if /:/ =~ name
				ns, name = name.split(/:/, 2)
				if ns == 'html' then ns = nil end
			end
			cname = name.gsub(/-/, '_').downcase
			cval = value.nil? ? true : value.gsub(/^["']|['"]$/, '')

			if ns.nil?
				@attrs[ cname.to_sym ] = cval
			else
				@attrs[ ns.to_sym ] ||= {}
				@attrs[ ns.to_sym ][ name.to_sym ] = cval
			end
		}
	end

	return @attrs
end

#endtag?Boolean

Returns true if this tag is an closing tag

Returns:

  • (Boolean)


239
# File 'lib/arrow/htmltokenizer.rb', line 239

def endtag?; @endtag; end

#escape_html(string) ⇒ Object

Escape special characters in the given string for display in an HTML inspection interface.



312
313
314
315
316
317
318
319
# File 'lib/arrow/htmltokenizer.rb', line 312

def escape_html( string )
	return "nil" if string.nil?
	string = string.inspect unless string.is_a?( String )
	string.
		gsub(/&/, '&amp;').
		gsub(/</, '&lt;').
		gsub(/>/, '&gt;')
end

#to_htmlObject

Return an HTML fragment that can be used to represent the token symbolically in a web-based introspection interface.



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/arrow/htmltokenizer.rb', line 276

def to_html
	tagopen, tagbody = @raw.split( /\s+/, 2 )
	# self.log.debug "tagopen = %p, tagbody = %p" % [ tagopen, tagbody ]

	tagopen = self.escape_html( tagopen ).sub( %r{^&lt;(/)?(\w+)} ) {|match|
		%Q{&lt;#$1<span class="tag-token-name">#$2</span>}
	}

	unless tagbody.nil?
		tagbody.sub!( />$/, '' )
		tagbody = self.escape_html( tagbody ).gsub( AttributePattern ) {|match|
			name, mid, val = match.split(/(\s*=\s*)/, 2)

			val.gsub!( /(\[\?(?:[^\?]|\?(?!\]))+\?\])/s ) {|m|
				%q{<span class="%s">%s</span>} %
					[ 'tag-attr-directive', m ]
			}

			%q{<span class="%s">%s</span>%s<span class="%s">%s</span>} % [
				'tag-token-attr-name',
				name,
				mid,
				'tag-token-attr-value',
				val,
			]
		}
		tagbody << '&gt;'
	end

	#self.log.debug "tagopen = %p; tagbody = %p" %
	#	[ tagopen, tagbody ]
	super { [tagopen, tagbody].compact.join(" ") }
end