📄 weiseditor.js
字号:
WeisEditor.prototype.SaveSend = function(W)
{
Editor.Config.CodeType.Value = W.$('selType').options[W.$('selType').selectedIndex].value;
Editor.Config.CodeType.Text = W.$('selType').options[W.$('selType').selectedIndex].text;
Editor.Config.CodeHTML = W.$('codearea').value;
Editor.Run();
}
WeisEditor.prototype.sourceCode = function(obj)
{
//Editor.Config.SaveSelection();
var hidArea = $(Editor.Config.EditorID+'Area');
var htmlStr = "";
if (Editor.Config.WEditorMode)
{
Editor.Config.HtmlEdit.focus();
WeisEditor.Run.SaveDataStep();
WeisEditor.Run.IsLocked = true;
WeisEditor.Run.ConfigUndo();
if (window.isIE) Editor.clearHTML(true);
Editor.setDisabled(true,obj);
Editor.Config.Document.body.innerHTML = htmlStr = Editor.replaceSPCTags(true);
WeisEditor.Run.SaveDataStep();
hidArea.style.display = '';
$(Editor.Config.EditorID).style.display = 'none';
hidArea.value = htmlStr;
Editor.Config.WEditorMode = false;
hidArea.focus();
}
else
{
WeisEditor.Run.SaveDataStep();
if (window.isIE) Editor.clearHTML(false);
Editor.setDisabled(false,obj);
hidArea.style.display = 'none';
$(Editor.Config.EditorID).style.display = '';
Editor.Config.Document.body.innerHTML = hidArea.value.trim();
WeisEditor.Run.SaveDataStep();
Editor.Config.WEditorMode = true;
Editor.Config.HtmlEdit.focus();
}
}
WeisEditor.prototype.clearHTML = function(A)
{
var hidArea = $(Editor.Config.EditorID+'Area');
var B = A ? Editor.Config.Document.body.innerHTML : hidArea.value.trim();
B = B.replace(/<p[^>]*?>[\s\n\r]*<hr[^>]*?>([\s\S]*?)<\/p>/gi,'<hr />$1');
B = B.replace(/<p[^>]*?>[\s\n\r]*<hr[^>]*?>([\s\S]*?)<\/p>/gi,'<hr />$1');
if (A)
Editor.Config.Document.body.innerHTML = B;
else
hidArea.value = B;
}
WeisEditor.prototype.ReplaceTables = function(C)
{
C = C.replace(/(<tbody>|<\/tbody>)/ig,"");
C = C.replace(/<table(.[^\<]*)/ig,"<table$1<tbody>");
C = C.replace(/<\/table>/ig,"<\/tbody><\/table>");
var T = /<(\/?)(table|tbody|tr|td)([^>]*?)>\n*/ig;
C = C.replace(T,"<$1$2$3>\n");
return C;
};
WeisEditor.prototype.replaceSPCTags = function(A)
{
var hidArea = $(Editor.Config.EditorID+'Area');
var C = A ? Editor.Config.Document.body.innerHTML : hidArea.value.trim();
if (!C) return "<br />";
C = C.replace(/<(\/?\w)[^>]*?>/ig,function(x){return x.toLowerCase();});
C = C.replace(/\n*\r*/ig,"");
C = C.replace(/<(\w)([^>]*?)(.*?)\/\1>\n*/ig,'<$1$2$3/$1>\n');
C = C.replace(/<br[^>]*>\n*/ig,'<br />\n');
C = C.replace(/<(img\s*[^>]*[^\/])>/ig,'<$1 />');
C = C.replace(/<(hr\s*[^>]*[^\/])>/ig,'<$1 />');
C = C.replace(/ ([^=]+)=([^"' >]+)/ig," $1=\"$2\"");
C = Editor.ReplaceTables(C);
if (!WeisConfig.UseFullPath)
{
C = C.replace(/<[a-z][^>]*\s*(href|src)\s*=[^>]+/ig,function($0,$1){
$0 = $0.replace(/&#(6[5-9]|[78][0-9]|9[0789]|1[01][0-9]|12[012]);?/g,function($0,$1){return String.fromCharCode($1);});
if (RegExp.$1)/(.*)/.exec('');
var Attris = "";
var re = new RegExp("\s*(href|src)(\s*=\s*)(\")("+WeisConfig.SiteDomain+")(\/)([^\"]+)(\")\s*",'gi');
re.test($0);
if (RegExp.$1)
{
return $0.replace(/\s*(href|src)(\s*=\s*)(")(http:\/\/)([^\/]+)(\/)([^"]+)(")\s*/ig," $1=\"\/$7\" ");
}
return $0;
});
}
C = C.trim();
if (!WeisConfig.isReplaceDangerousCode)
{
return C;
}
var S = /<script[^>]*?>(.*?)<\/script>/gi;
C = C.replace(S,'$1');
var F = /<iframe[^>]*?>.*<\/iframe>/gi;
C = C.replace(F,'');
//2009-5-16
C = C.replace(/<(script|link|style|iframe|\?|\%)(.|\n)*\/\1>\s*/ig,"");
C = C.replace(/<[^>]+/ig, function($0){return $0.replace(/\s*on\w+=[^ ]+/ig,"")});
C = C.replace(/<[a-z][^>]*\s*(href|src)\s*=[^>]+/ig,function($0,$1)
{
$0 = $0.replace(/&#(6[5-9]|[78][0-9]|9[0789]|1[01][0-9]|12[012]);?/g,function($0,$1){return String.fromCharCode($1);});
return $0.replace(/\s*(href|src)\s*=\s*("\s*(javascript|vbscript):[^"]+"|'\s*(javascript|vbscript):[^']+'|(javascript|vbscript):[^\s]+)/ig,"");
});
C = C.replace(/<[a-z][^>]*\s*style\s*=[^>]+/ig,function($0,$1){
$0 = $0.replace(/&#(6[5-9]|[78][0-9]|9[0789]|1[01][0-9]|12[012]);?/g,function($0,$1){return String.fromCharCode($1);});
return $0.replace(/\s*style\s*=\s*("[^"]+(expression)[^"]+"|'[^']+(expression)[^']+'|[^\s]+(expression)[^\s]+)\s*/ig,"");
});
return C;
}
WeisEditor.prototype.setDisabled = function(flag,obj)
{
var WE = $('Weiseditor');
var _DIV = WE.getElementsByTagName('DIV');
for (var i=0;i<_DIV.length;i++)
{
if (_DIV[i].className=='emlCss' || _DIV[i].className=='MouseOver')
{
if (_DIV[i] == obj)
{
if(flag)
{
_DIV[i].className = 'MouseOver';
_DIV[i].onmouseover = function() {this.className = 'MouseOver';}
_DIV[i].onmouseout = function() {this.className = 'MouseOver';}
}
else
{
_DIV[i].className = 'emlCss';
_DIV[i].onmouseover = function() {this.className = 'MouseOver';}
_DIV[i].onmouseout = function() {this.className = 'emlCss';}
}
}
else
{
if(flag)
{
_DIV[i].className = 'emlCss';
_DIV[i].onmouseover = function() {this.className = 'emlCss';}
_DIV[i].onmouseout = function() {this.className = 'emlCss';}
}
else
{
_DIV[i].className = 'emlCss';
_DIV[i].onmouseover = function() {this.className = 'MouseOver';}
_DIV[i].onmouseout = function() {this.className = 'emlCss';}
}
}
}
}
}
WeisEditor.Run = {};
WeisEditor.Run.SaveData = [];
WeisEditor.Run.CurrentIndex = -1;
WeisEditor.Run.MaxStep = 25;
WeisEditor.Run.Selection = [];
WeisEditor.Run.IsLocked = false;
WeisEditor.Run.Offset = 0;
WeisEditor.Run.Changed = false;
WeisEditor.Run.Typing = false;
WeisEditor.Run.SaveDataStep = function(evt)
{
if (!Editor.Config.WEditorMode) return;
WeisEditor.Run.IsLocked = false;
if (WeisEditor.Run.CurrentIndex==-1)
{
WeisEditor.Run.CurrentIndex++;
if (!WeisEditor.Run.SaveData[WeisEditor.Run.CurrentIndex])
WeisEditor.Run.SaveData[WeisEditor.Run.CurrentIndex] = new Object();
WeisEditor.Run.SaveData[WeisEditor.Run.CurrentIndex].bodyHTML = Editor.Config.Document.body.innerHTML;
var range = Editor.Config.GetRange();
WeisEditor.Run.SaveData[WeisEditor.Run.CurrentIndex].Range = (!window.isIE ? range.cloneRange() : range.duplicate());
Editor.Config.Range.setState();
WeisEditor.Run.SaveData[WeisEditor.Run.CurrentIndex].bookmark = WeisEditor.Run._getBookmark(range);
}
else
{
if (WeisEditor.Run.SaveData[WeisEditor.Run.CurrentIndex].bodyHTML==Editor.Config.Document.body.innerHTML)
return;
WeisEditor.Run.SetBookmark();
}
}
WeisEditor.Run.Redo = function()
{
if (!Editor.Config.WEditorMode) return;
WeisEditor.Run.IsLocked = true;
Editor.Config.Select();
WeisEditor.Run.ConfigUndo();
if (WeisEditor.Run.CurrentIndex < WeisEditor.Run.SaveData.length-1)
{
WeisEditor.Run.CurrentIndex++;
Editor.Config.Document.body.innerHTML = WeisEditor.Run.SaveData[WeisEditor.Run.CurrentIndex].bodyHTML;
WeisEditor.Run.moveToBookmark();
}
Editor.Config.Select();
}
WeisEditor.Run.Undo = function()
{
if (!Editor.Config.WEditorMode) return;
WeisEditor.Run.IsLocked = true;
Editor.Config.Select();
WeisEditor.Run.ConfigUndo();
if (WeisEditor.Run.CurrentIndex>0)
{
WeisEditor.Run.CurrentIndex -- ;
Editor.Config.Document.body.innerHTML = WeisEditor.Run.SaveData[WeisEditor.Run.CurrentIndex].bodyHTML;
WeisEditor.Run.moveToBookmark();
}
Editor.Config.Select();
}
WeisEditor.Run.ConfigUndo = function()
{
if (WeisEditor.Run.SaveData.length>=0&&WeisEditor.Run.Changed)
{
if (WeisEditor.Run.SaveData[WeisEditor.Run.CurrentIndex].bodyHTML!=Editor.Config.Document.body.innerHTML)
WeisEditor.Run.SetBookmark();
}
else if (!WeisEditor.Run.Changed&&WeisEditor.Run.Typing)
WeisEditor.Run.SetBookmark();
WeisEditor.Run.Changed = false;
WeisEditor.Run.Typing = false;
}
WeisEditor.Run.SetBookmark = function()
{
if (window.isIE)
if (Editor.Config.Selection().type.toLowerCase()=='control')
return;
WeisEditor.Run.CurrentIndex++ ;
if (!WeisEditor.Run.SaveData[WeisEditor.Run.CurrentIndex])
WeisEditor.Run.SaveData[WeisEditor.Run.CurrentIndex] = new Object();
WeisEditor.Run.SaveData[WeisEditor.Run.CurrentIndex].bodyHTML = Editor.Config.Document.body.innerHTML;
var range = Editor.Config.GetRange();
if (!range) return;
WeisEditor.Run.SaveData[WeisEditor.Run.CurrentIndex].Range = (!window.isIE ? range.cloneRange() : range.duplicate());
Editor.Config.Range.setState();
WeisEditor.Run.SaveData[WeisEditor.Run.CurrentIndex].bookmark = WeisEditor.Run._getBookmark(range);
if (WeisEditor.Run.CurrentIndex>=WeisEditor.Run.MaxStep)
WeisEditor.Run.SaveData.shift();
WeisEditor.Run.CurrentIndex = WeisEditor.Run.SaveData.length-1;
WeisEditor.Run.Changed = true;
WeisEditor.Run.Typing = false;
}
WeisEditor.Run.moveToBookmark = function()
{
if (WeisEditor.Run.SaveData[WeisEditor.Run.CurrentIndex].Range)
{
var Range = WeisEditor.Run.SaveData[WeisEditor.Run.CurrentIndex].Range;
var SEC = WeisEditor.Run.SetCaretToPoint();
if (!window.isIE)
{
if (SEC.Start.Node.nodeType==1)
{
Range.selectNode(SEC.Start.Node);
switch (SEC.Start.Node.nodeName)
{
case 'BR':
case 'HR':
case 'IMG':
case 'INPUT':
Range.selectNode(SEC.Start.Node.parentNode);
}
}
else if (SEC.Start.Node.nodeType==3)
{
Range.selectNode(SEC.Start.Node.parentNode);
}
if (SEC.End.Node.nodeType==1)
{
Range.selectNode(SEC.End.Node);
switch (SEC.End.Node.nodeName)
{
case 'BR':
case 'HR':
case 'IMG':
case 'INPUT':
Range.selectNode(SEC.End.Node.parentNode);
}
}
else if (SEC.End.Node.nodeType==3)
{
Range.selectNode(SEC.End.Node.parentNode);
}
}
Editor.Config.Range.setStart(SEC.Start.Node,SEC.Start.Offset,Range);
Editor.Config.Range.setEnd(SEC.End.Node,SEC.End.Offset,Range);
Editor.Config.getSelection.addRange(Range);
}
}
WeisEditor.Run._getBookmark = function()
{
var snode = Editor.Config.Range.startContainer;
var enode = Editor.Config.Range.endContainer;
var startOff = Editor.Config.Range.startOffset;
var endOff = Editor.Config.Range.endOffset;
var collapsed = Editor.Config.Range.collapsed;
if (collapsed)
{
S = E = WeisEditor.Run.bookMarkstart(snode,startOff);
}
else
{
var S = WeisEditor.Run.bookMarkstart(snode,startOff);
var E = WeisEditor.Run.bookMarkend(enode,endOff);
}
return {Start:S,End:E};
}
WeisEditor.Run.bookMarkstart = function(S,SO)
{
var P = 0,N = 0;
if (S.nodeType==1)
{
P = WeisEditor.Run.getNodeOffset(S);
N = SO;
}
else
{
P = WeisEditor.Run.getNodeOffset(S.parentNode);
N = Editor.Config.GetIndexNode(S);
}
N = N==-1?0:N;
return {P:P,N:N,O:SO};
}
WeisEditor.Run.bookMarkend = function(E,EO)
{
var P = 0,N = 0;
if (E.nodeType==1)
{
P = WeisEditor.Run.getNodeOffset(E);
N = EO - 1;
}
else
{
P = WeisEditor.Run.getNodeOffset(E.parentNode);
N = Editor.Config.GetIndexNode(E);
}
N = N==-1?0:N;
return {P:P,N:N,O:EO};
}
WeisEditor.Run.getNodeOffset = function(node)
{
var DOC = Editor.Config.Document.documentElement;
var NOSet = -1;
var C = [];
while(node&&node!=DOC)
{
var T = node.parentNode;
NOSet = -1;
if (T.nodeName=="#document")
{
break;
}
for(var i=0;i<T.childNodes.length;i++)
{
if (T.childNodes[i]&&T.childNodes[i].nodeType==3)
continue;
NOSet++;
if (T.childNodes[i]==node)
break;
}
C.unshift(NOSet);
node = node.parentNode;
}
return C;
}
WeisEditor.Run.SetCaretToPoint = function()
{
var DOC = Editor.Config.Document.documentElement;
var _WEC = WeisEditor.Run.S
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -