Here is what I have:
- DOM stored as text
- I have the string index of the area I want to get the parent node of, the index may or may not be the beginning of a tag (it will never be a partial of a tag, as it is a user selection
- I also have the htmltext at the index (obviously)
This is as far as I’ve gotten:
doc = Nokogiri::HTML(content.body)
I know nokogiri can do xpath things, but I don’t know if xpath can do standard text searches? the selection text could span multiple nodes, and I think that breaks xpath searching o.o
I’m using Ruby 1.8.7, and rails 2.3.8
There is no correlation between the index in a particular serialization of the XML Document and an element. The closest you could do:
Recursively, at each level of the DOM, serialize the element and see if its length (added to what you have so far) has reached your index.
Unfortunately this is not guaranteed to work, since:
Many different (non-canonical) serializations are possible that describe the same XML document (e.g.
foo="You said, "Hi!""vs.foo='You said, "Hi!"').Depending on whether you consider blank whitespace nodes as significant, two different XML documents might be treated the same (e.g.
<foo><bar>vs.<foo>\n\t<bar>)In HTML, additional non-significant whitespace might be stripped (e.g.
<p>a b</p>vs.<p>a b</p>).