
  2004.10.24  q1
    - Â¿¤¯¤Î¾ì¹ç¤ËÍ×ÁÇ¤¬Àµ¤·¤¤Æþ¤ì»Ò´Ø·¸¤Ë¤Ê¤ë¤è¤¦¤Ë½¤Àµ¡£
    - Í¾Ê¬¤Ê½ªÎ»¥¿¥°¤¬À¸À®¤µ¤ì¤ëÅÀ¤ò½¤Àµ¡£
    - ¥Æ¥¹¥È¥±¡¼¥¹¤òÄÉ²Ã¡£

diff -Naur htmlrepair-1.0.1.orig/htmlrepair.rb htmlrepair-1.0.1/htmlrepair.rb
--- htmlrepair-1.0.1.orig/htmlrepair.rb	2004-10-24 15:57:46.505917208 +0900
+++ htmlrepair-1.0.1/htmlrepair.rb	2004-10-24 20:36:22.324726488 +0900
@@ -22,9 +22,17 @@
 require "htmlsplit"
 
 class HTMLSplit
-	
+  CONTENT_FLOW = %w(div center blockquote ins del dd li form button th td iframe noscript)
+  NOT_FLOW = %w(area param dt dd li optgroup option legend
+                caption col colgroup thead tfoot tbody tr th td)
+  CONTENT_INLINE = %w(tt i b u s strike big small
+                      em strong dfn code samp kbd var cite abbr acronym
+                      sub sup span bdo font a p h1 h2 h3 h4 h5 h6 pre q dt label
+                      legend caption)
+  BLOCK = %w(p h1 h2 h3 h4 h5 h6 ul ol dir menu pre dl div center noscript noframes
+                  blockquote form isindex hr table fieldset address)
+
 	PARENTTAG = {
-		'p'			=>	%w(body table),
 		'a'			=>	%w(body),
 		'thead'		=>	%w(table),
 		'tfoot'		=>	%w(table),
@@ -44,9 +52,8 @@
 		'select'	=>	%w(form),
 		'keygen'	=>	%w(form),
 		'label'		=>	%w(form),
-		'fieldset'	=>	%w(form),
-		'legend'	=>	%w(fieldset),
-		'option'	=>	%w(select),
+    'option'   => %w(select optgroup),
+    'optgroup' => %w(select),
 	}
 	
 	def repair
