Note. firefox (in my test) does not stand out in text areas as you describe. In particular, this code:
<textarea cols="80" rows="10" id="1"></textarea> <script> elem = document.getElementById("1"); elem.value = '\ <Configuration>\n\ <Validator Expression="[^<]" />\n\ </Configuration>\ ' alert(elem.value); </script>
A warning is issued and displayed to the user without changes:
<Configuration> <Validator Expression="[^<]" /> </Configuration>
So, there may be one (non-viable?) Solution for your users to use firefox.
It seems that two parts of your question have been uncovered:
1 XML that you display is not displayed.
For example, " < " does not appear as "<". But since "<" is also not displayed as "<", information is lost and you cannot return it.
One solution is to delete all the characters & , so that < becomes &lt; The text box will not display as " < ". When you read this, it will be as it was in the first place. (I assume that textarea actually modifies the string, but firefox doesn't behave the way you report, so I can't verify this)
Another solution (mentioned already I think) is to create / buy / borrow a custom text area (not bad if simple, but there are all the editing keys, ctrl-C, ctrl-shift-left, etc.).
2 You would like users to not have to worry about shielding.
You're on the run-hell:
Replacing regular expressions will work mostly ... but how can you reliably determine the final quote (") when the user can (legally, within the conditions that you specify) enter:
<Configuration> <Validator Expression="[^"<]" /> </Configuration>
Looking at this in terms of regular expression syntax, he also cannot determine if final is “part of a regular expression or its end”. Regex syntax usually solves this problem with an explicit terminator, for example:
/[^"<]/
If users used this syntax (with a terminator), and you wrote a parser for it, you can determine when the regular expression ended, and therefore the next “character” is not part of the regular expression, but is part of XML and therefore what parts should to be shielded. I am not saying that you need it! I say this theoretically, it's pretty far from fast and dirty.
BTW: the same problem occurs for text inside an element. In accordance with the conditions that you have indicated, the following is acceptable:
<Configuration> <Expression></Expression></Expression> </Configuration>
The basic rule in the syntax that allows "any text" is that the delimiter must be escaped (for example, "or <)), so that the end can be recognized. In most syntaxes, there are also a bunch of other things, convenience / inconvenience. ( EDIT it will need to run for the escape character itself: for XML it is " & ", which when the literal is escaped as " & ". For regular expression it is C / unix -style " \ " which when the literal is escaped as " \\ ").
Nest syntax, and you're on the run-hell.
One simple solution for you is to tell your users: this is a quick and dirty configuration editor, so you don't get any “no need to run” fantasies "" mamby-pamby:
- A list of characters and screens to the text area, for example: "<" in the form of "
< ". - For XML that will not validate, show them the list again.
Looking back, I see that bobince gave me the same basic answer.