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.



526
527
528
# File 'lib/dommy/element.rb', line 526

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



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

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



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

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

#[]=(name, value) ⇒ Object



563
564
565
# File 'lib/dommy/element.rb', line 563

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

#__js_call__(method, args) ⇒ Object



614
615
616
617
618
619
620
621
622
623
624
625
626
627
# File 'lib/dommy/element.rb', line 614

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



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

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



603
604
605
606
607
608
609
610
611
612
# File 'lib/dommy/element.rb', line 603

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.



532
533
534
# File 'lib/dommy/element.rb', line 532

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

#css_text=(value) ⇒ Object



536
537
538
539
540
541
542
543
544
545
546
# File 'lib/dommy/element.rb', line 536

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



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

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

#lengthObject



548
549
550
# File 'lib/dommy/element.rb', line 548

def length
  properties.size
end

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

Returns:

  • (Boolean)


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

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