Class: Dommy::ClassList

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

Overview

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

Constant Summary collapse

JS_METHOD_NAMES =

Methods routed through js_call (keep in sync with its when-arms).

%w[add remove contains toggle replace item].freeze

Instance Method Summary collapse

Constructor Details

#initialize(element) ⇒ ClassList

Returns a new instance of ClassList.



420
421
422
# File 'lib/dommy/element.rb', line 420

def initialize(element)
  @element = element
end

Instance Method Details

#[](index) ⇒ Object



469
470
471
# File 'lib/dommy/element.rb', line 469

def [](index)
  item(index)
end

#__js_call__(method, args) ⇒ Object



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/dommy/element.rb', line 513

def __js_call__(method, args)
  case method
  when "add"
    update_tokens { |tokens| tokens | normalize_tokens(args) }
    nil
  when "remove"
    update_tokens { |tokens| tokens - normalize_tokens(args) }
    nil
  when "contains"
    class_tokens.include?(args[0].to_s)
  when "toggle"
    toggle(args[0], args[1])
  when "replace"
    replace(args[0], args[1])
  when "item"
    item(args[0])
  else
    nil
  end
end

#__js_get__(key) ⇒ Object



485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/dommy/element.rb', line 485

def __js_get__(key)
  case key
  when "length"
    length
  when "value"
    value
  else
    if key.is_a?(Integer) || key.to_s.match?(/\A\d+\z/)
      item(key.to_i)
    end
  end
end

#__js_method_names__Object



509
510
511
# File 'lib/dommy/element.rb', line 509

def __js_method_names__
  JS_METHOD_NAMES
end

#__js_set__(key, val) ⇒ Object



498
499
500
501
502
503
504
505
# File 'lib/dommy/element.rb', line 498

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

  nil
end

#add(*tokens) ⇒ Object



447
448
449
450
# File 'lib/dommy/element.rb', line 447

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)


443
444
445
# File 'lib/dommy/element.rb', line 443

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

#each(&blk) ⇒ Object



473
474
475
# File 'lib/dommy/element.rb', line 473

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

#item(index) ⇒ Object



430
431
432
# File 'lib/dommy/element.rb', line 430

def item(index)
  class_tokens[index.to_i]
end

#lengthObject Also known as: size



424
425
426
# File 'lib/dommy/element.rb', line 424

def length
  class_tokens.length
end

#remove(*tokens) ⇒ Object



452
453
454
455
# File 'lib/dommy/element.rb', line 452

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

#replace(old_token, new_token) ⇒ Object



457
458
459
460
461
462
463
464
465
466
467
# File 'lib/dommy/element.rb', line 457

def replace(old_token, new_token)
  old_s = validate_token(old_token)
  new_s = validate_token(new_token)
  tokens = class_tokens
  idx = tokens.index(old_s)
  return false unless idx

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

#to_aObject



477
478
479
# File 'lib/dommy/element.rb', line 477

def to_a
  class_tokens.dup
end

#to_sObject



481
482
483
# File 'lib/dommy/element.rb', line 481

def to_s
  value
end

#valueObject



434
435
436
# File 'lib/dommy/element.rb', line 434

def value
  @element.__dommy_backend_node__["class"].to_s
end

#value=(new_value) ⇒ Object



438
439
440
# File 'lib/dommy/element.rb', line 438

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