Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixing html encoding tests and fragments
- Loading branch information
1 parent
09d48a7
commit c23eb88
Showing
3 changed files
with
33 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters