Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@chrisroos
Created May 1, 2015 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisroos/64352d0a1e3a12d3acf4 to your computer and use it in GitHub Desktop.
Save chrisroos/64352d0a1e3a12d3acf4 to your computer and use it in GitHub Desktop.
Nokogiri nth-child problems with LibXML 2.9

Nokogiri nth-child problems with LibXML 2.9

I encounted this when using an older version of Nokogiri (1.5.11) and a newer version of LibXML (2.9.0).

These tests fail for me with Nokogiri 1.5.11 and pass using Nokogiri 1.6.6.2.

Comment out the version restriction in the Gemfile and bundle update nokogiri to see the tests pass.

Nokogiri version info

Nokogiri (1.5.11)

---
warnings: []
nokogiri: 1.5.11
ruby:
  version: 2.2.0
  platform: x86_64-darwin14
  description: ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]
  engine: ruby
libxml:
  binding: extension
  compiled: 2.9.0
  loaded: 2.9.0

Nokogiri (1.6.6.2)

---
warnings: []
nokogiri: 1.6.6.2
ruby:
  version: 2.2.0
  platform: x86_64-darwin14
  description: ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]
  engine: ruby
libxml:
  binding: extension
  source: packaged
  libxml2_path: "/private/tmp/wem-nokogiri/.bundle/gems/gems/nokogiri-1.6.6.2/ports/x86_64-apple-darwin14.0.0/libxml2/2.9.2"
  libxslt_path: "/private/tmp/wem-nokogiri/.bundle/gems/gems/nokogiri-1.6.6.2/ports/x86_64-apple-darwin14.0.0/libxslt/1.1.28"
  libxml2_patches:
  - 0001-Revert-Missing-initialization-for-the-catalog-module.patch
  - 0002-Fix-missing-entities-after-CVE-2014-3660-fix.patch
  libxslt_patches:
  - 0001-Adding-doc-update-related-to-1.1.28.patch
  - 0002-Fix-a-couple-of-places-where-f-printf-parameters-wer.patch
  - 0003-Initialize-pseudo-random-number-generator-with-curre.patch
  - 0004-EXSLT-function-str-replace-is-broken-as-is.patch
  - 0006-Fix-str-padding-to-work-with-UTF-8-strings.patch
  - 0007-Separate-function-for-predicate-matching-in-patterns.patch
  - 0008-Fix-direct-pattern-matching.patch
  - 0009-Fix-certain-patterns-with-predicates.patch
  - 0010-Fix-handling-of-UTF-8-strings-in-EXSLT-crypto-module.patch
  - 0013-Memory-leak-in-xsltCompileIdKeyPattern-error-path.patch
  - 0014-Fix-for-bug-436589.patch
  - 0015-Fix-mkdir-for-mingw.patch
  compiled: 2.9.2
  loaded: 2.9.2
source 'https://rubygems.org'
gem 'nokogiri', '1.5.11'
gem 'test-unit'
GEM
remote: https://rubygems.org/
specs:
nokogiri (1.5.11)
power_assert (0.2.3)
test-unit (3.0.9)
power_assert
PLATFORMS
ruby
DEPENDENCIES
nokogiri (= 1.5.11)
test-unit
require 'bundler/setup'
require 'nokogiri'
require 'test/unit'
class NokogiriNthChildTest < Test::Unit::TestCase
def setup
@doc = Nokogiri::HTML(html)
end
def test_should_return_container_1
nodes = @doc.css('.container:nth-child(1)')
assert_equal 1, nodes.length
assert_match /container-1/, nodes.first.attributes['class'].value
end
def test_should_return_container_2
nodes = @doc.css('.container:nth-child(2)')
assert_equal 1, nodes.length
assert_match /container-2/, nodes.first.attributes['class'].value
end
private
def html
<<-EndHtml
<div class="container container-1"></div>
<div class="container container-2"></div>
EndHtml
end
end
puts Nokogiri::VersionInfo.new.to_markdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment