Class: Dommy::StyleDeclaration

Inherits:
Object
  • Object
show all
Includes:
Bridge::Methods, Enumerable
Defined in:
lib/dommy/element.rb

Instance Method Summary collapse

Methods included from Bridge::Methods

included

Constructor Details

#initialize(element) ⇒ StyleDeclaration

Returns a new instance of StyleDeclaration.



1086
1087
1088
# File 'lib/dommy/element.rb', line 1086

def initialize(element)
  @element = element
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

camelCase JS property accessors → kebab-case CSS property name. style.backgroundColor = "red" becomes background-color: red.



1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
# File 'lib/dommy/element.rb', line 1126

def method_missing(name, *args)
  key = method_to_css_name(name)
  if name.to_s.end_with?("=")
    set_property(key, args.first)
  elsif properties.key?(key)
    properties[key]
  else
    ""
  end
end

Instance Method Details

#[](key) ⇒ Object

style[0] returns the property name at that index (matches style.item(i) in real DOM). String key form (style["color"]) is a convenience shortcut for getPropertyValue.



1108
1109
1110
1111
1112
1113
1114
# File 'lib/dommy/element.rb', line 1108

def [](key)
  if key.is_a?(Integer)
    properties.keys[key]
  else
    properties[key.to_s]
  end
end

#[]=(name, value) ⇒ Object



1116
1117
1118
# File 'lib/dommy/element.rb', line 1116

def []=(name, value)
  set_property(name, value)
end

#__js_call__(method, args) ⇒ Object



1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
# File 'lib/dommy/element.rb', line 1172

def __js_call__(method, args)
  case method
  when "setProperty"
    set_property(args[0], args[1])
  when "removeProperty"
    remove_property(args[0])
  when "getPropertyValue"
    properties[args[0].to_s]
  when "item"
    properties.keys[args[0].to_i]
  else
    nil
  end
end

#__js_get__(key) ⇒ Object



1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
# File 'lib/dommy/element.rb', line 1141

def __js_get__(key)
  case key
  when "cssText"
    css_text
  when "length"
    length
  else
    if key.is_a?(Integer) || key.to_s.match?(/\A-?\d+\z/)
      self[key.to_i]
    else
      # An unset CSS property reads as "" (per CSSStyleDeclaration), not nil —
      # `el.style.display` is "" until assigned, which `v-show` and other
      # display-toggling code compares against.
      properties[method_to_css_name(key)] || ""
    end
  end
end

#__js_set__(key, value) ⇒ Object



1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
# File 'lib/dommy/element.rb', line 1159

def __js_set__(key, value)
  case key
  when "cssText"
    self.css_text = value
  else
    set_property(method_to_css_name(key), value)
  end

  nil
end

#css_textObject

CSSStyleDeclaration interface: cssText serializes the parsed declaration block (prop: value; joined by spaces), dropping invalid declarations. The setter reparses and rewrites the style attribute in that form.



1093
1094
1095
# File 'lib/dommy/element.rb', line 1093

def css_text
  serialize_properties(properties)
end

#css_text=(value) ⇒ Object



1097
1098
1099
# File 'lib/dommy/element.rb', line 1097

def css_text=(value)
  write_properties(parse_declarations(value))
end

#each(&blk) ⇒ Object



1120
1121
1122
# File 'lib/dommy/element.rb', line 1120

def each(&blk)
  properties.keys.each(&blk)
end

#lengthObject



1101
1102
1103
# File 'lib/dommy/element.rb', line 1101

def length
  properties.size
end

#respond_to_missing?(_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


1137
1138
1139
# File 'lib/dommy/element.rb', line 1137

def respond_to_missing?(_name, _include_private = false)
  true
end