engine.rb 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. require 'set'
  2. require 'digest/sha1'
  3. require 'sass/cache_stores'
  4. require 'sass/tree/node'
  5. require 'sass/tree/root_node'
  6. require 'sass/tree/rule_node'
  7. require 'sass/tree/comment_node'
  8. require 'sass/tree/prop_node'
  9. require 'sass/tree/directive_node'
  10. require 'sass/tree/media_node'
  11. require 'sass/tree/variable_node'
  12. require 'sass/tree/mixin_def_node'
  13. require 'sass/tree/mixin_node'
  14. require 'sass/tree/function_node'
  15. require 'sass/tree/return_node'
  16. require 'sass/tree/extend_node'
  17. require 'sass/tree/if_node'
  18. require 'sass/tree/while_node'
  19. require 'sass/tree/for_node'
  20. require 'sass/tree/each_node'
  21. require 'sass/tree/debug_node'
  22. require 'sass/tree/warn_node'
  23. require 'sass/tree/import_node'
  24. require 'sass/tree/charset_node'
  25. require 'sass/tree/visitors/base'
  26. require 'sass/tree/visitors/perform'
  27. require 'sass/tree/visitors/cssize'
  28. require 'sass/tree/visitors/extend'
  29. require 'sass/tree/visitors/convert'
  30. require 'sass/tree/visitors/to_css'
  31. require 'sass/tree/visitors/deep_copy'
  32. require 'sass/tree/visitors/set_options'
  33. require 'sass/tree/visitors/check_nesting'
  34. require 'sass/selector'
  35. require 'sass/environment'
  36. require 'sass/script'
  37. require 'sass/scss'
  38. require 'sass/error'
  39. require 'sass/importers'
  40. require 'sass/shared'
  41. module Sass
  42. # A Sass mixin or function.
  43. #
  44. # `name`: `String`
  45. # : The name of the mixin/function.
  46. #
  47. # `args`: `Array<(String, Script::Node)>`
  48. # : The arguments for the mixin/function.
  49. # Each element is a tuple containing the name of the argument
  50. # and the parse tree for the default value of the argument.
  51. #
  52. # `environment`: {Sass::Environment}
  53. # : The environment in which the mixin/function was defined.
  54. # This is captured so that the mixin/function can have access
  55. # to local variables defined in its scope.
  56. #
  57. # `tree`: `Array<Tree::Node>`
  58. # : The parse tree for the mixin/function.
  59. Callable = Struct.new(:name, :args, :environment, :tree)
  60. # This class handles the parsing and compilation of the Sass template.
  61. # Example usage:
  62. #
  63. # template = File.load('stylesheets/sassy.sass')
  64. # sass_engine = Sass::Engine.new(template)
  65. # output = sass_engine.render
  66. # puts output
  67. class Engine
  68. include Sass::Util
  69. # A line of Sass code.
  70. #
  71. # `text`: `String`
  72. # : The text in the line, without any whitespace at the beginning or end.
  73. #
  74. # `tabs`: `Fixnum`
  75. # : The level of indentation of the line.
  76. #
  77. # `index`: `Fixnum`
  78. # : The line number in the original document.
  79. #
  80. # `offset`: `Fixnum`
  81. # : The number of bytes in on the line that the text begins.
  82. # This ends up being the number of bytes of leading whitespace.
  83. #
  84. # `filename`: `String`
  85. # : The name of the file in which this line appeared.
  86. #
  87. # `children`: `Array<Line>`
  88. # : The lines nested below this one.
  89. #
  90. # `comment_tab_str`: `String?`
  91. # : The prefix indentation for this comment, if it is a comment.
  92. class Line < Struct.new(:text, :tabs, :index, :offset, :filename, :children, :comment_tab_str)
  93. def comment?
  94. text[0] == COMMENT_CHAR && (text[1] == SASS_COMMENT_CHAR || text[1] == CSS_COMMENT_CHAR)
  95. end
  96. end
  97. # The character that begins a CSS property.
  98. PROPERTY_CHAR = ?:
  99. # The character that designates the beginning of a comment,
  100. # either Sass or CSS.
  101. COMMENT_CHAR = ?/
  102. # The character that follows the general COMMENT_CHAR and designates a Sass comment,
  103. # which is not output as a CSS comment.
  104. SASS_COMMENT_CHAR = ?/
  105. # The character that indicates that a comment allows interpolation
  106. # and should be preserved even in `:compressed` mode.
  107. SASS_LOUD_COMMENT_CHAR = ?!
  108. # The character that follows the general COMMENT_CHAR and designates a CSS comment,
  109. # which is embedded in the CSS document.
  110. CSS_COMMENT_CHAR = ?*
  111. # The character used to denote a compiler directive.
  112. DIRECTIVE_CHAR = ?@
  113. # Designates a non-parsed rule.
  114. ESCAPE_CHAR = ?\\
  115. # Designates block as mixin definition rather than CSS rules to output
  116. MIXIN_DEFINITION_CHAR = ?=
  117. # Includes named mixin declared using MIXIN_DEFINITION_CHAR
  118. MIXIN_INCLUDE_CHAR = ?+
  119. # The regex that matches and extracts data from
  120. # properties of the form `:name prop`.
  121. PROPERTY_OLD = /^:([^\s=:"]+)\s*(?:\s+|$)(.*)/
  122. # The default options for Sass::Engine.
  123. # @api public
  124. DEFAULT_OPTIONS = {
  125. :style => :nested,
  126. :load_paths => ['.'],
  127. :cache => true,
  128. :cache_location => './.sass-cache',
  129. :syntax => :sass,
  130. :filesystem_importer => Sass::Importers::Filesystem
  131. }.freeze
  132. # Converts a Sass options hash into a standard form, filling in
  133. # default values and resolving aliases.
  134. #
  135. # @param options [{Symbol => Object}] The options hash;
  136. # see {file:SASS_REFERENCE.md#sass_options the Sass options documentation}
  137. # @return [{Symbol => Object}] The normalized options hash.
  138. # @private
  139. def self.normalize_options(options)
  140. options = DEFAULT_OPTIONS.merge(options.reject {|k, v| v.nil?})
  141. # If the `:filename` option is passed in without an importer,
  142. # assume it's using the default filesystem importer.
  143. options[:importer] ||= options[:filesystem_importer].new(".") if options[:filename]
  144. # Tracks the original filename of the top-level Sass file
  145. options[:original_filename] ||= options[:filename]
  146. options[:cache_store] ||= Sass::CacheStores::Chain.new(
  147. Sass::CacheStores::Memory.new, Sass::CacheStores::Filesystem.new(options[:cache_location]))
  148. # Support both, because the docs said one and the other actually worked
  149. # for quite a long time.
  150. options[:line_comments] ||= options[:line_numbers]
  151. options[:load_paths] = options[:load_paths].map do |p|
  152. next p unless p.is_a?(String) || (defined?(Pathname) && p.is_a?(Pathname))
  153. options[:filesystem_importer].new(p.to_s)
  154. end
  155. # Backwards compatibility
  156. options[:property_syntax] ||= options[:attribute_syntax]
  157. case options[:property_syntax]
  158. when :alternate; options[:property_syntax] = :new
  159. when :normal; options[:property_syntax] = :old
  160. end
  161. options
  162. end
  163. # Returns the {Sass::Engine} for the given file.
  164. # This is preferable to Sass::Engine.new when reading from a file
  165. # because it properly sets up the Engine's metadata,
  166. # enables parse-tree caching,
  167. # and infers the syntax from the filename.
  168. #
  169. # @param filename [String] The path to the Sass or SCSS file
  170. # @param options [{Symbol => Object}] The options hash;
  171. # See {file:SASS_REFERENCE.md#sass_options the Sass options documentation}.
  172. # @return [Sass::Engine] The Engine for the given Sass or SCSS file.
  173. # @raise [Sass::SyntaxError] if there's an error in the document.
  174. def self.for_file(filename, options)
  175. had_syntax = options[:syntax]
  176. if had_syntax
  177. # Use what was explicitly specificed
  178. elsif filename =~ /\.scss$/
  179. options.merge!(:syntax => :scss)
  180. elsif filename =~ /\.sass$/
  181. options.merge!(:syntax => :sass)
  182. end
  183. Sass::Engine.new(File.read(filename), options.merge(:filename => filename))
  184. end
  185. # The options for the Sass engine.
  186. # See {file:SASS_REFERENCE.md#sass_options the Sass options documentation}.
  187. #
  188. # @return [{Symbol => Object}]
  189. attr_reader :options
  190. # Creates a new Engine. Note that Engine should only be used directly
  191. # when compiling in-memory Sass code.
  192. # If you're compiling a single Sass file from the filesystem,
  193. # use \{Sass::Engine.for\_file}.
  194. # If you're compiling multiple files from the filesystem,
  195. # use {Sass::Plugin}.
  196. #
  197. # @param template [String] The Sass template.
  198. # This template can be encoded using any encoding
  199. # that can be converted to Unicode.
  200. # If the template contains an `@charset` declaration,
  201. # that overrides the Ruby encoding
  202. # (see {file:SASS_REFERENCE.md#encodings the encoding documentation})
  203. # @param options [{Symbol => Object}] An options hash.
  204. # See {file:SASS_REFERENCE.md#sass_options the Sass options documentation}.
  205. # @see {Sass::Engine.for_file}
  206. # @see {Sass::Plugin}
  207. def initialize(template, options={})
  208. @options = self.class.normalize_options(options)
  209. @template = template
  210. end
  211. # Render the template to CSS.
  212. #
  213. # @return [String] The CSS
  214. # @raise [Sass::SyntaxError] if there's an error in the document
  215. # @raise [Encoding::UndefinedConversionError] if the source encoding
  216. # cannot be converted to UTF-8
  217. # @raise [ArgumentError] if the document uses an unknown encoding with `@charset`
  218. def render
  219. return _render unless @options[:quiet]
  220. Sass::Util.silence_sass_warnings {_render}
  221. end
  222. alias_method :to_css, :render
  223. # Parses the document into its parse tree. Memoized.
  224. #
  225. # @return [Sass::Tree::Node] The root of the parse tree.
  226. # @raise [Sass::SyntaxError] if there's an error in the document
  227. def to_tree
  228. @tree ||= @options[:quiet] ?
  229. Sass::Util.silence_sass_warnings {_to_tree} :
  230. _to_tree
  231. end
  232. # Returns the original encoding of the document,
  233. # or `nil` under Ruby 1.8.
  234. #
  235. # @return [Encoding, nil]
  236. # @raise [Encoding::UndefinedConversionError] if the source encoding
  237. # cannot be converted to UTF-8
  238. # @raise [ArgumentError] if the document uses an unknown encoding with `@charset`
  239. def source_encoding
  240. check_encoding!
  241. @original_encoding
  242. end
  243. # Gets a set of all the documents
  244. # that are (transitive) dependencies of this document,
  245. # not including the document itself.
  246. #
  247. # @return [[Sass::Engine]] The dependency documents.
  248. def dependencies
  249. _dependencies(Set.new, engines = Set.new)
  250. engines - [self]
  251. end
  252. # Helper for \{#dependencies}.
  253. #
  254. # @private
  255. def _dependencies(seen, engines)
  256. return if seen.include?(key = [@options[:filename], @options[:importer]])
  257. seen << key
  258. engines << self
  259. to_tree.grep(Tree::ImportNode) do |n|
  260. next if n.css_import?
  261. n.imported_file._dependencies(seen, engines)
  262. end
  263. end
  264. private
  265. def _render
  266. rendered = _to_tree.render
  267. return rendered if ruby1_8?
  268. begin
  269. # Try to convert the result to the original encoding,
  270. # but if that doesn't work fall back on UTF-8
  271. rendered = rendered.encode(source_encoding)
  272. rescue EncodingError
  273. end
  274. rendered.gsub(Regexp.new('\A@charset "(.*?)"'.encode(source_encoding)),
  275. "@charset \"#{source_encoding.name}\"".encode(source_encoding))
  276. end
  277. def _to_tree
  278. if (@options[:cache] || @options[:read_cache]) &&
  279. @options[:filename] && @options[:importer]
  280. key = sassc_key
  281. sha = Digest::SHA1.hexdigest(@template)
  282. if root = @options[:cache_store].retrieve(key, sha)
  283. root.options = @options
  284. return root
  285. end
  286. end
  287. check_encoding!
  288. if @options[:syntax] == :scss
  289. root = Sass::SCSS::Parser.new(@template, @options[:filename]).parse
  290. else
  291. root = Tree::RootNode.new(@template)
  292. append_children(root, tree(tabulate(@template)).first, true)
  293. end
  294. root.options = @options
  295. if @options[:cache] && key && sha
  296. begin
  297. old_options = root.options
  298. root.options = {}
  299. @options[:cache_store].store(key, sha, root)
  300. ensure
  301. root.options = old_options
  302. end
  303. end
  304. root
  305. rescue SyntaxError => e
  306. e.modify_backtrace(:filename => @options[:filename], :line => @line)
  307. e.sass_template = @template
  308. raise e
  309. end
  310. def sassc_key
  311. @options[:cache_store].key(*@options[:importer].key(@options[:filename], @options))
  312. end
  313. def check_encoding!
  314. return if @checked_encoding
  315. @checked_encoding = true
  316. @template, @original_encoding = check_sass_encoding(@template) do |msg, line|
  317. raise Sass::SyntaxError.new(msg, :line => line)
  318. end
  319. end
  320. def tabulate(string)
  321. tab_str = nil
  322. comment_tab_str = nil
  323. first = true
  324. lines = []
  325. string.gsub(/\r|\n|\r\n|\r\n/, "\n").scan(/^[^\n]*?$/).each_with_index do |line, index|
  326. index += (@options[:line] || 1)
  327. if line.strip.empty?
  328. lines.last.text << "\n" if lines.last && lines.last.comment?
  329. next
  330. end
  331. line_tab_str = line[/^\s*/]
  332. unless line_tab_str.empty?
  333. if tab_str.nil?
  334. comment_tab_str ||= line_tab_str
  335. next if try_comment(line, lines.last, "", comment_tab_str, index)
  336. comment_tab_str = nil
  337. end
  338. tab_str ||= line_tab_str
  339. raise SyntaxError.new("Indenting at the beginning of the document is illegal.",
  340. :line => index) if first
  341. raise SyntaxError.new("Indentation can't use both tabs and spaces.",
  342. :line => index) if tab_str.include?(?\s) && tab_str.include?(?\t)
  343. end
  344. first &&= !tab_str.nil?
  345. if tab_str.nil?
  346. lines << Line.new(line.strip, 0, index, 0, @options[:filename], [])
  347. next
  348. end
  349. comment_tab_str ||= line_tab_str
  350. if try_comment(line, lines.last, tab_str * lines.last.tabs, comment_tab_str, index)
  351. next
  352. else
  353. comment_tab_str = nil
  354. end
  355. line_tabs = line_tab_str.scan(tab_str).size
  356. if tab_str * line_tabs != line_tab_str
  357. message = <<END.strip.gsub("\n", ' ')
  358. Inconsistent indentation: #{Sass::Shared.human_indentation line_tab_str, true} used for indentation,
  359. but the rest of the document was indented using #{Sass::Shared.human_indentation tab_str}.
  360. END
  361. raise SyntaxError.new(message, :line => index)
  362. end
  363. lines << Line.new(line.strip, line_tabs, index, tab_str.size, @options[:filename], [])
  364. end
  365. lines
  366. end
  367. def try_comment(line, last, tab_str, comment_tab_str, index)
  368. return unless last && last.comment?
  369. # Nested comment stuff must be at least one whitespace char deeper
  370. # than the normal indentation
  371. return unless line =~ /^#{tab_str}\s/
  372. unless line =~ /^(?:#{comment_tab_str})(.*)$/
  373. raise SyntaxError.new(<<MSG.strip.gsub("\n", " "), :line => index)
  374. Inconsistent indentation:
  375. previous line was indented by #{Sass::Shared.human_indentation comment_tab_str},
  376. but this line was indented by #{Sass::Shared.human_indentation line[/^\s*/]}.
  377. MSG
  378. end
  379. last.comment_tab_str ||= comment_tab_str
  380. last.text << "\n" << line
  381. true
  382. end
  383. def tree(arr, i = 0)
  384. return [], i if arr[i].nil?
  385. base = arr[i].tabs
  386. nodes = []
  387. while (line = arr[i]) && line.tabs >= base
  388. if line.tabs > base
  389. raise SyntaxError.new("The line was indented #{line.tabs - base} levels deeper than the previous line.",
  390. :line => line.index) if line.tabs > base + 1
  391. nodes.last.children, i = tree(arr, i)
  392. else
  393. nodes << line
  394. i += 1
  395. end
  396. end
  397. return nodes, i
  398. end
  399. def build_tree(parent, line, root = false)
  400. @line = line.index
  401. node_or_nodes = parse_line(parent, line, root)
  402. Array(node_or_nodes).each do |node|
  403. # Node is a symbol if it's non-outputting, like a variable assignment
  404. next unless node.is_a? Tree::Node
  405. node.line = line.index
  406. node.filename = line.filename
  407. append_children(node, line.children, false)
  408. end
  409. node_or_nodes
  410. end
  411. def append_children(parent, children, root)
  412. continued_rule = nil
  413. continued_comment = nil
  414. children.each do |line|
  415. child = build_tree(parent, line, root)
  416. if child.is_a?(Tree::RuleNode)
  417. if child.continued? && child.children.empty?
  418. if continued_rule
  419. continued_rule.add_rules child
  420. else
  421. continued_rule = child
  422. end
  423. next
  424. elsif continued_rule
  425. continued_rule.add_rules child
  426. continued_rule.children = child.children
  427. continued_rule, child = nil, continued_rule
  428. end
  429. elsif continued_rule
  430. continued_rule = nil
  431. end
  432. if child.is_a?(Tree::CommentNode) && child.silent
  433. if continued_comment &&
  434. child.line == continued_comment.line +
  435. continued_comment.lines + 1
  436. continued_comment.value += ["\n"] + child.value
  437. next
  438. end
  439. continued_comment = child
  440. end
  441. check_for_no_children(child)
  442. validate_and_append_child(parent, child, line, root)
  443. end
  444. parent
  445. end
  446. def validate_and_append_child(parent, child, line, root)
  447. case child
  448. when Array
  449. child.each {|c| validate_and_append_child(parent, c, line, root)}
  450. when Tree::Node
  451. parent << child
  452. end
  453. end
  454. def check_for_no_children(node)
  455. return unless node.is_a?(Tree::RuleNode) && node.children.empty?
  456. Sass::Util.sass_warn(<<WARNING.strip)
  457. WARNING on line #{node.line}#{" of #{node.filename}" if node.filename}:
  458. This selector doesn't have any properties and will not be rendered.
  459. WARNING
  460. end
  461. def parse_line(parent, line, root)
  462. case line.text[0]
  463. when PROPERTY_CHAR
  464. if line.text[1] == PROPERTY_CHAR ||
  465. (@options[:property_syntax] == :new &&
  466. line.text =~ PROPERTY_OLD && $2.empty?)
  467. # Support CSS3-style pseudo-elements,
  468. # which begin with ::,
  469. # as well as pseudo-classes
  470. # if we're using the new property syntax
  471. Tree::RuleNode.new(parse_interp(line.text))
  472. else
  473. name, value = line.text.scan(PROPERTY_OLD)[0]
  474. raise SyntaxError.new("Invalid property: \"#{line.text}\".",
  475. :line => @line) if name.nil? || value.nil?
  476. parse_property(name, parse_interp(name), value, :old, line)
  477. end
  478. when ?$
  479. parse_variable(line)
  480. when COMMENT_CHAR
  481. parse_comment(line)
  482. when DIRECTIVE_CHAR
  483. parse_directive(parent, line, root)
  484. when ESCAPE_CHAR
  485. Tree::RuleNode.new(parse_interp(line.text[1..-1]))
  486. when MIXIN_DEFINITION_CHAR
  487. parse_mixin_definition(line)
  488. when MIXIN_INCLUDE_CHAR
  489. if line.text[1].nil? || line.text[1] == ?\s
  490. Tree::RuleNode.new(parse_interp(line.text))
  491. else
  492. parse_mixin_include(line, root)
  493. end
  494. else
  495. parse_property_or_rule(line)
  496. end
  497. end
  498. def parse_property_or_rule(line)
  499. scanner = Sass::Util::MultibyteStringScanner.new(line.text)
  500. hack_char = scanner.scan(/[:\*\.]|\#(?!\{)/)
  501. parser = Sass::SCSS::SassParser.new(scanner, @options[:filename], @line)
  502. unless res = parser.parse_interp_ident
  503. return Tree::RuleNode.new(parse_interp(line.text))
  504. end
  505. res.unshift(hack_char) if hack_char
  506. if comment = scanner.scan(Sass::SCSS::RX::COMMENT)
  507. res << comment
  508. end
  509. name = line.text[0...scanner.pos]
  510. if scanner.scan(/\s*:(?:\s|$)/)
  511. parse_property(name, res, scanner.rest, :new, line)
  512. else
  513. res.pop if comment
  514. Tree::RuleNode.new(res + parse_interp(scanner.rest))
  515. end
  516. end
  517. def parse_property(name, parsed_name, value, prop, line)
  518. if value.strip.empty?
  519. expr = Sass::Script::String.new("")
  520. else
  521. expr = parse_script(value, :offset => line.offset + line.text.index(value))
  522. end
  523. Tree::PropNode.new(parse_interp(name), expr, prop)
  524. end
  525. def parse_variable(line)
  526. name, value, default = line.text.scan(Script::MATCH)[0]
  527. raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath variable declarations.",
  528. :line => @line + 1) unless line.children.empty?
  529. raise SyntaxError.new("Invalid variable: \"#{line.text}\".",
  530. :line => @line) unless name && value
  531. expr = parse_script(value, :offset => line.offset + line.text.index(value))
  532. Tree::VariableNode.new(name, expr, default)
  533. end
  534. def parse_comment(line)
  535. if line.text[1] == CSS_COMMENT_CHAR || line.text[1] == SASS_COMMENT_CHAR
  536. silent = line.text[1] == SASS_COMMENT_CHAR
  537. if loud = line.text[2] == SASS_LOUD_COMMENT_CHAR
  538. value = self.class.parse_interp(line.text, line.index, line.offset, :filename => @filename)
  539. value[0].slice!(2) # get rid of the "!"
  540. else
  541. value = [line.text]
  542. end
  543. value = with_extracted_values(value) do |str|
  544. str = str.gsub(/^#{line.comment_tab_str}/m, '')[2..-1] # get rid of // or /*
  545. format_comment_text(str, silent)
  546. end
  547. Tree::CommentNode.new(value, silent, loud)
  548. else
  549. Tree::RuleNode.new(parse_interp(line.text))
  550. end
  551. end
  552. def parse_directive(parent, line, root)
  553. directive, whitespace, value = line.text[1..-1].split(/(\s+)/, 2)
  554. offset = directive.size + whitespace.size + 1 if whitespace
  555. # If value begins with url( or ",
  556. # it's a CSS @import rule and we don't want to touch it.
  557. if directive == "import"
  558. parse_import(line, value)
  559. elsif directive == "mixin"
  560. parse_mixin_definition(line)
  561. elsif directive == "include"
  562. parse_mixin_include(line, root)
  563. elsif directive == "function"
  564. parse_function(line, root)
  565. elsif directive == "for"
  566. parse_for(line, root, value)
  567. elsif directive == "each"
  568. parse_each(line, root, value)
  569. elsif directive == "else"
  570. parse_else(parent, line, value)
  571. elsif directive == "while"
  572. raise SyntaxError.new("Invalid while directive '@while': expected expression.") unless value
  573. Tree::WhileNode.new(parse_script(value, :offset => offset))
  574. elsif directive == "if"
  575. raise SyntaxError.new("Invalid if directive '@if': expected expression.") unless value
  576. Tree::IfNode.new(parse_script(value, :offset => offset))
  577. elsif directive == "debug"
  578. raise SyntaxError.new("Invalid debug directive '@debug': expected expression.") unless value
  579. raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath debug directives.",
  580. :line => @line + 1) unless line.children.empty?
  581. offset = line.offset + line.text.index(value).to_i
  582. Tree::DebugNode.new(parse_script(value, :offset => offset))
  583. elsif directive == "extend"
  584. raise SyntaxError.new("Invalid extend directive '@extend': expected expression.") unless value
  585. raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath extend directives.",
  586. :line => @line + 1) unless line.children.empty?
  587. offset = line.offset + line.text.index(value).to_i
  588. Tree::ExtendNode.new(parse_interp(value, offset))
  589. elsif directive == "warn"
  590. raise SyntaxError.new("Invalid warn directive '@warn': expected expression.") unless value
  591. raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath warn directives.",
  592. :line => @line + 1) unless line.children.empty?
  593. offset = line.offset + line.text.index(value).to_i
  594. Tree::WarnNode.new(parse_script(value, :offset => offset))
  595. elsif directive == "return"
  596. raise SyntaxError.new("Invalid @return: expected expression.") unless value
  597. raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath return directives.",
  598. :line => @line + 1) unless line.children.empty?
  599. offset = line.offset + line.text.index(value).to_i
  600. Tree::ReturnNode.new(parse_script(value, :offset => offset))
  601. elsif directive == "charset"
  602. name = value && value[/\A(["'])(.*)\1\Z/, 2] #"
  603. raise SyntaxError.new("Invalid charset directive '@charset': expected string.") unless name
  604. raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath charset directives.",
  605. :line => @line + 1) unless line.children.empty?
  606. Tree::CharsetNode.new(name)
  607. elsif directive == "media"
  608. Tree::MediaNode.new(value.split(',').map {|s| s.strip})
  609. else
  610. Tree::DirectiveNode.new(line.text)
  611. end
  612. end
  613. def parse_for(line, root, text)
  614. var, from_expr, to_name, to_expr = text.scan(/^([^\s]+)\s+from\s+(.+)\s+(to|through)\s+(.+)$/).first
  615. if var.nil? # scan failed, try to figure out why for error message
  616. if text !~ /^[^\s]+/
  617. expected = "variable name"
  618. elsif text !~ /^[^\s]+\s+from\s+.+/
  619. expected = "'from <expr>'"
  620. else
  621. expected = "'to <expr>' or 'through <expr>'"
  622. end
  623. raise SyntaxError.new("Invalid for directive '@for #{text}': expected #{expected}.")
  624. end
  625. raise SyntaxError.new("Invalid variable \"#{var}\".") unless var =~ Script::VALIDATE
  626. var = var[1..-1]
  627. parsed_from = parse_script(from_expr, :offset => line.offset + line.text.index(from_expr))
  628. parsed_to = parse_script(to_expr, :offset => line.offset + line.text.index(to_expr))
  629. Tree::ForNode.new(var, parsed_from, parsed_to, to_name == 'to')
  630. end
  631. def parse_each(line, root, text)
  632. var, list_expr = text.scan(/^([^\s]+)\s+in\s+(.+)$/).first
  633. if var.nil? # scan failed, try to figure out why for error message
  634. if text !~ /^[^\s]+/
  635. expected = "variable name"
  636. elsif text !~ /^[^\s]+\s+from\s+.+/
  637. expected = "'in <expr>'"
  638. end
  639. raise SyntaxError.new("Invalid for directive '@each #{text}': expected #{expected}.")
  640. end
  641. raise SyntaxError.new("Invalid variable \"#{var}\".") unless var =~ Script::VALIDATE
  642. var = var[1..-1]
  643. parsed_list = parse_script(list_expr, :offset => line.offset + line.text.index(list_expr))
  644. Tree::EachNode.new(var, parsed_list)
  645. end
  646. def parse_else(parent, line, text)
  647. previous = parent.children.last
  648. raise SyntaxError.new("@else must come after @if.") unless previous.is_a?(Tree::IfNode)
  649. if text
  650. if text !~ /^if\s+(.+)/
  651. raise SyntaxError.new("Invalid else directive '@else #{text}': expected 'if <expr>'.")
  652. end
  653. expr = parse_script($1, :offset => line.offset + line.text.index($1))
  654. end
  655. node = Tree::IfNode.new(expr)
  656. append_children(node, line.children, false)
  657. previous.add_else node
  658. nil
  659. end
  660. def parse_import(line, value)
  661. raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath import directives.",
  662. :line => @line + 1) unless line.children.empty?
  663. scanner = Sass::Util::MultibyteStringScanner.new(value)
  664. values = []
  665. loop do
  666. unless node = parse_import_arg(scanner)
  667. raise SyntaxError.new("Invalid @import: expected file to import, was #{scanner.rest.inspect}",
  668. :line => @line)
  669. end
  670. values << node
  671. break unless scanner.scan(/,\s*/)
  672. end
  673. if scanner.scan(/;/)
  674. raise SyntaxError.new("Invalid @import: expected end of line, was \";\".",
  675. :line => @line)
  676. end
  677. return values
  678. end
  679. def parse_import_arg(scanner)
  680. return if scanner.eos?
  681. unless (str = scanner.scan(Sass::SCSS::RX::STRING)) ||
  682. (uri = scanner.scan(Sass::SCSS::RX::URI))
  683. return Tree::ImportNode.new(scanner.scan(/[^,;]+/))
  684. end
  685. val = scanner[1] || scanner[2]
  686. scanner.scan(/\s*/)
  687. if media = scanner.scan(/[a-zA-Z].*/)
  688. Tree::DirectiveNode.new("@import #{str || uri} #{media}")
  689. elsif !scanner.match?(/[,;]|$/)
  690. raise SyntaxError.new("Invalid @import: \"#{str || uri} #{scanner.rest}\"")
  691. elsif uri
  692. Tree::DirectiveNode.new("@import #{uri}")
  693. elsif val =~ /^http:\/\//
  694. Tree::DirectiveNode.new("@import #{str}")
  695. else
  696. Tree::ImportNode.new(val)
  697. end
  698. end
  699. MIXIN_DEF_RE = /^(?:=|@mixin)\s*(#{Sass::SCSS::RX::IDENT})(.*)$/
  700. def parse_mixin_definition(line)
  701. name, arg_string = line.text.scan(MIXIN_DEF_RE).first
  702. raise SyntaxError.new("Invalid mixin \"#{line.text[1..-1]}\".") if name.nil?
  703. offset = line.offset + line.text.size - arg_string.size
  704. args = Script::Parser.new(arg_string.strip, @line, offset, @options).
  705. parse_mixin_definition_arglist
  706. Tree::MixinDefNode.new(name, args)
  707. end
  708. MIXIN_INCLUDE_RE = /^(?:\+|@include)\s*(#{Sass::SCSS::RX::IDENT})(.*)$/
  709. def parse_mixin_include(line, root)
  710. name, arg_string = line.text.scan(MIXIN_INCLUDE_RE).first
  711. raise SyntaxError.new("Invalid mixin include \"#{line.text}\".") if name.nil?
  712. offset = line.offset + line.text.size - arg_string.size
  713. args, keywords = Script::Parser.new(arg_string.strip, @line, offset, @options).
  714. parse_mixin_include_arglist
  715. raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath mixin directives.",
  716. :line => @line + 1) unless line.children.empty?
  717. Tree::MixinNode.new(name, args, keywords)
  718. end
  719. FUNCTION_RE = /^@function\s*(#{Sass::SCSS::RX::IDENT})(.*)$/
  720. def parse_function(line, root)
  721. name, arg_string = line.text.scan(FUNCTION_RE).first
  722. raise SyntaxError.new("Invalid function definition \"#{line.text}\".") if name.nil?
  723. offset = line.offset + line.text.size - arg_string.size
  724. args = Script::Parser.new(arg_string.strip, @line, offset, @options).
  725. parse_function_definition_arglist
  726. Tree::FunctionNode.new(name, args)
  727. end
  728. def parse_script(script, options = {})
  729. line = options[:line] || @line
  730. offset = options[:offset] || 0
  731. Script.parse(script, line, offset, @options)
  732. end
  733. def format_comment_text(text, silent)
  734. content = text.split("\n")
  735. if content.first && content.first.strip.empty?
  736. removed_first = true
  737. content.shift
  738. end
  739. return silent ? "//" : "/* */" if content.empty?
  740. content.last.gsub!(%r{ ?\*/ *$}, '')
  741. content.map! {|l| l.gsub!(/^\*( ?)/, '\1') || (l.empty? ? "" : " ") + l}
  742. content.first.gsub!(/^ /, '') unless removed_first
  743. if silent
  744. "//" + content.join("\n//")
  745. else
  746. # The #gsub fixes the case of a trailing */
  747. "/*" + content.join("\n *").gsub(/ \*\Z/, '') + " */"
  748. end
  749. end
  750. def parse_interp(text, offset = 0)
  751. self.class.parse_interp(text, @line, offset, :filename => @filename)
  752. end
  753. # It's important that this have strings (at least)
  754. # at the beginning, the end, and between each Script::Node.
  755. #
  756. # @private
  757. def self.parse_interp(text, line, offset, options)
  758. res = []
  759. rest = Sass::Shared.handle_interpolation text do |scan|
  760. escapes = scan[2].size
  761. res << scan.matched[0...-2 - escapes]
  762. if escapes % 2 == 1
  763. res << "\\" * (escapes - 1) << '#{'
  764. else
  765. res << "\\" * [0, escapes - 1].max
  766. res << Script::Parser.new(
  767. scan, line, offset + scan.pos - scan.matched_size, options).
  768. parse_interpolated
  769. end
  770. end
  771. res << rest
  772. end
  773. end
  774. end