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

http://github.com/scalate/scalate · Ruby · 67 lines · 39 code · 8 blank · 20 comment · 12 complexity · e6c0d823bbd37dc76bccfebde9d00ed4 MD5 · raw file

  1. require 'sass/script/literal'
  2. module Sass::Script
  3. # A SassScript object representing a CSS string *or* a CSS identifier.
  4. class String < Literal
  5. # The Ruby value of the string.
  6. #
  7. # @return [String]
  8. attr_reader :value
  9. # Whether this is a CSS string or a CSS identifier.
  10. # The difference is that strings are written with double-quotes,
  11. # while identifiers aren't.
  12. #
  13. # @return [Symbol] `:string` or `:identifier`
  14. attr_reader :type
  15. # In addition to setting the \{#context} of the string,
  16. # this sets the string to be an identifier if the context is `:equals`.
  17. #
  18. # @see Node#context=
  19. def context=(context)
  20. super
  21. @type = :identifier if context == :equals
  22. end
  23. # Creates a new string.
  24. #
  25. # @param value [String] See \{#value}
  26. # @param type [Symbol] See \{#type}
  27. def initialize(value, type = :identifier)
  28. super(value)
  29. @type = type
  30. end
  31. # @see Literal#plus
  32. def plus(other)
  33. other_str = other.is_a?(Sass::Script::String) ? other.value : other.to_s
  34. Sass::Script::String.new(self.value + other_str, self.type)
  35. end
  36. # @see Node#to_s
  37. def to_s(opts = {})
  38. if self.type == :identifier
  39. return %q{""} if context == :equals && self.value.size == 0
  40. return self.value.gsub("\n", " ")
  41. end
  42. return "\"#{value.gsub('"', "\\\"")}\"" if opts[:quote] == %q{"}
  43. return "'#{value.gsub("'", "\\'")}'" if opts[:quote] == %q{'}
  44. return "\"#{value}\"" unless value.include?('"')
  45. return "'#{value}'" unless value.include?("'")
  46. "\"#{value.gsub('"', "\\\"")}\"" #'
  47. end
  48. # @see Node#to_sass
  49. def to_sass(opts = {})
  50. if self.type == :identifier && context == :equals &&
  51. self.value !~ Sass::SCSS::RX::URI &&
  52. Sass::SCSS::RX.escape_ident(self.value).include?(?\\)
  53. return "unquote(#{Sass::Script::String.new(self.value, :string).to_sass})"
  54. else
  55. return to_s
  56. end
  57. end
  58. end
  59. end