Sha256: c88ec8b5436e7f82f1ac05c854662465c8a48eaef53eeef44f34543578994d15

Contents?: true

Size: 1.15 KB

Versions: 18

Compression:

Stored size: 1.15 KB

Contents

module Wovnrb
  class InputReplacer < ReplacerBase
    def initialize(text_index)
      @text_index = text_index
    end

    def replace(dom, lang)
      dom.xpath('//input').each do |node|
        next if wovn_ignore?(node)

        set_attribute('value', node, lang)       if replaceable_value? node
        set_attribute('placeholder', node, lang) if replaceable_placeholder? node
      end
    end

    private

    def set_attribute(name, node, lang)
      node_value = node.get_attribute(name).strip
      if @text_index[node_value] && @text_index[node_value][lang.lang_code] && @text_index[node_value][lang.lang_code].size > 0
        node.set_attribute(name, replace_text(node_value, @text_index[node_value][lang.lang_code][0]['data']))
      end
    end

    def replaceable_value?(node)
      return false unless ['submit', 'reset'].include? node.get_attribute('type')

      attribute_value = node.get_attribute('value')
      attribute_value && !attribute_value.empty?
    end

    def replaceable_placeholder?(node)
      attribute_placeholder = node.get_attribute('placeholder')
      attribute_placeholder && !attribute_placeholder.empty?
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
wovnrb-1.0.6 lib/wovnrb/html_replacers/input_replacer.rb
wovnrb-1.0.5 lib/wovnrb/html_replacers/input_replacer.rb
wovnrb-1.0.4 lib/wovnrb/html_replacers/input_replacer.rb
wovnrb-1.0.2 lib/wovnrb/html_replacers/input_replacer.rb
wovnrb-1.0.1 lib/wovnrb/html_replacers/input_replacer.rb
wovnrb-1.0.0 lib/wovnrb/html_replacers/input_replacer.rb
wovnrb-0.2.30 lib/wovnrb/html_replacers/input_replacer.rb
wovnrb-0.2.29 lib/wovnrb/html_replacers/input_replacer.rb
wovnrb-0.2.28 lib/wovnrb/html_replacers/input_replacer.rb
wovnrb-0.2.27 lib/wovnrb/html_replacers/input_replacer.rb
wovnrb-0.2.26 lib/wovnrb/html_replacers/input_replacer.rb
wovnrb-0.2.25 lib/wovnrb/html_replacers/input_replacer.rb
wovnrb-0.2.24 lib/wovnrb/html_replacers/input_replacer.rb
wovnrb-0.2.23 lib/wovnrb/html_replacers/input_replacer.rb
wovnrb-0.2.22 lib/wovnrb/html_replacers/input_replacer.rb
wovnrb-0.2.21 lib/wovnrb/html_replacers/input_replacer.rb
wovnrb-0.2.20 lib/wovnrb/html_replacers/input_replacer.rb
wovnrb-0.2.19 lib/wovnrb/html_replacers/input_replacer.rb