File: test_css_parser_offset_capture.rb

package info (click to toggle)
ruby-css-parser 1.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 380 kB
  • sloc: ruby: 3,093; makefile: 6
file content (107 lines) | stat: -rw-r--r-- 3,763 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# frozen_string_literal: true

require_relative 'test_helper'

# Test cases for the CssParser's loading functions.
class CssParserOffsetCaptureTests < Minitest::Test
  include CssParser

  def setup
    @cp = Parser.new
  end

  def test_capturing_offsets_for_local_file
    file_name = File.expand_path('fixtures/simple.css', __dir__)
    @cp.load_file!(file_name, capture_offsets: true)

    rules = @cp.find_rule_sets(['body', 'p'])

    # check that we found the body rule where we expected
    assert_equal 0, rules[0].offset.first
    assert_equal 43, rules[0].offset.last
    assert_equal file_name, rules[0].filename

    # and the p rule
    assert_equal 45, rules[1].offset.first
    assert_equal 63, rules[1].offset.last
    assert_equal file_name, rules[1].filename
  end

  # http://github.com/premailer/css_parser/issues#issue/4
  def test_capturing_offsets_from_remote_file
    # TODO: test SSL locally
    @cp.load_uri!("https://dialect.ca/inc/screen.css", capture_offsets: true)

    # there are a lot of rules in this file, but check some rule offsets
    rules = @cp.find_rule_sets(['#container', '#name_case_converter textarea'])
    assert_equal 2, rules.count

    assert_equal 2172, rules.first.offset.first
    assert_equal 2227, rules.first.offset.last
    assert_equal 'https://dialect.ca/inc/screen.css', rules.first.filename

    assert_equal 10_703, rules.last.offset.first
    assert_equal 10_752, rules.last.offset.last
    assert_equal 'https://dialect.ca/inc/screen.css', rules.last.filename
  end

  def test_capturing_offsets_from_string
    css = <<-CSS
      body { margin: 0px; }
      p { padding: 0px; }
      #content { font: 12px/normal sans-serif; }
      .content { color: red; }
    CSS
    @cp.load_string!(css, capture_offsets: true, filename: 'index.html')

    rules = @cp.find_rule_sets(['body', 'p', '#content', '.content'])
    assert_equal 4, rules.count

    assert_equal 6, rules[0].offset.first
    assert_equal 27, rules[0].offset.last
    assert_equal 'index.html', rules[0].filename

    assert_equal 34, rules[1].offset.first
    assert_equal 53, rules[1].offset.last
    assert_equal 'index.html', rules[1].filename

    assert_equal 60, rules[2].offset.first
    assert_equal 102, rules[2].offset.last
    assert_equal 'index.html', rules[2].filename

    assert_equal 109, rules[3].offset.first
    assert_equal 133, rules[3].offset.last
    assert_equal 'index.html', rules[3].filename
  end

  def test_capturing_offsets_with_imports
    base_dir = Pathname.new(__dir__).join('fixtures')
    @cp.load_file!('import1.css', base_dir: base_dir, capture_offsets: true)

    rules = @cp.find_rule_sets(['div', 'a', 'body', 'p'])

    # check that we found the div rule where we expected in the primary file
    assert_equal 'div', rules[0].selectors.join
    assert_equal 31, rules[0].offset.first
    assert_equal 51, rules[0].offset.last
    assert_equal base_dir.join('import1.css').to_s, rules[0].filename

    # check that the a rule in the first import is where we expect
    assert_equal 'a', rules[1].selectors.join
    assert_equal 26, rules[1].offset.first
    assert_equal 54, rules[1].offset.last
    assert_equal base_dir.join('subdir/import2.css').to_s, rules[1].filename

    # and the body rule in the second import
    assert_equal 'body', rules[2].selectors.join
    assert_equal 0, rules[2].offset.first
    assert_equal 43, rules[2].offset.last
    assert_equal base_dir.join('simple.css').to_s, rules[2].filename

    # as well as the p rule in the second import
    assert_equal 'p', rules[3].selectors.join
    assert_equal 45, rules[3].offset.first
    assert_equal 63, rules[3].offset.last
    assert_equal base_dir.join('simple.css').to_s, rules[3].filename
  end
end if false