Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript: Given an offset and substring length in an HTML string, what is the parent node?
    text
    copied!<p>My current project requires locating an array of strings within an element's text content, then wrapping those matching strings in <code>&lt;a&gt;</code> elements using JavaScript (requirements simplified here for clarity). I need to avoid jQuery if at all possible - at least including the full library.</p> <p>For example, given this block of HTML:</p> <pre><code>&lt;div&gt; &lt;p&gt;This is a paragraph of text used as an example in this Stack Overflow question.&lt;/p&gt; &lt;/div&gt; </code></pre> <p>and this array of strings to match:</p> <pre><code>['paragraph', 'example'] </code></pre> <p>I would need to arrive at this:</p> <pre><code>&lt;div&gt; &lt;p&gt;This is a &lt;a href="http://www.example.com/"&gt;paragraph&lt;/a&gt; of text used as an &lt;a href="http://www.example.com/"&gt;example&lt;/a&gt; in this Stack Overflow question.&lt;/p&gt; &lt;/div&gt; </code></pre> <p>I've arrived at a solution to this by using the <code>innerHTML()</code> method and some string manipulation - basically using the offsets (via <code>indexOf()</code>) and lengths of the strings in the array to break the HTML string apart at the appropriate character offsets and insert <code>&lt;a href="http://www.example.com/"&gt;</code> and <code>&lt;/a&gt;</code> tags where needed.</p> <p>However, an additional requirement has me stumped. I'm not allowed to wrap any matched strings in <code>&lt;a&gt;</code> elements if they're already in one, or if they're a descendant of a heading element (<code>&lt;h1&gt;</code> to <code>&lt;h6&gt;</code>).</p> <p>So, given the same array of strings above and this block of HTML (the term matching has to be case-insensitive, by the way):</p> <pre><code>&lt;div&gt; &lt;h1&gt;Example&lt;/a&gt; &lt;p&gt;This is a &lt;a href="http://www.example.com/"&gt;paragraph of text&lt;/a&gt; used as an example in this Stack Overflow question.&lt;/p&gt; &lt;/div&gt; </code></pre> <p>I would need to disregard both the occurrence of "Example" in the <code>&lt;h1&gt;</code> element, and the "paragraph" in <code>&lt;a href="http://www.example.com/"&gt;paragraph of text&lt;/a&gt;</code>.</p> <p>This suggests to me that I have to determine which node each matched string is in, and then traverse its ancestors until I hit <code>&lt;body&gt;</code>, checking to see if I encounter a <code>&lt;a&gt;</code> or <code>&lt;h_&gt;</code> node along the way.</p> <p>Firstly, does this sound reasonable? Is there a simpler or more obvious approach that I've failed to consider? It doesn't seem like regular expressions or another string-based comparison to find bounding tags would be robust - I'm thinking of issues like self-closing elements, irregularly nested tags, etc. There's also <a href="https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454">this</a>...</p> <p>Secondly, is this possible, and if so, how would I approach it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload