def self.fragment(*args)
doc = parse(*args)
fragment = Nokogiri::HTML::DocumentFragment.new(doc)
if doc.children.length != 1 or doc.children.first.name != 'html'
fragment = doc
else
children = doc.children.first.children
if children.length > 0 and doc.children.first.name = 'head'
fragment << children.shift.children
end
if children.length > 0 and doc.children.first.name = 'body'
fragment << children.shift.children
fragment << children
elsif children.length > 0 and doc.children.last.name = 'body'
body = children.pop
fragment << children
fragment << body.children
else
fragment << children
end
end
fragment
end