Class: Dommy::Internal::CSS::ComputedStyleDeclaration

Inherits:
Object
  • Object
show all
Includes:
Bridge::Methods
Defined in:
lib/dommy/internal/css/computed_style_declaration.rb

Overview

The read-only CSSStyleDeclaration that getComputedStyle returns. CSSOM's "live object" behavior is approximated by recomputing lazily: every read goes through Cascade.computed_style, whose per-generation memo makes repeated reads cheap while DOM mutations show up on the next read. ::before/::after expose cascaded computed declarations even though Dommy renders no generated boxes.

Constant Summary collapse

EMPTY =
{}.freeze
KNOWN_PSEUDO_ELEMENTS =

Pseudo-elements getComputedStyle exposes a cascaded computed declaration for (no box generation, just the cascade). Unknown pseudo-elements yield an empty declaration, as browsers do.

%w[
  before after first-line first-letter marker selection placeholder backdrop
].freeze

Instance Method Summary collapse

Methods included from Bridge::Methods

included

Constructor Details

#initialize(element, pseudo_element: nil) ⇒ ComputedStyleDeclaration

Returns a new instance of ComputedStyleDeclaration.



22
23
24
25
# File 'lib/dommy/internal/css/computed_style_declaration.rb', line 22

def initialize(element, pseudo_element: nil)
  @element = element
  @pseudo_element = pseudo_element
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Ruby-side property readers, e.g. computed.background_color or computed.backgroundColor.



97
98
99
100
101
102
# File 'lib/dommy/internal/css/computed_style_declaration.rb', line 97

def method_missing(name, *args)
  raise_read_only if name.to_s.end_with?("=")
  return super unless args.empty?

  styles.fetch(camel_to_kebab(name.to_s)) { return super }.to_s
end

Instance Method Details

#[](name) ⇒ Object



34
35
36
# File 'lib/dommy/internal/css/computed_style_declaration.rb', line 34

def [](name)
  get_property_value(name)
end

#__js_call__(method, args) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dommy/internal/css/computed_style_declaration.rb', line 82

def __js_call__(method, args)
  case method
  when "getPropertyValue"
    get_property_value(args[0])
  when "getPropertyPriority"
    ""
  when "item"
    item(args[0].to_i)
  when "setProperty", "removeProperty"
    raise_read_only
  end
end

#__js_get__(key) ⇒ Object

--- JS bridge ---



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dommy/internal/css/computed_style_declaration.rb', line 64

def __js_get__(key)
  case key
  when "cssText" then css_text
  when "length" then length
  when "parentRule" then nil
  else
    # An unset/unknown property reads as "" per CSSStyleDeclaration
    # (a detached element's whole declaration is empty), not undefined.
    styles[camel_to_kebab(key)].to_s
  end
end

#__js_set__(_key, _value) ⇒ Object



76
77
78
# File 'lib/dommy/internal/css/computed_style_declaration.rb', line 76

def __js_set__(_key, _value)
  Bridge::UNHANDLED
end

#css_textObject



46
47
48
# File 'lib/dommy/internal/css/computed_style_declaration.rb', line 46

def css_text
  styles.map { |name, value| "#{name}: #{value};" }.join(" ")
end

#get_property_value(name) ⇒ Object



27
28
29
30
31
32
# File 'lib/dommy/internal/css/computed_style_declaration.rb', line 27

def get_property_value(name)
  key = name.to_s
  # Custom properties are case-sensitive; everything else isn't.
  key = key.downcase unless key.start_with?("--")
  styles[key].to_s
end

#item(index) ⇒ Object



42
43
44
# File 'lib/dommy/internal/css/computed_style_declaration.rb', line 42

def item(index)
  styles.keys[index].to_s
end

#lengthObject



38
39
40
# File 'lib/dommy/internal/css/computed_style_declaration.rb', line 38

def length
  styles.size
end

#remove_propertyObject



58
59
60
# File 'lib/dommy/internal/css/computed_style_declaration.rb', line 58

def remove_property(*)
  raise_read_only
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/dommy/internal/css/computed_style_declaration.rb', line 104

def respond_to_missing?(name, include_private = false)
  styles.key?(camel_to_kebab(name.to_s)) || super
end

#set_propertyObject



54
55
56
# File 'lib/dommy/internal/css/computed_style_declaration.rb', line 54

def set_property(*, **)
  raise_read_only
end

#to_hObject



50
51
52
# File 'lib/dommy/internal/css/computed_style_declaration.rb', line 50

def to_h
  styles.dup
end