module Browser module DelegateNative # Provides a default initializer. This should be overridden in all but the # simplest cases. def initialize native @native = native end def [](property) method_missing(property) end # Fall back to native properties. If the message sent to this element is not # recognized, it checks to see if it is a property of the native element. It # also checks for variations of the message name, such as: # # :supported? => [:supported, :isSupported] # # If a property with the specified message name is found and it is a # function, that function is invoked with `args`. Otherwise, the property # is returned as is. def method_missing message, *args, &block if message.end_with? '=' message = message.chop property_name = property_for_message(message) arg = args[0] arg = if `arg && typeof arg.$to_n === 'function'` arg.to_n else arg end return `#@native[#{property_name}] = args[0]` else property_name = property_for_message(message) %x{ let value = #@native[#{property_name}]; let type = typeof(value); try { if (type === 'function') { #{ args.map! do |arg| if `arg && typeof arg.$to_n === 'function'` arg.to_n else arg end end } value = value.apply(#@native, args); } if (value instanceof HTMLCollection || value instanceof NodeList) { let a = []; for(let i=0; i