Sha256: 3ad07ce23bf0deeab4e06963cb9a7f3f533ac6eb05c34f419fa2ab52df41a178

Contents?: true

Size: 1.32 KB

Versions: 19

Compression:

Stored size: 1.32 KB

Contents

require 'uri'

module SCSSLint
  # Checks the format of URLs for unnecessary protocols or domains.
  class Linter::UrlFormat < Linter
    include LinterRegistry

    def visit_script_funcall(node)
      return unless node.name == 'url'

      if url_string?(node.args[0])
        url = node.args[0].value.value.to_s
        check_url(url, node)
      end

      yield
    end

    def visit_prop(node)
      if url_literal?(node.value)
        url = node.value.to_sass.sub(/^url\((.*)\)$/, '\\1')
        check_url(url, node)
      end

      yield
    end

  private

    def url_literal?(prop_value)
      return unless prop_value.is_a?(Sass::Script::Tree::Literal)
      return unless prop_value.value.is_a?(Sass::Script::Value::String)
      return unless prop_value.value.type == :identifier

      prop_value.to_sass.start_with?('url(')
    end

    def url_string?(arg)
      return unless arg.is_a?(Sass::Script::Tree::Literal)
      return unless arg.value.is_a?(Sass::Script::Value::String)

      arg.value.type == :string
    end

    def check_url(url, node)
      return if url.start_with?('data:')
      uri = URI(url)

      if uri.scheme || uri.host
        add_lint(node, "URL `#{url}` should not contain protocol or domain")
      end
    rescue URI::Error => ex
      add_lint(node, "Invalid URL `#{url}`: #{ex}")
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
scss_lint-0.52.0 lib/scss_lint/linter/url_format.rb
scss_lint-0.51.0 lib/scss_lint/linter/url_format.rb
scss_lint-0.50.3 lib/scss_lint/linter/url_format.rb
scss_lint-0.50.2 lib/scss_lint/linter/url_format.rb
scss_lint-0.50.1 lib/scss_lint/linter/url_format.rb
scss_lint-0.50.0 lib/scss_lint/linter/url_format.rb
scss_lint-0.49.0 lib/scss_lint/linter/url_format.rb
scss_lint-0.48.0 lib/scss_lint/linter/url_format.rb
scss_lint-0.47.1 lib/scss_lint/linter/url_format.rb
scss_lint-0.47.0 lib/scss_lint/linter/url_format.rb
scss_lint-0.46.0 lib/scss_lint/linter/url_format.rb
scss_lint-0.45.0 lib/scss_lint/linter/url_format.rb
scss_lint-0.44.0 lib/scss_lint/linter/url_format.rb
scss_lint-0.43.2 lib/scss_lint/linter/url_format.rb
scss_lint-0.43.1 lib/scss_lint/linter/url_format.rb
scss_lint-0.43.0 lib/scss_lint/linter/url_format.rb
scss_lint-0.42.2 lib/scss_lint/linter/url_format.rb
scss_lint-0.42.1 lib/scss_lint/linter/url_format.rb
scss_lint-0.42.0 lib/scss_lint/linter/url_format.rb