@@ -54,9 +61,7 @@
 		doc = []
 		@document.each {|e|
 			case e
-			when EmptyElementTag
-				doc.push e
-			when StartTag
+			when StartTag, EmptyElementTag
 				if PARENTTAG[e.name] && (a = tag.rindex(e.name))
 					#¥Í¥¹¥È¤«½ªÎ»¥¿¥°¤Î¾ÊÎ¬¤«¥Á¥§¥Ã¥¯
 					flag = true
@@ -70,17 +75,26 @@
 					if flag
 						#¾ÊÎ¬¤µ¤ì¤¿½ªÎ»¥¿¥°¤ò½ÐÎÏ
 						while t=tag.pop
-							c = EndTag.new(t)
-							doc.push c
+              doc.push EndTag.new(t)
 							if t==e.name
 								break
 							end
 						end
 					end
-				else
 				end
+
+        if NOT_FLOW.include?(e.name)
+          while CONTENT_FLOW.include?(tag.last) || CONTENT_INLINE.include?(tag.last)
+            doc.push EndTag.new(tag.pop)
+          end
+        elsif BLOCK.include?(e.name)
+          while CONTENT_INLINE.include?(tag.last)
+            doc.push EndTag.new(tag.pop)
+          end
+        end
+
 				#
-				tag.push e.name
+        tag.push e.name if e.is_a?(StartTag)
 				doc.push e
 			when EndTag
 				if tag.include?(e.name)
@@ -88,19 +102,11 @@
 						if t==e.name
 							break
 						else
-							c = EndTag.new(t)
-							doc.push c
+              doc.push EndTag.new(t)
 						end
 					end
-				else
+          doc.push e
 				end
-				doc.push e
-			when CharacterData
-				doc.push e
-			when Declaration
-				doc.push e
-			when Comment
-				doc.push e
 			else
 				doc.push e
 			end
diff -Naur htmlrepair-1.0.1.orig/repairtest.rb htmlrepair-1.0.1/repairtest.rb
--- htmlrepair-1.0.1.orig/repairtest.rb	2004-10-24 15:57:46.505917208 +0900
+++ htmlrepair-1.0.1/repairtest.rb	1970-01-01 09:00:00.000000000 +0900
@@ -1,15 +0,0 @@
-#/usr/bin/ruby
-
-require "htmlsplit"
-require "htmlrepair"
-
-#¥Æ¥¹¥È½èÍý
-html = open(ARGV[0]).read
-out = open(ARGV[1],"w")
-obj = HTMLSplit.new(html)
-obj.repair
-
-obj.document.each {|e|
-	out.write e.to_s
-}
-out.close
diff -Naur htmlrepair-1.0.1.orig/test/repair-test.rb htmlrepair-1.0.1/test/repair-test.rb
--- htmlrepair-1.0.1.orig/test/repair-test.rb	1970-01-01 09:00:00.000000000 +0900
+++ htmlrepair-1.0.1/test/repair-test.rb	2004-10-24 20:51:20.141237632 +0900
@@ -0,0 +1,130 @@
+
+# htmlrepair¤Î¥Æ¥¹¥È
+#     htmlrepair -- http://www.moonwolf.com/ruby/
+
+require "test/unit/testcase"
+require "test/unit/ui/console/testrunner"
+
+require "htmlrepair.rb"
+
+class HtmlRepairTest < Test::Unit::TestCase
+  # ½ªÎ»¥¿¥°¤Î¾ÊÎ¬
+  def test11
+    html = "<html>"
+    r = HTMLSplit.new(html); r.repair
+    assert_equal("<html></html>", r.document.to_s)
+  end
+
+  def test12
+    html = "<p>a<p>b"
+    r = HTMLSplit.new(html); r.repair
+    assert_equal("<p>a</p><p>b</p>", r.document.to_s)
+  end
+
+  # p¥¿¥°¡ÊÆâÍÆ¤¬%inline¡Ë¤Ïul¤ÎÁ°¤Ç½ªÎ»
+  # v1.0.1¤Ï¼ºÇÔ¡£   => <p>hoge<ul>foo<li>funi</li></ul></p>
+  def test21
+    html = "<p>hoge<ul>foo<li>funi</ul>"
+    r = HTMLSplit.new(html); r.repair
+    assert_equal("<p>hoge</p><ul>foo<li>funi</li></ul>", r.document.to_s)
+  end
+
+  # v1.0.1¤Ï¼ºÇÔ   =>   <p>hoge<table></table></p>
+  def test23
+    html = "<p>hoge<table></table>"
+    r = HTMLSplit.new(html); r.repair
+    assert_equal("<p>hoge</p><table></table>", r.document.to_s)
+  end
+
+  # li¤Î»Ò¤Ï%flow¤À¤¬¡¢%flow¤Ëli¤Ï´Þ¤Þ¤ì¤Ê¤¤¡£
+  def test31
+    html = "<ul><li>a<li>b<li>c</ul>"
+    r = HTMLSplit.new(html); r.repair
+    assert_equal("<ul><li>a</li><li>b</li><li>c</li></ul>", r.document.to_s)
+  end
+
+  # ul¤Ï%block¤Ç¡¢li¤Î»Ò¤Ë¤Ê¤ì¤ë
+  def test32
+    html = "<li><ul>foo</li>"
+    r = HTMLSplit.new(html); r.repair
+    assert_equal("<li><ul>foo</ul></li>", r.document.to_s)
+  end
+
+  # p (%block) ¤Ïdiv¤Î»Ò¤Ë¤Ê¤ì¤ë
+  def test33
+    html = "<div><p>foo</div>"
+    r = HTMLSplit.new(html); r.repair
+    assert_equal("<div><p>foo</p></div>", r.document.to_s)
+  end
+
+  # Æþ¤ì»Ò¤Ë¤Ê¤ë
+  # v1.0.1¤Ï¼ºÇÔ¡£
+  def test33a
+    html = "<fieldset><legend>hoge<fieldset>hogehoge"
+    r = HTMLSplit.new(html); r.repair
+    assert_equal("<fieldset><legend>hoge</legend><fieldset>hogehoge</fieldset></fieldset>",
+                 r.document.to_s)
+  end
+
+  # Íè¤Æ¤Ï¤Ê¤é¤Ê¤¤¥¿¥°¡£
+  # v1.0.1¤Ï¼ºÇÔ¡£
+  def test34
+    html = "<li>foo<dd>bar"
+    r = HTMLSplit.new(html)
+    r.repair
+    assert_equal("<li>foo</li><dd>bar</dd>", r.document.to_s)
+  end
+
+  # ÃÎ¤é¤Ê¤¤¥¿¥°
+  def test35
+    html = "<foo>hoge<bar>hogehoge"
+    r = HTMLSplit.new(html)
+    r.repair
+    assert_equal("<foo>hoge<bar>hogehoge</bar></foo>", r.document.to_s)
+  end
+
+  # Àµ¤·¤¯Æþ¤ì»Ò¤Ë¤Ê¤Ã¤Æ¤¤¤Ê¤¤
+  # v1.0.1¤Ï¼ºÇÔ  =>  <font>hoge<i>foo</i></font>bar</i>
+  def test41
+    html = "<font>hoge<i>foo</font>bar</i>"
+    r = HTMLSplit.new(html); r.repair
+    assert_equal("<font>hoge<i>foo</i></font>bar", r.document.to_s)
+  end
+
+  # É½
+  def test51
+    html = "<tr><td>foo<tr><td>bar"
+    r = HTMLSplit.new(html); r.repair
+    assert_equal("<tr><td>foo</td></tr><tr><td>bar</td></tr>", r.document.to_s)
+  end
+
+  # v1.0.1¤Ï¼ºÇÔ  <th>foo<td>bar</td></th>
+  def test52
+    html = "<th>foo<td>bar"
+    r = HTMLSplit.new(html); r.repair
+    assert_equal("<th>foo</th><td>bar</td>", r.document.to_s)
+  end
+
+  # empty tag
+  # v1.0.1¤Ï¼ºÇÔ  <p>foo<hr>bar</p>
+  def test61
+    html = "<p>foo<hr>bar"
+    r = HTMLSplit.new(html); r.repair
+    assert_equal("<p>foo</p><hr>bar", r.document.to_s)
+  end
+
+  # v1.0.1¤Ï¼ºÇÔ
+  def test71
+    html = "<select><optgroup><option>foo<option>bar<optgroup><option>baz</select>"
+    r = HTMLSplit.new(html); r.repair
+    assert_equal("<select><optgroup><option>foo</option><option>bar</option></optgroup><optgroup><option>baz</option></optgroup></select>", r.document.to_s)
+  end
+
+  def test72
+    html = "<a>foo<a>bar"
+    r = HTMLSplit.new(html); r.repair
+    assert_equal("<a>foo</a><a>bar</a>", r.document.to_s)
+  end
+end
+
+Test::Unit::UI::Console::TestRunner.new(HtmlRepairTest).start()
