Class: Rtlize::RTLizer

Inherits:
Object
  • Object
show all
Defined in:
lib/rtlize/rtlizer.rb

Class Method Summary collapse

Class Method Details

.block_regexpObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rtlize/rtlizer.rb', line 30

def block_regexp
  %r{
    [^\{\}]+ \{
      (?:
        (?:
          [^\{\}]+ \{
            [^\}]*
          \} [^\{\}]*
        )*
        |
        [^\{\}]+
      )
    \}
  }x
end

.rule_regexpObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rtlize/rtlizer.rb', line 46

def rule_regexp
  # Break the blocks' rules into their selector & declaration parts
  %r{
    ( [^\{\}]+ ) \{
      (
        (?:
          [^\{\}]+ \{
            [^\}]*
          \} [^\{\}]*
        )*
      )
    \}
    |
    ( [^\{\}]+ ) \{
      ( [^\{\}]+ )
    \}
  }x
end

.should_rtlize_selector?(selector) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
# File 'lib/rtlize/rtlizer.rb', line 65

def should_rtlize_selector?(selector)
  selector = selector.gsub(/\/\*[\s\S]+?\*\//, '') # Remove comments
  selector = selector.gsub(/['"]/, '') # Remove quote characters

  rtl_selector = Rtlize.rtl_selector.gsub(/['"]/, '') # Remove quote characters

  rtl_selector_regexp = /(^|\b|\s)#{Regexp.escape(rtl_selector)}($|\b|\s)/
  !selector.match(rtl_selector_regexp)
end

.simple_block_regexpObject



22
23
24
25
26
27
28
# File 'lib/rtlize/rtlizer.rb', line 22

def simple_block_regexp
  %r{
    ( [^\{\}]+ ) \{
      ( [^\{\}]+ )
    \}
  }x
end

.transform(css) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rtlize/rtlizer.rb', line 85

def transform(css)
  @no_invert = false

  css.gsub(block_regexp) do |block|
    block_selector_regexp = %r{ ^ [^\{\}]+ }x
    block_selector = block.match(block_selector_regexp).to_s
    next if block_selector.length == 0

    block.gsub(rule_regexp) do |rule|
      parts = rule.match(rule_regexp)
      if parts[1].nil?
        # Simple block
        selector, declarations = parts[3,4]
        transform_simple_block(selector, declarations)
      else
        # Nested blocks
        selector, declarations = parts[1,2]
        selector + '{' + transform_nested_blocks(declarations) + '}'
      end
    end
  end
end

.transform_nested_blocks(blocks) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/rtlize/rtlizer.rb', line 117

def transform_nested_blocks(blocks)
  blocks.gsub(simple_block_regexp) do |block|
    parts = block.match(simple_block_regexp)
    selector, declarations = parts[1,2]
    transform_simple_block(selector, declarations)
  end
end

.transform_simple_block(selector, declarations) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/rtlize/rtlizer.rb', line 108

def transform_simple_block(selector, declarations)
  update_no_invert(selector)
  if !@no_invert && should_rtlize_selector?(selector)
    selector + '{' + Rtlize::Declaration.transform_multiple(declarations) + '}'
  else
    selector + '{' + declarations + '}'
  end
end

.update_no_invert(selector) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/rtlize/rtlizer.rb', line 75

def update_no_invert(selector)
  # The CSS comment must start with "!" in order to be considered as important by the CSS compressor
  # otherwise, it will be removed by the asset pipeline before reaching this processor.
  if selector.match(/\/\*!= begin\(no-rtl\) \*\//)
    @no_invert = true
  elsif selector.match(/\/\*!= end\(no-rtl\) \*\//)
    @no_invert = false
  end
end