4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CSSのセレクタ一覧を機械的に抽出

Posted at

CSSの静的解析ツールをなんか作れないかなー、ということで、SassにあるCSSのパーサーを使ったらいいんじゃね?ということで、書いてみました。

だいぶ遅いので、もっと効率的な方法あるんじゃないかなーと思いつつ、とりあえずメモ。

print_selector.rb
require 'sass/css'
require 'pp'

def print_selector(file)
  css = ""
  File.open(file) do |io|
    css = io.read
  end
  css = Sass::CSS.new(css, {:filename => file})
  css_tree = css.__send__(:build_tree)
  css_tree.select {|n| n.is_a?(Sass::Tree::RuleNode) }.each do |rule|
    pp rule.parsed_rules.members
  end
end

print_selector(ARGV[0])
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?