class Phlex::HTML
def _attributes(**attributes)
def _attributes(**attributes) if attributes[:href]&.start_with?(/\s*javascript/) attributes[:href] = attributes[:href].sub(/^\s*(javascript:)+/, "") end buffer = +"" _build_attributes(attributes, buffer: buffer) unless self.class.rendered_at_least_once Phlex::ATTRIBUTE_CACHE[attributes.hash] = buffer.freeze end buffer end
def _build_attributes(attributes, buffer:)
def _build_attributes(attributes, buffer:) attributes.each do |k, v| next unless v name = case k when String k when Symbol k.name.tr("_", "-") else k.to_s end if HTML::EVENT_ATTRIBUTES[name] || name.match?(/[<>&"']/) raise ArgumentError, "Unsafe attribute name detected: #{k}." end case v when true buffer << " " << name when String buffer << " " << name << '="' << Hescape.escape_html(v) << '"' when Symbol buffer << " " << name << '="' << Hescape.escape_html(v.name) << '"' when Hash _build_attributes(v.transform_keys { "#{k}-#{_1.name.tr('_', '-')}" }, buffer: buffer) else buffer << " " << name << '="' << Hescape.escape_html(v.to_s) << '"' end end buffer end
def _output(content)
def _output(content) case content when String then Hescape.escape_html(content) when Symbol then Hescape.escape_html(content.name) else Hescape.escape_html(content.to_s) end end
def append=(value)
def append=(value) return unless value if value.html_safe? self.safe_append = value else @_target << case value when String then Hescape.escape_html(value) when Symbol then Hescape.escape_html(value.name) else Hescape.escape_html(value.to_s) end end end
def call(buffer = +"", view_context: nil, parent: nil, &block)
def call(buffer = +"", view_context: nil, parent: nil, &block) return buffer unless render? raise "The same view instance shouldn't be rendered twice" if rendered? @_rendered = true @_target = buffer @_view_context = view_context @_parent = parent @output_buffer = self template(&block) 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.html_safe end
def comment(content = "")
def comment(content = "") @_target << "<!-- " << Hescape.escape_html(content.to_s) << " -->" nil end
def doctype
def doctype @_target << DOCTYPE nil end
def helpers
def helpers @_view_context end
def html_safe?
def html_safe? true end
def render?
def render? true end
def rendered?
def rendered? @_rendered ||= false end
def safe_append=(value)
def safe_append=(value) return unless value @_target << case value when String then value when Symbol then value.name else value.to_s end end
def text(content)
def text(content) @_target << _output(content) 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) return unless block_given? original_length = @_target.length output = yield(self) unchanged = (original_length == @_target.length) if unchanged case output when String, Symbol, Integer, Float text(output) end end nil end