Class: Sass::Tree::PropNode

Inherits:
Node show all
Defined in:
lib/sass/tree/prop_node.rb,
lib/sass/css.rb

Overview

A static node reprenting a CSS property.

See Also:

Instance Attribute Summary collapse

Attributes inherited from Node

#children, #filename, #line, #options

Instance Method Summary collapse

Methods inherited from Node

#<<, #balance, #interpolate, #invisible?, #last, #perform, #perform_children, #render, #style, #to_s

Constructor Details

#initialize(name, value, prop_syntax) ⇒ PropNode

Returns a new instance of PropNode.

Parameters:

  • name (String)

    See #name

  • value (String)

    See #value

  • prop_syntax (Symbol)

    :new if this property uses a: b-style syntax, :old if it uses :a b-style syntax



33
34
35
36
37
38
39
# File 'lib/sass/tree/prop_node.rb', line 33

def initialize(name, value, prop_syntax)
  @name = name
  @value = value
  @indentation = 0
  @prop_syntax = prop_syntax
  super()
end

Instance Attribute Details

#indentationFixnum

How deep this property is indented relative to a normal property. This is only greater than 0 in the case that:

  • This node is in a static tree
  • The style is :nested
  • This is a child property of another property
  • The parent property has a value, and thus will be rendered

Returns:

  • (Fixnum)


27
28
29
# File 'lib/sass/tree/prop_node.rb', line 27

def indentation
  @indentation
end

#nameString

The name of the property.

Returns:

  • (String)


9
10
11
# File 'lib/sass/tree/prop_node.rb', line 9

def name
  @name
end

#valueString, Script::Node

The value of the property, either a plain string or a SassScript parse tree.

Returns:



15
16
17
# File 'lib/sass/tree/prop_node.rb', line 15

def value
  @value
end

Instance Method Details

#==(other) ⇒ Boolean

Compares the names and values of two properties.

Parameters:

  • other (Object)

    The object to compare with

Returns:

  • (Boolean)

    Whether or not this node and the other object are the same



46
47
48
# File 'lib/sass/tree/prop_node.rb', line 46

def ==(other)
  self.class == other.class && name == other.name && value == other.value && super
end

#_perform(environment) ⇒ Object (protected)

Returns this node's fully-resolved child properties, and/or this node.

Parameters:

  • environment (Sass::Environment)

    The lexical environment containing variable and mixin values



76
77
78
79
80
81
# File 'lib/sass/tree/prop_node.rb', line 76

def _perform(environment)
  node = super
  result = node.children.dup
  result.unshift(node) if !node.value.empty? || node.children.empty?
  result
end

#_to_s(tabs) ⇒ String (protected)

Computes the CSS for the property.

Parameters:

  • tabs (Fixnum)

    The level of indentation for the CSS

Returns:

  • (String)

    The resulting CSS

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sass/tree/prop_node.rb', line 57

def _to_s(tabs)
  if @options[:property_syntax] == :old && @prop_syntax == :new
    raise Sass::SyntaxError.new("Illegal property syntax: can't use new syntax when :property_syntax => :old is set.")
  elsif @options[:property_syntax] == :new && @prop_syntax == :old
    raise Sass::SyntaxError.new("Illegal property syntax: can't use old syntax when :property_syntax => :new is set.")
  elsif value[-1] == ?;
    raise Sass::SyntaxError.new("Invalid property: #{declaration.dump} (no \";\" required at end-of-line).")
  elsif value.empty?
    raise Sass::SyntaxError.new("Invalid property: #{declaration.dump} (no value).")
  end

  to_return = '  ' * (tabs - 1 + indentation) + name + ":" +
    (style == :compressed ? '' : ' ') + value + (style == :compressed ? "" : ";")
end

#invalid_child?(child) ⇒ String (protected)

Returns an error message if the given child node is invalid, and false otherwise.

Sass::Tree::PropNode only allows other Sass::Tree::PropNodes and CommentNodes as children.

Parameters:

Returns:

  • (String)

    An error message if the child is invalid, or nil otherwise



106
107
108
109
110
# File 'lib/sass/tree/prop_node.rb', line 106

def invalid_child?(child)
  if !child.is_a?(PropNode) && !child.is_a?(CommentNode)
    "Illegal nesting: Only properties may be nested beneath properties."
  end
end

#perform!(environment) ⇒ Object (protected)

Runs any SassScript that may be embedded in the property, and invludes the parent property, if any.

Parameters:

  • environment (Sass::Environment)

    The lexical environment containing variable and mixin values



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sass/tree/prop_node.rb', line 88

def perform!(environment)
  @name = interpolate(@name, environment)
  @value = @value.is_a?(String) ? interpolate(@value, environment) : @value.perform(environment).to_s
  super
  # Once we've called super, the child nodes have been dup'ed
  # so we can destructively modify them
  children.select {|c| c.is_a?(PropNode)}.each do |c|
    c.name = "#{name}-#{c.name}"
    c.indentation += 1 if style == :nested && [email protected]?
  end
end

#to_sass(tabs, opts = {}) ⇒ Object

See Also:



39
40
41
# File 'lib/sass/css.rb', line 39

def to_sass(tabs, opts = {})
  "#{'  ' * tabs}#{opts[:old] ? ':' : ''}#{name}#{opts[:old] ? '' : ':'} #{value}\n"
end