hpricot 0.8.1 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -132,6 +132,8 @@ static VALUE reProcInsParse;
132
132
  }
133
133
 
134
134
  action save_attr {
135
+ if (!S->xml)
136
+ akey = rb_funcall(akey, s_downcase, 0);
135
137
  ATTR(akey, aval);
136
138
  }
137
139
 
@@ -38,15 +38,20 @@ module Hpricot
38
38
  end
39
39
  end
40
40
 
41
- class Elem
42
- def initialize tag, attrs = nil, children = nil, etag = nil
43
- self.name, self.raw_attributes, self.children, self.etag =
44
- tag, attrs, children, etag
41
+ class Attributes
42
+ attr_accessor :element
43
+ def initialize e
44
+ @element = e
45
45
  end
46
- def empty?; children.nil? or children.empty? end
47
- def attributes
48
- if raw_attributes
49
- raw_attributes.inject({}) do |hsh, (k, v)|
46
+ def [] k
47
+ Hpricot.uxs((@element.raw_attributes || {})[k])
48
+ end
49
+ def []= k, v
50
+ (@element.raw_attributes ||= {})[k] = v.fast_xs
51
+ end
52
+ def to_hash
53
+ if @element.raw_attributes
54
+ @element.raw_attributes.inject({}) do |hsh, (k, v)|
50
55
  hsh[k] = Hpricot.uxs(v)
51
56
  hsh
52
57
  end
@@ -54,6 +59,23 @@ module Hpricot
54
59
  {}
55
60
  end
56
61
  end
62
+ def to_s
63
+ to_hash.to_s
64
+ end
65
+ def inspect
66
+ to_hash.inspect
67
+ end
68
+ end
69
+
70
+ class Elem
71
+ def initialize tag, attrs = nil, children = nil, etag = nil
72
+ self.name, self.raw_attributes, self.children, self.etag =
73
+ tag, attrs, children, etag
74
+ end
75
+ def empty?; children.nil? or children.empty? end
76
+ def attributes
77
+ Attributes.new self
78
+ end
57
79
  def to_plain_text
58
80
  if self.name == 'br'
59
81
  "\n"
@@ -102,10 +124,7 @@ module Hpricot
102
124
  class BogusETag
103
125
  def initialize name; self.name = name end
104
126
  def output(out, opts = {})
105
- out <<
106
- if_output(opts) do
107
- "</#{name}>"
108
- end
127
+ out << if_output(opts) { "" }
109
128
  end
110
129
  end
111
130
 
@@ -194,6 +194,7 @@ module Hpricot
194
194
  alias_method :innerHTML=, :inner_html=
195
195
 
196
196
  def reparent(nodes)
197
+ return unless nodes
197
198
  altered!
198
199
  [*nodes].each { |e| e.parent = self }
199
200
  end
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  #!/usr/bin/env ruby
2
3
 
3
4
  require 'test/unit'
@@ -35,14 +36,22 @@ class TestAlter < Test::Unit::TestCase
35
36
  end
36
37
 
37
38
  def test_change_attributes
38
- all_ps = (@basic/"p").attr("title", "Some Title")
39
+ all_ps = (@basic/"p").attr("title", "Some Title & Etc…")
39
40
  all_as = (@basic/"a").attr("href", "http://my_new_href.com")
40
41
  all_lb = (@basic/"link").attr("href") { |e| e.name }
41
- assert_changed(@basic, "p", all_ps) {|p| p.attributes["title"] == "Some Title"}
42
+ assert_changed(@basic, "p", all_ps) {|p| p.raw_attributes["title"] == "Some Title &amp; Etc&#8230;"}
42
43
  assert_changed(@basic, "a", all_as) {|a| a.attributes["href"] == "http://my_new_href.com"}
43
44
  assert_changed(@basic, "link", all_lb) {|a| a.attributes["href"] == "link" }
44
45
  end
45
46
 
47
+ def test_change_attributes2
48
+ all_as = (@basic%"a").attributes["href"] = "http://my_new_href.com"
49
+ all_ps = (@basic%"p").attributes["title"] = "Some Title & Etc…"
50
+ assert_equal (@basic%"a").raw_attributes["href"], "http://my_new_href.com"
51
+ assert_equal (@basic%"p").raw_attributes["title"], "Some Title &amp; Etc&#8230;"
52
+ assert_equal (@basic%"p").attributes["title"], "Some Title & Etc…"
53
+ end
54
+
46
55
  def test_remove_attr
