class Phlex::HTML
def _attributes(**attributes)
def _attributes(**attributes) ibutes[:href]&.start_with?(/\s*javascript/) utes[:href] = attributes[:href].sub(/^\s*(javascript:)+/, "") = +"" attributes(attributes, buffer: buffer) self.class.rendered_at_least_once :ATTRIBUTE_CACHE[attributes.hash] = buffer.freeze
def _build_attributes(attributes, buffer:)
def _build_attributes(attributes, buffer:) tes.each do |k, v| nless v case k String then k Symbol then k.name.tr("_", "-") k.to_s ct unsafe attribute names. Attribute names are considered unsafe if they match an event attribute or include unsafe characters. L::EVENT_ATTRIBUTES[name] || name.match?(/[<>&"']/) ArgumentError, "Unsafe attribute name detected: #{k}." rue r << " " << name tring r << " " << name << '="' << ERB::Util.html_escape(v) << '"' ymbol r << " " << name << '="' << ERB::Util.html_escape(v.name) << '"' ash d_attributes(v.transform_keys { "#{k}-#{_1.name.tr('_', '-')}" }, buffer: buffer) r << " " << name << '="' << ERB::Util.html_escape(v.to_s) << '"'
def after_template
def after_template
def around_template
def around_template template emplate
def before_template
def before_template
def call(buffer = +"", view_context: nil, parent: nil, &block)
def call(buffer = +"", view_context: nil, parent: nil, &block) return buffer unless render? @_target = buffer @_view_context = view_context @_parent = parent around_template do if block_given? template do |*args| if args.length > 0 yield_content_with_args(*args, &block) else yield_content(&block) end end else template end end self.class.rendered_at_least_once ||= true buffer end
def capture(&block)
def capture(&block) return unless block_given? original_buffer = @_target new_buffer = +"" @_target = new_buffer yield @_target = original_buffer new_buffer end
def comment(&block)
def comment(&block) @_target << "<!-- " yield_content(&block) @_target << " -->" nil end
def doctype
def doctype @_target << DOCTYPE nil end
def format
def format :html end
def format_object(oject)
def format_object(oject)
def render(renderable, *args, **kwargs, &block)
def render(renderable, *args, **kwargs, &block) case renderable when Phlex::HTML renderable.call(@_target, view_context: @_view_context, parent: self, &block) when Class if renderable < Phlex::HTML renderable.new.call(@_target, view_context: @_view_context, parent: self, &block) end else raise ArgumentError, "You can't render a #{renderable}." end nil end
def render?
def render?
def text(content)
def text(content) @_target << ERB::Util.html_escape( case content when String then content when Symbol then content.name when Integer, Float then content.to_s else format_object(content) || content.to_s end ) nil end
def unsafe_raw(content = nil, &block)
def unsafe_raw(content = nil, &block) @_target << (content || instance_exec(&block)) nil end
def whitespace
def whitespace @_target << " " if block_given? yield @_target << " " end nil end
def yield_content(&block)
def yield_content(&block) unless block_given? l_length = @_target.length = yield(self) ed = (original_length == @_target.length) anged ontent tring get << ERB::Util.html_escape(content) ymbol get << ERB::Util.html_escape(content.name) nteger, Float get << ERB::Util.html_escape(content.to_s) ormatted_object = format_object(content)) rget << ERB::Util.html_escape(formatted_object)
def yield_content_with_args(*args, &block)
def yield_content_with_args(*args, &block) unless block_given? l_length = @_target.length = yield(*args) ed = (original_length == @_target.length) anged ontent tring get << ERB::Util.html_escape(content) ymbol get << ERB::Util.html_escape(content.name) nteger, Float get << ERB::Util.html_escape(content.to_s) ormatted_object = format_object(content)) rget << ERB::Util.html_escape(formatted_object)