<commit_before>require 'oga'

module Oga
  module XML
    class Document
      # Premailer builds its css searches with `@` prepended to an attribute,
      # which breaks Oga. Since Premailer also expects `search` to be defined
      # on a document, we define it and just strip the `@` out before calling
      # Oga's build-in #css method.
      def search(query)
        css(query.gsub('@', ''))
      end
    end
  end<commit_msg>Make oga attributes behave the way premailer expects<commit_after>require 'oga'

module Pakyow
  class OgaAttributes
    def initialize(attributes)
      @attributes = attributes
    end

    def [](key)
      if key.is_a?(Integer)
        super
      else
        @attributes.find { |a| a.name.to_sym == key.to_sym }
      end
    end

    def method_missing(*args, &block)
      @attributes.send(*args, &block)
    end
  end
end

module Oga
  module XML
    class Document
      # Premailer builds its css searches with `@` prepended to an attribute,
      # which breaks Oga. Since Premailer also expects `search` to be defined
      # on a document, we define it and just strip the `@` out before calling
      # Oga's build-in #css method.
      def search(query)
        css(query.gsub('@', ''))
      end
    end

    class Element
      def attributes
        Pakyow::OgaAttributes.new(@attributes)
      end

      def []=(name, value)
        set(name, value)
      end

      def [](name)
        get(name)
      end
    end

    class Attribute
      def to_str
        value
      end
    end
  end
{'fraction_non_alphanumeric': 0.08408796895213454, 'fraction_numerical': 0.0, 'mean_word_length': 2.1765913757700206, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4890978', 'n_tokens_mistral': 474, 'n_tokens_neox': 462, 'n_words': 163}