Alex Navasardyan
Alex Navasardyan

Reputation: 536

How to convert InnerText to InnerHtml in Webbrowser Control in C#?

I'm working on a WYSIWYG editor with builtin spell checker Hunspell and online highlighting of misspelled words. I'm using Webbrowser control as a html handler. It's a way easy to spell check text than html in webbrowser control, but following this way I'm losing all html formatting. So the question is: is there any way to spell check body innertext and then convert it to body innerhtml with previous formatting? (with no use of HtmlAgilityPack or Majestic12 or SgmlReader or ZetaHtmlTidy).

Thanks in advance.

Upvotes: 3

Views: 2226

Answers (2)

Alex
Alex

Reputation: 68

I chose to check the spelling of the innerText property, but when replacing any changed words, I replaced them within the innerHTML. This was rather easy when changing all instances of a misspelled word. Simply use a Regular Expression to gather the indices of all matching words in the innerHTML and replace each one.

Regex wordEx = new Regex(@"[A-Za-z]", RegexOptions.Compiled);
MatchCollection mcol = wordEx.Matches(webEditor.Document.Body.InnerHtml);

foreach (Match m in mcol)
{
  //Basic checking for whether this word is an HTML tag. This is not perfect.
  if (m.Value == e.Word && webEditor.Document.Body.InnerHtml.Substring(m.Index -1, 1) != "<")
  {
    wordIndeces.Add(m.Index);
  }
}

foreach (int curWordTextIndex in wordIndeces)
{
   Word word = Word.GetWordFromPosition(webEditor.Document.Body.InnerHtml, curWordTextIndex);
   string tmpText = webEditor.Document.Body.InnerHtml.Remove(word.Start, word.Length);
   webEditor.Document.Body.InnerHtml = tmpText.Insert(word.Start, e.NewWord);
}

UpdateSpellingForm(e.TextIndex);

When replacing a single instance, I just looped through the InnerText to find which instance needs to be replaced. Then I looped through the InnerHTML until I found the correct instance and replaced it.

Upvotes: 0

gpmcadam
gpmcadam

Reputation: 6550

As opposed to checking the spelling of the innterText property of a given element, a better approach might be to loop through the child elements, and check the spelling of each child's innerText instead.

This approach, while possibly limiting context-based spell-checking, should keep the markup intact.

Note: You might want to take into consideration that each child node may also contain further children.

Upvotes: 1

Related Questions