Class: Postnhost::CssScope

Inherits:
Object
  • Object
show all
Defined in:
lib/postnhost/css_scope.rb

Constant Summary collapse

ROOT_SELECTOR =
"html[data-postnhost]"
ROOT_OR_DESCENDANT_SELECTOR =
":is(#{ROOT_SELECTOR}, #{ROOT_SELECTOR} *)"
GROUP_AT_RULES =
%w[container layer media starting-style supports].freeze

Class Method Summary collapse

Class Method Details

.call(css) ⇒ Object



11
12
13
14
15
# File 'lib/postnhost/css_scope.rb', line 11

def call(css)
  rules = Crass::Parser.parse_stylesheet(css, preserve_comments: true)
  scope_rules(rules)
  Crass::Parser.stringify(rules)
end

.scope_file(input_path, output_path = input_path) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/postnhost/css_scope.rb', line 17

def scope_file(input_path, output_path = input_path)
  output_path = File.expand_path(output_path)
  FileUtils.mkdir_p(File.dirname(output_path))
  temporary_path = "#{output_path}.tmp"
  File.write(temporary_path, call(File.read(input_path)))
  FileUtils.mv(temporary_path, output_path)
ensure
  FileUtils.rm_f(temporary_path) if temporary_path
end