Class: Sass::Tree::CommentNode

Inherits:
Node
  • Object
show all
Defined in:
lib/sass/tree/comment_node.rb

Overview

A static node representing a Sass comment (silent or loud).

See Also:

Constant Summary

Constants inherited from Node

Node::SAVED_OPTIONS

Instance Attribute Summary collapse

Attributes inherited from Node

#children, #filename, #has_children, #line, #options

Instance Method Summary collapse

Methods inherited from Node

#<<, #_around_dump, #_cssize, #balance, #check_child!, #children_to_src, #cssize, #cssize!, #dasherize, #do_extend, #each, #invalid_child?, #perform, #perform!, #perform_children, #run_interp, #selector_to_sass, #selector_to_scss, #selector_to_src, #semi, #style, #to_s, #to_src

Constructor Details

#initialize(value, silent) ⇒ CommentNode

Returns a new instance of CommentNode.

Parameters:



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

def initialize(value, silent)
  @lines = []
  @value = normalize_indentation value
  @silent = silent
  super()
end

Instance Attribute Details

#silentBoolean

Whether or not the comment is silent (that is, doesn't output to CSS).

Returns:

  • (Boolean)


16
17
18
# File 'lib/sass/tree/comment_node.rb', line 16

def silent
  @silent
end

#valueString

The text of the comment, not including /* and */.

Returns:

  • (String)


11
12
13
# File 'lib/sass/tree/comment_node.rb', line 11

def value
  @value
end

Instance Method Details

#==(other) ⇒ Boolean

Compares the contents of two comments.

Parameters:

  • other (Object)

    The object to compare with

Returns:

  • (Boolean)

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



32
33
34
# File 'lib/sass/tree/comment_node.rb', line 32

def ==(other)
  self.class == other.class && value == other.value && silent == other.silent
end

#_perform(environment) ⇒ Tree::Node+ (protected)

Removes this node from the tree if it's a silent comment.

Parameters:

  • environment (Sass::Environment)

    The lexical environment containing variable and mixin values

Returns:

See Also:



111
112
113
114
# File 'lib/sass/tree/comment_node.rb', line 111

def _perform(environment)
  return [] if @silent
  self
end

#to_s(tabs = 0) ⇒ String? (protected)

Computes the CSS for the comment.

Returns nil if this is a silent comment or the current style doesn't render comments.

Parameters:

  • tabs (Fixnum) (defaults to: 0)

    The level of indentation for the CSS

Returns:

  • (String, nil)

    The resulting CSS

See Also:



96
97
98
99
100
101
102
103
# File 'lib/sass/tree/comment_node.rb', line 96

def _to_s(tabs = 0, _ = nil)
  return if invisible?
  spaces = ('  ' * [tabs - 1 - value[/^ */].size, 0].max)

  content = value.gsub(/^/, spaces)
  content.gsub!(/\n +(\* *(?!\/))?/, ' ') if style == :compact
  content
end

#invisible?Boolean

Returns true if this is a silent comment or the current style doesn't render comments.

Returns:

  • (Boolean)


40
41
42
# File 'lib/sass/tree/comment_node.rb', line 40

def invisible?
  style == :compressed || @silent
end

#to_sass(tabs, opts = {})

See Also:



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

def to_sass(tabs, opts = {})
  content = value.gsub(/\*\/$/, '').rstrip
  if content =~ /\A[ \t]/
    # Re-indent SCSS comments like this:
    #     /* foo
    #   bar
    #       baz */
    content.gsub!(/^/, '   ')
    content.sub!(/\A([ \t]*)\/\*/, '/*\1')
  end

  content =
    unless content.include?("\n")
      content
    else
      content.gsub!(/\n( \*|\/\/)/, "\n  ")
      spaces = content.scan(/\n( *)/).map {|s| s.first.size}.min
      sep = silent ? "\n//" : "\n *"
      if spaces >= 2
        content.gsub(/\n  /, sep)
      else
        content.gsub(/\n#{' ' * spaces}/, sep)
      end
    end

  content.gsub!(/\A\/\*/, '//') if silent
  content.gsub!(/^/, '  ' * tabs)
  content.rstrip + "\n"
end

#to_scss(tabs, opts = {})

See Also:



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

def to_scss(tabs, opts = {})
  spaces = ('  ' * [tabs - value[/^ */].size, 0].max)
  if silent
    value.gsub(/^[\/ ]\*/, '//').gsub(/ *\*\/$/, '')
  else
    value
  end.gsub(/^/, spaces) + "\n"
end