Sha256: 892b782b04be20a083610389ed7bec7171653e22e96b3161ee9b1b5476f1dd43

Contents?: true

Size: 1.05 KB

Versions: 12

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

require 'nokogiri'

module ThemeCheck
  class ValidHTMLTranslation < JsonCheck
    severity :suggestion
    category :translation
    doc docs_url(__FILE__)

    def on_file(file)
      return unless file.name.start_with?("locales/")
      return unless file.content.is_a?(Hash)

      visit_nested(file.content)
    end

    private

    def html_key?(keys)
      pluralized_key = keys[-2] if keys.length > 1
      keys[-1].end_with?('_html') || pluralized_key.end_with?('_html')
    end

    def parse_and_add_offense(key, value)
      return unless value.is_a?(String)

      html = Nokogiri::HTML5.fragment(value, max_errors: -1)
      unless html.errors.empty?
        err_msg = html.errors.join("\n")
        add_offense("'#{key}' contains invalid HTML:\n#{err_msg}")
      end
    end

    def visit_nested(value, keys = [])
      if value.is_a?(Hash)
        value.each do |k, v|
          visit_nested(v, keys + [k])
        end
      elsif html_key?(keys)
        parse_and_add_offense(keys.join('.'), value)
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
theme-check-1.8.0 lib/theme_check/checks/valid_html_translation.rb
theme-check-1.7.2 lib/theme_check/checks/valid_html_translation.rb
theme-check-1.7.1 lib/theme_check/checks/valid_html_translation.rb
theme-check-1.7.0 lib/theme_check/checks/valid_html_translation.rb
theme-check-1.6.2 lib/theme_check/checks/valid_html_translation.rb
theme-check-1.6.1 lib/theme_check/checks/valid_html_translation.rb
theme-check-1.6.0 lib/theme_check/checks/valid_html_translation.rb
theme-check-1.5.2 lib/theme_check/checks/valid_html_translation.rb
theme-check-1.5.1 lib/theme_check/checks/valid_html_translation.rb
theme-check-1.5.0 lib/theme_check/checks/valid_html_translation.rb
theme-check-1.4.0 lib/theme_check/checks/valid_html_translation.rb
theme-check-1.3.0 lib/theme_check/checks/valid_html_translation.rb