47
56
  all_rl = (@basic/"link").remove_attr("href")
48
57
  assert_changed(@basic, "link", all_rl) { |link| link['href'].nil? }
@@ -70,6 +79,16 @@ class TestAlter < Test::Unit::TestCase
70
79
  assert_equal frag.to_s, "<b><i>A bit of HTML</i></b><beanPole>gravity</beanPole>"
71
80
  end
72
81
 
82
+ def test_reparent_empty_nodes
83
+ doc = Hpricot("<div/>")
84
+ doc.root.inner_html = "foo"
85
+ assert_equal doc.root.inner_html, "foo"
86
+ doc.root.inner_html = ""
87
+ assert_equal doc.root.inner_html, ""
88
+ doc.root.swap { b "test" }
89
+ assert_equal doc.root.inner_html, "test"
90
+ end
91
+
73
92
  def assert_changed original, selector, set, &block
74
93
  assert set.all?(&block)
75
94
  assert Hpricot(original.to_html).search(selector).all?(&block)
@@ -227,6 +227,14 @@ class TestParser < Test::Unit::TestCase
227
227
  assert_kind_of Hpricot::Elements, @boingboing.search('//div/p').search('a img')
228
228
  end
229
229
 
230
+ def test_attr_casing
231
+ doc = Hpricot("<a HREF='a'>A simple <b>test</b> string.</a>")
232
+ assert_equal (doc % :a)[:href], "a"
233
+ assert_equal (doc % :a)[:HREF], nil
234
+ assert_equal (doc % :a)['href'], "a"
235
+ assert_equal (doc % :a)['HREF'], nil
236
+ end
237
+
230
238
  def test_class_search
231
239
  # test case sent by Chih-Chao Lam
232
240
  doc = Hpricot("<div class=xyz'>abc</div>")
@@ -5,6 +5,15 @@ require 'test/unit'
5
5
  require 'hpricot'
6
6
  require 'load_files'
7
7
 
8
+ unless "".respond_to?(:lines)
9
+ require 'enumerator'
10
+ class String
11
+ def lines
12
+ Enumerable::Enumerator.new(self, :each_line)
13
+ end
14
+ end
15
+ end
16
+
8
17
  class TestPreserved < Test::Unit::TestCase
9
18
  def assert_roundtrip str
10
19
  doc = Hpricot(str)
@@ -53,6 +62,15 @@ class TestPreserved < Test::Unit::TestCase
53
62
  assert_roundtrip TestFiles::CY0
54
63
  end
55
64
 
65
+ def test_fixup_link
66
+ doc = %{<?xml version="1.0" encoding="UTF-8"?><rss><channel><link>ht</link></channel></rss>}
67
+ assert_roundtrip doc
68
+ assert_equal Hpricot(doc).to_s,
69
+ %{<?xml version="1.0" encoding="UTF-8"?><rss><channel><link />ht</channel></rss>}
70
+ assert_equal Hpricot.XML(doc).to_s,
71
+ %{<?xml version="1.0" encoding="UTF-8"?><rss><channel><link>ht</link></channel></rss>}
72
+ end
73
+
56
74
  def test_escaping_of_attrs
57
75
  # ampersands in URLs
58
76
  str = %{<a href="http://google.com/search?q=hpricot&amp;l=en">Google</a>}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hpricot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - why the lucky stiff
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-03 00:00:00 -07:00
12
+ date: 2009-11-05 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -18,8 +18,8 @@ email: why@ruby-lang.org
18
18
  executables: []
19
19
 
20
20
  extensions:
21
- - ext/hpricot_scan/extconf.rb
22
21
  - ext/fast_xs/extconf.rb
22
+ - ext/hpricot_scan/extconf.rb
23
23
  extra_rdoc_files:
24
24
  - README
25
25
  - CHANGELOG
@@ -29,54 +29,55 @@ files:
29
29
  - COPYING
30
30
  - README
31
31
  - Rakefile
