Skip to content

Commit

Permalink
fixing html encoding tests and fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Apr 28, 2010
1 parent 09d48a7 commit c23eb88
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
37 changes: 28 additions & 9 deletions lib/nokogiri/html/document_fragment.rb
@@ -1,17 +1,36 @@
module Nokogiri
module HTML
class DocumentFragment < Nokogiri::XML::DocumentFragment

class << self
####
# Create a Nokogiri::XML::DocumentFragment from +tags+
def parse tags
doc = HTML::Document.new
doc.encoding = 'UTF-8'
self.new(doc, tags)
end
####
# Create a Nokogiri::XML::DocumentFragment from +tags+
def self.parse tags
doc = HTML::Document.new
doc.encoding = 'UTF-8'
self.new(doc, tags)
end

def initialize document, tags = nil, ctx = nil
return self unless tags

children = if ctx
ctx.parse("<div>#{tags.strip}</div>").first.children
else
###
# This is a horrible hack, but I don't care
if tags.strip =~ /^<body/i
path = "/html/body"
else
path = "/html/body/node()"
end

HTML::Document.parse(
"<html><body>#{tags.strip}</body></html>",
nil,
document.encoding
).xpath(path)
end
children.each { |child| child.parent = self }
end
end
end
end
15 changes: 3 additions & 12 deletions lib/nokogiri/xml/document_fragment.rb
Expand Up @@ -11,19 +11,10 @@ def initialize document, tags = nil, ctx = nil
return self unless tags

children = if ctx
if document.html?
ctx.parse("<div>#{tags.strip}</div>").first.children
else
ctx.parse(tags.strip)
end
ctx.parse(tags.strip)
else
if document.html?
Nokogiri::HTML::Document.parse("<html><body>#{tags.strip}</body></html>") \
.xpath("/html/body/node()")
else
Nokogiri::XML::Document.parse("<root>#{tags.strip}</root>") \
.xpath("/root/node()")
end
XML::Document.parse("<root>#{tags.strip}</root>") \
.xpath("/root/node()")
end
children.each { |child| child.parent = self }
end
Expand Down
3 changes: 2 additions & 1 deletion lib/nokogiri/xml/node.rb
Expand Up @@ -416,7 +416,8 @@ def matches? selector
# Create a DocumentFragment containing +tags+ that is relative to _this_
# context node.
def fragment tags
Nokogiri::XML::DocumentFragment.new(document, tags, self)
type = document.html? ? Nokogiri::HTML : Nokogiri::XML
type::DocumentFragment.new(document, tags, self)
end

###
Expand Down

0 comments on commit c23eb88

Please sign in to comment.