|
1
|
|
|
/** |
|
2
|
|
|
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. |
|
3
|
|
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
( function() { |
|
7
|
|
|
CKEDITOR.on( 'dialogDefinition', function( ev ) { |
|
|
|
|
|
|
8
|
|
|
var tab, |
|
9
|
|
|
name = ev.data.name, |
|
10
|
|
|
definition = ev.data.definition; |
|
11
|
|
|
|
|
12
|
|
|
if ( name == 'link' ) { |
|
13
|
|
|
definition.removeContents( 'target' ); |
|
14
|
|
|
definition.removeContents( 'upload' ); |
|
15
|
|
|
definition.removeContents( 'advanced' ); |
|
16
|
|
|
tab = definition.getContents( 'info' ); |
|
17
|
|
|
tab.remove( 'emailSubject' ); |
|
18
|
|
|
tab.remove( 'emailBody' ); |
|
19
|
|
|
} else if ( name == 'image' ) { |
|
20
|
|
|
definition.removeContents( 'advanced' ); |
|
21
|
|
|
tab = definition.getContents( 'Link' ); |
|
22
|
|
|
tab.remove( 'cmbTarget' ); |
|
23
|
|
|
tab = definition.getContents( 'info' ); |
|
24
|
|
|
tab.remove( 'txtAlt' ); |
|
25
|
|
|
tab.remove( 'basic' ); |
|
26
|
|
|
} |
|
27
|
|
|
} ); |
|
28
|
|
|
|
|
29
|
|
|
var bbcodeMap = { b: 'strong', u: 'u', i: 'em', color: 'span', size: 'span', left: 'div', right: 'div', center: 'div', justify: 'div', quote: 'blockquote', code: 'code', url: 'a', email: 'span', img: 'span', '*': 'li', list: 'ol' }, |
|
30
|
|
|
convertMap = { strong: 'b', b: 'b', u: 'u', em: 'i', i: 'i', code: 'code', li: '*' }, |
|
31
|
|
|
tagnameMap = { strong: 'b', em: 'i', u: 'u', li: '*', ul: 'list', ol: 'list', code: 'code', a: 'link', img: 'img', blockquote: 'quote' }, |
|
32
|
|
|
stylesMap = { color: 'color', size: 'font-size', left: 'text-align', center: 'text-align', right: 'text-align', justify: 'text-align' }, |
|
33
|
|
|
attributesMap = { url: 'href', email: 'mailhref', quote: 'cite', list: 'listType' }; |
|
34
|
|
|
|
|
35
|
|
|
// List of block-like tags. |
|
36
|
|
|
var dtd = CKEDITOR.dtd, |
|
37
|
|
|
blockLikeTags = CKEDITOR.tools.extend( { table: 1 }, dtd.$block, dtd.$listItem, dtd.$tableContent, dtd.$list ); |
|
38
|
|
|
|
|
39
|
|
|
var semicolonFixRegex = /\s*(?:;\s*|$)/; |
|
40
|
|
|
|
|
41
|
|
|
function serializeStyleText( stylesObject ) { |
|
42
|
|
|
var styleText = ''; |
|
43
|
|
|
for ( var style in stylesObject ) { |
|
|
|
|
|
|
44
|
|
|
var styleVal = stylesObject[ style ], |
|
45
|
|
|
text = ( style + ':' + styleVal ).replace( semicolonFixRegex, ';' ); |
|
46
|
|
|
|
|
47
|
|
|
styleText += text; |
|
48
|
|
|
} |
|
49
|
|
|
return styleText; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// Maintain the map of smiley-to-description. |
|
53
|
|
|
// jscs:disable maximumLineLength |
|
54
|
|
|
var smileyMap = { smiley: ':)', sad: ':(', wink: ';)', laugh: ':D', cheeky: ':P', blush: ':*)', surprise: ':-o', indecision: ':|', angry: '>:(', angel: 'o:)', cool: '8-)', devil: '>:-)', crying: ';(', kiss: ':-*' }, |
|
55
|
|
|
// jscs:enable maximumLineLength |
|
56
|
|
|
smileyReverseMap = {}, |
|
57
|
|
|
smileyRegExp = []; |
|
58
|
|
|
|
|
59
|
|
|
// Build regexp for the list of smiley text. |
|
60
|
|
|
for ( var i in smileyMap ) { |
|
|
|
|
|
|
61
|
|
|
smileyReverseMap[ smileyMap[ i ] ] = i; |
|
62
|
|
|
smileyRegExp.push( smileyMap[ i ].replace( /\(|\)|\:|\/|\*|\-|\|/g, function( match ) { |
|
63
|
|
|
return '\\' + match; |
|
64
|
|
|
} ) ); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
smileyRegExp = new RegExp( smileyRegExp.join( '|' ), 'g' ); |
|
68
|
|
|
|
|
69
|
|
|
var decodeHtml = ( function() { |
|
70
|
|
|
var regex = [], |
|
71
|
|
|
entities = { |
|
72
|
|
|
nbsp: '\u00A0', // IE | FF |
|
73
|
|
|
shy: '\u00AD' // IE |
|
74
|
|
|
}; |
|
75
|
|
|
|
|
76
|
|
|
for ( var entity in entities ) |
|
|
|
|
|
|
77
|
|
|
regex.push( entity ); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
regex = new RegExp( '&(' + regex.join( '|' ) + ');', 'g' ); |
|
80
|
|
|
|
|
81
|
|
|
return function( html ) { |
|
82
|
|
|
return html.replace( regex, function( match, entity ) { |
|
83
|
|
|
return entities[ entity ]; |
|
84
|
|
|
} ); |
|
85
|
|
|
}; |
|
86
|
|
|
} )(); |
|
87
|
|
|
|
|
88
|
|
|
CKEDITOR.BBCodeParser = function() { |
|
89
|
|
|
this._ = { |
|
90
|
|
|
bbcPartsRegex: /(?:\[([^\/\]=]*?)(?:=([^\]]*?))?\])|(?:\[\/([a-z]{1,16})\])/ig |
|
91
|
|
|
}; |
|
92
|
|
|
}; |
|
93
|
|
|
|
|
94
|
|
|
CKEDITOR.BBCodeParser.prototype = { |
|
95
|
|
|
parse: function( bbcode ) { |
|
96
|
|
|
var parts, part, |
|
97
|
|
|
lastIndex = 0; |
|
98
|
|
|
|
|
99
|
|
|
while ( ( parts = this._.bbcPartsRegex.exec( bbcode ) ) ) { |
|
100
|
|
|
var tagIndex = parts.index; |
|
101
|
|
|
if ( tagIndex > lastIndex ) { |
|
102
|
|
|
var text = bbcode.substring( lastIndex, tagIndex ); |
|
103
|
|
|
this.onText( text, 1 ); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
lastIndex = this._.bbcPartsRegex.lastIndex; |
|
107
|
|
|
|
|
108
|
|
|
// "parts" is an array with the following items: |
|
109
|
|
|
// 0 : The entire match for opening/closing tags and line-break; |
|
110
|
|
|
// 1 : line-break; |
|
111
|
|
|
// 2 : open of tag excludes option; |
|
112
|
|
|
// 3 : tag option; |
|
113
|
|
|
// 4 : close of tag; |
|
114
|
|
|
|
|
115
|
|
|
part = ( parts[ 1 ] || parts[ 3 ] || '' ).toLowerCase(); |
|
116
|
|
|
// Unrecognized tags should be delivered as a simple text (https://dev.ckeditor.com/ticket/7860). |
|
117
|
|
|
if ( part && !bbcodeMap[ part ] ) { |
|
118
|
|
|
this.onText( parts[ 0 ] ); |
|
119
|
|
|
continue; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
// Opening tag |
|
123
|
|
|
if ( parts[ 1 ] ) { |
|
124
|
|
|
var tagName = bbcodeMap[ part ], |
|
125
|
|
|
attribs = {}, |
|
126
|
|
|
styles = {}, |
|
127
|
|
|
optionPart = parts[ 2 ]; |
|
128
|
|
|
|
|
129
|
|
|
// Special handling of justify tags, these provide the alignment as a tag name (#2248). |
|
130
|
|
|
if ( part == 'left' || part == 'right' || part == 'center' || part == 'justify' ) { |
|
131
|
|
|
optionPart = part; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if ( optionPart ) { |
|
135
|
|
|
if ( part == 'list' ) { |
|
136
|
|
|
if ( !isNaN( optionPart ) ) |
|
137
|
|
|
optionPart = 'decimal'; |
|
|
|
|
|
|
138
|
|
|
else if ( /^[a-z]+$/.test( optionPart ) ) |
|
139
|
|
|
optionPart = 'lower-alpha'; |
|
|
|
|
|
|
140
|
|
|
else if ( /^[A-Z]+$/.test( optionPart ) ) |
|
141
|
|
|
optionPart = 'upper-alpha'; |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if ( stylesMap[ part ] ) { |
|
145
|
|
|
// Font size represents percentage. |
|
146
|
|
|
if ( part == 'size' ) { |
|
147
|
|
|
optionPart += '%'; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
styles[ stylesMap[ part ] ] = optionPart; |
|
151
|
|
|
attribs.style = serializeStyleText( styles ); |
|
152
|
|
|
} else if ( attributesMap[ part ] ) { |
|
153
|
|
|
// All the input BBCode is encoded at the beginning so <> characters in the textual part |
|
154
|
|
|
// are later correctly preserved in HTML. However... it affects parts that now become |
|
155
|
|
|
// attributes, so we need to revert that. As a matter of fact, the content should not be |
|
156
|
|
|
// encoded at the beginning, but only later when creating text nodes (encoding should be more precise) |
|
157
|
|
|
// but it's too late not for such changes. |
|
158
|
|
|
attribs[ attributesMap[ part ] ] = CKEDITOR.tools.htmlDecode( optionPart ); |
|
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
// Two special handling - image and email, protect them |
|
163
|
|
|
// as "span" with an attribute marker. |
|
164
|
|
|
if ( part == 'email' || part == 'img' ) |
|
165
|
|
|
attribs.bbcode = part; |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
this.onTagOpen( tagName, attribs, CKEDITOR.dtd.$empty[ tagName ] ); |
|
168
|
|
|
} |
|
169
|
|
|
// Closing tag |
|
170
|
|
|
else if ( parts[ 3 ] ) { |
|
171
|
|
|
this.onTagClose( bbcodeMap[part] ); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
if ( bbcode.length > lastIndex ) |
|
176
|
|
|
this.onText( bbcode.substring( lastIndex, bbcode.length ), 1 ); |
|
|
|
|
|
|
177
|
|
|
} |
|
178
|
|
|
}; |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Creates a {@link CKEDITOR.htmlParser.fragment} from an HTML string. |
|
182
|
|
|
* |
|
183
|
|
|
* var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<b>Sample</b> Text' ); |
|
184
|
|
|
* alert( fragment.children[ 0 ].name ); // 'b' |
|
185
|
|
|
* alert( fragment.children[ 1 ].value ); // ' Text' |
|
186
|
|
|
* |
|
187
|
|
|
* @static |
|
188
|
|
|
* @member CKEDITOR.htmlParser.fragment |
|
189
|
|
|
* @param {String} source The HTML to be parsed, filling the fragment. |
|
190
|
|
|
* @returns {CKEDITOR.htmlParser.fragment} The fragment created. |
|
191
|
|
|
*/ |
|
192
|
|
|
CKEDITOR.htmlParser.fragment.fromBBCode = function( source ) { |
|
193
|
|
|
var parser = new CKEDITOR.BBCodeParser(), |
|
|
|
|
|
|
194
|
|
|
fragment = new CKEDITOR.htmlParser.fragment(), |
|
195
|
|
|
pendingInline = [], |
|
196
|
|
|
pendingBrs = 0, |
|
197
|
|
|
currentNode = fragment, |
|
198
|
|
|
returnPoint; |
|
199
|
|
|
|
|
200
|
|
|
function checkPending( newTagName ) { |
|
201
|
|
|
if ( pendingInline.length > 0 ) { |
|
202
|
|
|
for ( var i = 0; i < pendingInline.length; i++ ) { |
|
203
|
|
|
var pendingElement = pendingInline[ i ], |
|
204
|
|
|
pendingName = pendingElement.name, |
|
205
|
|
|
pendingDtd = CKEDITOR.dtd[ pendingName ], |
|
|
|
|
|
|
206
|
|
|
currentDtd = currentNode.name && CKEDITOR.dtd[ currentNode.name ]; |
|
|
|
|
|
|
207
|
|
|
|
|
208
|
|
|
if ( ( !currentDtd || currentDtd[ pendingName ] ) && ( !newTagName || !pendingDtd || pendingDtd[ newTagName ] || !CKEDITOR.dtd[ newTagName ] ) ) { |
|
209
|
|
|
// Get a clone for the pending element. |
|
210
|
|
|
pendingElement = pendingElement.clone(); |
|
211
|
|
|
|
|
212
|
|
|
// Add it to the current node and make it the current, |
|
213
|
|
|
// so the new element will be added inside of it. |
|
214
|
|
|
pendingElement.parent = currentNode; |
|
215
|
|
|
currentNode = pendingElement; |
|
216
|
|
|
|
|
217
|
|
|
// Remove the pending element (back the index by one |
|
218
|
|
|
// to properly process the next entry). |
|
219
|
|
|
pendingInline.splice( i, 1 ); |
|
220
|
|
|
i--; |
|
|
|
|
|
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
function checkPendingBrs( tagName, closing ) { |
|
227
|
|
|
var len = currentNode.children.length, |
|
228
|
|
|
previous = len > 0 && currentNode.children[ len - 1 ], |
|
229
|
|
|
lineBreakParent = !previous && writer.getRule( tagnameMap[ currentNode.name ], 'breakAfterOpen' ), |
|
230
|
|
|
lineBreakPrevious = previous && previous.type == CKEDITOR.NODE_ELEMENT && writer.getRule( tagnameMap[ previous.name ], 'breakAfterClose' ), |
|
|
|
|
|
|
231
|
|
|
lineBreakCurrent = tagName && writer.getRule( tagnameMap[ tagName ], closing ? 'breakBeforeClose' : 'breakBeforeOpen' ); |
|
232
|
|
|
|
|
233
|
|
|
if ( pendingBrs && ( lineBreakParent || lineBreakPrevious || lineBreakCurrent ) ) |
|
234
|
|
|
pendingBrs--; |
|
|
|
|
|
|
235
|
|
|
|
|
236
|
|
|
// 1. Either we're at the end of block, where it requires us to compensate the br filler |
|
237
|
|
|
// removing logic (from htmldataprocessor). |
|
238
|
|
|
// 2. Or we're at the end of pseudo block, where it requires us to compensate |
|
239
|
|
|
// the bogus br effect. |
|
240
|
|
|
if ( pendingBrs && tagName in blockLikeTags ) |
|
241
|
|
|
pendingBrs++; |
|
|
|
|
|
|
242
|
|
|
|
|
243
|
|
|
while ( pendingBrs && pendingBrs-- ) |
|
|
|
|
|
|
244
|
|
|
currentNode.children.push( previous = new CKEDITOR.htmlParser.element( 'br' ) ); |
|
|
|
|
|
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
function addElement( node, target ) { |
|
248
|
|
|
checkPendingBrs( node.name, 1 ); |
|
249
|
|
|
|
|
250
|
|
|
target = target || currentNode || fragment; |
|
251
|
|
|
|
|
252
|
|
|
var len = target.children.length, |
|
253
|
|
|
previous = len > 0 && target.children[ len - 1 ] || null; |
|
254
|
|
|
|
|
255
|
|
|
node.previous = previous; |
|
256
|
|
|
node.parent = target; |
|
257
|
|
|
|
|
258
|
|
|
target.children.push( node ); |
|
259
|
|
|
|
|
260
|
|
|
if ( node.returnPoint ) { |
|
261
|
|
|
currentNode = node.returnPoint; |
|
262
|
|
|
delete node.returnPoint; |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
parser.onTagOpen = function( tagName, attributes ) { |
|
267
|
|
|
var element = new CKEDITOR.htmlParser.element( tagName, attributes ); |
|
|
|
|
|
|
268
|
|
|
|
|
269
|
|
|
// This is a tag to be removed if empty, so do not add it immediately. |
|
270
|
|
|
if ( CKEDITOR.dtd.$removeEmpty[ tagName ] ) { |
|
271
|
|
|
pendingInline.push( element ); |
|
272
|
|
|
return; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
var currentName = currentNode.name; |
|
276
|
|
|
|
|
277
|
|
|
var currentDtd = currentName && ( CKEDITOR.dtd[ currentName ] || ( currentNode._.isBlockLike ? CKEDITOR.dtd.div : CKEDITOR.dtd.span ) ); |
|
278
|
|
|
|
|
279
|
|
|
// If the element cannot be child of the current element. |
|
280
|
|
|
if ( currentDtd && !currentDtd[ tagName ] ) { |
|
281
|
|
|
var reApply = false, |
|
282
|
|
|
addPoint; // New position to start adding nodes. |
|
283
|
|
|
|
|
284
|
|
|
// If the element name is the same as the current element name, |
|
285
|
|
|
// then just close the current one and append the new one to the |
|
286
|
|
|
// parent. This situation usually happens with <p>, <li>, <dt> and |
|
287
|
|
|
// <dd>, specially in IE. Do not enter in this if block in this case. |
|
288
|
|
|
if ( tagName == currentName ) |
|
289
|
|
|
addElement( currentNode, currentNode.parent ); |
|
|
|
|
|
|
290
|
|
|
else if ( tagName in CKEDITOR.dtd.$listItem ) { |
|
291
|
|
|
parser.onTagOpen( 'ul', {} ); |
|
292
|
|
|
addPoint = currentNode; |
|
293
|
|
|
reApply = true; |
|
294
|
|
|
} else { |
|
295
|
|
|
addElement( currentNode, currentNode.parent ); |
|
296
|
|
|
|
|
297
|
|
|
// The current element is an inline element, which |
|
298
|
|
|
// cannot hold the new one. Put it in the pending list, |
|
299
|
|
|
// and try adding the new one after it. |
|
300
|
|
|
pendingInline.unshift( currentNode ); |
|
301
|
|
|
reApply = true; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
if ( addPoint ) |
|
305
|
|
|
currentNode = addPoint; |
|
|
|
|
|
|
306
|
|
|
// Try adding it to the return point, or the parent element. |
|
307
|
|
|
else |
|
308
|
|
|
currentNode = currentNode.returnPoint || currentNode.parent; |
|
309
|
|
|
|
|
310
|
|
|
if ( reApply ) { |
|
311
|
|
|
parser.onTagOpen.apply( this, arguments ); |
|
312
|
|
|
return; |
|
313
|
|
|
} |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
checkPending( tagName ); |
|
317
|
|
|
checkPendingBrs( tagName ); |
|
318
|
|
|
|
|
319
|
|
|
element.parent = currentNode; |
|
320
|
|
|
element.returnPoint = returnPoint; |
|
321
|
|
|
returnPoint = 0; |
|
322
|
|
|
|
|
323
|
|
|
if ( element.isEmpty ) |
|
324
|
|
|
addElement( element ); |
|
|
|
|
|
|
325
|
|
|
else |
|
326
|
|
|
currentNode = element; |
|
327
|
|
|
}; |
|
328
|
|
|
|
|
329
|
|
|
parser.onTagClose = function( tagName ) { |
|
330
|
|
|
// Check if there is any pending tag to be closed. |
|
331
|
|
|
for ( var i = pendingInline.length - 1; i >= 0; i-- ) { |
|
332
|
|
|
// If found, just remove it from the list. |
|
333
|
|
|
if ( tagName == pendingInline[ i ].name ) { |
|
334
|
|
|
pendingInline.splice( i, 1 ); |
|
335
|
|
|
return; |
|
336
|
|
|
} |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
var pendingAdd = [], |
|
340
|
|
|
newPendingInline = [], |
|
341
|
|
|
candidate = currentNode; |
|
342
|
|
|
|
|
343
|
|
|
while ( candidate.type && candidate.name != tagName ) { |
|
344
|
|
|
// If this is an inline element, add it to the pending list, if we're |
|
345
|
|
|
// really closing one of the parents element later, they will continue |
|
346
|
|
|
// after it. |
|
347
|
|
|
if ( !candidate._.isBlockLike ) |
|
348
|
|
|
newPendingInline.unshift( candidate ); |
|
|
|
|
|
|
349
|
|
|
|
|
350
|
|
|
// This node should be added to it's parent at this point. But, |
|
351
|
|
|
// it should happen only if the closing tag is really closing |
|
352
|
|
|
// one of the nodes. So, for now, we just cache it. |
|
353
|
|
|
pendingAdd.push( candidate ); |
|
354
|
|
|
|
|
355
|
|
|
candidate = candidate.parent; |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
if ( candidate.type ) { |
|
359
|
|
|
// Add all elements that have been found in the above loop. |
|
360
|
|
|
for ( i = 0; i < pendingAdd.length; i++ ) { |
|
361
|
|
|
var node = pendingAdd[ i ]; |
|
362
|
|
|
addElement( node, node.parent ); |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
currentNode = candidate; |
|
366
|
|
|
|
|
367
|
|
|
|
|
368
|
|
|
addElement( candidate, candidate.parent ); |
|
369
|
|
|
|
|
370
|
|
|
// The parent should start receiving new nodes now, except if |
|
371
|
|
|
// addElement changed the currentNode. |
|
372
|
|
|
if ( candidate == currentNode ) |
|
373
|
|
|
currentNode = currentNode.parent; |
|
|
|
|
|
|
374
|
|
|
|
|
375
|
|
|
pendingInline = pendingInline.concat( newPendingInline ); |
|
376
|
|
|
} |
|
377
|
|
|
}; |
|
378
|
|
|
|
|
379
|
|
|
parser.onText = function( text ) { |
|
380
|
|
|
var currentDtd = CKEDITOR.dtd[ currentNode.name ]; |
|
|
|
|
|
|
381
|
|
|
if ( !currentDtd || currentDtd[ '#' ] ) { |
|
382
|
|
|
checkPendingBrs(); |
|
383
|
|
|
checkPending(); |
|
384
|
|
|
|
|
385
|
|
|
text.replace( /(\r\n|[\r\n])|[^\r\n]*/g, function( piece, lineBreak ) { |
|
386
|
|
|
if ( lineBreak !== undefined && lineBreak.length ) |
|
387
|
|
|
pendingBrs++; |
|
|
|
|
|
|
388
|
|
|
else if ( piece.length ) { |
|
389
|
|
|
var lastIndex = 0; |
|
390
|
|
|
|
|
391
|
|
|
// Create smiley from text emotion. |
|
392
|
|
|
piece.replace( smileyRegExp, function( match, index ) { |
|
393
|
|
|
addElement( new CKEDITOR.htmlParser.text( piece.substring( lastIndex, index ) ), currentNode ); |
|
|
|
|
|
|
394
|
|
|
addElement( new CKEDITOR.htmlParser.element( 'smiley', { desc: smileyReverseMap[ match ] } ), currentNode ); |
|
395
|
|
|
lastIndex = index + match.length; |
|
396
|
|
|
} ); |
|
397
|
|
|
|
|
398
|
|
|
if ( lastIndex != piece.length ) |
|
399
|
|
|
addElement( new CKEDITOR.htmlParser.text( piece.substring( lastIndex, piece.length ) ), currentNode ); |
|
|
|
|
|
|
400
|
|
|
} |
|
401
|
|
|
} ); |
|
402
|
|
|
} |
|
403
|
|
|
}; |
|
404
|
|
|
|
|
405
|
|
|
// Parse it. |
|
406
|
|
|
parser.parse( CKEDITOR.tools.htmlEncode( source ) ); |
|
407
|
|
|
|
|
408
|
|
|
// Close all hanging nodes. |
|
409
|
|
|
while ( currentNode.type != CKEDITOR.NODE_DOCUMENT_FRAGMENT ) { |
|
410
|
|
|
var parent = currentNode.parent, |
|
411
|
|
|
node = currentNode; |
|
412
|
|
|
|
|
413
|
|
|
addElement( node, parent ); |
|
414
|
|
|
currentNode = parent; |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
return fragment; |
|
418
|
|
|
}; |
|
419
|
|
|
|
|
420
|
|
|
var BBCodeWriter = CKEDITOR.tools.createClass( { |
|
421
|
|
|
$: function() { |
|
422
|
|
|
this._ = { |
|
423
|
|
|
output: [], |
|
424
|
|
|
rules: [] |
|
425
|
|
|
}; |
|
426
|
|
|
|
|
427
|
|
|
// List and list item. |
|
428
|
|
|
this.setRules( 'list', { breakBeforeOpen: 1, breakAfterOpen: 1, breakBeforeClose: 1, breakAfterClose: 1 } ); |
|
429
|
|
|
|
|
430
|
|
|
this.setRules( '*', { |
|
431
|
|
|
breakBeforeOpen: 1, |
|
432
|
|
|
breakAfterOpen: 0, |
|
433
|
|
|
breakBeforeClose: 1, |
|
434
|
|
|
breakAfterClose: 0 |
|
435
|
|
|
} ); |
|
436
|
|
|
|
|
437
|
|
|
this.setRules( 'quote', { |
|
438
|
|
|
breakBeforeOpen: 1, |
|
439
|
|
|
breakAfterOpen: 0, |
|
440
|
|
|
breakBeforeClose: 0, |
|
441
|
|
|
breakAfterClose: 1 |
|
442
|
|
|
} ); |
|
443
|
|
|
}, |
|
444
|
|
|
|
|
445
|
|
|
proto: { |
|
446
|
|
|
// |
|
447
|
|
|
// Sets formatting rules for a given tag. The possible rules are: |
|
448
|
|
|
// <ul> |
|
449
|
|
|
// <li><b>breakBeforeOpen</b>: break line before the opener tag for this element.</li> |
|
450
|
|
|
// <li><b>breakAfterOpen</b>: break line after the opener tag for this element.</li> |
|
451
|
|
|
// <li><b>breakBeforeClose</b>: break line before the closer tag for this element.</li> |
|
452
|
|
|
// <li><b>breakAfterClose</b>: break line after the closer tag for this element.</li> |
|
453
|
|
|
// </ul> |
|
454
|
|
|
// |
|
455
|
|
|
// All rules default to "false". Each call to the function overrides |
|
456
|
|
|
// already present rules, leaving the undefined untouched. |
|
457
|
|
|
// |
|
458
|
|
|
// @param {String} tagName The tag name to which set the rules. |
|
459
|
|
|
// @param {Object} rules An object containing the element rules. |
|
460
|
|
|
// @example |
|
461
|
|
|
// // Break line before and after "img" tags. |
|
462
|
|
|
// writer.setRules( 'list', |
|
463
|
|
|
// { |
|
464
|
|
|
// breakBeforeOpen : true |
|
465
|
|
|
// breakAfterOpen : true |
|
466
|
|
|
// }); |
|
467
|
|
|
setRules: function( tagName, rules ) { |
|
468
|
|
|
var currentRules = this._.rules[ tagName ]; |
|
469
|
|
|
|
|
470
|
|
|
if ( currentRules ) |
|
471
|
|
|
CKEDITOR.tools.extend( currentRules, rules, true ); |
|
|
|
|
|
|
472
|
|
|
else |
|
473
|
|
|
this._.rules[ tagName ] = rules; |
|
474
|
|
|
}, |
|
475
|
|
|
|
|
476
|
|
|
getRule: function( tagName, ruleName ) { |
|
477
|
|
|
return this._.rules[ tagName ] && this._.rules[ tagName ][ ruleName ]; |
|
478
|
|
|
}, |
|
479
|
|
|
|
|
480
|
|
|
openTag: function( tag ) { |
|
481
|
|
|
if ( tag in bbcodeMap ) { |
|
482
|
|
|
if ( this.getRule( tag, 'breakBeforeOpen' ) ) |
|
483
|
|
|
this.lineBreak( 1 ); |
|
|
|
|
|
|
484
|
|
|
|
|
485
|
|
|
this.write( '[', tag ); |
|
486
|
|
|
} |
|
487
|
|
|
}, |
|
488
|
|
|
|
|
489
|
|
|
openTagClose: function( tag ) { |
|
490
|
|
|
if ( tag == 'br' ) |
|
491
|
|
|
this._.output.push( '\n' ); |
|
|
|
|
|
|
492
|
|
|
else if ( tag in bbcodeMap ) { |
|
493
|
|
|
this.write( ']' ); |
|
494
|
|
|
if ( this.getRule( tag, 'breakAfterOpen' ) ) |
|
495
|
|
|
this.lineBreak( 1 ); |
|
|
|
|
|
|
496
|
|
|
} |
|
497
|
|
|
}, |
|
498
|
|
|
|
|
499
|
|
|
attribute: function( name, val ) { |
|
500
|
|
|
if ( name == 'option' ) { |
|
501
|
|
|
this.write( '=', val ); |
|
502
|
|
|
} |
|
503
|
|
|
}, |
|
504
|
|
|
|
|
505
|
|
|
closeTag: function( tag ) { |
|
506
|
|
|
if ( tag in bbcodeMap ) { |
|
507
|
|
|
if ( this.getRule( tag, 'breakBeforeClose' ) ) |
|
508
|
|
|
this.lineBreak( 1 ); |
|
|
|
|
|
|
509
|
|
|
|
|
510
|
|
|
tag != '*' && this.write( '[/', tag, ']' ); |
|
511
|
|
|
|
|
512
|
|
|
if ( this.getRule( tag, 'breakAfterClose' ) ) |
|
513
|
|
|
this.lineBreak( 1 ); |
|
|
|
|
|
|
514
|
|
|
} |
|
515
|
|
|
}, |
|
516
|
|
|
|
|
517
|
|
|
text: function( text ) { |
|
518
|
|
|
this.write( text ); |
|
519
|
|
|
}, |
|
520
|
|
|
|
|
521
|
|
|
comment: function() {}, |
|
522
|
|
|
|
|
523
|
|
|
// Output line-break for formatting. |
|
524
|
|
|
lineBreak: function() { |
|
525
|
|
|
// Avoid line break when: |
|
526
|
|
|
// 1) Previous tag already put one. |
|
527
|
|
|
// 2) We're at output start. |
|
528
|
|
|
if ( !this._.hasLineBreak && this._.output.length ) { |
|
529
|
|
|
this.write( '\n' ); |
|
530
|
|
|
this._.hasLineBreak = 1; |
|
531
|
|
|
} |
|
532
|
|
|
}, |
|
533
|
|
|
|
|
534
|
|
|
write: function() { |
|
535
|
|
|
this._.hasLineBreak = 0; |
|
536
|
|
|
var data = Array.prototype.join.call( arguments, '' ); |
|
537
|
|
|
this._.output.push( data ); |
|
538
|
|
|
}, |
|
539
|
|
|
|
|
540
|
|
|
reset: function() { |
|
541
|
|
|
this._.output = []; |
|
542
|
|
|
this._.hasLineBreak = 0; |
|
543
|
|
|
}, |
|
544
|
|
|
|
|
545
|
|
|
getHtml: function( reset ) { |
|
546
|
|
|
var bbcode = this._.output.join( '' ); |
|
547
|
|
|
|
|
548
|
|
|
if ( reset ) |
|
549
|
|
|
this.reset(); |
|
|
|
|
|
|
550
|
|
|
|
|
551
|
|
|
return decodeHtml( bbcode ); |
|
552
|
|
|
} |
|
553
|
|
|
} |
|
554
|
|
|
} ); |
|
555
|
|
|
|
|
556
|
|
|
var writer = new BBCodeWriter(); |
|
557
|
|
|
|
|
558
|
|
|
CKEDITOR.plugins.add( 'bbcode', { |
|
559
|
|
|
requires: 'entities', |
|
560
|
|
|
|
|
561
|
|
|
// Adapt some critical editor configuration for better support |
|
562
|
|
|
// of BBCode environment. |
|
563
|
|
|
beforeInit: function( editor ) { |
|
564
|
|
|
var config = editor.config; |
|
565
|
|
|
|
|
566
|
|
|
CKEDITOR.tools.extend( config, { |
|
|
|
|
|
|
567
|
|
|
// This one is for backwards compatibility only as |
|
568
|
|
|
// editor#enterMode is already set at this stage (https://dev.ckeditor.com/ticket/11202). |
|
569
|
|
|
enterMode: CKEDITOR.ENTER_BR, |
|
570
|
|
|
basicEntities: false, |
|
571
|
|
|
entities: false, |
|
572
|
|
|
fillEmptyBlocks: false |
|
573
|
|
|
}, true ); |
|
574
|
|
|
|
|
575
|
|
|
editor.filter.disable(); |
|
576
|
|
|
|
|
577
|
|
|
// Since CKEditor 4.3, editor#(active)enterMode is set before |
|
578
|
|
|
// beforeInit. Properties got to be updated (https://dev.ckeditor.com/ticket/11202). |
|
579
|
|
|
editor.activeEnterMode = editor.enterMode = CKEDITOR.ENTER_BR; |
|
580
|
|
|
}, |
|
581
|
|
|
|
|
582
|
|
|
init: function( editor ) { |
|
583
|
|
|
var config = editor.config; |
|
584
|
|
|
|
|
585
|
|
|
function BBCodeToHtml( code ) { |
|
586
|
|
|
var fragment = CKEDITOR.htmlParser.fragment.fromBBCode( code ), |
|
|
|
|
|
|
587
|
|
|
writer = new CKEDITOR.htmlParser.basicWriter(); |
|
588
|
|
|
|
|
589
|
|
|
fragment.writeHtml( writer, bbcodeFilter ); |
|
590
|
|
|
return writer.getHtml( true ); |
|
591
|
|
|
} |
|
592
|
|
|
|
|
593
|
|
|
var bbcodeFilter = new CKEDITOR.htmlParser.filter(); |
|
|
|
|
|
|
594
|
|
|
bbcodeFilter.addRules( { |
|
595
|
|
|
elements: { |
|
596
|
|
|
blockquote: function( element ) { |
|
597
|
|
|
var quoted = new CKEDITOR.htmlParser.element( 'div' ); |
|
|
|
|
|
|
598
|
|
|
quoted.children = element.children; |
|
599
|
|
|
element.children = [ quoted ]; |
|
600
|
|
|
var citeText = element.attributes.cite; |
|
601
|
|
|
if ( citeText ) { |
|
602
|
|
|
var cite = new CKEDITOR.htmlParser.element( 'cite' ); |
|
603
|
|
|
cite.add( new CKEDITOR.htmlParser.text( citeText.replace( /^"|"$/g, '' ) ) ); |
|
604
|
|
|
delete element.attributes.cite; |
|
605
|
|
|
element.children.unshift( cite ); |
|
606
|
|
|
} |
|
607
|
|
|
}, |
|
608
|
|
|
span: function( element ) { |
|
609
|
|
|
var bbcode; |
|
610
|
|
|
if ( ( bbcode = element.attributes.bbcode ) ) { |
|
611
|
|
|
if ( bbcode == 'img' ) { |
|
612
|
|
|
element.name = 'img'; |
|
613
|
|
|
element.attributes.src = element.children[ 0 ].value; |
|
614
|
|
|
element.children = []; |
|
615
|
|
|
} else if ( bbcode == 'email' ) { |
|
616
|
|
|
element.name = 'a'; |
|
617
|
|
|
element.attributes.href = 'mailto:' + element.children[ 0 ].value; |
|
618
|
|
|
} |
|
619
|
|
|
|
|
620
|
|
|
delete element.attributes.bbcode; |
|
621
|
|
|
} |
|
622
|
|
|
}, |
|
623
|
|
|
ol: function( element ) { |
|
624
|
|
|
if ( element.attributes.listType ) { |
|
625
|
|
|
if ( element.attributes.listType != 'decimal' ) |
|
626
|
|
|
element.attributes.style = 'list-style-type:' + element.attributes.listType; |
|
|
|
|
|
|
627
|
|
|
} else { |
|
628
|
|
|
element.name = 'ul'; |
|
629
|
|
|
} |
|
630
|
|
|
|
|
631
|
|
|
delete element.attributes.listType; |
|
632
|
|
|
}, |
|
633
|
|
|
a: function( element ) { |
|
634
|
|
|
if ( !element.attributes.href ) |
|
635
|
|
|
element.attributes.href = element.children[ 0 ].value; |
|
|
|
|
|
|
636
|
|
|
}, |
|
637
|
|
|
smiley: function( element ) { |
|
638
|
|
|
element.name = 'img'; |
|
639
|
|
|
|
|
640
|
|
|
var description = element.attributes.desc, |
|
641
|
|
|
image = config.smiley_images[ CKEDITOR.tools.indexOf( config.smiley_descriptions, description ) ], |
|
|
|
|
|
|
642
|
|
|
src = CKEDITOR.tools.htmlEncode( config.smiley_path + image ); |
|
643
|
|
|
|
|
644
|
|
|
element.attributes = { |
|
645
|
|
|
src: src, |
|
646
|
|
|
'data-cke-saved-src': src, |
|
647
|
|
|
title: description, |
|
648
|
|
|
alt: description |
|
649
|
|
|
}; |
|
650
|
|
|
} |
|
651
|
|
|
} |
|
652
|
|
|
} ); |
|
653
|
|
|
|
|
654
|
|
|
editor.dataProcessor.htmlFilter.addRules( { |
|
655
|
|
|
elements: { |
|
656
|
|
|
$: function( element ) { |
|
657
|
|
|
var attributes = element.attributes, |
|
658
|
|
|
style = CKEDITOR.tools.parseCssText( attributes.style, 1 ), |
|
|
|
|
|
|
659
|
|
|
value; |
|
660
|
|
|
|
|
661
|
|
|
var tagName = element.name; |
|
662
|
|
|
if ( tagName in convertMap ) |
|
663
|
|
|
tagName = convertMap[ tagName ]; |
|
|
|
|
|
|
664
|
|
|
else if ( tagName == 'span' ) { |
|
665
|
|
|
if ( ( value = style.color ) ) { |
|
666
|
|
|
tagName = 'color'; |
|
667
|
|
|
value = CKEDITOR.tools.convertRgbToHex( value ); |
|
668
|
|
|
} else if ( ( value = style[ 'font-size' ] ) ) { |
|
669
|
|
|
var percentValue = value.match( /(\d+)%$/ ); |
|
670
|
|
|
if ( percentValue ) { |
|
671
|
|
|
value = percentValue[ 1 ]; |
|
672
|
|
|
tagName = 'size'; |
|
673
|
|
|
} |
|
674
|
|
|
} |
|
675
|
|
|
} else if ( tagName == 'ol' || tagName == 'ul' ) { |
|
676
|
|
|
if ( ( value = style[ 'list-style-type' ] ) ) { |
|
677
|
|
|
switch ( value ) { |
|
678
|
|
|
case 'lower-alpha': |
|
679
|
|
|
value = 'a'; |
|
680
|
|
|
break; |
|
681
|
|
|
case 'upper-alpha': |
|
682
|
|
|
value = 'A'; |
|
683
|
|
|
break; |
|
684
|
|
|
} |
|
685
|
|
|
} else if ( tagName == 'ol' ) { |
|
686
|
|
|
value = 1; |
|
687
|
|
|
} |
|
688
|
|
|
|
|
689
|
|
|
tagName = 'list'; |
|
690
|
|
|
} else if ( tagName == 'blockquote' ) { |
|
691
|
|
|
try { |
|
692
|
|
|
var cite = element.children[ 0 ], |
|
693
|
|
|
quoted = element.children[ 1 ], |
|
694
|
|
|
citeText = cite.name == 'cite' && cite.children[ 0 ].value; |
|
695
|
|
|
|
|
696
|
|
|
if ( citeText ) { |
|
697
|
|
|
value = '"' + citeText + '"'; |
|
698
|
|
|
element.children = quoted.children; |
|
699
|
|
|
} |
|
700
|
|
|
|
|
701
|
|
|
} catch ( er ) {} |
|
|
|
|
|
|
702
|
|
|
|
|
703
|
|
|
tagName = 'quote'; |
|
704
|
|
|
} else if ( tagName == 'a' ) { |
|
705
|
|
|
if ( ( value = attributes.href ) ) { |
|
706
|
|
|
if ( value.indexOf( 'mailto:' ) !== -1 ) { |
|
707
|
|
|
tagName = 'email'; |
|
708
|
|
|
// [email] should have a single text child with email address. |
|
709
|
|
|
element.children = [ new CKEDITOR.htmlParser.text( value.replace( 'mailto:', '' ) ) ]; |
|
710
|
|
|
value = ''; |
|
711
|
|
|
} else { |
|
712
|
|
|
var singleton = element.children.length == 1 && element.children[ 0 ]; |
|
|
|
|
|
|
713
|
|
|
if ( singleton && singleton.type == CKEDITOR.NODE_TEXT && singleton.value == value ) |
|
714
|
|
|
value = ''; |
|
|
|
|
|
|
715
|
|
|
|
|
716
|
|
|
tagName = 'url'; |
|
717
|
|
|
} |
|
718
|
|
|
} |
|
719
|
|
|
} else if ( tagName == 'img' ) { |
|
720
|
|
|
element.isEmpty = 0; |
|
721
|
|
|
|
|
722
|
|
|
// Translate smiley (image) to text emotion. |
|
723
|
|
|
var src = attributes[ 'data-cke-saved-src' ] || attributes.src, |
|
724
|
|
|
alt = attributes.alt; |
|
725
|
|
|
|
|
726
|
|
|
if ( src && src.indexOf( editor.config.smiley_path ) != -1 && alt ) |
|
727
|
|
|
return new CKEDITOR.htmlParser.text( smileyMap[ alt ] ); |
|
|
|
|
|
|
728
|
|
|
else |
|
729
|
|
|
element.children = [ new CKEDITOR.htmlParser.text( src ) ]; |
|
730
|
|
|
} |
|
731
|
|
|
|
|
732
|
|
|
element.name = tagName; |
|
733
|
|
|
value && ( element.attributes.option = value ); |
|
734
|
|
|
|
|
735
|
|
|
return null; |
|
736
|
|
|
}, |
|
737
|
|
|
|
|
738
|
|
|
div: function( element ) { |
|
739
|
|
|
var alignment = CKEDITOR.tools.parseCssText( element.attributes.style, 1 )[ 'text-align' ] || ''; |
|
|
|
|
|
|
740
|
|
|
|
|
741
|
|
|
if ( alignment ) { |
|
|
|
|
|
|
742
|
|
|
element.name = alignment; |
|
743
|
|
|
return null; |
|
744
|
|
|
} |
|
745
|
|
|
}, |
|
746
|
|
|
|
|
747
|
|
|
// Remove any bogus br from the end of a pseudo block, |
|
748
|
|
|
// e.g. <div>some text<br /><p>paragraph</p></div> |
|
749
|
|
|
br: function( element ) { |
|
750
|
|
|
var next = element.next; |
|
751
|
|
|
if ( next && next.name in blockLikeTags ) |
|
|
|
|
|
|
752
|
|
|
return false; |
|
|
|
|
|
|
753
|
|
|
} |
|
754
|
|
|
} |
|
755
|
|
|
}, 1 ); |
|
756
|
|
|
|
|
757
|
|
|
editor.dataProcessor.writer = writer; |
|
758
|
|
|
|
|
759
|
|
|
function onSetData( evt ) { |
|
760
|
|
|
var bbcode = evt.data.dataValue; |
|
761
|
|
|
evt.data.dataValue = BBCodeToHtml( bbcode ); |
|
762
|
|
|
} |
|
763
|
|
|
|
|
764
|
|
|
// Skip the first "setData" call from inline creator, to allow content of |
|
765
|
|
|
// HTML to be loaded from the page element. |
|
766
|
|
|
if ( editor.elementMode == CKEDITOR.ELEMENT_MODE_INLINE ) |
|
767
|
|
|
editor.once( 'contentDom', function() { |
|
|
|
|
|
|
768
|
|
|
editor.on( 'setData', onSetData ); |
|
769
|
|
|
} ); |
|
770
|
|
|
else |
|
771
|
|
|
editor.on( 'setData', onSetData ); |
|
772
|
|
|
|
|
773
|
|
|
}, |
|
774
|
|
|
|
|
775
|
|
|
afterInit: function( editor ) { |
|
776
|
|
|
var filters; |
|
777
|
|
|
if ( editor._.elementsPath ) { |
|
778
|
|
|
// Eliminate irrelevant elements from displaying, e.g body and p. |
|
779
|
|
|
if ( ( filters = editor._.elementsPath.filters ) ) { |
|
780
|
|
|
filters.push( function( element ) { |
|
781
|
|
|
var htmlName = element.getName(), |
|
782
|
|
|
name = tagnameMap[ htmlName ] || false; |
|
783
|
|
|
|
|
784
|
|
|
// Specialized anchor presents as email. |
|
785
|
|
|
if ( name == 'link' && element.getAttribute( 'href' ).indexOf( 'mailto:' ) === 0 ) |
|
786
|
|
|
name = 'email'; |
|
|
|
|
|
|
787
|
|
|
// Styled span could be either size or color. |
|
788
|
|
|
else if ( htmlName == 'span' ) { |
|
789
|
|
|
if ( element.getStyle( 'font-size' ) ) |
|
790
|
|
|
name = 'size'; |
|
|
|
|
|
|
791
|
|
|
else if ( element.getStyle( 'color' ) ) |
|
792
|
|
|
name = 'color'; |
|
|
|
|
|
|
793
|
|
|
// Styled div could be align |
|
794
|
|
|
} else if ( htmlName == 'div' && element.getStyle( 'text-align' ) ) { |
|
795
|
|
|
name = element.getStyle( 'text-align' ); |
|
796
|
|
|
} else if ( name == 'img' ) { |
|
797
|
|
|
var src = element.data( 'cke-saved-src' ) || element.getAttribute( 'src' ); |
|
798
|
|
|
if ( src && src.indexOf( editor.config.smiley_path ) === 0 ) |
|
799
|
|
|
name = 'smiley'; |
|
|
|
|
|
|
800
|
|
|
} |
|
801
|
|
|
|
|
802
|
|
|
return name; |
|
803
|
|
|
} ); |
|
804
|
|
|
} |
|
805
|
|
|
} |
|
806
|
|
|
} |
|
807
|
|
|
} ); |
|
808
|
|
|
|
|
809
|
|
|
} )(); |
|
810
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.