Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#css_asset_path(content) ⇒ Object



50
51
52
# File 'lib/jigsaw-bpa-lib.rb', line 50

def css_asset_path(content)
  content.xpath(%q(//link[@rel="stylesheet" and not(starts-with(@href, "/assets")) and not(starts-with(@href, "https://fonts.googleapis.com"))])).empty? ? false : true
end

#css_in_head(content) ⇒ Object



34
35
36
# File 'lib/jigsaw-bpa-lib.rb', line 34

def css_in_head(content)
  content.xpath(%q(//link[@rel="stylesheet" and not(ancestor::head) and not(starts-with(@href, "https://fonts.googleapis.com"))])).empty? ? false : true
end

#css_other_attr(content) ⇒ Object



38
39
40
# File 'lib/jigsaw-bpa-lib.rb', line 38

def css_other_attr(content)
  content.xpath(%q(//link[@rel="stylesheet"]/@*[not(name()='rel') and not(name()='href') and not(name()='type')])).empty? ? false : true
end

#get_body_from_source(source) ⇒ Object



4
5
6
# File 'lib/jigsaw-bpa-lib.rb', line 4

def get_body_from_source(source)
  Oga.parse_html(open(source).read)
end

#js_asset_path(content) ⇒ Object



54
55
56
# File 'lib/jigsaw-bpa-lib.rb', line 54

def js_asset_path(content)
  content.xpath(%q(//script[@src and not(starts-with(@src, "http")) and not(starts-with(@src, "/assets"))])).empty? ? false : true
end

#js_defer_async(content) ⇒ Object



42
43
44
# File 'lib/jigsaw-bpa-lib.rb', line 42

def js_defer_async(content)
  content.xpath(%q(//script[@src and (@defer or @async)])).empty? ? false : true
end

#js_inline(content) ⇒ Object



46
47
48
# File 'lib/jigsaw-bpa-lib.rb', line 46

def js_inline(content)
  content.xpath(%q(//script[not(@src) and not(@data-pagespeed-no-defer)])).empty? ? false : true
end

#run_all_tests(source) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/jigsaw-bpa-lib.rb', line 8

def run_all_tests(source)
  body = get_body_from_source(source)

  failed = false

  failed = run_test("Thou shalt not have CSS outside of head tag",              :css_in_head,    body) || failed
  failed = run_test("Thou shalt not have other attributes in style references", :css_other_attr, body) || failed
  failed = run_test("Thou shalt not use defer async in script tags",            :js_defer_async, body) || failed
  failed = run_test("Thou shalt not have inline script tags",                   :js_inline,      body) || failed
  failed = run_test("Thou shalt not deliver CSS outside of /assets",            :css_asset_path, body) || failed
  failed = run_test("Thou shalt not deliver JS outside of /assets",             :js_asset_path,  body) || failed

  abort("At least one test failed") if failed
end

#run_test(name, test, content) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/jigsaw-bpa-lib.rb', line 23

def run_test(name,test,content)
  print "#{name}".ljust(60,'.')
  failed = send(test,content)
  if failed
    print "failed\n"
  else
    print "succeeded\n"
  end
  failed
end