Class: Dommy::CSSNamespace

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

Overview

window.CSS namespace object — escape() for safe selector building (used by Turbo and friends) and supports() backed by the @supports condition evaluator.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Bridge::Methods

included

Class Method Details

.escape(value) ⇒ Object

CSSOM CSS.escape — escape a string for use as an identifier in a selector. Follows the spec's char rules closely enough for selectors.



744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
# File 'lib/dommy/css.rb', line 744

def self.escape(value)
  str = value.to_s
  out = +""
  str.each_char.with_index do |ch, i|
    code = ch.ord
    if code.zero?
      out << "\uFFFD"
    elsif (code >= 0x01 && code <= 0x1F) || code == 0x7F ||
          (i.zero? && code >= 0x30 && code <= 0x39) ||
          (i == 1 && code >= 0x30 && code <= 0x39 && str[0] == "-")
      out << "\\#{code.to_s(16)} "
    elsif code >= 0x80 || code == 0x2D || code == 0x5F ||
          (code >= 0x30 && code <= 0x39) ||
          (code >= 0x41 && code <= 0x5A) || (code >= 0x61 && code <= 0x7A)
      out << ch
    else
      out << "\\#{ch}"
    end
  end
  out
end

Instance Method Details

#__js_call__(method, args) ⇒ Object



722
723
724
725
726
727
728
729
# File 'lib/dommy/css.rb', line 722

def __js_call__(method, args)
  case method
  when "escape"
    self.class.escape(args[0])
  when "supports"
    supports?(args)
  end
end

#__js_get__(_key) ⇒ Object

method-only; any property read is absent



717
# File 'lib/dommy/css.rb', line 717

def __js_get__(_key) = Bridge::ABSENT # method-only; any property read is absent

#__js_set__(_key, _value) ⇒ Object



718
# File 'lib/dommy/css.rb', line 718

def __js_set__(_key, _value) = Bridge::UNHANDLED

#supports?(args) ⇒ Boolean

CSS.supports(property, value) checks one declaration; CSS.supports( conditionText) evaluates a full . Both go through the same optimistic evaluator the cascade uses for @supports.

Returns:

  • (Boolean)


734
735
736
737
738
739
740
# File 'lib/dommy/css.rb', line 734

def supports?(args)
  if args.length >= 2 && !args[1].nil?
    Internal::CSS::Supports.supports_declaration?(args[0], args[1])
  else
    Internal::CSS::Supports.match?(args[0])
  end
end