Class: Dommy::RuleStyleDeclaration

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

Overview

CSSStyleRule#style — a live, mutable CSSStyleDeclaration backed by a rule's declaration block. Reads come from the parsed block; every write reserializes the block and hands it back to the owning CSSRule, which rebuilds its cssText and invalidates the document's computed-style cache.

Instance Method Summary collapse

Methods included from Bridge::Methods

included

Constructor Details

#initialize(rule, body_text) ⇒ RuleStyleDeclaration

Returns a new instance of RuleStyleDeclaration.



288
289
290
291
# File 'lib/dommy/css.rb', line 288

def initialize(rule, body_text)
  @rule = rule
  @props = parse(body_text)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

camelCase / snake_case property accessors (style.backgroundColor).



354
355
356
357
358
359
360
361
362
363
# File 'lib/dommy/css.rb', line 354

def method_missing(name, *args)
  str = name.to_s
  if str.end_with?("=")
    set_property(css_name(str[0..-2]), args.first)
  elsif args.empty?
    get_property_value(css_name(str))
  else
    super
  end
end

Instance Method Details

#[](key) ⇒ Object



321
322
323
# File 'lib/dommy/css.rb', line 321

def [](key)
  key.is_a?(Integer) ? @props.keys[key].to_s : get_property_value(key)
end

#[]=(name, value) ⇒ Object



325
326
327
# File 'lib/dommy/css.rb', line 325

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

#__js_call__(method, args) ⇒ Object



394
395
396
397
398
399
400
401
402
# File 'lib/dommy/css.rb', line 394

def __js_call__(method, args)
  case method
  when "getPropertyValue" then get_property_value(args[0])
  when "getPropertyPriority" then get_property_priority(args[0])
  when "setProperty" then set_property(args[0], args[1], args[2])
  when "removeProperty" then remove_property(args[0])
  when "item" then item(args[0].to_i)
  end
end

#__js_get__(key) ⇒ Object



369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/dommy/css.rb', line 369

def __js_get__(key)
  case key
  when "cssText" then css_text
  when "length" then length
  when "parentRule" then @rule
  else
    if key.is_a?(Integer) || key.to_s.match?(/\A\d+\z/)
      self[key.to_i]
    else
      get_property_value(css_name(key.to_s))
    end
  end
end

#__js_set__(key, value) ⇒ Object



383
384
385
386
387
388
389
390
# File 'lib/dommy/css.rb', line 383

def __js_set__(key, value)
  case key
  when "cssText" then self.css_text = value
  else set_property(css_name(key.to_s), value)
  end

  nil
end

#css_textObject



337
338
339
340
341
342
# File 'lib/dommy/css.rb', line 337

def css_text
  @props.map do |name, entry|
    important = entry[:priority] == "important" ? " !important" : ""
    "#{name}: #{entry[:value]}#{important};"
  end.join(" ")
end

#css_text=(text) ⇒ Object



344
345
346
347
# File 'lib/dommy/css.rb', line 344

def css_text=(text)
  @props = parse(text)
  flush!
end

#each(&blk) ⇒ Object



349
350
351
# File 'lib/dommy/css.rb', line 349

def each(&blk)
  @props.keys.each(&blk)
end

#get_property_priority(name) ⇒ Object



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

def get_property_priority(name)
  entry = @props[name.to_s]
  entry ? entry[:priority] : ""
end

#get_property_value(name) ⇒ Object



293
294
295
296
# File 'lib/dommy/css.rb', line 293

def get_property_value(name)
  entry = @props[name.to_s]
  entry ? entry[:value] : ""
end

#item(index) ⇒ Object



333
334
335
# File 'lib/dommy/css.rb', line 333

def item(index)
  @props.keys[index.to_i].to_s
end

#lengthObject



329
330
331
# File 'lib/dommy/css.rb', line 329

def length
  @props.size
end

#remove_property(name) ⇒ Object



315
316
317
318
319
# File 'lib/dommy/css.rb', line 315

def remove_property(name)
  removed = @props.delete(name.to_s)
  flush!
  removed ? removed[:value] : ""
end

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

Returns:

  • (Boolean)


365
366
367
# File 'lib/dommy/css.rb', line 365

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

#set_property(name, value, priority = nil) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
# File 'lib/dommy/css.rb', line 303

def set_property(name, value, priority = nil)
  key = name.to_s
  if value.nil? || value.to_s.empty?
    @props.delete(key)
  else
    important = priority.to_s.downcase == "important" ? "important" : ""
    @props[key] = {value: value.to_s, priority: important}
  end
  flush!
  nil
end