PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/ruby/1.9.1/gems/sass-3.2.5/lib/sass/engine.rb

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