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.



909
910
911
# File 'lib/dommy/element.rb', line 909

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`.



956
957
958
959
960
961
962
963
964
965
# File 'lib/dommy/element.rb', line 956

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` returns the property name at that index (matches `style.item(i)` in real DOM). String key form (`style`) is a convenience shortcut for `getPropertyValue`.



938
939
940
941
942
943
944
# File 'lib/dommy/element.rb', line 938

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

#[]=(name, value) ⇒ Object



946
947
948
# File 'lib/dommy/element.rb', line 946

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

#__js_call__(method, args) ⇒ Object



999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
# File 'lib/dommy/element.rb', line 999

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



971
972
973
974
975
976
977
978
979
980
981
982
983
984
# File 'lib/dommy/element.rb', line 971

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
      properties[method_to_css_name(key)]
    end
  end
end

#__js_set__(key, value) ⇒ Object



986
987
988
989
990
991
992
993
994
995
# File 'lib/dommy/element.rb', line 986

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 round-trips the full ‘style` attribute. Setter parses semicolon-separated entries.



915
916
917
# File 'lib/dommy/element.rb', line 915

def css_text
  properties.map { |k, v| "#{k}:#{v}" }.join(";")
end

#css_text=(value) ⇒ Object



919
920
921
922
923
924
925
926
927
928
929
# File 'lib/dommy/element.rb', line 919

def css_text=(value)
  props = {}
  value.to_s.split(";").each do |entry|
    key, val = entry.split(":", 2)
    next unless key && val

    props[key.strip] = val.strip
  end

  write_properties(props)
end

#each(&blk) ⇒ Object



950
951
952
# File 'lib/dommy/element.rb', line 950

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

#lengthObject



931
932
933
# File 'lib/dommy/element.rb', line 931

def length
  properties.size
end

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

Returns:

  • (Boolean)


967
968
969
# File 'lib/dommy/element.rb', line 967

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