PageRenderTime 23ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/scalate-jruby/src/main/resources/haml-3.0.25/lib/sass/script/string_interpolation.rb

http://github.com/scalate/scalate
Ruby | 93 lines | 64 code | 8 blank | 21 comment | 14 complexity | 1ff565f2f656c067eb521da6f02482f3 MD5 | raw file
  1. module Sass::Script
  2. # A SassScript object representing `#{}` interpolation within a string.
  3. #
  4. # @see Interpolation
  5. class StringInterpolation < Node
  6. # Interpolation in a string is of the form `"before #{mid} after"`,
  7. # where `before` and `after` may include more interpolation.
  8. #
  9. # @param before [Node] The string before the interpolation
  10. # @param mid [Node] The SassScript within the interpolation
  11. # @param after [Node] The string after the interpolation
  12. def initialize(before, mid, after)
  13. @before = before
  14. @mid = mid
  15. @after = after
  16. end
  17. # @return [String] A human-readable s-expression representation of the interpolation
  18. def inspect
  19. "(string_interpolation #{@before.inspect} #{@mid.inspect} #{@after.inspect})"
  20. end
  21. # @see Node#to_sass
  22. def to_sass(opts = {})
  23. # We can get rid of all of this when we remove the deprecated :equals context
  24. before_unquote, before_quote_char, before_str = parse_str(@before.to_sass(opts))
  25. after_unquote, after_quote_char, after_str = parse_str(@after.to_sass(opts))
  26. unquote = before_unquote || after_unquote ||
  27. (before_quote_char && !after_quote_char && !after_str.empty?) ||
  28. (!before_quote_char && after_quote_char && !before_str.empty?)
  29. quote_char =
  30. if before_quote_char && after_quote_char && before_quote_char != after_quote_char
  31. before_str.gsub!("\\'", "'")
  32. before_str.gsub!('"', "\\\"")
  33. after_str.gsub!("\\'", "'")
  34. after_str.gsub!('"', "\\\"")
  35. '"'
  36. else
  37. before_quote_char || after_quote_char
  38. end
  39. res = ""
  40. res << 'unquote(' if unquote
  41. res << quote_char if quote_char
  42. res << before_str
  43. res << '#{' << @mid.to_sass(opts) << '}'
  44. res << after_str
  45. res << quote_char if quote_char
  46. res << ')' if unquote
  47. res
  48. end
  49. # Returns the three components of the interpolation, `before`, `mid`, and `after`.
  50. #
  51. # @return [Array<Node>]
  52. # @see #initialize
  53. # @see Node#children
  54. def children
  55. [@before, @mid, @after].compact
  56. end
  57. protected
  58. # Evaluates the interpolation.
  59. #
  60. # @param environment [Sass::Environment] The environment in which to evaluate the SassScript
  61. # @return [Sass::Script::String] The SassScript string that is the value of the interpolation
  62. def _perform(environment)
  63. res = ""
  64. before = @before.perform(environment)
  65. res << before.value
  66. mid = @mid.perform(environment)
  67. res << (mid.is_a?(Sass::Script::String) ? mid.value : mid.to_s)
  68. res << @after.perform(environment).value
  69. opts(Sass::Script::String.new(res, before.type))
  70. end
  71. private
  72. def parse_str(str)
  73. case str
  74. when /^unquote\((["'])(.*)\1\)$/
  75. return true, $1, $2
  76. when '""'
  77. return false, nil, ""
  78. when /^(["'])(.*)\1$/
  79. return false, $1, $2
  80. else
  81. return false, nil, str
  82. end
  83. end
  84. end
  85. end