如何在C#中的Webbrowser控件中将InnerText转换为InnerHtml?

时间:2010-03-13 22:36:10

标签: c# controls browser wysiwyg spell-checking

我正在使用内置拼写检查程序Hunspell的WYSIWYG编辑器以及拼写错误单词的在线突出显示。我正在使用Webbrowser控件作为html处理程序。在webbrowser控件中,这是一种比html更容易拼写检查文本的方法,但是按照这种方式我丢失了所有的html格式。 所以问题是:有没有办法拼写检查body innertext然后用以前的格式将其转换为body innerhtml? (不使用HtmlAgilityPackMajestic12SgmlReaderZetaHtmlTidy)。

提前致谢。

2 个答案:

答案 0 :(得分:1)

与检查给定元素的innterText属性的拼写相反,更好的方法可能是遍历子元素,并检查每个孩子的innerText的拼写。

这种方法虽然可能限制基于上下文的拼写检查,但应保持标记不变。

注意:您可能需要考虑每个子节点还包含其他子节点。

答案 1 :(得分:0)

我选择检查innerText属性的拼写,但是当替换任何更改的单词时,我在innerHTML中替换它们。更改拼写错误的单词的所有实例时,这很容易。只需使用正则表达式来收集innerHTML中所有匹配单词的索引并替换每个单词。

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);

当替换单个实例时,我只是通过InnerText循环查找需要替换的实例。然后我循环使用InnerHTML,直到找到正确的实例并替换它。

相关问题