Class: Dommy::StyleDeclaration

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

Instance Method Summary collapse

Constructor Details

#initialize(element) ⇒ StyleDeclaration

Returns a new instance of StyleDeclaration.



544
545
546
# File 'lib/dommy/element.rb', line 544

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



591
592
593
594
595
596
597
598
599
600
# File 'lib/dommy/element.rb', line 591

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



573
574
575
576
577
578
579
# File 'lib/dommy/element.rb', line 573

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

#[]=(name, value) ⇒ Object



581
582
583
# File 'lib/dommy/element.rb', line 581

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

#__js_call__(method, args) ⇒ Object



632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/dommy/element.rb', line 632

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



606
607
608
609
610
611
612
613
614
615
616
617
618
619
# File 'lib/dommy/element.rb', line 606

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



621
622
623
624
625
626
627
628
629
630
# File 'lib/dommy/element.rb', line 621

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.



550
551
552
# File 'lib/dommy/element.rb', line 550

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

#css_text=(value) ⇒ Object



554
555
556
557
558
559
560
561
562
563
564
# File 'lib/dommy/element.rb', line 554

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



585
586
587
# File 'lib/dommy/element.rb', line 585

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

#lengthObject



566
567
568
# File 'lib/dommy/element.rb', line 566

def length
  properties.size
end

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

Returns:

  • (Boolean)


602
603
604
# File 'lib/dommy/element.rb', line 602

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