PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/bundle/ruby/1.9.1/gems/sass-3.1.15/lib/sass/engine.rb

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