Class: Dommy::ClassList

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

Overview

(‘LiveChildren` removed — `el.children` now returns a `Dommy::HTMLCollection` initialized with a re-evaluating block.)

Instance Method Summary collapse

Methods included from Bridge::Methods

included

Constructor Details

#initialize(element, attribute = "class") ⇒ ClassList

‘attribute` is the content attribute this token list reflects (“class” for `classList`, “rel” for `relList`, “sandbox”, “sizes”, “for”, …).



609
610
611
612
# File 'lib/dommy/element.rb', line 609

def initialize(element, attribute = "class")
  @element = element
  @attribute = attribute
end

Instance Method Details

#[](index) ⇒ Object



670
671
672
# File 'lib/dommy/element.rb', line 670

def [](index)
  item(index)
end

#__js_call__(method, args) ⇒ Object



719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
# File 'lib/dommy/element.rb', line 719

def __js_call__(method, args)
  case method
  when "add"
    update_tokens { |tokens| tokens | normalize_tokens(args) }
    Bridge::UNDEFINED
  when "remove"
    update_tokens { |tokens| tokens - normalize_tokens(args) }
    Bridge::UNDEFINED
  when "contains"
    # contains() does not validate; null coerces to the string "null".
    class_tokens.include?(stringify_token(args[0]))
  when "toggle"
    toggle(args[0], args[1])
  when "replace"
    replace(args[0], args[1])
  when "item"
    item(args[0])
  when "toString"
    value
  else
    nil
  end
end

#__js_get__(key) ⇒ Object



686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
# File 'lib/dommy/element.rb', line 686

def __js_get__(key)
  case key
  when "length"
    length
  when "value"
    value
  else
    # Indexed getter: `classList[i]` is an undefined-returning indexed
    # property — out-of-range or negative indices yield JS `undefined`
    # (unlike `item(i)`, which returns null). Returning Ruby nil here would
    # marshal as JS null, so use the UNDEFINED sentinel.
    if key.is_a?(Integer) || key.to_s.match?(/\A-?\d+\z/)
      i = key.to_i
      token = i.negative? ? nil : class_tokens[i]
      token.nil? ? Bridge::UNDEFINED : token
    end
  end
end

#__js_set__(key, val) ⇒ Object



705
706
707
708
709
710
711
712
# File 'lib/dommy/element.rb', line 705

def __js_set__(key, val)
  case key
  when "value"
    self.value = val
  end

  nil
end

#add(*tokens) ⇒ Object



640
641
642
643
# File 'lib/dommy/element.rb', line 640

def add(*tokens)
  update_tokens { |existing| existing | normalize_tokens(tokens) }
  nil
end

#contains?(token) ⇒ Boolean

Spec: contains() does NOT validate (no SyntaxError on empty).

Returns:

  • (Boolean)


636
637
638
# File 'lib/dommy/element.rb', line 636

def contains?(token)
  class_tokens.include?(token.to_s)
end

#each(&blk) ⇒ Object



674
675
676
# File 'lib/dommy/element.rb', line 674

def each(&blk)
  class_tokens.each(&blk)
end

#item(index) ⇒ Object



620
621
622
623
624
625
# File 'lib/dommy/element.rb', line 620

def item(index)
  i = index.to_i
  return nil if i.negative?

  class_tokens[i]
end

#lengthObject Also known as: size



614
615
616
# File 'lib/dommy/element.rb', line 614

def length
  class_tokens.length
end

#remove(*tokens) ⇒ Object



645
646
647
648
# File 'lib/dommy/element.rb', line 645

def remove(*tokens)
  update_tokens { |existing| existing - normalize_tokens(tokens) }
  nil
end

#replace(old_token, new_token) ⇒ Object



650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/dommy/element.rb', line 650

def replace(old_token, new_token)
  # Spec order: both tokens' empty checks (SyntaxError) precede both
  # whitespace checks (InvalidCharacterError) — so replace(" ", "") is a
  # SyntaxError (the empty newToken), not an InvalidCharacterError.
  old_s = stringify_token(old_token)
  new_s = stringify_token(new_token)
  raise DOMException::SyntaxError, "token is empty" if old_s.empty? || new_s.empty?
  if old_s.match?(/[ \t\n\f\r]/) || new_s.match?(/[ \t\n\f\r]/)
    raise DOMException::InvalidCharacterError, "token contains whitespace"
  end

  tokens = class_tokens
  idx = tokens.index(old_s)
  return false unless idx

  tokens[idx] = new_s
  @element.set_attribute(@attribute, tokens.uniq.join(" "))
  true
end

#to_aObject



678
679
680
# File 'lib/dommy/element.rb', line 678

def to_a
  class_tokens.dup
end

#to_sObject



682
683
684
# File 'lib/dommy/element.rb', line 682

def to_s
  value
end

#valueObject



627
628
629
# File 'lib/dommy/element.rb', line 627

def value
  @element.__dommy_backend_node__[@attribute].to_s
end

#value=(new_value) ⇒ Object



631
632
633
# File 'lib/dommy/element.rb', line 631

def value=(new_value)
  @element.set_attribute(@attribute, new_value.to_s)
end