lib/nano_css.rb in isomorfeus-preact-22.9.0.rc9 vs lib/nano_css.rb in isomorfeus-preact-22.10.0.rc1

- old
+ new

@@ -39,26 +39,21 @@ attr_reader :renderer def initialize(config = nil, given_renderer: nil) config = {} unless config - if RUBY_ENGINE == 'opal' - @client = `typeof window === "object"` - else - @client = false - end if given_renderer @renderer = given_renderer else @renderer = { raw: '', pfx: '_', hydrate_force_put: false, prefixes: ['-webkit-', '-moz-', '-o-', ''] } @renderer.merge!(config) end @hydrated = {} - if @client + if RUBY_ENGINE == 'opal' unless @renderer.key?(:sh) e = `document.createElement('style')` @renderer[:sh] = e `document.head.appendChild(e)` end @@ -74,14 +69,14 @@ unless @@unitless_css_properties @@unitless_css_properties = {} UNITLESS_NUMBER_PROPS.each do |prop| @@unitless_css_properties[prop] = 1 - @@unitless_css_properties['-webkit-' + prop] = 1 - @@unitless_css_properties['-ms-' + prop] = 1 - @@unitless_css_properties['-moz-' + prop] = 1 - @@unitless_css_properties['-o-' + prop] = 1 + @@unitless_css_properties["-webkit-#{prop}"] = 1 + @@unitless_css_properties["-ms-#{prop}"] = 1 + @@unitless_css_properties["-moz-#{prop}"] = 1 + @@unitless_css_properties["-o-#{prop}"] = 1 end end unless given_renderer put('', { '@keyframes fadein' => { from: { opacity: 0 }, to: { opacity: 1 }}, @@ -108,19 +103,19 @@ def hash_str(str) h = 5381 str.each_codepoint do |cp| h = (h * 33) ^ cp end - '_' + h.abs.to_s(36) + "_#{h.abs.to_s(36)}" end def kebab(prop) prop.to_s.gsub(KEBAB_REGEX, '-$&').downcase.to_sym end def put(css_selector, decls, atrule = nil) - return if @client && !@renderer[:hydrate_force_put] && @hydrated.key?(css_selector) + return if RUBY_ENGINE == 'opal' && !@renderer[:hydrate_force_put] && @hydrated.key?(css_selector) str = '' postponed = [] decls.each do |prop, value| @@ -130,12 +125,12 @@ str += decl(prop, value) end end unless str.empty? - str = css_selector + '{' + str + '}' - put_raw(atrule ? atrule + '{' + str + '}' : str) + str = "#{css_selector}{#{str}}" + put_raw(atrule ? "#{atrule}{#{str}}" : str) end postponed.each do |prop| if prop[0] === '@' && prop != '@font-face' put_at(css_selector, decls[prop], prop) @@ -155,12 +150,12 @@ end str += "#{keyframe}{#{str_decls}}" end @renderer[:prefixes].each do |prefix| - raw_key_frames = prelude.sub('@keyframes', "@#{prefix}keyframes") + "{#{str}}" - if @client + raw_key_frames = "#{prelude.sub('@keyframes', "@#{prefix}keyframes")}{#{str}}" + if RUBY_ENGINE == 'opal' ksh = @renderer[:ksh] `ksh.appendChild(document.createTextNode(raw_key_frames))` else put_raw(raw_key_frames) end @@ -169,42 +164,40 @@ return end put(nil, keyframes, prelude) end - def put_raw(raw_css_rule) - @client ? put_raw_client(raw_css_rule) : put_raw_ssr(raw_css_rule) + if RUBY_ENGINE == 'opal' + def put_raw(raw_css_rule) + # .insertRule() is faster than .appendChild(), that's why we use it in PROD. + # But CSS injected using .insertRule() is not displayed in Chrome Devtools + sheet = @renderer[:sh].JS[:sheet] + # Unknown pseudo-selectors will throw, this try/catch swallows all errors. + `sheet.insertRule(raw_css_rule, sheet.cssRules.length)` rescue nil + end + else + def put_raw(raw_css_rule) + @renderer[:raw] << raw_css_rule + end end - def put_raw_ssr(raw_css_rule) - @renderer[:raw] << raw_css_rule - end - - def put_raw_client(raw_css_rule) - # .insertRule() is faster than .appendChild(), that's why we use it in PROD. - # But CSS injected using .insertRule() is not displayed in Chrome Devtools - sheet = @renderer[:sh].JS[:sheet] - # Unknown pseudo-selectors will throw, this try/catch swallows all errors. - `sheet.insertRule(raw_css_rule, sheet.cssRules.length)` rescue nil - end - # addons # rule def rule(css, block = nil) block = block || hash(css) - block = @renderer[:pfx] + block - put('.' + block, css) + block = "#{@renderer[:pfx]}#{block}" + put(".#{block}", css) - ' ' + block + " #{block}" end # sheet def delete_from_sheet(rule_name) - selector_rule_name = "._" + rule_name + "-" + selector_rule_name = "._#{rule_name}-" if renderer[:sh] && renderer[:sh].JS[:sheet] sheet = renderer[:sh].JS[:sheet] css_rules = sheet.JS[:cssRules] %x{ let i = 0; @@ -254,10 +247,10 @@ else if parents.empty? result.push(sel) else parents.each do |parent| - result.push(parent + ' ' + sel) + result.push("#{parent} #{sel}") end end end end