# backtick_javascript: true 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 = arg.to_n if `arg && typeof arg.$to_n === 'function'` return `#@native[#{property_name}] = arg` else property_name = property_for_message(message) %x{ let value = #@native[#{property_name}]; let type = typeof(value); if (type === 'undefined') { return #{super}; } try { if (type === 'function') { #{args.map! { |arg| `arg && typeof arg.$to_n === 'function'` ? arg.to_n : arg }} value = value.apply(#@native, args); } if (value instanceof HTMLCollection || value instanceof NodeList) { let a = []; for(let i=0; i