forked from sds/scss-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_format.rb
56 lines (43 loc) · 1.34 KB
/
url_format.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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:') || url.include?('${')
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