I need to implement auto-capitalization inside of a Telerik RadEditor control on an ASPX page as a user types.
This can be an IE specific solution (IE6+).
I currently capture every keystroke (down/up) as the user types to support a separate feature called "macros" that are essentially short keywords that expand into formatted text. i.e. the macro "so" could auto expand upon hitting spacebar to "stackoverflow".
That said, I have access to the keyCode information, as well I am using the TextRange methods to select a word ("so") and expanding it to "stackoverflow". Thus, I have some semblence of context.
However, I need to check this context to know whether I should auto-capitalize. This also needs to work regardless of whether a macro is involved.
Since I'm monitoring keystrokes for the macros, should I just monitor for punctuation (it's more than just periods that signal a capital letter) and auto-cap the next letter typed, or should I use TextRange and analyze context?
I'm not sure if this is what you're trying to do, but here is a function (reference) to convert a given string to title case:
function toTitleCase(str) {
return str.replace(/([\w&`'‘’"“.@:\/\{\(\[<>_]+-? *)/g, function(match, p1, index, title){ // ' fix syntax highlighting
if (index > 0 && title.charAt(index - 2) != ":" &&
match.search(/^(a(nd?|s|t)?|b(ut|y)|en|for|i[fn]|o[fnr]|t(he|o)|vs?\.?|via)[ -]/i) > -1)
return match.toLowerCase();
if (title.substring(index - 1, index + 1).search(/['"_{([]/) > -1)
return match.charAt(0) + match.charAt(1).toUpperCase() + match.substr(2);
if (match.substr(1).search(/[A-Z]+|&|[\w]+[._][\w]+/) > -1 ||
title.substring(index - 1, index + 1).search(/[\])}]/) > -1)
return match;
return match.charAt(0).toUpperCase() + match.substr(1);
});
}
Have you tried to apply the text-transform CSS style to your controls?
Sometimes, not to do it is the right answer to a coding problem.
I really would NOT do this, unless you feel you can write a script to correctly set the case in the following sentence, if you were to first convert it to lowercase and pass it into the script.
Jean-Luc "The King" O'Brien MacHenry van d'Graaf IIV (PhD, OBE), left his Macintosh with in Macdonald's with his friends MacIntosh and MacDonald. Jesus gave His Atari ST at AT&T's "Aids for AIDS" gig in St George's st, with Van Halen in van Henry's van, performing The Tempest.
You have set yourself up for a fall by trying to create a Natural Language Parser. You can never do this as well as the user will. At best, you can do an approximation, and give the user the ability to edit and force a correction when you get it wrong. But often in such cases, the editing is more work than just doing it manually and right in the first place.
That said, if you have the space and power to store and search a large n-gram corpus of suitably capitalized words, you would at least be able to have a wild stab at the most likely desired case.
You pose an interesting question. Acting upon each key press may be more limiting because you will not know what comes immediately after a given keycode (the complexity of undoing a reaction that turns out to be incorrect could mean having to go to a TextRange-based routine anyway). Granted, I haven't wrestled with code on this problem to date, so this is a hypothesis in my head.
At any length, here's a Title Casing function (java implementation inspired by a John Gruber blogging automation) which may spur ideas when it comes to handling the actual casing code:
http://individed.com/code/to-title-case/