We've recently added an extension method (string.Highlight(string target)
) to wrap occurences of the target text with <span class="highlighted"></span>
, and are using it for all text displayed on the page.
The initial issue we ran into was that rather than the text being wrapped in the tag, the text was wrapped in plaintext "<span clas..."
. We've managed to resolve this with the exception of text within a link.
<%= Html.ActionLink(linkText.Highlight(word), action) %>
This sticks the text "<span class..."
into the link, which is not what we want. Is there a way to apply our highlighting class to some of the text within the link, or should we forget about it?
The extension method:
public static string Highlight(this string text, this string target)
{
return text.Replace(target, @"<span class=""highlighted"">" + target + "</span>";
}