32
- - test/test_paths.rb
33
- - test/test_preserved.rb
34
- - test/test_parser.rb
35
- - test/files
32
+ - test/files/basic.xhtml
33
+ - test/files/boingboing.html
36
34
  - test/files/cy0.html
35
+ - test/files/immob.html
37
36
  - test/files/pace_application.html
38
- - test/files/basic.xhtml
37
+ - test/files/tenderlove.html
38
+ - test/files/uswebgen.html
39
39
  - test/files/utf8.html
40
- - test/files/boingboing.html
41
40
  - test/files/week9.html
42
- - test/files/tenderlove.html
43
- - test/files/immob.html
44
41
  - test/files/why.xml
45
- - test/files/uswebgen.html
46
42
  - test/load_files.rb
47
43
  - test/nokogiri-bench.rb
44
+ - test/test_alter.rb
48
45
  - test/test_builder.rb
46
+ - test/test_parser.rb
47
+ - test/test_paths.rb
48
+ - test/test_preserved.rb
49
49
  - test/test_xml.rb
50
- - test/test_alter.rb
51
- - lib/hpricot
52
- - lib/hpricot/tags.rb
50
+ - lib/hpricot/blankslate.rb
53
51
  - lib/hpricot/builder.rb
54
- - lib/hpricot/traverse.rb
55
52
  - lib/hpricot/elements.rb
56
- - lib/hpricot/modules.rb
53
+ - lib/hpricot/htmlinfo.rb
57
54
  - lib/hpricot/inspect.rb
55
+ - lib/hpricot/modules.rb
56
+ - lib/hpricot/parse.rb
58
57
  - lib/hpricot/tag.rb
59
- - lib/hpricot/blankslate.rb
58
+ - lib/hpricot/tags.rb
59
+ - lib/hpricot/traverse.rb
60
60
  - lib/hpricot/xchar.rb
61
- - lib/hpricot/htmlinfo.rb
62
- - lib/hpricot/parse.rb
63
61
  - lib/hpricot.rb
64
62
  - extras/mingw-rbconfig.rb
65
63
  - ext/hpricot_scan/hpricot_scan.h
66
- - ext/hpricot_scan/HpricotScanService.java
67
64
  - ext/fast_xs/FastXsService.java
68
- - ext/hpricot_scan/hpricot_scan.c
69
- - ext/hpricot_scan/hpricot_css.c
65
+ - ext/hpricot_scan/HpricotCss.java
66
+ - ext/hpricot_scan/HpricotScanService.java
70
67
  - ext/fast_xs/fast_xs.c
71
- - ext/hpricot_scan/test.rb
72
- - ext/hpricot_scan/extconf.rb
68
+ - ext/hpricot_scan/hpricot_css.c
69
+ - ext/hpricot_scan/hpricot_scan.c
73
70
  - ext/fast_xs/extconf.rb
74
- - ext/hpricot_scan/hpricot_scan.rl
75
- - ext/hpricot_scan/hpricot_scan.java.rl
76
- - ext/hpricot_scan/hpricot_css.rl
71
+ - ext/hpricot_scan/extconf.rb
77
72
  - ext/hpricot_scan/hpricot_common.rl
73
+ - ext/hpricot_scan/hpricot_css.java.rl
74
+ - ext/hpricot_scan/hpricot_css.rl
75
+ - ext/hpricot_scan/hpricot_scan.java.rl
76
+ - ext/hpricot_scan/hpricot_scan.rl
78
77
  has_rdoc: true
79
78
  homepage: http://code.whytheluckystiff.net/hpricot/
79
+ licenses: []
80
+
80
81
  post_install_message:
81
82
  rdoc_options:
82
83
  - --quiet
@@ -102,9 +103,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
103
  requirements: []
103
104
 
104
105
  rubyforge_project: hobix
105
- rubygems_version: 1.3.1
106
+ rubygems_version: 1.3.5
106
107
  signing_key:
107
- specification_version: 2
108
+ specification_version: 3
108
109
  summary: a swift, liberal HTML parser with a fantastic library
109
110
  test_files: []
110
111
 
@@ -1,4 +0,0 @@
1
- require './hpricot_scan.so'
2
-
3
- doc = "<doc><person><test>YESSS</test></person><train>SET</train></doc>"
4
- p Hpricot.scan(doc)