Class: Sass::Selector::Element

Inherits:
Simple
  • Object
show all
Defined in:
/home/nweiz/code/sass-site/.sass/lib/sass/selector.rb

Overview

An element selector (e.g. h1).

Instance Attribute Summary (collapse)

Attributes inherited from Simple

#filename, #line

Instance Method Summary (collapse)

Methods inherited from Simple

#eql?, #equality_key, #hash, #inspect, #unify_namespaces, #unique?

Constructor Details

- (Element) initialize(name, namespace)

Returns a new instance of Element

Parameters:

  • name (String)

    The element name

  • namespace (String, nil)


220
221
222
223
# File '/home/nweiz/code/sass-site/.sass/lib/sass/selector.rb', line 220

def initialize(name, namespace)
  @name = name
  @namespace = namespace
end

Instance Attribute Details

- (String) name (readonly)

The element name.

Returns:

  • (String)


210
211
212
# File '/home/nweiz/code/sass-site/.sass/lib/sass/selector.rb', line 210

def name
  @name
end

- (String?) namespace (readonly)

The selector namespace. nil means the default namespace, "" means no namespace, "*" means any namespace.

Returns:

  • (String, nil)


216
217
218
# File '/home/nweiz/code/sass-site/.sass/lib/sass/selector.rb', line 216

def namespace
  @namespace
end

Instance Method Details

- specificity



265
266
267
# File '/home/nweiz/code/sass-site/.sass/lib/sass/selector.rb', line 265

def specificity
  1
end

- to_s(opts = {})

See Also:

  • Selector#to_s


226
227
228
# File '/home/nweiz/code/sass-site/.sass/lib/sass/selector.rb', line 226

def to_s(opts = {})
  @namespace ? "#{@namespace}|#{@name}" : @name
end

- unify(sels)

TODO:

There are lots of cases that this documentation specifies; make sure we thoroughly test all of them.

TODO:

Keep track of whether a default namespace has been declared and handle namespace-unspecified selectors accordingly.

Unification of an element selector is somewhat complicated, especially when a namespace is specified. First, if sel contains another Sass::Selector::Element with a different #name, then the selectors can't be unified and nil is returned.

Otherwise, if sel doesn't specify a namespace, or it specifies any namespace (via "*"), then it's returned with this element selector (e.g. .foo becomes a.foo or svg|a.foo). Similarly, if this selector doesn't specify a namespace, the namespace from sel is used.

If both this selector and sel specify namespaces, those namespaces are unified via Simple#unify_namespaces and the unified namespace is used, if possible.

See Also:

  • Selector#unify


252
253
254
255
256
257
258
259
260
261
262
# File '/home/nweiz/code/sass-site/.sass/lib/sass/selector.rb', line 252

def unify(sels)
  case sels.first
  when Universal;
  when Element; return unless name == sels.first.name
  else return [self] + sels
  end

  ns, accept = unify_namespaces(namespace, sels.first.namespace)
  return unless accept
  [Element.new(name, ns)] + sels[1..-1]
end