PageRenderTime 60ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 2ms

/BundleTransformer.SassAndScss/Resources/sass_in_one.rb

http://bundletransformer.codeplex.com
Ruby | 12296 lines | 6778 code | 1263 blank | 4255 comment | 960 complexity | 0f37ab1933d39476db91811877f06a4d MD5 | raw file
Possible License(s): Apache-2.0, CC-BY-SA-3.0, BSD-2-Clause, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. # This is necessary to set so that the Haml code that tries to load Sass
  2. # knows that Sass is indeed loading,
  3. # even if there's some crazy autoload stuff going on.
  4. SASS_BEGUN_TO_LOAD = true unless defined?(SASS_BEGUN_TO_LOAD)
  5. # This is necessary for loading Sass when Haml is required in Rails 3.
  6. # Once the split is complete, we can remove it.
  7. #require 'erb'
  8. require 'set'
  9. require 'enumerator'
  10. require 'stringio'
  11. require 'strscan'
  12. require 'rbconfig'
  13. module Sass
  14. # The root directory of the Sass source tree.
  15. # This may be overridden by the package manager
  16. # if the lib directory is separated from the main source tree.
  17. # @api public
  18. ROOT_DIR = File.expand_path(File.join(__FILE__, "../../.."))
  19. end
  20. require 'set'
  21. module Sass
  22. module Util
  23. # A map from sets to values.
  24. # A value is \{#\[]= set} by providing a set (the "set-set") and a value,
  25. # which is then recorded as corresponding to that set.
  26. # Values are \{#\[] accessed} by providing a set (the "get-set")
  27. # and returning all values that correspond to set-sets
  28. # that are subsets of the get-set.
  29. #
  30. # SubsetMap preserves the order of values as they're inserted.
  31. #
  32. # @example
  33. # ssm = SubsetMap.new
  34. # ssm[Set[1, 2]] = "Foo"
  35. # ssm[Set[2, 3]] = "Bar"
  36. # ssm[Set[1, 2, 3]] = "Baz"
  37. #
  38. # ssm[Set[1, 2, 3]] #=> ["Foo", "Bar", "Baz"]
  39. class SubsetMap
  40. # Creates a new, empty SubsetMap.
  41. def initialize
  42. @hash = {}
  43. @vals = []
  44. end
  45. # Whether or not this SubsetMap has any key-value pairs.
  46. #
  47. # @return [Boolean]
  48. def empty?
  49. @hash.empty?
  50. end
  51. # Associates a value with a set.
  52. # When `set` or any of its supersets is accessed,
  53. # `value` will be among the values returned.
  54. #
  55. # Note that if the same `set` is passed to this method multiple times,
  56. # all given `value`s will be associated with that `set`.
  57. #
  58. # This runs in `O(n)` time, where `n` is the size of `set`.
  59. #
  60. # @param set [#to_set] The set to use as the map key. May not be empty.
  61. # @param value [Object] The value to associate with `set`.
  62. # @raise [ArgumentError] If `set` is empty.
  63. def []=(set, value)
  64. raise ArgumentError.new("SubsetMap keys may not be empty.") if set.empty?
  65. index = @vals.size
  66. @vals << value
  67. set.each do |k|
  68. @hash[k] ||= []
  69. @hash[k] << [set, set.to_set, index]
  70. end
  71. end
  72. # Returns all values associated with subsets of `set`.
  73. #
  74. # In the worst case, this runs in `O(m*max(n, log m))` time,
  75. # where `n` is the size of `set`
  76. # and `m` is the number of assocations in the map.
  77. # However, unless many keys in the map overlap with `set`,
  78. # `m` will typically be much smaller.
  79. #
  80. # @param set [Set] The set to use as the map key.
  81. # @return [Array<(Object, #to_set)>] An array of pairs,
  82. # where the first value is the value associated with a subset of `set`,
  83. # and the second value is that subset of `set`
  84. # (or whatever `#to_set` object was used to set the value)
  85. # This array is in insertion order.
  86. # @see #[]
  87. def get(set)
  88. res = set.map do |k|
  89. next unless subsets = @hash[k]
  90. subsets.map do |subenum, subset, index|
  91. next unless subset.subset?(set)
  92. [index, subenum]
  93. end
  94. end
  95. res = Sass::Util.flatten(res, 1)
  96. res.compact!
  97. res.uniq!
  98. res.sort!
  99. res.map! {|i, s| [@vals[i], s]}
  100. return res
  101. end
  102. # Same as \{#get}, but doesn't return the subsets of the argument
  103. # for which values were found.
  104. #
  105. # @param set [Set] The set to use as the map key.
  106. # @return [Array] The array of all values
  107. # associated with subsets of `set`, in insertion order.
  108. # @see #get
  109. def [](set)
  110. get(set).map {|v, _| v}
  111. end
  112. end
  113. end
  114. end
  115. module Sass
  116. # A module containing various useful functions.
  117. module Util
  118. extend self
  119. # An array of ints representing the Ruby version number.
  120. # @api public
  121. RUBY_VERSION = ::RUBY_VERSION.split(".").map {|s| s.to_i}
  122. # The Ruby engine we're running under. Defaults to `"ruby"`
  123. # if the top-level constant is undefined.
  124. # @api public
  125. RUBY_ENGINE = defined?(::RUBY_ENGINE) ? ::RUBY_ENGINE : "ruby"
  126. # Returns the path of a file relative to the Sass root directory.
  127. #
  128. # @param file [String] The filename relative to the Sass root
  129. # @return [String] The filename relative to the the working directory
  130. def scope(file)
  131. File.join(Sass::ROOT_DIR, file)
  132. end
  133. # Converts an array of `[key, value]` pairs to a hash.
  134. #
  135. # @example
  136. # to_hash([[:foo, "bar"], [:baz, "bang"]])
  137. # #=> {:foo => "bar", :baz => "bang"}
  138. # @param arr [Array<(Object, Object)>] An array of pairs
  139. # @return [Hash] A hash
  140. def to_hash(arr)
  141. Hash[arr.compact]
  142. end
  143. # Maps the keys in a hash according to a block.
  144. #
  145. # @example
  146. # map_keys({:foo => "bar", :baz => "bang"}) {|k| k.to_s}
  147. # #=> {"foo" => "bar", "baz" => "bang"}
  148. # @param hash [Hash] The hash to map
  149. # @yield [key] A block in which the keys are transformed
  150. # @yieldparam key [Object] The key that should be mapped
  151. # @yieldreturn [Object] The new value for the key
  152. # @return [Hash] The mapped hash
  153. # @see #map_vals
  154. # @see #map_hash
  155. def map_keys(hash)
  156. to_hash(hash.map {|k, v| [yield(k), v]})
  157. end
  158. # Maps the values in a hash according to a block.
  159. #
  160. # @example
  161. # map_values({:foo => "bar", :baz => "bang"}) {|v| v.to_sym}
  162. # #=> {:foo => :bar, :baz => :bang}
  163. # @param hash [Hash] The hash to map
  164. # @yield [value] A block in which the values are transformed
  165. # @yieldparam value [Object] The value that should be mapped
  166. # @yieldreturn [Object] The new value for the value
  167. # @return [Hash] The mapped hash
  168. # @see #map_keys
  169. # @see #map_hash
  170. def map_vals(hash)
  171. to_hash(hash.map {|k, v| [k, yield(v)]})
  172. end
  173. # Maps the key-value pairs of a hash according to a block.
  174. #
  175. # @example
  176. # map_hash({:foo => "bar", :baz => "bang"}) {|k, v| [k.to_s, v.to_sym]}
  177. # #=> {"foo" => :bar, "baz" => :bang}
  178. # @param hash [Hash] The hash to map
  179. # @yield [key, value] A block in which the key-value pairs are transformed
  180. # @yieldparam [key] The hash key
  181. # @yieldparam [value] The hash value
  182. # @yieldreturn [(Object, Object)] The new value for the `[key, value]` pair
  183. # @return [Hash] The mapped hash
  184. # @see #map_keys
  185. # @see #map_vals
  186. def map_hash(hash, &block)
  187. to_hash(hash.map(&block))
  188. end
  189. # Computes the powerset of the given array.
  190. # This is the set of all subsets of the array.
  191. #
  192. # @example
  193. # powerset([1, 2, 3]) #=>
  194. # Set[Set[], Set[1], Set[2], Set[3], Set[1, 2], Set[2, 3], Set[1, 3], Set[1, 2, 3]]
  195. # @param arr [Enumerable]
  196. # @return [Set<Set>] The subsets of `arr`
  197. def powerset(arr)
  198. arr.inject([Set.new].to_set) do |powerset, el|
  199. new_powerset = Set.new
  200. powerset.each do |subset|
  201. new_powerset << subset
  202. new_powerset << subset + [el]
  203. end
  204. new_powerset
  205. end
  206. end
  207. # Restricts a number to falling within a given range.
  208. # Returns the number if it falls within the range,
  209. # or the closest value in the range if it doesn't.
  210. #
  211. # @param value [Numeric]
  212. # @param range [Range<Numeric>]
  213. # @return [Numeric]
  214. def restrict(value, range)
  215. [[value, range.first].max, range.last].min
  216. end
  217. # Concatenates all strings that are adjacent in an array,
  218. # while leaving other elements as they are.
  219. #
  220. # @example
  221. # merge_adjacent_strings([1, "foo", "bar", 2, "baz"])
  222. # #=> [1, "foobar", 2, "baz"]
  223. # @param arr [Array]
  224. # @return [Array] The enumerable with strings merged
  225. def merge_adjacent_strings(arr)
  226. # Optimize for the common case of one element
  227. return arr if arr.size < 2
  228. arr.inject([]) do |a, e|
  229. if e.is_a?(String)
  230. if a.last.is_a?(String)
  231. a.last << e
  232. else
  233. a << e.dup
  234. end
  235. else
  236. a << e
  237. end
  238. a
  239. end
  240. end
  241. # Intersperses a value in an enumerable, as would be done with `Array#join`
  242. # but without concatenating the array together afterwards.
  243. #
  244. # @param enum [Enumerable]
  245. # @param val
  246. # @return [Array]
  247. def intersperse(enum, val)
  248. enum.inject([]) {|a, e| a << e << val}[0...-1]
  249. end
  250. # Substitutes a sub-array of one array with another sub-array.
  251. #
  252. # @param ary [Array] The array in which to make the substitution
  253. # @param from [Array] The sequence of elements to replace with `to`
  254. # @param to [Array] The sequence of elements to replace `from` with
  255. def substitute(ary, from, to)
  256. res = ary.dup
  257. i = 0
  258. while i < res.size
  259. if res[i...i+from.size] == from
  260. res[i...i+from.size] = to
  261. end
  262. i += 1
  263. end
  264. res
  265. end
  266. # Destructively strips whitespace from the beginning and end
  267. # of the first and last elements, respectively,
  268. # in the array (if those elements are strings).
  269. #
  270. # @param arr [Array]
  271. # @return [Array] `arr`
  272. def strip_string_array(arr)
  273. arr.first.lstrip! if arr.first.is_a?(String)
  274. arr.last.rstrip! if arr.last.is_a?(String)
  275. arr
  276. end
  277. # Return an array of all possible paths through the given arrays.
  278. #
  279. # @param arrs [Array<Array>]
  280. # @return [Array<Arrays>]
  281. #
  282. # @example
  283. # paths([[1, 2], [3, 4], [5]]) #=>
  284. # # [[1, 3, 5],
  285. # # [2, 3, 5],
  286. # # [1, 4, 5],
  287. # # [2, 4, 5]]
  288. def paths(arrs)
  289. arrs.inject([[]]) do |paths, arr|
  290. flatten(arr.map {|e| paths.map {|path| path + [e]}}, 1)
  291. end
  292. end
  293. # Computes a single longest common subsequence for `x` and `y`.
  294. # If there are more than one longest common subsequences,
  295. # the one returned is that which starts first in `x`.
  296. #
  297. # @param x [Array]
  298. # @param y [Array]
  299. # @yield [a, b] An optional block to use in place of a check for equality
  300. # between elements of `x` and `y`.
  301. # @yieldreturn [Object, nil] If the two values register as equal,
  302. # this will return the value to use in the LCS array.
  303. # @return [Array] The LCS
  304. def lcs(x, y, &block)
  305. x = [nil, *x]
  306. y = [nil, *y]
  307. block ||= proc {|a, b| a == b && a}
  308. lcs_backtrace(lcs_table(x, y, &block), x, y, x.size-1, y.size-1, &block)
  309. end
  310. # Returns information about the caller of the previous method.
  311. #
  312. # @param entry [String] An entry in the `#caller` list, or a similarly formatted string
  313. # @return [[String, Fixnum, (String, nil)]] An array containing the filename, line, and method name of the caller.
  314. # The method name may be nil
  315. def caller_info(entry = caller[1])
  316. info = entry.scan(/^(.*?):(-?.*?)(?::.*`(.+)')?$/).first
  317. info[1] = info[1].to_i
  318. # This is added by Rubinius to designate a block, but we don't care about it.
  319. info[2].sub!(/ \{\}\Z/, '') if info[2]
  320. info
  321. end
  322. # Returns whether one version string represents a more recent version than another.
  323. #
  324. # @param v1 [String] A version string.
  325. # @param v2 [String] Another version string.
  326. # @return [Boolean]
  327. def version_gt(v1, v2)
  328. # Construct an array to make sure the shorter version is padded with nil
  329. Array.new([v1.length, v2.length].max).zip(v1.split("."), v2.split(".")) do |_, p1, p2|
  330. p1 ||= "0"
  331. p2 ||= "0"
  332. release1 = p1 =~ /^[0-9]+$/
  333. release2 = p2 =~ /^[0-9]+$/
  334. if release1 && release2
  335. # Integer comparison if both are full releases
  336. p1, p2 = p1.to_i, p2.to_i
  337. next if p1 == p2
  338. return p1 > p2
  339. elsif !release1 && !release2
  340. # String comparison if both are prereleases
  341. next if p1 == p2
  342. return p1 > p2
  343. else
  344. # If only one is a release, that one is newer
  345. return release1
  346. end
  347. end
  348. end
  349. # Returns whether one version string represents the same or a more
  350. # recent version than another.
  351. #
  352. # @param v1 [String] A version string.
  353. # @param v2 [String] Another version string.
  354. # @return [Boolean]
  355. def version_geq(v1, v2)
  356. version_gt(v1, v2) || !version_gt(v2, v1)
  357. end
  358. # Throws a NotImplementedError for an abstract method.
  359. #
  360. # @param obj [Object] `self`
  361. # @raise [NotImplementedError]
  362. def abstract(obj)
  363. raise NotImplementedError.new("#{obj.class} must implement ##{caller_info[2]}")
  364. end
  365. # Silence all output to STDERR within a block.
  366. #
  367. # @yield A block in which no output will be printed to STDERR
  368. def silence_warnings
  369. the_real_stderr, $stderr = $stderr, StringIO.new
  370. yield
  371. ensure
  372. $stderr = the_real_stderr
  373. end
  374. @@silence_warnings = false
  375. # Silences all Sass warnings within a block.
  376. #
  377. # @yield A block in which no Sass warnings will be printed
  378. def silence_sass_warnings
  379. old_silence_warnings = @@silence_warnings
  380. @@silence_warnings = true
  381. yield
  382. ensure
  383. @@silence_warnings = old_silence_warnings
  384. end
  385. # The same as `Kernel#warn`, but is silenced by \{#silence\_sass\_warnings}.
  386. #
  387. # @param msg [String]
  388. def sass_warn(msg)
  389. return if @@silence_warnings
  390. warn(msg)
  391. end
  392. ## Cross Rails Version Compatibility
  393. # Returns the root of the Rails application,
  394. # if this is running in a Rails context.
  395. # Returns `nil` if no such root is defined.
  396. #
  397. # @return [String, nil]
  398. def rails_root
  399. if defined?(::Rails.root)
  400. return ::Rails.root.to_s if ::Rails.root
  401. raise "ERROR: Rails.root is nil!"
  402. end
  403. return RAILS_ROOT.to_s if defined?(RAILS_ROOT)
  404. return nil
  405. end
  406. # Returns the environment of the Rails application,
  407. # if this is running in a Rails context.
  408. # Returns `nil` if no such environment is defined.
  409. #
  410. # @return [String, nil]
  411. def rails_env
  412. return ::Rails.env.to_s if defined?(::Rails.env)
  413. return RAILS_ENV.to_s if defined?(RAILS_ENV)
  414. return nil
  415. end
  416. # Returns whether this environment is using ActionPack
  417. # version 3.0.0 or greater.
  418. #
  419. # @return [Boolean]
  420. def ap_geq_3?
  421. ap_geq?("3.0.0.beta1")
  422. end
  423. # Returns whether this environment is using ActionPack
  424. # of a version greater than or equal to that specified.
  425. #
  426. # @param version [String] The string version number to check against.
  427. # Should be greater than or equal to Rails 3,
  428. # because otherwise ActionPack::VERSION isn't autoloaded
  429. # @return [Boolean]
  430. def ap_geq?(version)
  431. # The ActionPack module is always loaded automatically in Rails >= 3
  432. return false unless defined?(ActionPack) && defined?(ActionPack::VERSION) &&
  433. defined?(ActionPack::VERSION::STRING)
  434. version_geq(ActionPack::VERSION::STRING, version)
  435. end
  436. # Returns an ActionView::Template* class.
  437. # In pre-3.0 versions of Rails, most of these classes
  438. # were of the form `ActionView::TemplateFoo`,
  439. # while afterwards they were of the form `ActionView;:Template::Foo`.
  440. #
  441. # @param name [#to_s] The name of the class to get.
  442. # For example, `:Error` will return `ActionView::TemplateError`
  443. # or `ActionView::Template::Error`.
  444. def av_template_class(name)
  445. return ActionView.const_get("Template#{name}") if ActionView.const_defined?("Template#{name}")
  446. return ActionView::Template.const_get(name.to_s)
  447. end
  448. ## Cross-OS Compatibility
  449. # Whether or not this is running on Windows.
  450. #
  451. # @return [Boolean]
  452. def windows?
  453. RbConfig::CONFIG['host_os'] =~ /mswin|windows|mingw/i
  454. end
  455. # Whether or not this is running on IronRuby.
  456. #
  457. # @return [Boolean]
  458. def ironruby?
  459. RUBY_ENGINE == "ironruby"
  460. end
  461. ## Cross-Ruby-Version Compatibility
  462. # Whether or not this is running under Ruby 1.8 or lower.
  463. #
  464. # Note that IronRuby counts as Ruby 1.8,
  465. # because it doesn't support the Ruby 1.9 encoding API.
  466. #
  467. # @return [Boolean]
  468. def ruby1_8?
  469. # IronRuby says its version is 1.9, but doesn't support any of the encoding APIs.
  470. # We have to fall back to 1.8 behavior.
  471. ironruby? || (Sass::Util::RUBY_VERSION[0] == 1 && Sass::Util::RUBY_VERSION[1] < 9)
  472. end
  473. # Whether or not this is running under Ruby 1.8.6 or lower.
  474. # Note that lower versions are not officially supported.
  475. #
  476. # @return [Boolean]
  477. def ruby1_8_6?
  478. ruby1_8? && Sass::Util::RUBY_VERSION[2] < 7
  479. end
  480. # Checks that the encoding of a string is valid in Ruby 1.9
  481. # and cleans up potential encoding gotchas like the UTF-8 BOM.
  482. # If it's not, yields an error string describing the invalid character
  483. # and the line on which it occurrs.
  484. #
  485. # @param str [String] The string of which to check the encoding
  486. # @yield [msg] A block in which an encoding error can be raised.
  487. # Only yields if there is an encoding error
  488. # @yieldparam msg [String] The error message to be raised
  489. # @return [String] `str`, potentially with encoding gotchas like BOMs removed
  490. def check_encoding(str)
  491. if ruby1_8?
  492. return str.gsub(/\A\xEF\xBB\xBF/, '') # Get rid of the UTF-8 BOM
  493. elsif str.valid_encoding?
  494. # Get rid of the Unicode BOM if possible
  495. if str.encoding.name =~ /^UTF-(8|16|32)(BE|LE)?$/
  496. return str.gsub(Regexp.new("\\A\uFEFF".encode(str.encoding.name)), '')
  497. else
  498. return str
  499. end
  500. end
  501. encoding = str.encoding
  502. newlines = Regexp.new("\r\n|\r|\n".encode(encoding).force_encoding("binary"))
  503. str.force_encoding("binary").split(newlines).each_with_index do |line, i|
  504. begin
  505. line.encode(encoding)
  506. rescue Encoding::UndefinedConversionError => e
  507. yield <<MSG.rstrip, i + 1
  508. Invalid #{encoding.name} character #{e.error_char.dump}
  509. MSG
  510. end
  511. end
  512. return str
  513. end
  514. # Like {\#check\_encoding}, but also checks for a `@charset` declaration
  515. # at the beginning of the file and uses that encoding if it exists.
  516. #
  517. # The Sass encoding rules are simple.
  518. # If a `@charset` declaration exists,
  519. # we assume that that's the original encoding of the document.
  520. # Otherwise, we use whatever encoding Ruby has.
  521. # Then we convert that to UTF-8 to process internally.
  522. # The UTF-8 end result is what's returned by this method.
  523. #
  524. # @param str [String] The string of which to check the encoding
  525. # @yield [msg] A block in which an encoding error can be raised.
  526. # Only yields if there is an encoding error
  527. # @yieldparam msg [String] The error message to be raised
  528. # @return [(String, Encoding)] The original string encoded as UTF-8,
  529. # and the source encoding of the string (or `nil` under Ruby 1.8)
  530. # @raise [Encoding::UndefinedConversionError] if the source encoding
  531. # cannot be converted to UTF-8
  532. # @raise [ArgumentError] if the document uses an unknown encoding with `@charset`
  533. def check_sass_encoding(str, &block)
  534. return check_encoding(str, &block), nil if ruby1_8?
  535. # We allow any printable ASCII characters but double quotes in the charset decl
  536. bin = str.dup.force_encoding("BINARY")
  537. encoding = Sass::Util::ENCODINGS_TO_CHECK.find do |enc|
  538. bin =~ Sass::Util::CHARSET_REGEXPS[enc]
  539. end
  540. charset, bom = $1, $2
  541. if charset
  542. charset = charset.force_encoding(encoding).encode("UTF-8")
  543. if endianness = encoding[/[BL]E$/]
  544. begin
  545. Encoding.find(charset + endianness)
  546. charset << endianness
  547. rescue ArgumentError # Encoding charset + endianness doesn't exist
  548. end
  549. end
  550. str.force_encoding(charset)
  551. elsif bom
  552. str.force_encoding(encoding)
  553. end
  554. str = check_encoding(str, &block)
  555. return str.encode("UTF-8"), str.encoding
  556. end
  557. unless ruby1_8?
  558. # @private
  559. def _enc(string, encoding)
  560. string.encode(encoding).force_encoding("BINARY")
  561. end
  562. # We could automatically add in any non-ASCII-compatible encodings here,
  563. # but there's not really a good way to do that
  564. # without manually checking that each encoding
  565. # encodes all ASCII characters properly,
  566. # which takes long enough to affect the startup time of the CLI.
  567. ENCODINGS_TO_CHECK = %w[UTF-8 UTF-16BE UTF-16LE UTF-32BE UTF-32LE]
  568. CHARSET_REGEXPS = Hash.new do |h, e|
  569. h[e] =
  570. begin
  571. # /\A(?:\uFEFF)?@charset "(.*?)"|\A(\uFEFF)/
  572. Regexp.new(/\A(?:#{_enc("\uFEFF", e)})?#{
  573. _enc('@charset "', e)}(.*?)#{_enc('"', e)}|\A(#{
  574. _enc("\uFEFF", e)})/)
  575. rescue
  576. # /\A@charset "(.*?)"/
  577. Regexp.new(/\A#{_enc('@charset "', e)}(.*?)#{_enc('"', e)}/)
  578. end
  579. end
  580. end
  581. # Checks to see if a class has a given method.
  582. # For example:
  583. #
  584. # Sass::Util.has?(:public_instance_method, String, :gsub) #=> true
  585. #
  586. # Method collections like `Class#instance_methods`
  587. # return strings in Ruby 1.8 and symbols in Ruby 1.9 and on,
  588. # so this handles checking for them in a compatible way.
  589. #
  590. # @param attr [#to_s] The (singular) name of the method-collection method
  591. # (e.g. `:instance_methods`, `:private_methods`)
  592. # @param klass [Module] The class to check the methods of which to check
  593. # @param method [String, Symbol] The name of the method do check for
  594. # @return [Boolean] Whether or not the given collection has the given method
  595. def has?(attr, klass, method)
  596. klass.send("#{attr}s").include?(ruby1_8? ? method.to_s : method.to_sym)
  597. end
  598. # A version of `Enumerable#enum_with_index` that works in Ruby 1.8 and 1.9.
  599. #
  600. # @param enum [Enumerable] The enumerable to get the enumerator for
  601. # @return [Enumerator] The with-index enumerator
  602. def enum_with_index(enum)
  603. ruby1_8? ? enum.enum_with_index : enum.each_with_index
  604. end
  605. # A version of `Enumerable#enum_cons` that works in Ruby 1.8 and 1.9.
  606. #
  607. # @param enum [Enumerable] The enumerable to get the enumerator for
  608. # @param n [Fixnum] The size of each cons
  609. # @return [Enumerator] The consed enumerator
  610. def enum_cons(enum, n)
  611. ruby1_8? ? enum.enum_cons(n) : enum.each_cons(n)
  612. end
  613. # A version of `Enumerable#enum_slice` that works in Ruby 1.8 and 1.9.
  614. #
  615. # @param enum [Enumerable] The enumerable to get the enumerator for
  616. # @param n [Fixnum] The size of each slice
  617. # @return [Enumerator] The consed enumerator
  618. def enum_slice(enum, n)
  619. ruby1_8? ? enum.enum_slice(n) : enum.each_slice(n)
  620. end
  621. # Returns the ASCII code of the given character.
  622. #
  623. # @param c [String] All characters but the first are ignored.
  624. # @return [Fixnum] The ASCII code of `c`.
  625. def ord(c)
  626. ruby1_8? ? c[0] : c.ord
  627. end
  628. # Flattens the first `n` nested arrays in a cross-version manner.
  629. #
  630. # @param arr [Array] The array to flatten
  631. # @param n [Fixnum] The number of levels to flatten
  632. # @return [Array] The flattened array
  633. def flatten(arr, n)
  634. return arr.flatten(n) unless ruby1_8_6?
  635. return arr if n == 0
  636. arr.inject([]) {|res, e| e.is_a?(Array) ? res.concat(flatten(e, n - 1)) : res << e}
  637. end
  638. # Returns the hash code for a set in a cross-version manner.
  639. # Aggravatingly, this is order-dependent in Ruby 1.8.6.
  640. #
  641. # @param set [Set]
  642. # @return [Fixnum] The order-independent hashcode of `set`
  643. def set_hash(set)
  644. return set.hash unless ruby1_8_6?
  645. set.map {|e| e.hash}.uniq.sort.hash
  646. end
  647. # Tests the hash-equality of two sets in a cross-version manner.
  648. # Aggravatingly, this is order-dependent in Ruby 1.8.6.
  649. #
  650. # @param set1 [Set]
  651. # @param set2 [Set]
  652. # @return [Boolean] Whether or not the sets are hashcode equal
  653. def set_eql?(set1, set2)
  654. return set1.eql?(set2) unless ruby1_8_6?
  655. set1.to_a.uniq.sort_by {|e| e.hash}.eql?(set2.to_a.uniq.sort_by {|e| e.hash})
  656. end
  657. # Like `Object#inspect`, but preserves non-ASCII characters rather than escaping them under Ruby 1.9.2.
  658. # This is necessary so that the precompiled Haml template can be `#encode`d into `@options[:encoding]`
  659. # before being evaluated.
  660. #
  661. # @param obj {Object}
  662. # @return {String}
  663. def inspect_obj(obj)
  664. return obj.inspect unless version_geq(::RUBY_VERSION, "1.9.2")
  665. return ':' + inspect_obj(obj.to_s) if obj.is_a?(Symbol)
  666. return obj.inspect unless obj.is_a?(String)
  667. '"' + obj.gsub(/[\x00-\x7F]+/) {|s| s.inspect[1...-1]} + '"'
  668. end
  669. ## Static Method Stuff
  670. # The context in which the ERB for \{#def\_static\_method} will be run.
  671. class StaticConditionalContext
  672. # @param set [#include?] The set of variables that are defined for this context.
  673. def initialize(set)
  674. @set = set
  675. end
  676. # Checks whether or not a variable is defined for this context.
  677. #
  678. # @param name [Symbol] The name of the variable
  679. # @return [Boolean]
  680. def method_missing(name, *args, &block)
  681. super unless args.empty? && block.nil?
  682. @set.include?(name)
  683. end
  684. end
  685. private
  686. # Calculates the memoization table for the Least Common Subsequence algorithm.
  687. # Algorithm from [Wikipedia](http://en.wikipedia.org/wiki/Longest_common_subsequence_problem#Computing_the_length_of_the_LCS)
  688. def lcs_table(x, y)
  689. c = Array.new(x.size) {[]}
  690. x.size.times {|i| c[i][0] = 0}
  691. y.size.times {|j| c[0][j] = 0}
  692. (1...x.size).each do |i|
  693. (1...y.size).each do |j|
  694. c[i][j] =
  695. if yield x[i], y[j]
  696. c[i-1][j-1] + 1
  697. else
  698. [c[i][j-1], c[i-1][j]].max
  699. end
  700. end
  701. end
  702. return c
  703. end
  704. # Computes a single longest common subsequence for arrays x and y.
  705. # Algorithm from [Wikipedia](http://en.wikipedia.org/wiki/Longest_common_subsequence_problem#Reading_out_an_LCS)
  706. def lcs_backtrace(c, x, y, i, j, &block)
  707. return [] if i == 0 || j == 0
  708. if v = yield(x[i], y[j])
  709. return lcs_backtrace(c, x, y, i-1, j-1, &block) << v
  710. end
  711. return lcs_backtrace(c, x, y, i, j-1, &block) if c[i][j-1] > c[i-1][j]
  712. return lcs_backtrace(c, x, y, i-1, j, &block)
  713. end
  714. end
  715. end
  716. module Sass
  717. # Handles Sass version-reporting.
  718. # Sass not only reports the standard three version numbers,
  719. # but its Git revision hash as well,
  720. # if it was installed from Git.
  721. module Version
  722. include Sass::Util
  723. # Returns a hash representing the version of Sass.
  724. # The `:major`, `:minor`, and `:teeny` keys have their respective numbers as Fixnums.
  725. # The `:name` key has the name of the version.
  726. # The `:string` key contains a human-readable string representation of the version.
  727. # The `:number` key is the major, minor, and teeny keys separated by periods.
  728. # If Sass is checked out from Git, the `:rev` key will have the revision hash.
  729. # For example:
  730. #
  731. # {
  732. # :string => "2.1.0.9616393",
  733. # :rev => "9616393b8924ef36639c7e82aa88a51a24d16949",
  734. # :number => "2.1.0",
  735. # :major => 2, :minor => 1, :teeny => 0
  736. # }
  737. #
  738. # If a prerelease version of Sass is being used,
  739. # the `:string` and `:number` fields will reflect the full version
  740. # (e.g. `"2.2.beta.1"`), and the `:teeny` field will be `-1`.
  741. # A `:prerelease` key will contain the name of the prerelease (e.g. `"beta"`),
  742. # and a `:prerelease_number` key will contain the rerelease number.
  743. # For example:
  744. #
  745. # {
  746. # :string => "3.0.beta.1",
  747. # :number => "3.0.beta.1",
  748. # :major => 3, :minor => 0, :teeny => -1,
  749. # :prerelease => "beta",
  750. # :prerelease_number => 1
  751. # }
  752. #
  753. # @return [{Symbol => String/Fixnum}] The version hash
  754. def version
  755. return @@version if defined?(@@version)
  756. #numbers = File.read(scope('VERSION')).strip.split('.').
  757. numbers = "3.2.0.alpha.0".strip.split('.').
  758. map {|n| n =~ /^[0-9]+$/ ? n.to_i : n}
  759. name = "Bleeding Edge"
  760. @@version = {
  761. :major => numbers[0],
  762. :minor => numbers[1],
  763. :teeny => numbers[2],
  764. :name => name
  765. }
  766. if numbers[3].is_a?(String)
  767. @@version[:teeny] = -1
  768. @@version[:prerelease] = numbers[3]
  769. @@version[:prerelease_number] = numbers[4]
  770. end
  771. @@version[:number] = numbers.join('.')
  772. @@version[:string] = @@version[:number].dup
  773. if rev = revision_number
  774. @@version[:rev] = rev
  775. unless rev[0] == ?(
  776. @@version[:string] << "." << rev[0...7]
  777. end
  778. end
  779. @@version[:string] << " (#{name})"
  780. @@version
  781. end
  782. private
  783. def revision_number
  784. if File.exists?(scope('REVISION'))
  785. rev = File.read(scope('REVISION')).strip
  786. return rev unless rev =~ /^([a-f0-9]+|\(.*\))$/ || rev == '(unknown)'
  787. end
  788. return unless File.exists?(scope('.git/HEAD'))
  789. rev = File.read(scope('.git/HEAD')).strip
  790. return rev unless rev =~ /^ref: (.*)$/
  791. ref_name = $1
  792. ref_file = scope(".git/#{ref_name}")
  793. info_file = scope(".git/info/refs")
  794. return File.read(ref_file).strip if File.exists?(ref_file)
  795. return unless File.exists?(info_file)
  796. File.open(info_file) do |f|
  797. f.each do |l|
  798. sha, ref = l.strip.split("\t", 2)
  799. next unless ref == ref_name
  800. return sha
  801. end
  802. end
  803. return nil
  804. end
  805. end
  806. extend Sass::Version
  807. # A string representing the version of Sass.
  808. # A more fine-grained representation is available from Sass.version.
  809. # @api public
  810. VERSION = version[:string] unless defined?(Sass::VERSION)
  811. end
  812. # The module that contains everything Sass-related:
  813. #
  814. # * {Sass::Engine} is the class used to render Sass/SCSS within Ruby code.
  815. # * {Sass::Plugin} is interfaces with web frameworks (Rails and Merb in particular).
  816. # * {Sass::SyntaxError} is raised when Sass encounters an error.
  817. # * {Sass::CSS} handles conversion of CSS to Sass.
  818. #
  819. # Also see the {file:SASS_REFERENCE.md full Sass reference}.
  820. module Sass
  821. # Compile a Sass or SCSS string to CSS.
  822. # Defaults to SCSS.
  823. #
  824. # @param contents [String] The contents of the Sass file.
  825. # @param options [{Symbol => Object}] An options hash;
  826. # see {file:SASS_REFERENCE.md#sass_options the Sass options documentation}
  827. # @raise [Sass::SyntaxError] if there's an error in the document
  828. # @raise [Encoding::UndefinedConversionError] if the source encoding
  829. # cannot be converted to UTF-8
  830. # @raise [ArgumentError] if the document uses an unknown encoding with `@charset`
  831. def self.compile(contents, options = {})
  832. options[:syntax] ||= :scss
  833. Engine.new(contents, options).to_css
  834. end
  835. # Compile a file on disk to CSS.
  836. #
  837. # @param filename [String] The path to the Sass, SCSS, or CSS file on disk.
  838. # @param options [{Symbol => Object}] An options hash;
  839. # see {file:SASS_REFERENCE.md#sass_options the Sass options documentation}
  840. # @raise [Sass::SyntaxError] if there's an error in the document
  841. # @raise [Encoding::UndefinedConversionError] if the source encoding
  842. # cannot be converted to UTF-8
  843. # @raise [ArgumentError] if the document uses an unknown encoding with `@charset`
  844. #
  845. # @overload compile_file(filename, options = {})
  846. # Return the compiled CSS rather than writing it to a file.
  847. #
  848. # @return [String] The compiled CSS.
  849. #
  850. # @overload compile_file(filename, css_filename, options = {})
  851. # Write the compiled CSS to a file.
  852. #
  853. # @param css_filename [String] The location to which to write the compiled CSS.
  854. def self.compile_file(filename, *args)
  855. options = args.last.is_a?(Hash) ? args.pop : {}
  856. css_filename = args.shift
  857. result = Sass::Engine.for_file(filename, options).render
  858. if css_filename
  859. options[:css_filename] ||= css_filename
  860. open(css_filename,"w") {|css_file| css_file.write(result)}
  861. nil
  862. else
  863. result
  864. end
  865. end
  866. end
  867. require 'strscan'
  868. require 'set'
  869. require 'digest/sha1'
  870. require 'stringio'
  871. module Sass
  872. # Sass cache stores are in charge of storing cached information,
  873. # especially parse trees for Sass documents.
  874. #
  875. # User-created importers must inherit from {CacheStores::Base}.
  876. module CacheStores
  877. end
  878. end
  879. module Sass
  880. module CacheStores
  881. # An abstract base class for backends for the Sass cache.
  882. # Any key-value store can act as such a backend;
  883. # it just needs to implement the
  884. # \{#_store} and \{#_retrieve} methods.
  885. #
  886. # To use a cache store with Sass,
  887. # use the {file:SASS_REFERENCE.md#cache_store-option `:cache_store` option}.
  888. #
  889. # @abstract
  890. class Base
  891. # Store cached contents for later retrieval
  892. # Must be implemented by all CacheStore subclasses
  893. #
  894. # Note: cache contents contain binary data.
  895. #
  896. # @param key [String] The key to store the contents under
  897. # @param version [String] The current sass version.
  898. # Cached contents must not be retrieved across different versions of sass.
  899. # @param sha [String] The sha of the sass source.
  900. # Cached contents must not be retrieved if the sha has changed.
  901. # @param contents [String] The contents to store.
  902. def _store(key, version, sha, contents)
  903. raise "#{self.class} must implement #_store."
  904. end
  905. # Retrieved cached contents.
  906. # Must be implemented by all subclasses.
  907. #
  908. # Note: if the key exists but the sha or version have changed,
  909. # then the key may be deleted by the cache store, if it wants to do so.
  910. #
  911. # @param key [String] The key to retrieve
  912. # @param version [String] The current sass version.
  913. # Cached contents must not be retrieved across different versions of sass.
  914. # @param sha [String] The sha of the sass source.
  915. # Cached contents must not be retrieved if the sha has changed.
  916. # @return [String] The contents that were previously stored.
  917. # @return [NilClass] when the cache key is not found or the version or sha have changed.
  918. def _retrieve(key, version, sha)
  919. raise "#{self.class} must implement #_retrieve."
  920. end
  921. # Store a {Sass::Tree::RootNode}.
  922. #
  923. # @param key [String] The key to store it under.
  924. # @param sha [String] The checksum for the contents that are being stored.
  925. # @param obj [Object] The object to cache.
  926. def store(key, sha, root)
  927. _store(key, Sass::VERSION, sha, Marshal.dump(root))
  928. rescue TypeError, LoadError => e
  929. Sass::Util.sass_warn "Warning. Error encountered while saving cache #{path_to(key)}: #{e}"
  930. end
  931. # Retrieve a {Sass::Tree::RootNode}.
  932. #
  933. # @param key [String] The key the root element was stored under.
  934. # @param sha [String] The checksum of the root element's content.
  935. # @return [Object] The cached object.
  936. def retrieve(key, sha)
  937. contents = _retrieve(key, Sass::VERSION, sha)
  938. Marshal.load(contents) if contents
  939. rescue EOFError, TypeError, ArgumentError, LoadError => e
  940. Sass::Util.sass_warn "Warning. Error encountered while reading cache #{path_to(key)}: #{e}"
  941. end
  942. # Return the key for the sass file.
  943. #
  944. # The `(sass_dirname, sass_basename)` pair
  945. # should uniquely identify the Sass document,
  946. # but otherwise there are no restrictions on their content.
  947. #
  948. # @param sass_dirname [String]
  949. # The fully-expanded location of the Sass file.
  950. # This corresponds to the directory name on a filesystem.
  951. # @param sass_basename [String] The name of the Sass file that is being referenced.
  952. # This corresponds to the basename on a filesystem.
  953. def key(sass_dirname, sass_basename)
  954. dir = Digest::SHA1.hexdigest(sass_dirname)
  955. filename = "#{sass_basename}c"
  956. "#{dir}/#{filename}"
  957. end
  958. end
  959. end
  960. end
  961. module Sass
  962. module CacheStores
  963. # A backend for the Sass cache using the filesystem.
  964. class Filesystem < Base
  965. # The directory where the cached files will be stored.
  966. #
  967. # @return [String]
  968. attr_accessor :cache_location
  969. # @param cache_location [String] see \{#cache\_location}
  970. def initialize(cache_location)
  971. @cache_location = cache_location
  972. end
  973. # @see Base#\_retrieve
  974. def _retrieve(key, version, sha)
  975. return unless File.readable?(path_to(key))
  976. contents = nil
  977. File.open(path_to(key), "rb") do |f|
  978. if f.readline("\n").strip == version && f.readline("\n").strip == sha
  979. return f.read
  980. end
  981. end
  982. File.unlink path_to(key)
  983. nil
  984. rescue EOFError, TypeError, ArgumentError => e
  985. Sass::Util.sass_warn "Warning. Error encountered while reading cache #{path_to(key)}: #{e}"
  986. end
  987. # @see Base#\_store
  988. def _store(key, version, sha, contents)
  989. # return unless File.writable?(File.dirname(@cache_location))
  990. # return if File.exists?(@cache_location) && !File.writable?(@cache_location)
  991. compiled_filename = path_to(key)
  992. # return if File.exists?(File.dirname(compiled_filename)) && !File.writable?(File.dirname(compiled_filename))
  993. # return if File.exists?(compiled_filename) && !File.writable?(compiled_filename)
  994. FileUtils.mkdir_p(File.dirname(compiled_filename))
  995. File.open(compiled_filename, "wb") do |f|
  996. f.puts(version)
  997. f.puts(sha)
  998. f.write(contents)
  999. end
  1000. rescue Errno::EACCES
  1001. #pass
  1002. end
  1003. private
  1004. # Returns the path to a file for the given key.
  1005. #
  1006. # @param key [String]
  1007. # @return [String] The path to the cache file.
  1008. def path_to(key)
  1009. File.join(cache_location, key)
  1010. end
  1011. end
  1012. end
  1013. end
  1014. module Sass
  1015. module CacheStores
  1016. # A backend for the Sass cache using in-process memory.
  1017. class Memory < Base
  1018. # Since the {Memory} store is stored in the Sass tree's options hash,
  1019. # when the options get serialized as part of serializing the tree,
  1020. # you get crazy exponential growth in the size of the cached objects
  1021. # unless you don't dump the cache.
  1022. #
  1023. # @private
  1024. def _dump(depth)
  1025. ""
  1026. end
  1027. # If we deserialize this class, just make a new empty one.
  1028. #
  1029. # @private
  1030. def self._load(repr)
  1031. Memory.new
  1032. end
  1033. # Create a new, empty cache store.
  1034. def initialize
  1035. @contents = {}
  1036. end
  1037. # @see Base#retrieve
  1038. def retrieve(key, sha)
  1039. if @contents.has_key?(key)
  1040. return unless @contents[key][:sha] == sha
  1041. obj = @contents[key][:obj]
  1042. obj.respond_to?(:deep_copy) ? obj.deep_copy : obj.dup
  1043. end
  1044. end
  1045. # @see Base#store
  1046. def store(key, sha, obj)
  1047. @contents[key] = {:sha => sha, :obj => obj}
  1048. end
  1049. # Destructively clear the cache.
  1050. def reset!
  1051. @contents = {}
  1052. end
  1053. end
  1054. end
  1055. end
  1056. module Sass
  1057. module CacheStores
  1058. # A meta-cache that chains multiple caches together.
  1059. # Specifically:
  1060. #
  1061. # * All `#store`s are passed to all caches.
  1062. # * `#retrieve`s are passed to each cache until one has a hit.
  1063. # * When one cache has a hit, the value is `#store`d in all earlier caches.
  1064. class Chain < Base
  1065. # Create a new cache chaining the given caches.
  1066. #
  1067. # @param caches [Array<Sass::CacheStores::Base>] The caches to chain.
  1068. def initialize(*caches)
  1069. @caches = caches
  1070. end
  1071. # @see Base#store
  1072. def store(key, sha, obj)
  1073. @caches.each {|c| c.store(key, sha, obj)}
  1074. end
  1075. # @see Base#retrieve
  1076. def retrieve(key, sha)
  1077. @caches.each_with_index do |c, i|
  1078. next unless obj = c.retrieve(key, sha)
  1079. @caches[0...i].each {|c| c.store(key, sha, obj)}
  1080. return obj
  1081. end
  1082. nil
  1083. end
  1084. end
  1085. end
  1086. end
  1087. module Sass
  1088. # A namespace for nodes in the Sass parse tree.
  1089. #
  1090. # The Sass parse tree has three states: dynamic, static Sass, and static CSS.
  1091. #
  1092. # When it's first parsed, a Sass document is in the dynamic state.
  1093. # It has nodes for mixin definitions and `@for` loops and so forth,
  1094. # in addition to nodes for CSS rules and properties.
  1095. # Nodes that only appear in this state are called **dynamic nodes**.
  1096. #
  1097. # {Tree::Visitors::Perform} creates a static Sass tree, which is different.
  1098. # It still has nodes for CSS rules and properties
  1099. # but it doesn't have any dynamic-generation-related nodes.
  1100. # The nodes in this state are in the same structure as the Sass document:
  1101. # rules and properties are nested beneath one another.
  1102. # Nodes that can be in this state or in the dynamic state
  1103. # are called **static nodes**.
  1104. #
  1105. # {Tree::Visitors::Cssize} is then used to create a static CSS tree.
  1106. # This is like a static Sass tree,
  1107. # but the structure exactly mirrors that of the generated CSS.
  1108. # Rules and properties can't be nested beneath one another in this state.
  1109. #
  1110. # Finally, {Tree::Visitors::ToCss} can be called on a static CSS tree
  1111. # to get the actual CSS code as a string.
  1112. module Tree
  1113. # The abstract superclass of all parse-tree nodes.
  1114. class Node
  1115. include Enumerable
  1116. # The child nodes of this node.
  1117. #
  1118. # @return [Array<Tree::Node>]
  1119. attr_accessor :children
  1120. # Whether or not this node has child nodes.
  1121. # This may be true even when \{#children} is empty,
  1122. # in which case this node has an empty block (e.g. `{}`).
  1123. #
  1124. # @return [Boolean]
  1125. attr_accessor :has_children
  1126. # The line of the document on which this node appeared.
  1127. #
  1128. # @return [Fixnum]
  1129. attr_accessor :line
  1130. # The name of the document on which this node appeared.
  1131. #
  1132. # @return [String]
  1133. attr_writer :filename
  1134. # The options hash for the node.
  1135. # See {file:SASS_REFERENCE.md#sass_options the Sass options documentation}.
  1136. #
  1137. # @return [{Symbol => Object}]
  1138. attr_reader :options
  1139. def initialize
  1140. @children = []
  1141. end
  1142. # Sets the options hash for the node and all its children.
  1143. #
  1144. # @param options [{Symbol => Object}] The options
  1145. # @see #options
  1146. def options=(options)
  1147. children.each {|c| c.options = options}
  1148. @options = options
  1149. end
  1150. # @private
  1151. def children=(children)
  1152. self.has_children ||= !children.empty?
  1153. @children = children
  1154. end
  1155. # The name of the document on which this node appeared.
  1156. #
  1157. # @return [String]
  1158. def filename
  1159. @filename || (@options && @options[:filename])
  1160. end
  1161. # Appends a child to the node.
  1162. #
  1163. # @param child [Tree::Node, Array<Tree::Node>] The child node or nodes
  1164. # @raise [Sass::SyntaxError] if `child` is invalid
  1165. def <<(child)
  1166. return if child.nil?
  1167. if child.is_a?(Array)
  1168. child.each {|c| self << c}
  1169. else
  1170. self.has_children = true
  1171. @children << child
  1172. end
  1173. end
  1174. # Compares this node and another object (only other {Tree::Node}s will be equal).
  1175. # This does a structural comparison;
  1176. # if the contents of the nodes and all the child nodes are equivalent,
  1177. # then the nodes are as well.
  1178. #
  1179. # Only static nodes need to override this.
  1180. #
  1181. # @param other [Object] The object to compare with
  1182. # @return [Boolean] Whether or not this node and the other object
  1183. # are the same
  1184. # @see Sass::Tree
  1185. def ==(other)
  1186. self.class == other.class && other.children == children
  1187. end
  1188. # True if \{#to\_s} will return `nil`;
  1189. # that is, if the node shouldn't be rendered.
  1190. # Should only be called in a static tree.
  1191. #
  1192. # @return [Boolean]
  1193. def invisible?; false; end
  1194. # The output style. See {file:SASS_REFERENCE.md#sass_options the Sass options documentation}.
  1195. #
  1196. # @return [Symbol]
  1197. def style
  1198. @options[:style]
  1199. end
  1200. # Computes the CSS corresponding to this static CSS tree.
  1201. #
  1202. # @return [String, nil] The resulting CSS
  1203. # @see Sass::Tree
  1204. def to_s
  1205. Sass::Tree::Visitors::ToCss.visit(self)
  1206. end
  1207. # Converts a static CSS tree (e.g. the output of \{Tree::Visitors::Cssize})
  1208. # into another static CSS tree,
  1209. # with the given extensions applied to all relevant {RuleNode}s.
  1210. #
  1211. # @todo Link this to the reference documentation on `@extend`
  1212. # when such a thing exists.
  1213. #
  1214. # @param extends [Sass::Util::SubsetMap{Selector::Simple => Selector::Sequence}]
  1215. # The extensions to perform on this tree
  1216. # @return [Tree::Node] The resulting tree of static CSS nodes.
  1217. # @raise [Sass::SyntaxError] Only if there's a programmer error
  1218. # and this is not a static CSS tree
  1219. def do_extend(extends)
  1220. node = dup
  1221. node.children = children.map {|c| c.do_extend(extends)}
  1222. node
  1223. rescue Sass::SyntaxError => e
  1224. e.modify_backtrace(:filename => filename, :line => line)
  1225. raise e
  1226. end
  1227. # Iterates through each node in the tree rooted at this node
  1228. # in a pre-order walk.
  1229. #
  1230. # @yield node
  1231. # @yieldparam node [Node] a node in the tree
  1232. def each(&block)
  1233. yield self
  1234. children.each {|c| c.each(&block)}
  1235. end
  1236. # Converts a node to Sass code that will generate it.
  1237. #
  1238. # @param options [{Symbol => Object}] An options hash (see {Sass::CSS#initialize})
  1239. # @return [String] The Sass code corresponding to the node
  1240. def to_sass(options = {})
  1241. Sass::Tree::Visitors::Convert.visit(self, options, :sass)
  1242. end
  1243. # Converts a node to SCSS code that will generate it.
  1244. #
  1245. # @param options [{Symbol => Object}] An options hash (see {Sass::CSS#initialize})
  1246. # @return [String] The Sass code corresponding to the node
  1247. def to_scss(options = {})
  1248. Sass::Tree::Visitors::Convert.visit(self, options, :scss)
  1249. end
  1250. # Return a deep clone of this node.
  1251. # The child nodes are cloned, but options are not.
  1252. #
  1253. # @return [Node]
  1254. def deep_copy
  1255. node = dup
  1256. node.children = children.map {|c| c.deep_copy}
  1257. node
  1258. end
  1259. protected
  1260. # @see Sass::Shared.balance
  1261. # @raise [Sass::SyntaxError] if the brackets aren't balanced
  1262. def balance(*args)
  1263. res = Sass::Shared.balance(*args)
  1264. return res if res
  1265. raise Sass::SyntaxError.new("Unbalanced brackets.", :line => line)
  1266. end
  1267. end
  1268. end
  1269. end
  1270. module Sass
  1271. module Tree
  1272. # A static node that is the root node of the Sass document.
  1273. class RootNode < Node
  1274. # The Sass template from which this node was created
  1275. #
  1276. # @param template [String]
  1277. attr_reader :template
  1278. # @param template [String] The Sass template from which this node was created
  1279. def initialize(template)
  1280. super()
  1281. @template = template
  1282. end
  1283. # Runs the dynamic Sass code *and* computes the CSS for the tree.
  1284. # @see #to_s
  1285. def render
  1286. Visitors::CheckNesting.visit(self)
  1287. result = Visitors::Perform.visit(self)
  1288. Visitors::CheckNesting.visit(result) # Check again to validate mixins
  1289. result, extends = Visitors::Cssize.visit(result)
  1290. result = result.do_extend(extends) unless extends.empty?
  1291. result.to_s
  1292. end
  1293. end
  1294. end
  1295. end
  1296. require 'pathname'
  1297. require 'uri'
  1298. module Sass::Tree
  1299. # A static node reprenting a CSS rule.
  1300. #
  1301. # @see Sass::Tree
  1302. class RuleNode < Node
  1303. # The character used to include the parent selector
  1304. PARENT = '&'
  1305. # The CSS selector for this rule,
  1306. # interspersed with {Sass::Script::Node}s
  1307. # representing `#{}`-interpolation.
  1308. # Any adjacent strings will be merged together.
  1309. #
  1310. # @return [Array<String, Sass::Script::Node>]
  1311. attr_accessor :rule
  1312. # The CSS selector for this rule,
  1313. # without any unresolved interpolation
  1314. # but with parent references still intact.
  1315. # It's only set once {Tree::Node#perform} has been called.
  1316. #
  1317. # @return [Selector::CommaSequence]
  1318. attr_accessor :parsed_rules
  1319. # The CSS selector for this rule,
  1320. # without any unresolved interpolation or parent references.
  1321. # It's only set once {Tree::Visitors::Cssize} has been run.
  1322. #
  1323. # @return [Selector::CommaSequence]
  1324. attr_accessor :resolved_rules
  1325. # How deep this rule is indented
  1326. # relative to a base-level rule.
  1327. # This is only greater than 0 in the case that:
  1328. #
  1329. # * This node is in a CSS tree
  1330. # * The style is :nested
  1331. # * This is a child rule of another rule
  1332. # * The parent rule has properties, and thus will be rendered
  1333. #
  1334. # @return [Fixnum]
  1335. attr_accessor :tabs
  1336. # Whether or not this rule is the last rule in a nested group.
  1337. # This is only set in a CSS tree.
  1338. #
  1339. # @return [Boolean]
  1340. attr_accessor :group_end
  1341. # @param rule [Array<String, Sass::Script::Node>]
  1342. # The CSS rule. See \{#rule}
  1343. def initialize(rule)
  1344. merged = Sass::Util.merge_adjacent_strings(rule)
  1345. @rule = Sass::Util.strip_string_array(merged)
  1346. @tabs = 0
  1347. try_to_parse_non_interpolated_rules
  1348. super()
  1349. end

Large files files are truncated, but you can click here to view the full file