utils.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. module ExtJS
  2. module SassExtensions
  3. module Functions
  4. module Utils
  5. @maps = Array.new()
  6. class << self; attr_accessor :maps; end
  7. def parsebox(list, n)
  8. assert_type n, :Number
  9. if !n.int?
  10. raise ArgumentError.new("List index #{n} must be an integer")
  11. elsif n.to_i < 1
  12. raise ArgumentError.new("List index #{n} must be greater than or equal to 1")
  13. elsif n.to_i > 4
  14. raise ArgumentError.new("A box string can't contain more then 4")
  15. end
  16. new_list = list.clone.to_a
  17. size = new_list.size
  18. if n.to_i >= size
  19. if size == 1
  20. new_list[1] = new_list[0]
  21. new_list[2] = new_list[0]
  22. new_list[3] = new_list[0]
  23. elsif size == 2
  24. new_list[2] = new_list[0]
  25. new_list[3] = new_list[1]
  26. elsif size == 3
  27. new_list[3] = new_list[1]
  28. end
  29. end
  30. new_list.to_a[n.to_i - 1]
  31. end
  32. def parseint(value)
  33. Sass::Script::Number.new(value.to_i)
  34. end
  35. def ERROR(message)
  36. raise ArgumentError.new(message)
  37. end
  38. def map_create()
  39. map = Hash.new()
  40. id = Utils.maps.length;
  41. Utils.maps.insert(id, map);
  42. Sass::Script::Number.new(id+1)
  43. end
  44. def map_get(mapId, key)
  45. id = mapId.to_i()-1
  46. map = Utils.maps[id]
  47. k = key.to_s()
  48. v = map[k]
  49. if !v
  50. v = Sass::Script::String.new("")
  51. end
  52. v
  53. end
  54. def map_put(mapId, key, value)
  55. id = mapId.to_i()-1
  56. map = Utils.maps[id]
  57. k = key.to_s()
  58. map[k] = value
  59. end
  60. # Joins 2 file paths using the path separator
  61. def file_join(path1, path2)
  62. path1 = path1.value
  63. path2 = path2.value
  64. path = path1.empty? ? path2 : File.join(path1, path2)
  65. Sass::Script::String.new(path)
  66. end
  67. def theme_image_exists(directory, path)
  68. result = false
  69. where_to_look = File.join(directory.value, path.value)
  70. if where_to_look && FileTest.exists?("#{where_to_look}")
  71. result = true
  72. end
  73. return Sass::Script::Bool.new(result)
  74. end
  75. # workaround for lack of @error directive in sass 3.1
  76. def error(message)
  77. raise Sass::SyntaxError, message.value
  78. end
  79. # This function is primarily to support compatibility when moving from sass 3.1 to 3.2
  80. # because of the change in behavior of the null keyword when used with !default.
  81. # in 3.1 variables defaulted to null are considered to have an assigned value
  82. # and thus cannot be reassigned. In 3.2 defaulting to null is the same as leaving
  83. # the variable undeclared
  84. def is_null(value)
  85. n = false
  86. begin
  87. # in Sass 3.2 null values are an instance of Sass::Script::Null
  88. # this throws an exception in Sass 3.1 because the Null class doesn't exist
  89. n = (value.is_a? Sass::Script::Null) || (value.is_a? Sass::Script::String) && value.value == 'null' || value.value == 'none'
  90. rescue NameError=>e
  91. # Sass 3.1 processes null values as a string == "null"
  92. n = (value.is_a? Sass::Script::String) && value.value == 'null' || value.value == 'none'
  93. end
  94. return Sass::Script::Bool.new(n)
  95. end
  96. end
  97. end
  98. end
  99. end
  100. module Sass::Script::Functions
  101. include ExtJS::SassExtensions::Functions::Utils
  102. end