forked from fex-team/ueditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor_all.js
executable file
·17318 lines (15910 loc) · 686 KB
/
editor_all.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function(){UEDITOR_CONFIG = window.UEDITOR_CONFIG || {};
var baidu = window.baidu || {};
window.baidu = baidu;
window.UE = baidu.editor = {};
UE.plugins = {};
UE.commands = {};
UE.instants = {};
UE.I18N = {};
UE.version = "1.2.4.0";
var dom = UE.dom = {};
/**
* @file
* @name UE.browser
* @short Browser
* @desc UEditor中采用的浏览器判断模块
*/
var browser = UE.browser = function(){
var agent = navigator.userAgent.toLowerCase(),
opera = window.opera,
browser = {
/**
* 检测浏览器是否为IE
* @name ie
* @grammar UE.browser.ie => true|false
*/
ie : !!window.ActiveXObject,
/**
* 检测浏览器是否为Opera
* @name opera
* @grammar UE.browser.opera => true|false
*/
opera : ( !!opera && opera.version ),
/**
* 检测浏览器是否为webkit内核
* @name webkit
* @grammar UE.browser.webkit => true|false
*/
webkit : ( agent.indexOf( ' applewebkit/' ) > -1 ),
/**
* 检测浏览器是否为mac系统下的浏览器
* @name mac
* @grammar UE.browser.mac => true|false
*/
mac : ( agent.indexOf( 'macintosh' ) > -1 ),
/**
* 检测浏览器是否处于怪异模式
* @name quirks
* @grammar UE.browser.quirks => true|false
*/
quirks : ( document.compatMode == 'BackCompat' )
};
/**
* 检测浏览器是否处为gecko内核
* @name gecko
* @grammar UE.browser.gecko => true|false
*/
browser.gecko =( navigator.product == 'Gecko' && !browser.webkit && !browser.opera );
var version = 0;
// Internet Explorer 6.0+
if ( browser.ie ){
version = parseFloat( agent.match( /msie (\d+)/ )[1] );
/**
* 检测浏览器是否为 IE9 模式
* @name ie9Compat
* @grammar UE.browser.ie9Compat => true|false
*/
browser.ie9Compat = document.documentMode == 9;
/**
* 检测浏览器是否为 IE8 浏览器
* @name ie8
* @grammar UE.browser.ie8 => true|false
*/
browser.ie8 = !!document.documentMode;
/**
* 检测浏览器是否为 IE8 模式
* @name ie8Compat
* @grammar UE.browser.ie8Compat => true|false
*/
browser.ie8Compat = document.documentMode == 8;
/**
* 检测浏览器是否运行在 兼容IE7模式
* @name ie7Compat
* @grammar UE.browser.ie7Compat => true|false
*/
browser.ie7Compat = ( ( version == 7 && !document.documentMode )
|| document.documentMode == 7 );
/**
* 检测浏览器是否IE6模式或怪异模式
* @name ie6Compat
* @grammar UE.browser.ie6Compat => true|false
*/
browser.ie6Compat = ( version < 7 || browser.quirks );
}
// Gecko.
if ( browser.gecko ){
var geckoRelease = agent.match( /rv:([\d\.]+)/ );
if ( geckoRelease )
{
geckoRelease = geckoRelease[1].split( '.' );
version = geckoRelease[0] * 10000 + ( geckoRelease[1] || 0 ) * 100 + ( geckoRelease[2] || 0 ) * 1;
}
}
/**
* 检测浏览器是否为chrome
* @name chrome
* @grammar UE.browser.chrome => true|false
*/
if (/chrome\/(\d+\.\d)/i.test(agent)) {
browser.chrome = + RegExp['\x241'];
}
/**
* 检测浏览器是否为safari
* @name safari
* @grammar UE.browser.safari => true|false
*/
if(/(\d+\.\d)?(?:\.\d)?\s+safari\/?(\d+\.\d+)?/i.test(agent) && !/chrome/i.test(agent)){
browser.safari = + (RegExp['\x241'] || RegExp['\x242']);
}
// Opera 9.50+
if ( browser.opera )
version = parseFloat( opera.version() );
// WebKit 522+ (Safari 3+)
if ( browser.webkit )
version = parseFloat( agent.match( / applewebkit\/(\d+)/ )[1] );
/**
* 浏览器版本判断
* IE系列返回值为5,6,7,8,9,10等
* gecko系列会返回10900,158900等.
* webkit系列会返回其build号 (如 522等).
* @name version
* @grammar UE.browser.version => number
* @example
* if ( UE.browser.ie && UE.browser.version == 6 ){
* alert( "Ouch!居然是万恶的IE6!" );
* }
*/
browser.version = version;
/**
* 是否是兼容模式的浏览器
* @name isCompatible
* @grammar UE.browser.isCompatible => true|false
* @example
* if ( UE.browser.isCompatible ){
* alert( "你的浏览器相当不错哦!" );
* }
*/
browser.isCompatible =
!browser.mobile && (
( browser.ie && version >= 6 ) ||
( browser.gecko && version >= 10801 ) ||
( browser.opera && version >= 9.5 ) ||
( browser.air && version >= 1 ) ||
( browser.webkit && version >= 522 ) ||
false );
return browser;
}();
//快捷方式
var ie = browser.ie,
webkit = browser.webkit,
gecko = browser.gecko,
opera = browser.opera;
/**
* @file
* @name UE.Utils
* @short Utils
* @desc UEditor封装使用的静态工具函数
* @import editor.js
*/
var utils = UE.utils = {
/**
* 遍历数组,对象,nodeList
* @name each
* @grammar UE.utils.each(obj,iterator,[context])
* @since 1.2.4+
* @desc
* * obj 要遍历的对象
* * iterator 遍历的方法,方法的第一个是遍历的值,第二个是索引,第三个是obj
* * context iterator的上下文
* @example
* UE.utils.each([1,2],function(v,i){
* console.log(v)//值
* console.log(i)//索引
* })
* UE.utils.each(document.getElementsByTagName('*'),function(n){
* console.log(n.tagName)
* })
*/
each : function(obj, iterator, context) {
if (obj == null) return;
if (Array.prototype.forEach && obj.forEach === Array.prototype.forEach) {
obj.forEach(iterator, context);
} else if (obj.length === +obj.length) {
for (var i = 0, l = obj.length; i < l; i++) {
if(iterator.call(context, obj[i], i, obj) === false)return;
}
} else {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if(iterator.call(context, obj[key], key, obj) === false)return
}
}
}
},
makeInstance:function (obj) {
var noop = new Function();
noop.prototype = obj;
obj = new noop;
noop.prototype = null;
return obj;
},
/**
* 将source对象中的属性扩展到target对象上
* @name extend
* @grammar UE.utils.extend(target,source) => Object //覆盖扩展
* @grammar UE.utils.extend(target,source,true) ==> Object //保留扩展
*/
extend:function (t, s, b) {
if (s) {
for (var k in s) {
if (!b || !t.hasOwnProperty(k)) {
t[k] = s[k];
}
}
}
return t;
},
/**
* 模拟继承机制,subClass继承superClass
* @name inherits
* @grammar UE.utils.inherits(subClass,superClass) => subClass
* @example
* function SuperClass(){
* this.name = "小李";
* }
* SuperClass.prototype = {
* hello:function(str){
* console.log(this.name + str);
* }
* }
* function SubClass(){
* this.name = "小张";
* }
* UE.utils.inherits(SubClass,SuperClass);
* var sub = new SubClass();
* sub.hello("早上好!"); ==> "小张早上好!"
*/
inherits:function (subClass, superClass) {
var oldP = subClass.prototype,
newP = utils.makeInstance(superClass.prototype);
utils.extend(newP, oldP, true);
subClass.prototype = newP;
return (newP.constructor = subClass);
},
/**
* 用指定的context作为fn上下文,也就是this
* @name bind
* @grammar UE.utils.bind(fn,context) => fn
*/
bind:function (fn, context) {
return function () {
return fn.apply(context, arguments);
};
},
/**
* 创建延迟delay执行的函数fn
* @name defer
* @grammar UE.utils.defer(fn,delay) =>fn //延迟delay毫秒执行fn,返回fn
* @grammar UE.utils.defer(fn,delay,exclusion) =>fn //延迟delay毫秒执行fn,若exclusion为真,则互斥执行fn
* @example
* function test(){
* console.log("延迟输出!");
* }
* //非互斥延迟执行
* var testDefer = UE.utils.defer(test,1000);
* testDefer(); => "延迟输出!";
* testDefer(); => "延迟输出!";
* //互斥延迟执行
* var testDefer1 = UE.utils.defer(test,1000,true);
* testDefer1(); => //本次不执行
* testDefer1(); => "延迟输出!";
*/
defer:function (fn, delay, exclusion) {
var timerID;
return function () {
if (exclusion) {
clearTimeout(timerID);
}
timerID = setTimeout(fn, delay);
};
},
/**
* 查找元素item在数组array中的索引, 若找不到返回-1
* @name indexOf
* @grammar UE.utils.indexOf(array,item) => index|-1 //默认从数组开头部开始搜索
* @grammar UE.utils.indexOf(array,item,start) => index|-1 //start指定开始查找的位置
*/
indexOf:function (array, item, start) {
var index = -1;
start = this.isNumber(start) ? start : 0;
this.each(array,function(v,i){
if(i >= start && v === item){
index = i;
return false;
}
});
return index;
},
/**
* 移除数组array中的元素item
* @name removeItem
* @grammar UE.utils.removeItem(array,item)
*/
removeItem:function (array, item) {
for (var i = 0, l = array.length; i < l; i++) {
if (array[i] === item) {
array.splice(i, 1);
i--;
}
}
},
/**
* 删除字符串str的首尾空格
* @name trim
* @grammar UE.utils.trim(str) => String
*/
trim:function (str) {
return str.replace(/(^[ \t\n\r]+)|([ \t\n\r]+$)/g, '');
},
/**
* 将字符串list(以','分隔)或者数组list转成哈希对象
* @name listToMap
* @grammar UE.utils.listToMap(list) => Object //Object形如{test:1,br:1,textarea:1}
*/
listToMap:function (list) {
if (!list)return {};
list = utils.isArray(list) ? list : list.split(',');
for (var i = 0, ci, obj = {}; ci = list[i++];) {
obj[ci.toUpperCase()] = obj[ci] = 1;
}
return obj;
},
/**
* 将str中的html符号转义,默认将转义''&<">''四个字符,可自定义reg来确定需要转义的字符
* @name unhtml
* @grammar UE.utils.unhtml(str); => String
* @grammar UE.utils.unhtml(str,reg) => String
* @example
* var html = '<body>You say:"你好!Baidu & UEditor!"</body>';
* UE.utils.unhtml(html); ==> <body>You say:"你好!Baidu & UEditor!"</body>
* UE.utils.unhtml(html,/[<>]/g) ==> <body>You say:"你好!Baidu & UEditor!"</body>
*/
unhtml:function (str, reg) {
return str ? str.replace(reg || /[&<">]/g, function (m) {
return {
'<':'<',
'&':'&',
'"':'"',
'>':'>'
}[m]
}) : '';
},
/**
* 将str中的转义字符还原成html字符
* @name html
* @grammar UE.utils.html(str) => String //详细参见<code><a href = '#unhtml'>unhtml</a></code>
*/
html:function (str) {
return str ? str.replace(/&((g|l|quo)t|amp);/g, function (m) {
return {
'<':'<',
'&':'&',
'"':'"',
'>':'>'
}[m]
}) : '';
},
/**
* 将css样式转换为驼峰的形式。如font-size => fontSize
* @name cssStyleToDomStyle
* @grammar UE.utils.cssStyleToDomStyle(cssName) => String
*/
cssStyleToDomStyle:function () {
var test = document.createElement('div').style,
cache = {
'float':test.cssFloat != undefined ? 'cssFloat' : test.styleFloat != undefined ? 'styleFloat' : 'float'
};
return function (cssName) {
return cache[cssName] || (cache[cssName] = cssName.toLowerCase().replace(/-./g, function (match) {
return match.charAt(1).toUpperCase();
}));
};
}(),
/**
* 动态加载文件到doc中,并依据obj来设置属性,加载成功后执行回调函数fn
* @name loadFile
* @grammar UE.utils.loadFile(doc,obj)
* @grammar UE.utils.loadFile(doc,obj,fn)
* @example
* //指定加载到当前document中一个script文件,加载成功后执行function
* utils.loadFile( document, {
* src:"test.js",
* tag:"script",
* type:"text/javascript",
* defer:"defer"
* }, function () {
* console.log('加载成功!')
* });
*/
loadFile:function () {
var tmpList = [];
function getItem(doc,obj){
for(var i= 0,ci;ci=tmpList[i++];){
if(ci.doc === doc && ci.url == (obj.src || obj.href)){
return ci;
}
}
}
return function (doc, obj, fn) {
var item = getItem(doc,obj);
if (item) {
if(item.ready){
fn && fn();
}else{
item.funs.push(fn)
}
return;
}
tmpList.push({
doc:doc,
url:obj.src||obj.href,
funs:[fn]
});
if (!doc.body) {
var html = [];
for(var p in obj){
if(p == 'tag')continue;
html.push(p + '="' + obj[p] + '"')
}
doc.write('<' + obj.tag + ' ' + html.join(' ') + ' ></'+obj.tag+'>');
return;
}
if (obj.id && doc.getElementById(obj.id)) {
return;
}
var element = doc.createElement(obj.tag);
delete obj.tag;
for (var p in obj) {
element.setAttribute(p, obj[p]);
}
element.onload = element.onreadystatechange = function () {
if (!this.readyState || /loaded|complete/.test(this.readyState)) {
item = getItem(doc,obj)
if (item.funs.length > 0) {
item.ready = 1;
for (var fi; fi = item.funs.pop();) {
fi();
}
}
element.onload = element.onreadystatechange = null;
}
};
doc.getElementsByTagName("head")[0].appendChild(element);
}
}(),
/**
* 判断obj对象是否为空
* @name isEmptyObject
* @grammar UE.utils.isEmptyObject(obj) => true|false
* @example
* UE.utils.isEmptyObject({}) ==>true
* UE.utils.isEmptyObject([]) ==>true
* UE.utils.isEmptyObject("") ==>true
*/
isEmptyObject:function (obj) {
if (obj == null) return true;
if (this.isArray(obj) || this.isString(obj)) return obj.length === 0;
for (var key in obj) if (obj.hasOwnProperty(key)) return false;
return true;
},
/**
* 统一将颜色值使用16进制形式表示
* @name fixColor
* @grammar UE.utils.fixColor(name,value) => value
* @example
* rgb(255,255,255) => "#ffffff"
*/
fixColor:function (name, value) {
if (/color/i.test(name) && /rgba?/.test(value)) {
var array = value.split(",");
if (array.length > 3)
return "";
value = "#";
for (var i = 0, color; color = array[i++];) {
color = parseInt(color.replace(/[^\d]/gi, ''), 10).toString(16);
value += color.length == 1 ? "0" + color : color;
}
value = value.toUpperCase();
}
return value;
},
/**
* 只针对border,padding,margin做了处理,因为性能问题
* @public
* @function
* @param {String} val style字符串
*/
optCss:function (val) {
var padding, margin, border;
val = val.replace(/(padding|margin|border)\-([^:]+):([^;]+);?/gi, function (str, key, name, val) {
if (val.split(' ').length == 1) {
switch (key) {
case 'padding':
!padding && (padding = {});
padding[name] = val;
return '';
case 'margin':
!margin && (margin = {});
margin[name] = val;
return '';
case 'border':
return val == 'initial' ? '' : str;
}
}
return str;
});
function opt(obj, name) {
if (!obj) {
return '';
}
var t = obj.top , b = obj.bottom, l = obj.left, r = obj.right, val = '';
if (!t || !l || !b || !r) {
for (var p in obj) {
val += ';' + name + '-' + p + ':' + obj[p] + ';';
}
} else {
val += ';' + name + ':' +
(t == b && b == l && l == r ? t :
t == b && l == r ? (t + ' ' + l) :
l == r ? (t + ' ' + l + ' ' + b) : (t + ' ' + r + ' ' + b + ' ' + l)) + ';'
}
return val;
}
val += opt(padding, 'padding') + opt(margin, 'margin');
return val.replace(/^[ \n\r\t;]*|[ \n\r\t]*$/, '').replace(/;([ \n\r\t]+)|\1;/g, ';')
.replace(/(&((l|g)t|quot|#39))?;{2,}/g, function (a, b) {
return b ? b + ";;" : ';'
});
},
/**
* 深度克隆对象,从source到target
* @name clone
* @grammar UE.utils.clone(source) => anthorObj 新的对象是完整的source的副本
* @grammar UE.utils.clone(source,target) => target包含了source的所有内容,重名会覆盖
*/
clone:function (source, target) {
var tmp;
target = target || {};
for (var i in source) {
if (source.hasOwnProperty(i)) {
tmp = source[i];
if (typeof tmp == 'object') {
target[i] = utils.isArray(tmp) ? [] : {};
utils.clone(source[i], target[i])
} else {
target[i] = tmp;
}
}
}
return target;
},
/**
* 转换cm/pt到px
* @name transUnitToPx
* @grammar UE.utils.transUnitToPx('20pt') => '27px'
* @grammar UE.utils.transUnitToPx('0pt') => '0'
*/
transUnitToPx : function(val){
if(!/(pt|cm)/.test(val)){
return val
}
var unit;
val.replace(/([\d.]+)(\w+)/,function(str,v,u){
val = v;
unit = u;
});
switch(unit){
case 'cm':
val = parseFloat(val) * 25;
break;
case 'pt':
val = Math.round(parseFloat(val) * 96 / 72);
}
return val + (val?'px':'');
},
/**
* DomReady方法,回调函数将在dom树ready完成后执行
* @name domReady
* @grammar UE.utils.domReady(fn) => fn //返回一个延迟执行的方法
*/
domReady:function () {
var fnArr = [];
function doReady(doc) {
//确保onready只执行一次
doc.isReady = true;
for (var ci; ci = fnArr.pop();ci()){}
}
return function (onready,win) {
win = win || window;
var doc = win.document;
onready && fnArr.push(onready);
if (doc.readyState === "complete") {
doReady(doc);
}else{
doc.isReady && doReady(doc);
if (browser.ie) {
(function () {
if (doc.isReady) return;
try {
doc.documentElement.doScroll("left");
} catch (error) {
setTimeout(arguments.callee, 0);
return;
}
doReady(doc);
})();
win.attachEvent('onload', function(){
doReady(doc)
});
} else {
doc.addEventListener("DOMContentLoaded", function () {
doc.removeEventListener("DOMContentLoaded", arguments.callee, false);
doReady(doc);
}, false);
win.addEventListener('load', function(){doReady(doc)}, false);
}
}
}
}(),
/**
* 动态添加css样式
* @name cssRule
* @grammar UE.utils.cssRule('添加的样式的节点名称',['样式','放到哪个document上'])
* @grammar UE.utils.cssRule('body','body{background:#ccc}') => null //给body添加背景颜色
* @grammar UE.utils.cssRule('body') =>样式的字符串 //取得key值为body的样式的内容,如果没有找到key值先关的样式将返回空,例如刚才那个背景颜色,将返回 body{background:#ccc}
* @grammar UE.utils.cssRule('body','') =>null //清空给定的key值的背景颜色
*/
cssRule : browser.ie ? function(key,style,doc){
var indexList,index;
doc = doc || document;
if(doc.indexList){
indexList = doc.indexList;
}else{
indexList = doc.indexList = {};
}
var sheetStyle;
if(!indexList[key]){
if(style === undefined){
return ''
}
sheetStyle = doc.createStyleSheet('',index = doc.styleSheets.length);
indexList[key] = index;
}else{
sheetStyle = doc.styleSheets[indexList[key]];
}
if(style === undefined){
return sheetStyle.cssText
}
sheetStyle.cssText = style || ''
}:function(key,style,doc){
doc = doc || document;
var head = doc.getElementsByTagName('head')[0],node;
if(!(node = doc.getElementById(key))){
if(style === undefined){
return ''
}
node = doc.createElement('style');
node.id = key;
head.appendChild(node)
}
if(style === undefined){
return node.innerHTML
}
if(style !== ''){
node.innerHTML = style;
}else{
head.removeChild(node)
}
}
};
/**
* 判断str是否为字符串
* @name isString
* @grammar UE.utils.isString(str) => true|false
*/
/**
* 判断array是否为数组
* @name isArray
* @grammar UE.utils.isArray(obj) => true|false
*/
/**
* 判断obj对象是否为方法
* @name isFunction
* @grammar UE.utils.isFunction(obj) => true|false
*/
/**
* 判断obj对象是否为数字
* @name isNumber
* @grammar UE.utils.isNumber(obj) => true|false
*/
utils.each(['String','Function','Array','Number'],function(v){
UE.utils['is' + v] = function(obj){
return Object.prototype.toString.apply(obj) == '[object ' + v + ']';
}
});
/**
* @file
* @name UE.EventBase
* @short EventBase
* @import editor.js,core/utils.js
* @desc UE采用的事件基类,继承此类的对应类将获取addListener,removeListener,fireEvent方法。
* 在UE中,Editor以及所有ui实例都继承了该类,故可以在对应的ui对象以及editor对象上使用上述方法。
*/
var EventBase = UE.EventBase = function () {};
EventBase.prototype = {
/**
* 注册事件监听器
* @name addListener
* @grammar editor.addListener(types,fn) //types为事件名称,多个可用空格分隔
* @example
* editor.addListener('selectionchange',function(){
* console.log("选区已经变化!");
* })
* editor.addListener('beforegetcontent aftergetcontent',function(type){
* if(type == 'beforegetcontent'){
* //do something
* }else{
* //do something
* }
* console.log(this.getContent) // this是注册的事件的编辑器实例
* })
*/
addListener:function (types, listener) {
types = utils.trim(types).split(' ');
for (var i = 0, ti; ti = types[i++];) {
getListener(this, ti, true).push(listener);
}
},
/**
* 移除事件监听器
* @name removeListener
* @grammar editor.removeListener(types,fn) //types为事件名称,多个可用空格分隔
* @example
* //changeCallback为方法体
* editor.removeListener("selectionchange",changeCallback);
*/
removeListener:function (types, listener) {
types = utils.trim(types).split(' ');
for (var i = 0, ti; ti = types[i++];) {
utils.removeItem(getListener(this, ti) || [], listener);
}
},
/**
* 触发事件
* @name fireEvent
* @grammar editor.fireEvent(types) //types为事件名称,多个可用空格分隔
* @example
* editor.fireEvent("selectionchange");
*/
fireEvent:function (types) {
types = utils.trim(types).split(' ');
for (var i = 0, ti; ti = types[i++];) {
var listeners = getListener(this, ti),
r, t, k;
if (listeners) {
k = listeners.length;
while (k--) {
t = listeners[k].apply(this, arguments);
if (t !== undefined) {
r = t;
}
}
}
if (t = this['on' + ti.toLowerCase()]) {
r = t.apply(this, arguments);
}
}
return r;
}
};
/**
* 获得对象所拥有监听类型的所有监听器
* @public
* @function
* @param {Object} obj 查询监听器的对象
* @param {String} type 事件类型
* @param {Boolean} force 为true且当前所有type类型的侦听器不存在时,创建一个空监听器数组
* @returns {Array} 监听器数组
*/
function getListener(obj, type, force) {
var allListeners;
type = type.toLowerCase();
return ( ( allListeners = ( obj.__allListeners || force && ( obj.__allListeners = {} ) ) )
&& ( allListeners[type] || force && ( allListeners[type] = [] ) ) );
}
///import editor.js
///import core/dom/dom.js
/**
* dtd html语义化的体现类
* @constructor
* @namespace dtd
*/
var dtd = dom.dtd = (function() {
function _( s ) {
for (var k in s) {
s[k.toUpperCase()] = s[k];
}
return s;
}
function X( t ) {
var a = arguments;
for ( var i=1; i<a.length; i++ ) {
var x = a[i];
for ( var k in x ) {
if (!t.hasOwnProperty(k)) {
t[k] = x[k];
}
}
}
return t;
}
var A = _({isindex:1,fieldset:1}),
B = _({input:1,button:1,select:1,textarea:1,label:1}),
C = X( _({a:1}), B ),
D = X( {iframe:1}, C ),
E = _({hr:1,ul:1,menu:1,div:1,blockquote:1,noscript:1,table:1,center:1,address:1,dir:1,pre:1,h5:1,dl:1,h4:1,noframes:1,h6:1,ol:1,h1:1,h3:1,h2:1}),
F = _({ins:1,del:1,script:1,style:1}),
G = X( _({b:1,acronym:1,bdo:1,'var':1,'#':1,abbr:1,code:1,br:1,i:1,cite:1,kbd:1,u:1,strike:1,s:1,tt:1,strong:1,q:1,samp:1,em:1,dfn:1,span:1}), F ),
H = X( _({sub:1,img:1,embed:1,object:1,sup:1,basefont:1,map:1,applet:1,font:1,big:1,small:1}), G ),
I = X( _({p:1}), H ),
J = X( _({iframe:1}), H, B ),
K = _({img:1,embed:1,noscript:1,br:1,kbd:1,center:1,button:1,basefont:1,h5:1,h4:1,samp:1,h6:1,ol:1,h1:1,h3:1,h2:1,form:1,font:1,'#':1,select:1,menu:1,ins:1,abbr:1,label:1,code:1,table:1,script:1,cite:1,input:1,iframe:1,strong:1,textarea:1,noframes:1,big:1,small:1,span:1,hr:1,sub:1,bdo:1,'var':1,div:1,object:1,sup:1,strike:1,dir:1,map:1,dl:1,applet:1,del:1,isindex:1,fieldset:1,ul:1,b:1,acronym:1,a:1,blockquote:1,i:1,u:1,s:1,tt:1,address:1,q:1,pre:1,p:1,em:1,dfn:1}),
L = X( _({a:0}), J ),//a不能被切开,所以把他
M = _({tr:1}),
N = _({'#':1}),
O = X( _({param:1}), K ),
P = X( _({form:1}), A, D, E, I ),
Q = _({li:1}),
R = _({style:1,script:1}),
S = _({base:1,link:1,meta:1,title:1}),
T = X( S, R ),
U = _({head:1,body:1}),
V = _({html:1});
var block = _({address:1,blockquote:1,center:1,dir:1,div:1,dl:1,fieldset:1,form:1,h1:1,h2:1,h3:1,h4:1,h5:1,h6:1,hr:1,isindex:1,menu:1,noframes:1,ol:1,p:1,pre:1,table:1,ul:1}),
//针对优酷的embed他添加了结束标识,导致粘贴进来会变成两个,暂时去掉 ,embed:1
empty = _({area:1,base:1,br:1,col:1,hr:1,img:1,input:1,link:1,meta:1,param:1,embed:1});
return _({
// $ 表示自定的属性
// body外的元素列表.
$nonBodyContent: X( V, U, S ),
//块结构元素列表
$block : block,
//内联元素列表
$inline : L,
$body : X( _({script:1,style:1}), block ),
$cdata : _({script:1,style:1}),
//自闭和元素
$empty : empty,
//不是自闭合,但不能让range选中里边
$nonChild : _({iframe:1,textarea:1}),
//列表元素列表
$listItem : _({dd:1,dt:1,li:1}),
//列表根元素列表
$list: _({ul:1,ol:1,dl:1}),
//不能认为是空的元素
$isNotEmpty : _({table:1,ul:1,ol:1,dl:1,iframe:1,area:1,base:1,col:1,hr:1,img:1,embed:1,input:1,link:1,meta:1,param:1}),
//如果没有子节点就可以删除的元素列表,像span,a
$removeEmpty : _({a:1,abbr:1,acronym:1,address:1,b:1,bdo:1,big:1,cite:1,code:1,del:1,dfn:1,em:1,font:1,i:1,ins:1,label:1,kbd:1,q:1,s:1,samp:1,small:1,span:1,strike:1,strong:1,sub:1,sup:1,tt:1,u:1,'var':1}),
$removeEmptyBlock : _({'p':1,'div':1}),
//在table元素里的元素列表
$tableContent : _({caption:1,col:1,colgroup:1,tbody:1,td:1,tfoot:1,th:1,thead:1,tr:1,table:1}),
//不转换的标签
$notTransContent : _({pre:1,script:1,style:1,textarea:1}),
html: U,
head: T,
style: N,
script: N,
body: P,
base: {},
link: {},
meta: {},
title: N,
col : {},
tr : _({td:1,th:1}),
img : {},
embed: {},
colgroup : _({thead:1,col:1,tbody:1,tr:1,tfoot:1}),
noscript : P,
td : P,
br : {},
th : P,
center : P,
kbd : L,
button : X( I, E ),
basefont : {},
h5 : L,
h4 : L,
samp : L,
h6 : L,
ol : Q,
h1 : L,
h3 : L,
option : N,
h2 : L,
form : X( A, D, E, I ),
select : _({optgroup:1,option:1}),
font : L,
ins : L,
menu : Q,
abbr : L,
label : L,
table : _({thead:1,col:1,tbody:1,tr:1,colgroup:1,caption:1,tfoot:1}),
code : L,
tfoot : M,
cite : L,
li : P,
input : {},
iframe : P,
strong : L,
textarea : N,
noframes : P,
big : L,
small : L,
span :_({'#':1,br:1}),
hr : L,
dt : L,
sub : L,