Sha256: 4786605d2eb50519296aa37341d1509d5a0a18ce141b9f1815769a6971ee328a

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

using Nokogiri::XML::Range::Refinement

module Nokogiri::XML
  module Replacable
    def replace_data(offset, count, data)
      len = length
      raise IndexSizeError, 'offset is greater than node length' if offset > len

      count = len - offset if offset + count > len
      encoding = content.encoding
      utf16_content = content.encode('UTF-16LE')
      utf16_data = data.encode('UTF-16LE')
      result = utf16_content.byteslice(0, offset * 2) + utf16_data + utf16_content.byteslice(offset * 2, utf16_content.bytesize)
      delete_offset = offset + utf16_data.bytesize / 2
      result = result.byteslice(0, delete_offset * 2) + result.byteslice((delete_offset + count) * 2, result.bytesize)

      self.content = result.encode(encoding)
    end

    def substring_data(offset, count)
      len = length
      raise IndexSizeError, 'offset is greater than node length' if offset > len

      encoding = content.encoding
      utf16_content = content.encode('UTF-16LE')

      byte_length = [utf16_content.bytesize - offset * 2, count * 2].min
      utf16_content.byteslice(offset * 2, byte_length).encode(encoding)
    end
  end

  class CharacterData
    include Replacable
  end

  class ProcessingInstruction
    include Replacable
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nokogiri-xml-range-0.1.0 lib/nokogiri/xml/replacable.rb