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

Instance Method Summary collapse

Constructor Details

#initialize(element) ⇒ ClassList

Returns a new instance of ClassList.



299
300
301
# File 'lib/dommy/element.rb', line 299

def initialize(element)
  @element = element
end

Instance Method Details

#[](index) ⇒ Object



348
349
350
# File 'lib/dommy/element.rb', line 348

def [](index)
  item(index)
end

#__js_call__(method, args) ⇒ Object



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/dommy/element.rb', line 386

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



364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/dommy/element.rb', line 364

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_set__(key, val) ⇒ Object



377
378
379
380
381
382
383
384
# File 'lib/dommy/element.rb', line 377

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

  nil
end

#add(*tokens) ⇒ Object



326
327
328
329
# File 'lib/dommy/element.rb', line 326

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)


322
323
324
# File 'lib/dommy/element.rb', line 322

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

#each(&blk) ⇒ Object



352
353
354
# File 'lib/dommy/element.rb', line 352

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

#item(index) ⇒ Object



309
310
311
# File 'lib/dommy/element.rb', line 309

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

#lengthObject Also known as: size



303
304
305
# File 'lib/dommy/element.rb', line 303

def length
  class_tokens.length
end

#remove(*tokens) ⇒ Object



331
332
333
334
# File 'lib/dommy/element.rb', line 331

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

#replace(old_token, new_token) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
# File 'lib/dommy/element.rb', line 336

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



356
357
358
# File 'lib/dommy/element.rb', line 356

def to_a
  class_tokens.dup
end

#to_sObject



360
361
362
# File 'lib/dommy/element.rb', line 360

def to_s
  value
end

#valueObject



313
314
315
# File 'lib/dommy/element.rb', line 313

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

#value=(new_value) ⇒ Object



317
318
319
# File 'lib/dommy/element.rb', line 317

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