/// <summary> /// Spell check the content of an XML reader /// </summary> /// <param name="reader">The reader to spell check</param> /// <returns>False if it was canceled or True if it completed.</returns> public bool SpellCheckXmlReader(XmlReader reader) { IXmlLineInfo lineInfo = (IXmlLineInfo)reader; TextLocation location = new TextLocation(); bool wasCancelled = false; int idx; reader.MoveToContent(); while(!reader.EOF && !wasCancelled) { switch(reader.NodeType) { case XmlNodeType.Element: location.Line = lineInfo.LineNumber; location.Column = lineInfo.LinePosition; if(reader.HasAttributes) { for(idx = 0; idx < reader.AttributeCount; idx++) { reader.MoveToAttribute(idx); if(SpellCheckerConfiguration.SpellCheckedXmlAttributes.Contains(reader.LocalName)) { // Set the approximate position of the value assuming the format: // attrName="value" location.Line = lineInfo.LineNumber; location.Column = lineInfo.LinePosition + reader.Settings.LinePositionOffset + reader.Name.Length + 2; // The value must be encoded to get an accurate position (quotes excluded) wasCancelled = !this.SpellCheckInternal(WebUtility.HtmlEncode( reader.Value).Replace(""", "\"").Replace("'", "'"), location); } } reader.MoveToElement(); } // Is it an element in which to skip the content? if(SpellCheckerConfiguration.IgnoredXmlElements.Contains(reader.LocalName)) { reader.Skip(); continue; } break; case XmlNodeType.Comment: case XmlNodeType.CDATA: location.Line = lineInfo.LineNumber; location.Column = lineInfo.LinePosition; wasCancelled = !this.SpellCheckInternal(reader.Value, location); break; case XmlNodeType.Text: location.Line = lineInfo.LineNumber; location.Column = lineInfo.LinePosition; // The value must be encoded to get an accurate position (quotes excluded) wasCancelled = !this.SpellCheckInternal(WebUtility.HtmlEncode(reader.Value).Replace( """, "\"").Replace("'", "'"), location); break; default: break; } reader.Read(); } return wasCancelled; }
/// <summary> /// Spell check the content of an XML reader /// </summary> /// <param name="reader">The reader to spell check</param> /// <returns>False if it was canceled or True if it completed.</returns> public bool SpellCheckXmlReader(XmlReader reader) { IXmlLineInfo lineInfo = (IXmlLineInfo)reader; TextLocation location = new TextLocation(); bool wasCancelled = false; int idx; reader.MoveToContent(); while (!reader.EOF && !wasCancelled) { switch (reader.NodeType) { case XmlNodeType.Element: location.Line = lineInfo.LineNumber; location.Column = lineInfo.LinePosition; if (reader.HasAttributes) { for (idx = 0; idx < reader.AttributeCount; idx++) { reader.MoveToAttribute(idx); if (SpellCheckerConfiguration.SpellCheckedXmlAttributes.Contains(reader.LocalName)) { // Set the approximate position of the value assuming the format: // attrName="value" location.Line = lineInfo.LineNumber; location.Column = lineInfo.LinePosition + reader.Settings.LinePositionOffset + reader.Name.Length + 2; // The value must be encoded to get an accurate position (quotes excluded) wasCancelled = !this.SpellCheckInternal(WebUtility.HtmlEncode( reader.Value).Replace(""", "\"").Replace("'", "'"), location); } } reader.MoveToElement(); } // Is it an element in which to skip the content? if (SpellCheckerConfiguration.IgnoredXmlElements.Contains(reader.LocalName)) { reader.Skip(); continue; } break; case XmlNodeType.Comment: case XmlNodeType.CDATA: location.Line = lineInfo.LineNumber; location.Column = lineInfo.LinePosition; wasCancelled = !this.SpellCheckInternal(reader.Value, location); break; case XmlNodeType.Text: location.Line = lineInfo.LineNumber; location.Column = lineInfo.LinePosition; // The value must be encoded to get an accurate position (quotes excluded) wasCancelled = !this.SpellCheckInternal(WebUtility.HtmlEncode(reader.Value).Replace( """, "\"").Replace("'", "'"), location); break; default: break; } reader.Read(); } return(wasCancelled); }
/// <summary> /// Spell check the given text /// </summary> /// <param name="text">The text to spell check</param> /// <param name="location">The location of the text being spell checked</param> /// <returns>False if it was canceled or True if it completed.</returns> private bool SpellCheckInternal(string text, TextLocation location) { SpellingEventArgs se = null; List<Match> xmlTags = null; TextLocation priorWord = new TextLocation(); string currentWord; int textIdx; // Signal the start and allow it to be canceled CancelEventArgs ce = new CancelEventArgs(); this.OnSpellCheckTextStarting(ce); if(ce.Cancel) { this.OnSpellCheckTextCancelled(EventArgs.Empty); return false; } // Note the location of all XML elements if needed if(SpellCheckerConfiguration.IgnoreXmlElementsInText) xmlTags = reXml.Matches(text).OfType<Match>().ToList(); // Spell check each word in the given text foreach(var word in GetWordsInText(text)) { currentWord = text.Substring(word.Start, word.Length); textIdx = word.Start; if(!IsProbablyARealWord(currentWord) || (xmlTags != null && xmlTags.Count != 0 && xmlTags.Any(match => textIdx >= match.Index && textIdx <= match.Index + match.Length - 1))) continue; if(!dictionary.ShouldIgnoreWord(currentWord) && !dictionary.IsSpelledCorrectly(currentWord)) { // Sometimes it flags a word as misspelled if it ends with "'s". Try checking the word // without the "'s". If ignored or correct without it, don't flag it. This appears to be // caused by the definitions in the dictionary rather than Hunspell. if(currentWord.EndsWith("'s", StringComparison.OrdinalIgnoreCase)) { currentWord = currentWord.Substring(0, currentWord.Length - 2); if(dictionary.ShouldIgnoreWord(currentWord) || dictionary.IsSpelledCorrectly(currentWord)) continue; currentWord += "'s"; } se = new SpellingEventArgs(currentWord, location.ToPoint(text, textIdx)); this.OnMisspelledWord(se); if(se.Cancel) break; } else if(priorWord.Length != 0 && String.Compare(text.Substring(priorWord.Start, priorWord.Length), currentWord, StringComparison.OrdinalIgnoreCase) == 0 && IsAllWhitespace(text, priorWord.Start + priorWord.Length, word.Start - 1)) { se = new SpellingEventArgs(currentWord, location.ToPoint(text, textIdx)); this.OnDoubledWord(se); if(se.Cancel) break; } priorWord = word; } if(se != null && se.Cancel) { this.OnSpellCheckTextCancelled(EventArgs.Empty); return false; } this.OnSpellCheckTextCompleted(EventArgs.Empty); return true; }
/// <summary> /// Spell check the given text /// </summary> /// <param name="text">The text to spell check</param> /// <param name="location">The location of the text being spell checked</param> /// <returns>False if it was canceled or True if it completed.</returns> private bool SpellCheckInternal(string text, TextLocation location) { SpellingEventArgs se = null; List <Match> xmlTags = null; TextLocation priorWord = new TextLocation(); string currentWord; int textIdx; // Signal the start and allow it to be canceled CancelEventArgs ce = new CancelEventArgs(); this.OnSpellCheckTextStarting(ce); if (ce.Cancel) { this.OnSpellCheckTextCancelled(EventArgs.Empty); return(false); } // Note the location of all XML elements if needed if (SpellCheckerConfiguration.IgnoreXmlElementsInText) { xmlTags = reXml.Matches(text).OfType <Match>().ToList(); } // Spell check each word in the given text foreach (var word in GetWordsInText(text)) { currentWord = text.Substring(word.Start, word.Length); textIdx = word.Start; if (!IsProbablyARealWord(currentWord) || (xmlTags != null && xmlTags.Count != 0 && xmlTags.Any(match => textIdx >= match.Index && textIdx <= match.Index + match.Length - 1))) { continue; } if (!dictionary.ShouldIgnoreWord(currentWord) && !dictionary.IsSpelledCorrectly(currentWord)) { // Sometimes it flags a word as misspelled if it ends with "'s". Try checking the word // without the "'s". If ignored or correct without it, don't flag it. This appears to be // caused by the definitions in the dictionary rather than Hunspell. if (currentWord.EndsWith("'s", StringComparison.OrdinalIgnoreCase)) { currentWord = currentWord.Substring(0, currentWord.Length - 2); if (dictionary.ShouldIgnoreWord(currentWord) || dictionary.IsSpelledCorrectly(currentWord)) { continue; } currentWord += "'s"; } se = new SpellingEventArgs(currentWord, location.ToPoint(text, textIdx)); this.OnMisspelledWord(se); if (se.Cancel) { break; } } else if (priorWord.Length != 0 && String.Compare(text.Substring(priorWord.Start, priorWord.Length), currentWord, StringComparison.OrdinalIgnoreCase) == 0 && IsAllWhitespace(text, priorWord.Start + priorWord.Length, word.Start - 1)) { se = new SpellingEventArgs(currentWord, location.ToPoint(text, textIdx)); this.OnDoubledWord(se); if (se.Cancel) { break; } } priorWord = word; } if (se != null && se.Cancel) { this.OnSpellCheckTextCancelled(EventArgs.Empty); return(false); } this.OnSpellCheckTextCompleted(EventArgs.Empty); return(true); }