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

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

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



21
22
23
24
25
26
# File 'lib/sass/tree/prop_node.rb', line 21

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

Instance Attribute Details

#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



33
34
35
# File 'lib/sass/tree/prop_node.rb', line 33

def ==(other)
  self.class == other.class && name == other.name && value == other.value && super
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



97
98
99
100
101
# File 'lib/sass/tree/prop_node.rb', line 97

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.

Parameters:

  • environment (Sass::Environment)

    The lexical environment containing variable and mixin values



85
86
87
88
89
# File 'lib/sass/tree/prop_node.rb', line 85

def perform!(environment)
  @name = interpolate(@name, environment)
  @value = @value.is_a?(String) ? interpolate(@value, environment) : @value.perform(environment).to_s
  super
end

#to_s(tabs, parent_name = nil) ⇒ String

Computes the CSS for the property.

Parameters:

  • tabs (Fixnum)

    The level of indentation for the CSS

  • parent_name (String) (defaults to: nil)

    The name of the parent property (e.g. text) or nil

Returns:

  • (String)

    The resulting CSS

Raises:



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
# File 'lib/sass/tree/prop_node.rb', line 43

def to_s(tabs, parent_name = nil)
  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.")
  end

  if value[-1] == ?;
    raise Sass::SyntaxError.new("Invalid property: #{declaration.dump} (no \";\" required at end-of-line).", @line)
  end
  real_name = name
  real_name = "#{parent_name}-#{real_name}" if parent_name
  
  if value.empty? && children.empty?
    raise Sass::SyntaxError.new("Invalid property: #{declaration.dump} (no value).", @line)
  end
  
  join_string = case style
                when :compact; ' '
                when :compressed; ''
                else "\n"
                end
  spaces = '  ' * (tabs - 1)
  to_return = ''
  if !value.empty?
    to_return << "#{spaces}#{real_name}:#{style == :compressed ? '' : ' '}#{value};#{join_string}"
  end
  
  children.each do |kid|
    next if kid.invisible?
    to_return << kid.to_s(tabs, real_name) << join_string
  end
  
  (style == :compressed && parent_name) ? to_return : to_return[0...-1]
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