/usr/lib/ruby/vendor_ruby/hpricot/builder.rb is in ruby-hpricot 0.8.6-5ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | require 'hpricot/tags'
require 'fast_xs'
require 'hpricot/blankslate'
require 'hpricot/htmlinfo'
module Hpricot
# XML unescape
def self.uxs(str)
str.to_s.
gsub(/\&(\w+);/) { [NamedCharacters[$1] || 63].pack("U*") }. # 63 = ?? (query char)
gsub(/\&\#(\d+);/) { [$1.to_i].pack("U*") }.
gsub(/\&\#x([0-9a-fA-F]+);/) { [$1.to_i(16)].pack("U*") }
end
def self.build(ele = Doc.new, assigns = {}, &blk)
ele.extend Builder
assigns.each do |k, v|
ele.instance_variable_set("@#{k}", v)
end
ele.instance_eval(&blk)
ele
end
module Builder
@@default = {
:indent => 0,
:output_helpers => true,
:output_xml_instruction => true,
:output_meta_tag => true,
:auto_validation => true,
:tagset => Hpricot::XHTMLTransitional,
:root_attributes => {
:xmlns => 'http://www.w3.org/1999/xhtml', :'xml:lang' => 'en', :lang => 'en'
}
}
def self.set(option, value)
@@default[option] = value
end
def add_child ele
ele.parent = self
self.children ||= []
self.children << ele
ele
end
# Write a +string+ to the HTML stream, making sure to escape it.
def text!(string)
add_child Text.new(string.fast_xs)
end
# Write a +string+ to the HTML stream without escaping it.
def text(string)
add_child Text.new(string)
nil
end
alias_method :<<, :text
alias_method :concat, :text
# Create a tag named +tag+. Other than the first argument which is the tag name,
# the arguments are the same as the tags implemented via method_missing.
def tag!(tag, *args, &block)
ele_id = nil
if @auto_validation and @tagset
if !@tagset.tagset.has_key?(tag)
raise InvalidXhtmlError, "no element `#{tag}' for #{tagset.doctype}"
elsif args.last.respond_to?(:to_hash)
attrs = args.last.to_hash
if @tagset.forms.include?(tag) and attrs[:id]
attrs[:name] ||= attrs[:id]
end
attrs.each do |k, v|
atname = k.to_s.downcase.intern
unless k =~ /:/ or @tagset.tagset[tag].include? atname
raise InvalidXhtmlError, "no attribute `#{k}' on #{tag} elements"
end
if atname == :id
ele_id = v.to_s
if @elements.has_key? ele_id
raise InvalidXhtmlError, "id `#{ele_id}' already used (id's must be unique)."
end
end
end
end
end
# turn arguments into children or attributes
childs = []
attrs = args.grep(Hash)
childs.concat((args - attrs).flatten.map do |x|
if x.respond_to? :to_html
Hpricot.make(x.to_html)
elsif x
Text.new(x.fast_xs)
end
end.flatten)
attrs = attrs.inject({}) do |hsh, ath|
ath.each do |k, v|
hsh[k] = v.to_s.fast_xs if v
end
hsh
end
# create the element itself
tag = tag.to_s
f = Elem.new(tag, attrs, childs, ETag.new(tag))
# build children from the block
if block
build(f, &block)
end
add_child f
f
end
def build(*a, &b)
Hpricot.build(*a, &b)
end
# Every HTML tag method goes through an html_tag call. So, calling <tt>div</tt> is equivalent
# to calling <tt>html_tag(:div)</tt>. All HTML tags in Hpricot's list are given generated wrappers
# for this method.
#
# If the @auto_validation setting is on, this method will check for many common mistakes which
# could lead to invalid XHTML.
def html_tag(sym, *args, &block)
if @auto_validation and @tagset.self_closing.include?(sym) and block
raise InvalidXhtmlError, "the `#{sym}' element is self-closing, please remove the block"
elsif args.empty? and block.nil?
CssProxy.new(self, sym)
else
tag!(sym, *args, &block)
end
end
XHTMLTransitional.tags.each do |k|
class_eval %{
def #{k}(*args, &block)
html_tag(#{k.inspect}, *args, &block)
end
}
end
def doctype(target, pub, sys)
add_child DocType.new(target, pub, sys)
end
remove_method :head
# Builds a head tag. Adds a <tt>meta</tt> tag inside with Content-Type
# set to <tt>text/html; charset=utf-8</tt>.
def head(*args, &block)
tag!(:head, *args) do
tag!(:meta, "http-equiv" => "Content-Type", "content" => "text/html; charset=utf-8") if @output_meta_tag
instance_eval(&block)
end
end
# Builds an html tag. An XML 1.0 instruction and an XHTML 1.0 Transitional doctype
# are prepended. Also assumes <tt>:xmlns => "http://www.w3.org/1999/xhtml",
# :lang => "en"</tt>.
def xhtml_transitional(attrs = {}, &block)
# self.tagset = Hpricot::XHTMLTransitional
xhtml_html(attrs, &block)
end
# Builds an html tag with XHTML 1.0 Strict doctype instead.
def xhtml_strict(attrs = {}, &block)
# self.tagset = Hpricot::XHTMLStrict
xhtml_html(attrs, &block)
end
private
def xhtml_html(attrs = {}, &block)
instruct! if @output_xml_instruction
doctype(:html, *@@default[:tagset].doctype)
tag!(:html, @@default[:root_attributes].merge(attrs), &block)
end
end
# Class used by Markaby::Builder to store element options. Methods called
# against the CssProxy object are added as element classes or IDs.
#
# See the README for examples.
class CssProxy < BlankSlate
# Creates a CssProxy object.
def initialize(builder, sym)
@builder, @sym, @attrs = builder, sym, {}
end
# Adds attributes to an element. Bang methods set the :id attribute.
# Other methods add to the :class attribute.
def method_missing(id_or_class, *args, &block)
if (idc = id_or_class.to_s) =~ /!$/
@attrs[:id] = $`
else
@attrs[:class] = @attrs[:class].nil? ? idc : "#{@attrs[:class]} #{idc}".strip
end
if block or args.any?
args.push(@attrs)
return @builder.tag!(@sym, *args, &block)
end
return self
end
end
end
|