Sha256: 090bdd6793dd01eb1603a8e7230449a23eaefc3f799b7a004265c4ddbceec4f3
Contents?: true
Size: 1.63 KB
Versions: 8
Compression:
Stored size: 1.63 KB
Contents
require 'volt/page/bindings/base_binding' require 'volt/page/bindings/html_safe/string_extension' module Volt class ContentBinding < BaseBinding HTML_ESCAPE_REGEXP = /[&"'><\n]/ HTML_ESCAPE = { '&' => '&', '>' => '>', '<' => '<', '"' => '"', "'" => ''', "\n" => "<br />\n" } def initialize(page, target, context, binding_name, getter) super(page, target, context, binding_name) # Listen for changes @computation = -> do begin res = @context.instance_eval(&getter) rescue => e Volt.logger.error("ContentBinding Error: #{e.inspect}") '' end end.watch_and_resolve! do |result| update(result) end end def update(value) value = (value || '').to_s unless value.is_a?(String) html_safe = value.html_safe? # Exception values display the exception as a string value = value.to_s # Update the html in this section # TODO: Move the formatter into another class. # The html safe check lets us know that if string can be rendered # directly as html unless html_safe # Escape any < and >, but convert newlines to br's, and fix quotes and value = html_escape(value) end # Assign the content dom_section.html = value # dom_section.text = value end def html_escape(str) # https://github.com/opal/opal/issues/798 str.gsub(HTML_ESCAPE_REGEXP) do |char| HTML_ESCAPE[char] end end def remove if @computation @computation.stop @computation = nil end super end end end
Version data entries
8 entries across 8 versions & 1 rubygems