Like leaves on the wind
-
Make your way to IRB within the project root directory
$ cd to/this/directory $ irb
-
Read the HTML file
html = File.read('test.html')
-
Pass the returned string to the
DOMReader
- NOTE: this will automatically build the tree from the string
d = DOMReader.new(html)
-
Want a quick way to verify the rendered output is the same as the HTML string?
html.gsub(/\s/, '') == d.render.gsub(/\s/, '')
-
Render the tree to a string
puts d.render
-
Search for nodes by
:element
,:text
, or an attributed.search_by(:element, 'html') d.search_by(:text, 'One h1') d.search_by(:id, 'main-area') d.search_by(:class, 'top-div')
-
Search all the descendents of a node with
#search_children
d.search_children(d.root, :element, 'li')
-
Search only direct children of a node with
#search_ancestors
d.search_ancestors(d.root, :element, 'head')