lib/preact.rb in isomorfeus-preact-23.1.0.rc2 vs lib/preact.rb in isomorfeus-preact-23.6.0.rc1

- old
+ new

@@ -51,10 +51,11 @@ } const EMPTY_OBJ = {}; const EMPTY_ARR = []; const slice = EMPTY_ARR.slice; + const isArray = Array.isArray; function assign(obj, props) { for (let i in props) obj[i] = props[i]; return obj; } @@ -276,11 +277,11 @@ str, null, null, str ); - } else if (Array.isArray(childVNode)) { + } else if (isArray(childVNode)) { childVNode = newParentVNode._children[i] = self.createVNode( Opal.Fragment, #{{ children: `childVNode` }}, null, null, @@ -436,15 +437,15 @@ } } } function eventProxy(e) { - this._listeners[e.type + false].$call(#{Browser::Event.new(`e`)}); + this._listeners[e.type + false].$call(#{::Browser::Event.new(`e`)}); } function eventProxyCapture(e) { - this._listeners[e.type + true].$call(#{Browser::Event.new(`e`)}); + this._listeners[e.type + true].$call(#{::Browser::Event.new(`e`)}); } const IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|itera/i; function setStyle(style, key, value) { @@ -531,31 +532,33 @@ name !== 'form' && // Default value in browsers is `-1` and an empty string is // cast to `0` instead name !== 'tabIndex' && name !== 'download' && + name !== 'rowSpan' && + name !== 'colSpan' && name in dom ) { try { dom[name] = (value == null || value === nil) ? '' : value; // labelled break is 1b smaller here than a return statement (sorry) break o; } catch (e) {} } - // ARIA-attributes have a different notion of boolean values. - // The value `false` is different from the attribute not - // existing on the DOM, so we can't remove it. For non-boolean - // ARIA-attributes we could treat false as a removal, but the - // amount of exceptions would cost us too many bytes. On top of - // that other VDOM frameworks also always stringify `false`. + // aria- and data- attributes have no boolean representation. + // A `false` value is different from the attribute not being + // present, so we can't remove it. For non-boolean aria + // attributes we could treat false as a removal, but the + // amount of exceptions would cost too many bytes. On top of + // that other frameworks generally stringify `false`. if (typeof value === 'function') { // never serialize functions as attribute values } else if ( value != null && value !== nil && - (value !== false || (name[0] === 'a' && name[1] === 'r')) + (value !== false || (name[4] === '-')) ) { dom.setAttribute(name, value); } else { dom.removeAttribute(name); } @@ -686,11 +689,11 @@ newVNode._children = []; } else { i = newVNode.props["$[]"]("children"); diffChildren( dom, - Array.isArray(i) ? i : [i], + isArray(i) ? i : [i], newVNode, oldVNode, globalContext, isSvg && nodeType !== 'foreignObject', excessDomChildren, @@ -893,11 +896,11 @@ renderResult = renderResult.props["$[]"]("children"); } diffChildren( parentDom, - Array.isArray(renderResult) ? renderResult : [renderResult], + isArray(renderResult) ? renderResult : [renderResult], newVNode, oldVNode, globalContext, isSvg, excessDomChildren, @@ -968,11 +971,11 @@ } }); } let vnodeId = 0; - const vnode_class = #{VNode}; + const vnode_class = #{::VNode}; function is_a_vnode(type) { return type === vnode_class; } function is_nil() { return false; } function vnode_eql(me, other) { for(let prop in me) { @@ -1114,13 +1117,14 @@ } } self.process._rerenderCount = 0; } else - IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|^--/i + IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|^--/i.freeze JS_TO_CSS = {} - ENCODED_ENTITIES = /[&<>"]/ + ENCODED_ENTITIES = /[&<>"]/.freeze + ENTITY_REPLACEMENTS = { '&'.freeze => '&amp;'.freeze, '<'.freeze => '&lt;'.freeze, '>'.freeze => '&gt;'.freeze, '"'.freeze => '&quot;'.freeze }.freeze end class << self def _ctxi @_ctxi ||= 0 @@ -1154,23 +1158,23 @@ normalized_props[:children] = children unless children.nil? if RUBY_ENGINE == 'opal' `self.createVNode(vnode.type, normalized_props, #{key || `vnode.key`}, #{ref || `vnode.ref`}, null)` else - VNode.new(vnode.type, normalized_props, key || vnode.key, ref || vnode.ref) + ::VNode.new(vnode.type, normalized_props, key || vnode.key, ref || vnode.ref) end end def create_context(const_name, default_value = nil) - context = Preact::Context.new(default_value) - Object.const_set(const_name, context) + context = ::Preact::Context.new(default_value) + ::Object.const_set(const_name, context) end def _init_render self.render_buffer = [] self.rerender_queue = [] - Isomorfeus.reset_something_loading + ::Isomorfeus.reset_something_loading end if RUBY_ENGINE == 'opal' attr_accessor :_vnode_id attr_accessor :render_buffer @@ -1242,11 +1246,11 @@ return element_or_query elsif `(typeof element_or_query === 'string')` || element_or_query.is_a?(String) return `document.body.querySelector(element_or_query)` elsif `(typeof element_or_query === 'function')` return element_or_query - elsif element_or_query.is_a?(Browser::Element) + elsif element_or_query.is_a?(::Browser::Element) return element_or_query.to_n else return element_or_query end end @@ -1262,48 +1266,28 @@ element_or_query = element_or_query_to_n(element_or_query) `self.render(null, element_or_query)` end else # RUBY_ENGINE def render_buffer - Thread.current[:@_isomorfeus_preact_render_buffer] + ::Thread.current[:@_isomorfeus_preact_render_buffer] end def render_buffer=(i) - Thread.current[:@_isomorfeus_preact_render_buffer] = i + ::Thread.current[:@_isomorfeus_preact_render_buffer] = i end def rerender_queue - Thread.current[:@_isomorfeus_preact_rerender_queue] + ::Thread.current[:@_isomorfeus_preact_rerender_queue] end def rerender_queue=(i) - Thread.current[:@_isomorfeus_preact_rerender_queue] = i + ::Thread.current[:@_isomorfeus_preact_rerender_queue] = i end - def is_renderable?(block_result) - block_result && - (block_result.is_a?(VNode) || block_result.is_a?(String) || block_result.is_a?(Numeric) || - (block_result.is_a?(Array) && block_result.length > 0 && is_renderable?(block_result[0]))) - end - def _encode_entities(input) - s = input.to_s - return s unless ENCODED_ENTITIES.match?(s) - # TODO performance maybe, maybe similar to new js way, need to measure - # for (; i<str.length; i++) { - # switch (str.charCodeAt(i)) { - # case 60: ch = '&lt;'; break; - # case 62: ch = '&gt;'; break; - # case 34: ch = '&quot;'; break; - # case 38: ch = '&amp;'; break; - # default: continue; - # } - # if (i > start) out += str.slice(start, i); - # out += ch; - # start = i + 1; - # } - s.gsub(/&/, '&amp;').gsub(/</, '&lt;').gsub(/>/, '&gt;').gsub(/"/, '&quot;') + return input unless input.match?(ENCODED_ENTITIES) + input.gsub(ENCODED_ENTITIES, ENTITY_REPLACEMENTS) end def _style_obj_to_css(v) str = '' v.each do |prop, val| @@ -1318,19 +1302,12 @@ end end return str.empty? ? nil : str end - def _render_element(element, props, &block) - pr = Preact.render_buffer - pr[pr.length-1] << create_element(element, props, nil, &block) - nil - end - def render_to_string(vnode, context = nil) _init_render - context = {} unless context - _render_to_string(vnode, context, false, nil) + _render_to_string(vnode, context) end end # RUBY_ENGINE end end