在线Html编辑器粘贴过滤技术详解(二)

简介:

本章我们将来说一说filterPasteData函数的实现。

上篇中提到我们采用的粘贴方式是浏览器自己提供的,只是使用了不少技巧,使得粘贴的东西不直接进入Html编辑器的iframe,所以从某种意义上讲,我们获得的粘贴数据内容都是html。根据我们之前所说的三种需求,文本、html以及Word内容,我们可以把过滤的内容分为两大类:

a. html

b. Word xml

之所以纯文本可以当做html就是因为贴进来就是当html处理了,只不过是不带标签的html。

说到过滤,自然要做匹配和替换,于是我们很自然的想到了正则表达式,先来处理比较简单的html过滤。我定义了以下一些规则:

* 移除html, body, form, doctype, head, script,style, textarea, button,select, option, input,span标签

* 移除id, name, class, language,type属性

* 移除on开头的属性,如onclick

* 移除a, table, tr,td,tbody, thead, th, img,input,iframe, div标签(我们的系统中不支持table)

* 移除换行符(\n, \r)

以上这些标签都是要移除的对象,因为它们并没有给系统带来任何好处,还会降低安全性。当然根据实际需要,你可以适当保留一些标签,比如select, button, input, img, table等

?
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
function  FilterPasteText(str)
{
     str = str.replace(/\r\n|\n|\r/ig,  "" );
     //remove html body form
     str = str.replace(/<\/?(html|body|form)(?=[\s\/>])[^>]*>/ig,  "" );
     //remove doctype
     str = str.replace(/<(!DOCTYPE)(\n|.)*?>/ig,  "" );
     //remove xml tags
     str = str.replace(/<(\/?(\?xml(:\w+)?|xml|\w+:\w+)(?=[\s\/>]))[^>]*>/gi, "" );
     //remove head
     str = str.replace(/<head[^>]*>(\n|.)*?<\/head>/ig,  "" );
     //remove <xxx />
     str = str.replace(/<(script|style|link|title|meta|textarea|option|select|iframe|hr)(\n|.)*?\/>/ig,  "" );
     //remove empty span
     str = str.replace(/<span[^>]*?><\/span>/ig,  "" );
     //remove <xxx>...</xxx>
     str = str.replace(/<(head|script|style|textarea|button|select|option|iframe)[^>]*>(\n|.)*?<\/\1>/ig,  "" );
     //remove table and <a> tag, <img> tag,<input> tag (this can help filter unclosed tag)
     str = str.replace(/<\/?(a|table|tr|td|tbody|thead|th|img|input|iframe|div)[^>]*>/ig,  "" );
     //remove bad attributes
     do  {
         len = str.length;
         str = str.replace(/(<[a-z][^>]*\s)(?:id|name|language|type|class|on\w+|\w+:\w+)=(?: "[^" ]* "|\w+)\s?/gi, " $1");
     }  while  (len != str.length);
     
     return  str;
}

这里str.replace(/<(html|body|form)[^>]*>([\s\S]+)<\/\1>/ig, “$2”)的意思是保留<html></html>里面的内容,但是移除<html>标签本身。

这里str.replace(/<input(\n|.)*?>/ig, "");的意思是移除input标签。

以下是一些测试数据


<td>abc
<td>cde

替换前 替换后
<html><body>abc</body></html> abc

<html> 
<head> 
<script type="text/javascript"> 
... 
</script> 
</head> 
<body> 
<script type="text/javascript"> 
... 
</script> 
<form action="target.html"> 
abc 
</form> 
</body> 
</html>

abc

<html> 
<body> 
<form action="target.html"> 
<table> 


</form> 
</body> 
</html>

abccde

好了,接下来就来讲word xml的过滤。要想过滤word xml就必须知道当前贴进来的是word的数据,而非普通html,tinymce使用了一个技巧,如下:

?
1
2
3
4
5
function  IsWordDocument(strValue)
{
     var  re= new  RegExp(/(class=\"?Mso|style=\"[^\"]*\bmso\-|w:WordDocument)/ig);
     return  re.test(strValue);
}

只要是从Word贴过来的都会有这样的标记:class=”MsoNormal”或<w:WordDocument></w:WordDocument>或style=”mso-font-charset:0”

一旦检测到这样的标签,就可以认为这是从Word粘贴过来的,当然如果有人特别无聊,故意做了这样的标签或样式贴进来也没关系,因为过滤器会把没有用的东西过滤掉,那些不在规则里面的不管你是Word数据还是html数据,都尽量保留。

Word的过滤会比普通的html复杂很多,主要原因还是浏览器自动贴进来的word xml保留了很多东西,通常一段十分简单的文本数据,通过word xml表示可能字符长度要达到上万。

举个例子:这里有一段很简单的Word文本,如下

image

它对应的粘贴结果却是下面这么一大段,是不是很恐怖?

?
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
< meta  http-equiv = "Content-Type"  content = "text/html; charset=utf-8" >< meta  name = "ProgId"  content = "Word.Document" >< meta  name = "Generator"  content = "Microsoft Word 12" >< meta  name = "Originator"  content = "Microsoft Word 12" >< link  rel = "File-List"  href = "file:///C:%5CUsers%5CTony%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml" >< link  rel = "themeData"  href = "file:///C:%5CUsers%5CTony%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx" >< link  rel = "colorSchemeMapping"  href = "file:///C:%5CUsers%5CTony%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml" > <!--[if gte mso 9]><xml>
  <w:WordDocument>
   <w:View>Normal</w:View>
   <w:Zoom>0</w:Zoom>
   <w:TrackMoves/>
   <w:TrackFormatting/>
   <w:PunctuationKerning/>
   <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing>
   <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>
   <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>
   <w:ValidateAgainstSchemas/>
   <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
   <w:IgnoreMixedContent>false</w:IgnoreMixedContent>
   <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
   <w:DoNotPromoteQF/>
   <w:LidThemeOther>EN-US</w:LidThemeOther>
   <w:LidThemeAsian>ZH-CN</w:LidThemeAsian>
   <w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
   <w:Compatibility>
    <w:SpaceForUL/>
    <w:BalanceSingleByteDoubleByteWidth/>
    <w:DoNotLeaveBackslashAlone/>
    <w:ULTrailSpace/>
    <w:DoNotExpandShiftReturn/>
    <w:AdjustLineHeightInTable/>
    <w:BreakWrappedTables/>
    <w:SnapToGridInCell/>
    <w:WrapTextWithPunct/>
    <w:UseAsianBreakRules/>
    <w:DontGrowAutofit/>
    <w:SplitPgBreakAndParaMark/>
    <w:DontVertAlignCellWithSp/>
    <w:DontBreakConstrainedForcedTables/>
    <w:DontVertAlignInTxbx/>
    <w:Word11KerningPairs/>
    <w:CachedColBalance/>
    <w:UseFELayout/>
   </w:Compatibility>
   <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
   <m:mathPr>
    <m:mathFont m:val="Cambria Math"/>
    <m:brkBin m:val="before"/>
    <m:brkBinSub m:val="&#45;-"/>
    <m:smallFrac m:val="off"/>
    <m:dispDef/>
    <m:lMargin m:val="0"/>
    <m:rMargin m:val="0"/>
    <m:defJc m:val="centerGroup"/>
    <m:wrapIndent m:val="1440"/>
    <m:intLim m:val="subSup"/>
    <m:naryLim m:val="undOvr"/>
   </m:mathPr></w:WordDocument>
</xml><![endif]--> <!--[if gte mso 9]><xml>
  <w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true"
   DefSemiHidden="true" DefQFormat="false" DefPriority="99"
   LatentStyleCount="267">
   <w:LsdException Locked="false" Priority="0" SemiHidden="false"
    UnhideWhenUsed="false" QFormat="true" Name="Normal"/>
   <w:LsdException Locked="false" Priority="9" SemiHidden="false"
    UnhideWhenUsed="false" QFormat="true" Name="heading 1"/>
   <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2"/>
   <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3"/>
   <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4"/>
   <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5"/>
   <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6"/>
   <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7"/>
   <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8"/>
   <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9"/>
   <w:LsdException Locked="false" Priority="39" Name="toc 1"/>
   <w:LsdException Locked="false" Priority="39" Name="toc 2"/>
   <w:LsdException Locked="false" Priority="39" Name="toc 3"/>
   <w:LsdException Locked="false" Priority="39" Name="toc 4"/>
   <w:LsdException Locked="false" Priority="39" Name="toc 5"/>
   <w:LsdException Locked="false" Priority="39" Name="toc 6"/>
   <w:LsdException Locked="false" Priority="39" Name="toc 7"/>
   <w:LsdException Locked="false" Priority="39" Name="toc 8"/>
   <w:LsdException Locked="false" Priority="39" Name="toc 9"/>
   <w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption"/>
   <w:LsdException Locked="false" Priority="10" SemiHidden="false"
    UnhideWhenUsed="false" QFormat="true" Name="Title"/>
   <w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font"/>
   <w:LsdException Locked="false" Priority="11" SemiHidden="false"
    UnhideWhenUsed="false" QFormat="true" Name="Subtitle"/>
   <w:LsdException Locked="false" Priority="22" SemiHidden="false"
    UnhideWhenUsed="false" QFormat="true" Name="Strong"/>
   <w:LsdException Locked="false" Priority="20" SemiHidden="false"
    UnhideWhenUsed="false" QFormat="true" Name="Emphasis"/>
   <w:LsdException Locked="false" Priority="59" SemiHidden="false"
    UnhideWhenUsed="false" Name="Table Grid"/>
   <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text"/>
   <w:LsdException Locked="false" Priority="1" SemiHidden="false"
    UnhideWhenUsed="false" QFormat="true" Name="No Spacing"/>
   <w:LsdException Locked="false" Priority="60" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light Shading"/>
   <w:LsdException Locked="false" Priority="61" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light List"/>
   <w:LsdException Locked="false" Priority="62" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light Grid"/>
   <w:LsdException Locked="false" Priority="63" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Shading 1"/>
   <w:LsdException Locked="false" Priority="64" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Shading 2"/>
   <w:LsdException Locked="false" Priority="65" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium List 1"/>
   <w:LsdException Locked="false" Priority="66" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium List 2"/>
   <w:LsdException Locked="false" Priority="67" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 1"/>
   <w:LsdException Locked="false" Priority="68" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 2"/>
   <w:LsdException Locked="false" Priority="69" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 3"/>
   <w:LsdException Locked="false" Priority="70" SemiHidden="false"
    UnhideWhenUsed="false" Name="Dark List"/>
   <w:LsdException Locked="false" Priority="71" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful Shading"/>
   <w:LsdException Locked="false" Priority="72" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful List"/>
   <w:LsdException Locked="false" Priority="73" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful Grid"/>
   <w:LsdException Locked="false" Priority="60" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light Shading Accent 1"/>
   <w:LsdException Locked="false" Priority="61" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light List Accent 1"/>
   <w:LsdException Locked="false" Priority="62" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light Grid Accent 1"/>
   <w:LsdException Locked="false" Priority="63" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1"/>
   <w:LsdException Locked="false" Priority="64" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1"/>
   <w:LsdException Locked="false" Priority="65" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium List 1 Accent 1"/>
   <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision"/>
   <w:LsdException Locked="false" Priority="34" SemiHidden="false"
    UnhideWhenUsed="false" QFormat="true" Name="List Paragraph"/>
   <w:LsdException Locked="false" Priority="29" SemiHidden="false"
    UnhideWhenUsed="false" QFormat="true" Name="Quote"/>
   <w:LsdException Locked="false" Priority="30" SemiHidden="false"
    UnhideWhenUsed="false" QFormat="true" Name="Intense Quote"/>
   <w:LsdException Locked="false" Priority="66" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium List 2 Accent 1"/>
   <w:LsdException Locked="false" Priority="67" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1"/>
   <w:LsdException Locked="false" Priority="68" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1"/>
   <w:LsdException Locked="false" Priority="69" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1"/>
   <w:LsdException Locked="false" Priority="70" SemiHidden="false"
    UnhideWhenUsed="false" Name="Dark List Accent 1"/>
   <w:LsdException Locked="false" Priority="71" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful Shading Accent 1"/>
   <w:LsdException Locked="false" Priority="72" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful List Accent 1"/>
   <w:LsdException Locked="false" Priority="73" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful Grid Accent 1"/>
   <w:LsdException Locked="false" Priority="60" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light Shading Accent 2"/>
   <w:LsdException Locked="false" Priority="61" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light List Accent 2"/>
   <w:LsdException Locked="false" Priority="62" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light Grid Accent 2"/>
   <w:LsdException Locked="false" Priority="63" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2"/>
   <w:LsdException Locked="false" Priority="64" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2"/>
   <w:LsdException Locked="false" Priority="65" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium List 1 Accent 2"/>
   <w:LsdException Locked="false" Priority="66" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium List 2 Accent 2"/>
   <w:LsdException Locked="false" Priority="67" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2"/>
   <w:LsdException Locked="false" Priority="68" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2"/>
   <w:LsdException Locked="false" Priority="69" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2"/>
   <w:LsdException Locked="false" Priority="70" SemiHidden="false"
    UnhideWhenUsed="false" Name="Dark List Accent 2"/>
   <w:LsdException Locked="false" Priority="71" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful Shading Accent 2"/>
   <w:LsdException Locked="false" Priority="72" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful List Accent 2"/>
   <w:LsdException Locked="false" Priority="73" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful Grid Accent 2"/>
   <w:LsdException Locked="false" Priority="60" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light Shading Accent 3"/>
   <w:LsdException Locked="false" Priority="61" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light List Accent 3"/>
   <w:LsdException Locked="false" Priority="62" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light Grid Accent 3"/>
   <w:LsdException Locked="false" Priority="63" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3"/>
   <w:LsdException Locked="false" Priority="64" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3"/>
   <w:LsdException Locked="false" Priority="65" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium List 1 Accent 3"/>
   <w:LsdException Locked="false" Priority="66" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium List 2 Accent 3"/>
   <w:LsdException Locked="false" Priority="67" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3"/>
   <w:LsdException Locked="false" Priority="68" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3"/>
   <w:LsdException Locked="false" Priority="69" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3"/>
   <w:LsdException Locked="false" Priority="70" SemiHidden="false"
    UnhideWhenUsed="false" Name="Dark List Accent 3"/>
   <w:LsdException Locked="false" Priority="71" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful Shading Accent 3"/>
   <w:LsdException Locked="false" Priority="72" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful List Accent 3"/>
   <w:LsdException Locked="false" Priority="73" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful Grid Accent 3"/>
   <w:LsdException Locked="false" Priority="60" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light Shading Accent 4"/>
   <w:LsdException Locked="false" Priority="61" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light List Accent 4"/>
   <w:LsdException Locked="false" Priority="62" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light Grid Accent 4"/>
   <w:LsdException Locked="false" Priority="63" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4"/>
   <w:LsdException Locked="false" Priority="64" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4"/>
   <w:LsdException Locked="false" Priority="65" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium List 1 Accent 4"/>
   <w:LsdException Locked="false" Priority="66" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium List 2 Accent 4"/>
   <w:LsdException Locked="false" Priority="67" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4"/>
   <w:LsdException Locked="false" Priority="68" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4"/>
   <w:LsdException Locked="false" Priority="69" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4"/>
   <w:LsdException Locked="false" Priority="70" SemiHidden="false"
    UnhideWhenUsed="false" Name="Dark List Accent 4"/>
   <w:LsdException Locked="false" Priority="71" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful Shading Accent 4"/>
   <w:LsdException Locked="false" Priority="72" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful List Accent 4"/>
   <w:LsdException Locked="false" Priority="73" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful Grid Accent 4"/>
   <w:LsdException Locked="false" Priority="60" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light Shading Accent 5"/>
   <w:LsdException Locked="false" Priority="61" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light List Accent 5"/>
   <w:LsdException Locked="false" Priority="62" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light Grid Accent 5"/>
   <w:LsdException Locked="false" Priority="63" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5"/>
   <w:LsdException Locked="false" Priority="64" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5"/>
   <w:LsdException Locked="false" Priority="65" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium List 1 Accent 5"/>
   <w:LsdException Locked="false" Priority="66" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium List 2 Accent 5"/>
   <w:LsdException Locked="false" Priority="67" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5"/>
   <w:LsdException Locked="false" Priority="68" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5"/>
   <w:LsdException Locked="false" Priority="69" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5"/>
   <w:LsdException Locked="false" Priority="70" SemiHidden="false"
    UnhideWhenUsed="false" Name="Dark List Accent 5"/>
   <w:LsdException Locked="false" Priority="71" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful Shading Accent 5"/>
   <w:LsdException Locked="false" Priority="72" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful List Accent 5"/>
   <w:LsdException Locked="false" Priority="73" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful Grid Accent 5"/>
   <w:LsdException Locked="false" Priority="60" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light Shading Accent 6"/>
   <w:LsdException Locked="false" Priority="61" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light List Accent 6"/>
   <w:LsdException Locked="false" Priority="62" SemiHidden="false"
    UnhideWhenUsed="false" Name="Light Grid Accent 6"/>
   <w:LsdException Locked="false" Priority="63" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6"/>
   <w:LsdException Locked="false" Priority="64" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6"/>
   <w:LsdException Locked="false" Priority="65" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium List 1 Accent 6"/>
   <w:LsdException Locked="false" Priority="66" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium List 2 Accent 6"/>
   <w:LsdException Locked="false" Priority="67" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6"/>
   <w:LsdException Locked="false" Priority="68" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6"/>
   <w:LsdException Locked="false" Priority="69" SemiHidden="false"
    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6"/>
   <w:LsdException Locked="false" Priority="70" SemiHidden="false"
    UnhideWhenUsed="false" Name="Dark List Accent 6"/>
   <w:LsdException Locked="false" Priority="71" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful Shading Accent 6"/>
   <w:LsdException Locked="false" Priority="72" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful List Accent 6"/>
   <w:LsdException Locked="false" Priority="73" SemiHidden="false"
    UnhideWhenUsed="false" Name="Colorful Grid Accent 6"/>
   <w:LsdException Locked="false" Priority="19" SemiHidden="false"
    UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis"/>
   <w:LsdException Locked="false" Priority="21" SemiHidden="false"
    UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis"/>
   <w:LsdException Locked="false" Priority="31" SemiHidden="false"
    UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference"/>
   <w:LsdException Locked="false" Priority="32" SemiHidden="false"
    UnhideWhenUsed="false" QFormat="true" Name="Intense Reference"/>
   <w:LsdException Locked="false" Priority="33" SemiHidden="false"
    UnhideWhenUsed="false" QFormat="true" Name="Book Title"/>
   <w:LsdException Locked="false" Priority="37" Name="Bibliography"/>
   <w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading"/>
  </w:LatentStyles>
</xml><![endif]--> < style >
<!--
  /* Font Definitions */
  @font-face
     {font-family:Wingdings;
     panose-1:5 0 0 0 0 0 0 0 0 0;
     mso-font-charset:2;
     mso-generic-font-family:auto;
     mso-font-pitch:variable;
     mso-font-signature:0 268435456 0 0 -2147483648 0;}
@font-face
     {font-family:宋体;
     panose-1:2 1 6 0 3 1 1 1 1 1;
     mso-font-alt:SimSun;
     mso-font-charset:134;
     mso-generic-font-family:auto;
     mso-font-pitch:variable;
     mso-font-signature:3 680460288 22 0 262145 0;}
@font-face
     {font-family:"Cambria Math";
     panose-1:2 4 5 3 5 4 6 3 2 4;
     mso-font-charset:1;
     mso-generic-font-family:roman;
     mso-font-format:other;
     mso-font-pitch:variable;
     mso-font-signature:0 0 0 0 0 0;}
@font-face
     {font-family:Calibri;
     panose-1:2 15 5 2 2 2 4 3 2 4;
     mso-font-charset:0;
     mso-generic-font-family:swiss;
     mso-font-pitch:variable;
     mso-font-signature:-1610611985 1073750139 0 0 159 0;}
@font-face
     {font-family:"\@宋体";
     panose-1:2 1 6 0 3 1 1 1 1 1;
     mso-font-charset:134;
     mso-generic-font-family:auto;
     mso-font-pitch:variable;
     mso-font-signature:3 680460288 22 0 262145 0;}
  /* Style Definitions */
  p.MsoNormal, li.MsoNormal, div.MsoNormal
     {mso-style-unhide:no;
     mso-style-qformat:yes;
     mso-style-parent:"";
     margin:0cm;
     margin-bottom:.0001pt;
     text-align:justify;
     text-justify:inter-ideograph;
     mso-pagination:none;
     font-size:10.5pt;
     mso-bidi-font-size:11.0pt;
     font-family:"Calibri","sans-serif";
     mso-ascii-font-family:Calibri;
     mso-ascii-theme-font:minor-latin;
     mso-fareast-font-family:宋体;
     mso-fareast-theme-font:minor-fareast;
     mso-hansi-font-family:Calibri;
     mso-hansi-theme-font:minor-latin;
     mso-bidi-font-family:"Times New Roman";
     mso-bidi-theme-font:minor-bidi;
     mso-font-kerning:1.0pt;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
     {mso-style-priority:34;
     mso-style-unhide:no;
     mso-style-qformat:yes;
     margin:0cm;
     margin-bottom:.0001pt;
     text-align:justify;
     text-justify:inter-ideograph;
     text-indent:21.0pt;
     mso-char-indent-count:2.0;
     mso-pagination:none;
     font-size:10.5pt;
     mso-bidi-font-size:11.0pt;
     font-family:"Calibri","sans-serif";
     mso-ascii-font-family:Calibri;
     mso-ascii-theme-font:minor-latin;
     mso-fareast-font-family:宋体;
     mso-fareast-theme-font:minor-fareast;
     mso-hansi-font-family:Calibri;
     mso-hansi-theme-font:minor-latin;
     mso-bidi-font-family:"Times New Roman";
     mso-bidi-theme-font:minor-bidi;
     mso-font-kerning:1.0pt;}
.MsoChpDefault
     {mso-style-type:export-only;
     mso-default-props:yes;
     mso-bidi-font-family:"Times New Roman";
     mso-bidi-theme-font:minor-bidi;}
  /* Page Definitions */
  @page
     {mso-page-border-surround-header:no;
     mso-page-border-surround-footer:no;}
@page Section1
     {size:612.0pt 792.0pt;
     margin:72.0pt 90.0pt 72.0pt 90.0pt;
     mso-header-margin:36.0pt;
     mso-footer-margin:36.0pt;
     mso-paper-source:0;}
div.Section1
     {page:Section1;}
  /* List Definitions */
  @list l0
     {mso-list-id:1862015348;
     mso-list-type:hybrid;
     mso-list-template-ids:1873287652 67698689 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l0:level1
     {mso-level-number-format:bullet;
     mso-level-text:l;
     mso-level-tab-stop:none;
     mso-level-number-position:left;
     margin-left:21.0pt;
     text-indent:-21.0pt;
     font-family:Wingdings;}
ol
     {margin-bottom:0cm;}
ul
     {margin-bottom:0cm;}
-->
</ style > <!--[if gte mso 10]>
<style>
  /* Style Definitions */
  table.MsoNormalTable
     {mso-style-name:普通表格;
     mso-tstyle-rowband-size:0;
     mso-tstyle-colband-size:0;
     mso-style-noshow:yes;
     mso-style-priority:99;
     mso-style-qformat:yes;
     mso-style-parent:"";
     mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
     mso-para-margin:0cm;
     mso-para-margin-bottom:.0001pt;
     mso-pagination:widow-orphan;
     font-size:10.5pt;
     mso-bidi-font-size:11.0pt;
     font-family:"Calibri","sans-serif";
     mso-ascii-font-family:Calibri;
     mso-ascii-theme-font:minor-latin;
     mso-hansi-font-family:Calibri;
     mso-hansi-theme-font:minor-latin;
     mso-font-kerning:1.0pt;}
</style>
<![endif]-->
 
< p  class = "MsoNormal" >< span  lang = "EN-US" >Abcde< p ></ p >
 
< p  class = "MsoListParagraph"  style = "margin-left: 21pt; text-indent: -21pt;" > <!--[if !supportLists]--> < span  style = "font-family: Wingdings;"  lang = "EN-US" >< span  style = "" >l< span  style = "font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;" >&nbsp;  <!--[endif]--> < span  style = "font-family: 宋体;" >你好< p ></ p >
 
< p  class = "MsoListParagraph"  style = "margin-left: 21pt; text-indent: -21pt;" > <!--[if !supportLists]--> < span  style = "font-family: Wingdings;"  lang = "EN-US" >< span  style = "" >l< span  style = "font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;" >&nbsp;  <!--[endif]--> < span  lang = "EN-US" >Aaa< span  style = "color: rgb(192, 0, 0);" >aaaaaaaaaaa< p ></ p >
 
< p  class = "MsoListParagraph"  style = "margin-left: 21pt; text-indent: -21pt;" > <!--[if !supportLists]--> < span  style = "font-family: Wingdings;"  lang = "EN-US" >< span  style = "" >l< span  style = "font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;" >&nbsp;  <!--[endif]--> < u >< span  lang = "EN-US" >Bbbb< b  style = "" >bbbb</ b >bbb< o:p ></ o:p ></ u >< p ></ p >
 
< p  class = "MsoListParagraph"  style = "margin-left: 21pt; text-indent: -21pt;" > <!--[if !supportLists]--> < span  style = "font-family: Wingdings;"  lang = "EN-US" >< span  style = "" >l< span  style = "font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;" >&nbsp;  <!--[endif]--> < u >< span  lang = "EN-US" >Ccccccccccccc< o:p ></ o:p ></ u >< p ></ p >
如果你原来只允许用户输入1000个字符,你在数据库里面只给了nvarchar(1000),这就意味着这东西保存进去必然会被截断,而且可能连正文部分都没保存进去,这肯定是不允许的。这也解释了为什么主流html编辑器提供了专门的Word粘贴功能。

上面那段东西其实真正有用的东西也就是下面这么点:

?
1
2
3
4
5
< p >< span >Abcde</ p >
< p  style = "margin-left: 21pt; text-indent: -21pt;" >< span  style = "font-family: Wingdings;" >< span  style = "" >l< span  style = "font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;" >&nbsp; < span  style = "font-family: 宋体;" >你好< p ></ p >
< p  style = "margin-left: 21pt; text-indent: -21pt;" >< span  style = "font-family: Wingdings;" >< span  style = "" >l< span  style = "font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;" >&nbsp; < span >Aaa< span  style = "color: rgb(192, 0, 0);" >aaaaaaaaaaa< p ></ p >
< p  style = "margin-left: 21pt; text-indent: -21pt;" >< span  style = "font-family: Wingdings;" >< span  style = "" >l< span  style = "font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;" >&nbsp; < u >< span >Bbbb< b  style = "" >bbbb</ b >bbb</ u >< p ></ p >
< p  style = "margin-left: 21pt; text-indent: -21pt;" >< span  style = "font-family: Wingdings;" >< span  style = "" >l< span  style = "font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;" >&nbsp; < u >< span >Ccccccccccccc</ u >< p ></ p >

所以这就是我们需要的过滤结果,接下来就看你JavaScript正则表达式有多牛了!

我主要设置了以下这些规则:

1. 去除\r\n, \n, \r

2. 去除文本开头的&nbsp;和文本结尾的&nbsp;

3. 去除Word的条件注释(如<!—[if gte mso 9]>)

4. 去除script, xml:*, img, meta,link, style, *:*标签

5. 将Word的抬头转换成<strong>标签

6. 去除lang属性

7. 去除或替换所有没有意义的样式标签,如mso-padding-alt, mso-margin-top-alt,horiz-align, mso-default-width, mso-zero-height等

对应代码如下:

?
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
function  FilterPasteWord(str)
{
     //remove link break
     str = str.replace(/\r\n|\n|\r/ig,  "" );
     //remove &nbsp; entities at the start of contents
     str = str.replace(/^\s*(&nbsp;)+/ig, "" );
     //remove &nbsp; entities at the end of contents
     str = str.replace(/(&nbsp;|<br[^>]*>)+\s*$/ig, "" );
     // Word comments like conditional comments etc
     str = str.replace(/<!--[\s\S]*?-->/ig,  "" );   
     // Remove comments, scripts (e.g., msoShowComment), XML tag, VML content, MS Office namespaced tags, and a few other tags
     str = str.replace(/<(!|script[^>]*>.*?<\/script(?=[>\s])|\/?(\?xml(:\w+)?|xml|img|meta|link|style|\w:\w+)(?=[\s\/>]))[^>]*>/gi, "" );
 
     //convert word headers to strong
     str = str.replace(/<p [^>]*class= "?MsoHeading" ?[^>]*>(.*?)<\/p>/gi,  "<p><strong>$1</strong></p>" );
     //remove lang attribute
     str = str.replace(/(lang)\s*=\s*([\ '\"]?)[\w-]+\2/ig, "");
     // Examine all styles: delete junk, transform some, and keep the rest
     str = str.replace(/(<[a-z][^>]*)\sstyle="([^"]*)"/gi,function(str, tag, style) {
                                 var n = [],
                                     i = 0,
                                     s = style.trim().replace(/&quot;/gi, "' ").split( ";" );
 
                                 // Examine each style definition within the tag's style attribute
                                 for ( var  i=0;i<s.length;i++)
                                 {
                                     v=s[i];
                                     var  name, value,
                                     parts = v.split( ":" );
 
                                     if  (parts.length == 2) {
                                         name = parts[0].toLowerCase();
                                         value = parts[1].toLowerCase();
                                         // Translate certain MS Office styles into their CSS equivalents
                                         switch  (name) {
                                             case  "mso-padding-alt" :
                                             case  "mso-padding-top-alt" :
                                             case  "mso-padding-right-alt" :
                                             case  "mso-padding-bottom-alt" :
                                             case  "mso-padding-left-alt" :
                                             case  "mso-margin-alt" :
                                             case  "mso-margin-top-alt" :
                                             case  "mso-margin-right-alt" :
                                             case  "mso-margin-bottom-alt" :
                                             case  "mso-margin-left-alt" :
                                             case  "mso-table-layout-alt" :
                                             case  "mso-height" :
                                             case  "mso-width" :
                                             case  "mso-vertical-align-alt" :
                                                 n[i++] = name.replace(/^mso-|-alt$/g,  "" ) +  ":"  + ensureUnits(value);
                                                 continue ;
 
                                             case  "horiz-align" :
                                                 n[i++] =  "text-align:"  + value;
                                                 continue ;
 
                                             case  "vert-align" :
                                                 n[i++] =  "vertical-align:"  + value;
                                                 continue ;
 
                                             case  "font-color" :
                                             case  "mso-foreground" :
                                                 n[i++] =  "color:"  + value;
                                                 continue ;
 
                                             case  "mso-background" :
                                             case  "mso-highlight" :
                                                 n[i++] =  "background:"  + value;
                                                 continue ;
 
                                             case  "mso-default-height" :
                                                 n[i++] =  "min-height:"  + ensureUnits(value);
                                                 continue ;
 
                                             case  "mso-default-width" :
                                                 n[i++] =  "min-; + ensureUnits(value);
                                                 continue;
 
                                             case " mso-padding-between-alt ":
                                                 n[i++] = " border-collapse:separate;border-spacing: " + ensureUnits(value);
                                                 continue;
 
                                             case " text-line-through ":
                                                 if ((value == " single ") || (value == " double ")) {
                                                     n[i++] = " text-decoration:line-through ";
                                                 }
                                                 continue;
 
                                             case " mso-zero-height ":
                                                 if (value == " yes ") {
                                                     n[i++] = " display:none ";
                                                 }
                                                 continue;
                                         }
                                         // Eliminate all MS Office style definitions that have no CSS equivalent by examining the first characters in the name
                                         if (/^(mso|column|font-emph|lang|layout|line-break|list-image|nav|panose|punct|row|ruby|sep|size|src|tab-|table-border|text-(?:align|decor|indent|trans)|top-bar|version|vnd|word-break)/.test(name)) {
                                             continue;
                                         }
                                         // If it reached this point, it must be a valid CSS style
                                         n[i++] = name + " : " + parts[1];        // Lower-case name, but keep value case
                                     }
                                 }
                                 // If style attribute contained any valid styles the re-write it; otherwise delete style attribute.
                                 if (i > 0) {
                                     return tag + ' style=" ' + n.join(' ; ') + ' "';
                                 }  else  {
                                     return  tag;
                                 }
                             });
 
     return  str;
}

到此,你应该对这项技术有了很详细的了解了。当然具体过滤哪些东西,你还是要多实践多测试,比如前两天客户一不小心把youtube视频贴近系统里面了,于是我又过滤了object, embed, param标签。当然有了这样一个模型之后,规则的维护变得很方便,无形中降低了代码的维护成本。










本文转自 瞿杰 51CTO博客,原文链接:http://blog.51cto.com/tonyqus/1131887,如需转载请自行联系原作者
目录
相关文章
程序技术好文:自定义GridLookUpEdit编辑器
程序技术好文:自定义GridLookUpEdit编辑器
|
移动开发 前端开发 JavaScript
基于 HTML5 和 Canvas 开发的在线图片编辑器
基于 HTML5 和 Canvas 开发的在线图片编辑器
964 0
|
Linux UED C++
MarkEdit:相信做技术的同学都会喜欢这个编辑器的,MarkEdit编辑器,让Markdown编辑变得简单而强大,超赞~~~
嗨,大家好,我是小华同学。MarkEdit是一款跨平台的Markdown编辑器,支持Windows、macOS和Linux,提供实时预览、快捷键、自定义主题和插件系统等核心功能。它适用于日常笔记、技术文档、博客写作、学术论文和电子书制作等多种场景。
2247 31
|
前端开发 JavaScript 搜索推荐
HTML与CSS在Web组件化中的核心作用及前端技术趋势
本文探讨了HTML与CSS在Web组件化中的核心作用及前端技术趋势。从结构定义、语义化到样式封装与布局控制,两者不仅提升了代码复用率和可维护性,还通过响应式设计、动态样式等技术增强了用户体验。面对兼容性、代码复杂度等挑战,文章提出了相应的解决策略,强调了持续创新的重要性,旨在构建高效、灵活的Web应用。
657 6
|
机器学习/深度学习 移动开发 自然语言处理
HTML5与神经网络技术的结合有哪些其他应用
HTML5与神经网络技术的结合有哪些其他应用
320 3
|
机器学习/深度学习 移动开发 自然语言处理
HTML5与神经网络技术的结合有哪些
HTML5与神经网络技术的结合有哪些
387 2
|
存储 移动开发 前端开发
HTML5时代来临,这些新特性你掌握了吗?一篇文章带你玩转Web前端技术潮流!
【8月更文挑战第26天】HTML5(简称H5)作为新一代Web标准,相比HTML4带来了诸多增强功能。
691 2
|
前端开发 JavaScript 数据安全/隐私保护
【海贼王航海日志:前端技术探索】HTML你学会了吗?(二)
【海贼王航海日志:前端技术探索】HTML你学会了吗?(二)
294 1
|
编解码 移动开发 前端开发
【海贼王航海日志:前端技术探索】HTML你学会了吗?(一)
【海贼王航海日志:前端技术探索】HTML你学会了吗?(一)
345 1
|
移动开发 前端开发 JavaScript
HTML与现代Web开发的不同技术
【10月更文挑战第11天】HTML与现代Web开发的不同技术
400 0