Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserve white space with HtmlAgilityPack

I'm trying to highlight a text in the html string by using Html Agility Pack. I'm able to replace text with <span class="highlight">, but when I replace the text, the white space around the span tag is disappeared. For example, if the text is "This text will be highlighted", it results as "This text will be<span class='highlighted'>highlighted</span>", and white space gone before the span tag. This merges the words before and after the span with the span text. I simply do a recursive loop like that:

  1. Get first child node
  2. if the node is #text, than node.InnerHtml = InnerText.Replace(search_term, span_code)
  3. if node has child node goto step 1
  4. goto next sibling then go to step 1

Then I get the InnerHtml of the HtmlDocument as a result. I tried put space before <span and after </span>, but it removed them. I tried HtmlDocument.OptionWriteEmptyNodes = true; it didn't work either. I replaced all "\n" and "\t" chars with a space before creating HtmlDocument and after getting the html string, and it didn't affect neither.

How can I preserve the white space when using Html Agility Pack?

like image 592
oruchreis Avatar asked Oct 10 '22 16:10

oruchreis


1 Answers

Actually HtmlDocument.OptionWriteEmptyNodes = true; did what I want. I realized now.

like image 200
oruchreis Avatar answered Oct 13 '22 12:10

oruchreis