:
- XML!= HTML, , html , XML .
- html , (escape html ).
- , , ( ).
, html- '::
function removeHtmlTags( text, initPos )
{
if (undefined===removeHtmlTags.htmlTagRegexp)
{
removeHtmlTags.htmlTagRegexp = new RegExp('</?(?:article|aside|bdi|command|'+
'details|dialog|summary|figure|figcaption|footer|header|hgroup|mark|'+
'meter|nav|progress|ruby|rt|rp|section|time|wbr|audio|'+
'video|source|embed|track|canvas|datalist|keygen|output|'+
'!--|!DOCTYPE|a|abbr|address|area|b|base|bdo|blockquote|body|'+
'br|button|canvas|caption|cite|code|col|colgroup|dd|del|dfn|div|'+
'dl|dt|em|embed|fieldset|figcaption|figure|footer|form|h1|h2|h3|h4|'+
'h5|h6|head|hr|html|i|iframe|img|input|ins|kdb|keygen|label|legend|'+
'li|link|map|menu|meta|noscript|object|ol|optgroup|option|p|param|'+
'pre|q|s|samp|script|select|small|source|span|strong|style|sub|'+
'sup|table|tbody|td|textarea|tfoot|th|thead|title|tr|u|ul|var|'+
'acronym|applet|basefont|big|center|dir|font|frame|'+
'frameset|noframes|strike|tt)(?:(?: [^<>]*)>|>?)', 'i');
}
var thereIsMore=true;
removeHtmlTags.htmlTagRegexp.lastIndex=0;
while (thereIsMore)
{
var str = text.match(removeHtmlTags.htmlTagRegexp);
if ( str!=null)
{
text = text.replace(str[0], '');
if (str.index < initPos)
initPos= Math.max(initPos-str[0].length,str.index);
}
else thereIsMore = false;
}
if (initPos<0) initPos=0;
return {text: text, pos: initPos};
}
:
, :
'<div>' -> ''
'<div selected' -> ' selected'
'<div selected>' -> ''
'<div >' -> ''
-, , / carret, textarea reset. , , - .
function caretPosition(node, value)
{
var caretPos = 0;
value = parseInt(value);
if (!isNaN(value))
{
if (node.selectionStart)
{
node.selectionStart=value;
node.selectionEnd= value;
}
else if(node.setSelectionRang)
{
node.focus();
node.setSelectionRange(value, value);
}
else if (node.createTextRange)
{
var range = node.createTextRange();
range.collapse(true);
range.moveEnd('character', value);
range.moveStart('character', value);
range.select();
}
}
if (node.selectionStart) return node.selectionStart;
else if (document.selection)
{
node.focus();
var sel = document.selection.createRange();
sel.moveStart('character', -node.value.length);
caretPos = sel.text.length;
}
return caretPos;
}
-, html- carret.
function updateText()
{
var t = document.getElementById('text');
var pos = caretPosition(t);
var result = removeHtmlTags(t.value, pos);
t.value = result.text;
caretPosition(t, result.pos);
}
, :
"oninput" , (ofc) IE .
HTML:
<html>
<head>
<script type="text/javascript">
</script>
</head>
<body>
<textarea cols="50" rows="10" oninput="updateText();"
ondrop="setTimeout('updateText();',0);"
onpaste="setTimeout('updateText();',0);"
onkeyup="updateText();" id='text'></textarea>
</body>
</html>
, :-)
Escain