Skip to content

Instantly share code, notes, and snippets.

View Serabe's full-sized avatar

Sergio Arbeo Serabe

View GitHub Profile
sergio@Ystad:~/Programming/projects/nokogiri/lib$ irb1.8 -rubygems -rnokogiri
irb(main):001:0> fruits = Nokogiri::XML(<<-eoxml)
irb(main):002:1" <Fruit xmlns='http://www.fruits.org'>
irb(main):003:1" </Fruit>
irb(main):004:1" eoxml
=> <?xml version="1.0"?>
<Fruit xmlns="http://www.fruits.org">
</Fruit>
irb(main):005:0> apple = fruits.fragment('<Apple/>')
// C code
// the child was a text node that was coalesced. we need to have the object
// point at SOMETHING, or we'll totally bomb out.
if (reparented != node) {
DATA_PTR(node_obj) = reparented ;
}
@Serabe
Serabe / gist:144923
Created July 11, 2009 00:07
Code to implement in Nokogiri4J
/**
* xmlGetNodePath:
* @node: a node
*
* Build a structure based Path for the given node
*
* Returns the new path or NULL in case of error. The caller must free
* the returned string
*/
xmlChar *
def test_document_has_errors
doc = Nokogiri::XML(<<-eoxml)
<foo><bar></foo>
eoxml
assert doc.errors.length > 0
doc.errors.each do |error|
assert_match error.message, error.inspect
assert_match error.message, error.to_s
end
end
[alias]
st = status
ci = commit
co = checkout
svnup = svn fetch
sup = svn fetch
svnci = svn dcommit
sci = svn dcommit
up = pull --rebase
sreb = svn rebase
<?xml version="1.0" ?>
<hi>
<Im>
a not-well-formed XML
</hi>
def test_node_added_to_root_should_get_namespace
fruits = Nokogiri::XML(<<-eoxml)
<Fruit xmlns='http://www.fruits.org'>
</Fruit>
eoxml
apple = fruits.fragment('<Apple/>')
fruits << apple
assert_equal 1, fruits.xpath('//xmlns:Apple').length
end
def test_singleton_methods
assert node_set = @xml.search('//name')
assert node_set.length > 0
node = node_set.first
def node.test
'test'
end
assert node_set = @xml.search('//name')
assert_equal 'test', node_set.first.test
end
irb(main):003:0> DP = Java::OrgCybernekoHtmlParsers::DOMParser
=> Java::OrgCybernekoHtmlParsers::DOMParser
irb(main):004:0> dp = DP.new
=> #<Java::OrgCybernekoHtmlParsers::DOMParser:0x69e94001>
irb(main):005:0> dp.parse "<html><head></head><body></body></html>".to_java_string
NativeException: java.net.MalformedURLException: no protocol: <html><head></head><body></body></html>
from java/net/URL.java:583:in `<init>'
from java/net/URL.java:480:in `<init>'
from java/net/URL.java:429:in `<init>'
from org/cyberneko/html/HTMLScanner.java:862:in `setInputSource'
if Nokogiri.uses_libxml?
def test_namespace_without_an_href_on_html_node
# because microsoft word's HTML formatting does this. ick.
xml = Nokogiri::HTML.parse <<-EOF
<div><o:p>foo</o:p></div>
EOF
assert_not_nil(node = xml.at('p'))
assert_equal 1, node.namespaces.keys.size