1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
require 'set'
module Sass
# The abstract base class for lexical environments for SassScript.
class BaseEnvironment
class << self
# Note: when updating this,
# update sass/yard/inherited_hash.rb as well.
def inherited_hash_accessor(name)
inherited_hash_reader(name)
inherited_hash_writer(name)
end
def inherited_hash_reader(name)
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{name}(name)
_#{name}(name.tr('_', '-'))
end
def _#{name}(name)
(@#{name}s && @#{name}s[name]) || @parent && @parent._#{name}(name)
end
protected :_#{name}
def is_#{name}_global?(name)
return !@parent if @#{name}s && @#{name}s.has_key?(name)
@parent && @parent.is_#{name}_global?(name)
end
RUBY
end
def inherited_hash_writer(name)
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def set_#{name}(name, value)
name = name.tr('_', '-')
@#{name}s[name] = value unless try_set_#{name}(name, value)
end
def try_set_#{name}(name, value)
@#{name}s ||= {}
if @#{name}s.include?(name)
@#{name}s[name] = value
true
elsif @parent && !@parent.global?
@parent.try_set_#{name}(name, value)
else
false
end
end
protected :try_set_#{name}
def set_local_#{name}(name, value)
@#{name}s ||= {}
@#{name}s[name.tr('_', '-')] = value
end
def set_global_#{name}(name, value)
global_env.set_#{name}(name, value)
end
RUBY
end
end
# The options passed to the Sass Engine.
attr_reader :options
attr_writer :caller
attr_writer :content
attr_writer :selector
# variable
# Script::Value
inherited_hash_reader :var
# mixin
# Sass::Callable
inherited_hash_reader :mixin
# function
# Sass::Callable
inherited_hash_reader :function
# @param options [{Symbol => Object}] The options hash. See
# {file:SASS_REFERENCE.md#Options the Sass options documentation}.
# @param parent [Environment] See \{#parent}
def initialize(parent = nil, options = nil)
@parent = parent
@options = options || (parent && parent.options) || {}
@stack = @parent.nil? ? Sass::Stack.new : nil
@caller = nil
@content = nil
@filename = nil
@functions = nil
@mixins = nil
@selector = nil
@vars = nil
end
# Returns whether this is the global environment.
#
# @return [Boolean]
def global?
@parent.nil?
end
# The environment of the caller of this environment's mixin or function.
# @return {Environment?}
def caller
@caller || (@parent && @parent.caller)
end
# The content passed to this environment. This is naturally only set
# for mixin body environments with content passed in.
#
# @return {[Array<Sass::Tree::Node>, Environment]?} The content nodes and
# the lexical environment of the content block.
def content
@content || (@parent && @parent.content)
end
# The selector for the current CSS rule, or nil if there is no
# current CSS rule.
#
# @return [Selector::CommaSequence?] The current selector, with any
# nesting fully resolved.
def selector
@selector || (@caller && @caller.selector) || (@parent && @parent.selector)
end
# The top-level Environment object.
#
# @return [Environment]
def global_env
@global_env ||= global? ? self : @parent.global_env
end
# The import/mixin stack.
#
# @return [Sass::Stack]
def stack
@stack || global_env.stack
end
end
# The lexical environment for SassScript.
# This keeps track of variable, mixin, and function definitions.
#
# A new environment is created for each level of Sass nesting.
# This allows variables to be lexically scoped.
# The new environment refers to the environment in the upper scope,
# so it has access to variables defined in enclosing scopes,
# but new variables are defined locally.
#
# Environment also keeps track of the {Engine} options
# so that they can be made available to {Sass::Script::Functions}.
class Environment < BaseEnvironment
# The enclosing environment,
# or nil if this is the global environment.
#
# @return [Environment]
attr_reader :parent
# variable
# Script::Value
inherited_hash_writer :var
# mixin
# Sass::Callable
inherited_hash_writer :mixin
# function
# Sass::Callable
inherited_hash_writer :function
end
# A read-only wrapper for a lexical environment for SassScript.
class ReadOnlyEnvironment < BaseEnvironment
def initialize(parent = nil, options = nil)
super
@content_cached = nil
end
# The read-only environment of the caller of this environment's mixin or function.
#
# @see BaseEnvironment#caller
# @return {ReadOnlyEnvironment}
def caller
return @caller if @caller
env = super
@caller ||= env.is_a?(ReadOnlyEnvironment) ? env : ReadOnlyEnvironment.new(env, env.options)
end
# The content passed to this environment. If the content's environment isn't already
# read-only, it's made read-only.
#
# @see BaseEnvironment#content
#
# @return {[Array<Sass::Tree::Node>, ReadOnlyEnvironment]?} The content nodes and
# the lexical environment of the content block.
# Returns `nil` when there is no content in this environment.
def content
# Return the cached content from a previous invocation if any
return @content if @content_cached
# get the content with a read-write environment from the superclass
read_write_content = super
if read_write_content
tree, env = read_write_content
# make the content's environment read-only
if env && !env.is_a?(ReadOnlyEnvironment)
env = ReadOnlyEnvironment.new(env, env.options)
end
@content_cached = true
@content = [tree, env]
else
@content_cached = true
@content = nil
end
end
end
# An environment that can write to in-scope global variables, but doesn't
# create new variables in the global scope. Useful for top-level control
# directives.
class SemiGlobalEnvironment < Environment
def try_set_var(name, value)
@vars ||= {}
if @vars.include?(name)
@vars[name] = value
true
elsif @parent
@parent.try_set_var(name, value)
else
false
end
end
end
end
|