Skip to content

`encode': no implicit conversion of nil into String (TypeError) #2784

@deril

Description

@deril

Please describe the bug

Nokogiri raises an error while trying to encode an empty string to nil encoding

Help us reproduce what you're seeing

#! /usr/bin/env ruby

require 'nokogiri'
require 'minitest/autorun'

class Test < MiniTest::Spec
  describe "Node#css" do
    it "should find a div using chained classes" do
      xml = "<root/>"
      
      doc = Nokogiri::XML(xml)
      node = doc.css(".node")
      
      assert_equal "", node.to_html
    end
  end
end

Expected behavior

nokogiri doesn't fail on converting to_html empty node set

Environment

# Nokogiri (1.14.1)
    ---
    warnings: []
    nokogiri:
      version: 1.14.1
      cppflags:
      - "-I/Users/deril/.frum/versions/3.1.3/lib/ruby/gems/3.1.0/gems/nokogiri-1.14.1-arm64-darwin/ext/nokogiri"
      - "-I/Users/deril/.frum/versions/3.1.3/lib/ruby/gems/3.1.0/gems/nokogiri-1.14.1-arm64-darwin/ext/nokogiri/include"
      - "-I/Users/deril/.frum/versions/3.1.3/lib/ruby/gems/3.1.0/gems/nokogiri-1.14.1-arm64-darwin/ext/nokogiri/include/libxml2"
      ldflags: []
    ruby:
      version: 3.1.3
      platform: arm64-darwin22
      gem_platform: arm64-darwin-22
      description: ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [arm64-darwin22]
      engine: ruby
    libxml:
      source: packaged
      precompiled: true
      patches:
      - 0001-Remove-script-macro-support.patch
      - 0002-Update-entities-to-remove-handling-of-ssi.patch
      - 0003-libxml2.la-is-in-top_builddir.patch
      - '0009-allow-wildcard-namespaces.patch'
      libxml2_path: "/Users/deril/.frum/versions/3.1.3/lib/ruby/gems/3.1.0/gems/nokogiri-1.14.1-arm64-darwin/ext/nokogiri"
      memory_management: ruby
      iconv_enabled: true
      compiled: 2.10.3
      loaded: 2.10.3
    libxslt:
      source: packaged
      precompiled: true
      patches:
      - 0001-update-automake-files-for-arm64.patch
      datetime_enabled: true
      compiled: 1.1.37
      loaded: 1.1.37
    other_libraries:
      zlib: 1.2.13
      libiconv: '1.17'
      libgumbo: 1.0.0-nokogiri```

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions