我選擇檢查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循環,直到找到正確的實例並將其替換。
你可以發表一個代碼示例,當你使用所述控件時,結果是什麼,失去格式? – 2010-03-13 22:38:46
我正在使用來自hunspell網站的示例代碼片段。我可以突出顯示拼寫錯誤的單詞,但是我不能在innerhtml中做到這一點,而不會丟失它的格式。說實話,解析和拼寫檢查innerhtml是一個屁股疼痛(選擇包裹在標籤中的單詞,在另一個標籤中拼寫拼寫錯誤的單詞以突出顯示它們,還有一個單詞可以用幾個標籤分隔)。這就是爲什麼我問的簡單方法將HTML轉換爲文本。 – 2010-03-13 23:09:02