1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* @module moodle-editor_atto-editor
|
|
|
18 |
* @submodule clean
|
|
|
19 |
*/
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Functions for the Atto editor to clean the generated content.
|
|
|
23 |
*
|
|
|
24 |
* See {{#crossLink "M.editor_atto.Editor"}}{{/crossLink}} for details.
|
|
|
25 |
*
|
|
|
26 |
* @namespace M.editor_atto
|
|
|
27 |
* @class EditorClean
|
|
|
28 |
*/
|
|
|
29 |
|
|
|
30 |
function EditorClean() {}
|
|
|
31 |
|
|
|
32 |
EditorClean.ATTRS = {
|
|
|
33 |
};
|
|
|
34 |
|
|
|
35 |
EditorClean.prototype = {
|
|
|
36 |
/**
|
|
|
37 |
* Clean the generated HTML content without modifying the editor content.
|
|
|
38 |
*
|
|
|
39 |
* This includes removes all YUI ids from the generated content.
|
|
|
40 |
*
|
|
|
41 |
* @return {string} The cleaned HTML content.
|
|
|
42 |
*/
|
|
|
43 |
getCleanHTML: function() {
|
|
|
44 |
// Clone the editor so that we don't actually modify the real content.
|
|
|
45 |
var editorClone = this.editor.cloneNode(true),
|
|
|
46 |
html;
|
|
|
47 |
|
|
|
48 |
// Remove all YUI IDs.
|
|
|
49 |
Y.each(editorClone.all('[id^="yui"]'), function(node) {
|
|
|
50 |
node.removeAttribute('id');
|
|
|
51 |
});
|
|
|
52 |
|
|
|
53 |
editorClone.all('.atto_control').remove(true);
|
|
|
54 |
html = editorClone.get('innerHTML');
|
|
|
55 |
|
|
|
56 |
// Revert untouched editor contents to an empty string.
|
|
|
57 |
var emptyContents = [
|
|
|
58 |
// For FF and Chrome.
|
|
|
59 |
'<p></p>',
|
|
|
60 |
'<p><br></p>',
|
|
|
61 |
'<br>',
|
|
|
62 |
'<p dir="rtl" style="text-align: right;"></p>',
|
|
|
63 |
'<p dir="rtl" style="text-align: right;"><br></p>',
|
|
|
64 |
'<p dir="ltr" style="text-align: left;"></p>',
|
|
|
65 |
'<p dir="ltr" style="text-align: left;"><br></p>',
|
|
|
66 |
// For IE 9 and 10.
|
|
|
67 |
'<p> </p>',
|
|
|
68 |
'<p><br> </p>',
|
|
|
69 |
'<p dir="rtl" style="text-align: right;"> </p>',
|
|
|
70 |
'<p dir="rtl" style="text-align: right;"><br> </p>',
|
|
|
71 |
'<p dir="ltr" style="text-align: left;"> </p>',
|
|
|
72 |
'<p dir="ltr" style="text-align: left;"><br> </p>'
|
|
|
73 |
];
|
|
|
74 |
if (emptyContents.includes(html)) {
|
|
|
75 |
return '';
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
// Remove any and all nasties from source.
|
|
|
79 |
return this._cleanHTML(html);
|
|
|
80 |
},
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Clean the HTML content of the editor.
|
|
|
84 |
*
|
|
|
85 |
* @method cleanEditorHTML
|
|
|
86 |
* @chainable
|
|
|
87 |
*/
|
|
|
88 |
cleanEditorHTML: function() {
|
|
|
89 |
var startValue = this.editor.get('innerHTML');
|
|
|
90 |
this.editor.set('innerHTML', this._cleanHTML(startValue));
|
|
|
91 |
|
|
|
92 |
return this;
|
|
|
93 |
},
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Clean the specified HTML content and remove any content which could cause issues.
|
|
|
97 |
*
|
|
|
98 |
* @method _cleanHTML
|
|
|
99 |
* @private
|
|
|
100 |
* @param {String} content The content to clean
|
|
|
101 |
* @param {Boolean} deepClean If true, do a more in depth (and resource intensive) cleaning of the HTML.
|
|
|
102 |
* @return {String} The cleaned HTML
|
|
|
103 |
*/
|
|
|
104 |
_cleanHTML: function(content, deepClean) {
|
|
|
105 |
// Removing limited things that can break the page or a disallowed, like unclosed comments, style blocks, etc.
|
|
|
106 |
|
|
|
107 |
var rules = [
|
|
|
108 |
// Remove any style blocks. Some browsers do not work well with them in a contenteditable.
|
|
|
109 |
// Plus style blocks are not allowed in body html, except with "scoped", which most browsers don't support as of 2015.
|
|
|
110 |
// Reference: "http://stackoverflow.com/questions/1068280/javascript-regex-multiline-flag-doesnt-work"
|
|
|
111 |
{regex: /<style[^>]*>[\s\S]*?<\/style>/gi, replace: ""},
|
|
|
112 |
|
|
|
113 |
// Remove any open HTML comment opens that are not followed by a close. This can completely break page layout.
|
|
|
114 |
{regex: /<!--(?![\s\S]*?-->)/gi, replace: ""},
|
|
|
115 |
|
|
|
116 |
// Source: "http://www.codinghorror.com/blog/2006/01/cleaning-words-nasty-html.html"
|
|
|
117 |
// Remove forbidden tags for content, title, meta, style, st0-9, head, font, html, body, link.
|
|
|
118 |
{regex: /<\/?(?:title|meta|style|st\d|head\b|font|html|body|link)[^>]*?>/gi, replace: ""}
|
|
|
119 |
];
|
|
|
120 |
|
|
|
121 |
content = this._filterContentWithRules(content, rules);
|
|
|
122 |
|
|
|
123 |
if (deepClean) {
|
|
|
124 |
content = this._cleanHTMLLists(content);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
return content;
|
|
|
128 |
},
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Take the supplied content and run on the supplied regex rules.
|
|
|
132 |
*
|
|
|
133 |
* @method _filterContentWithRules
|
|
|
134 |
* @private
|
|
|
135 |
* @param {String} content The content to clean
|
|
|
136 |
* @param {Array} rules An array of structures: [ {regex: /something/, replace: "something"}, {...}, ...]
|
|
|
137 |
* @return {String} The cleaned content
|
|
|
138 |
*/
|
|
|
139 |
_filterContentWithRules: function(content, rules) {
|
|
|
140 |
var i = 0;
|
|
|
141 |
for (i = 0; i < rules.length; i++) {
|
|
|
142 |
content = content.replace(rules[i].regex, rules[i].replace);
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
return content;
|
|
|
146 |
},
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
* Intercept and clean html paste events.
|
|
|
150 |
*
|
|
|
151 |
* @method pasteCleanup
|
|
|
152 |
* @param {Object} sourceEvent The YUI EventFacade object
|
|
|
153 |
* @return {Boolean} True if the passed event should continue, false if not.
|
|
|
154 |
*/
|
|
|
155 |
pasteCleanup: function(sourceEvent) {
|
|
|
156 |
// We only expect paste events, but we will check anyways.
|
|
|
157 |
if (sourceEvent.type === 'paste') {
|
|
|
158 |
// Register the delayed paste cleanup. We will cancel it if we register the fallback cleanup.
|
|
|
159 |
var delayedCleanup = this.postPasteCleanupDelayed();
|
|
|
160 |
// The YUI event wrapper doesn't provide paste event info, so we need the underlying event.
|
|
|
161 |
var event = sourceEvent._event;
|
|
|
162 |
// Check if we have a valid clipboardData object in the event.
|
|
|
163 |
// IE has a clipboard object at window.clipboardData, but as of IE 11, it does not provide HTML content access.
|
|
|
164 |
if (event && event.clipboardData && event.clipboardData.getData && event.clipboardData.types) {
|
|
|
165 |
// Check if there is HTML type to be pasted, if we can get it, we want to scrub before insert.
|
|
|
166 |
var types = event.clipboardData.types;
|
|
|
167 |
var isHTML = false;
|
|
|
168 |
// Different browsers use different containers to hold the types, so test various functions.
|
|
|
169 |
if (typeof types.contains === 'function') {
|
|
|
170 |
isHTML = types.contains('text/html');
|
|
|
171 |
} else if (typeof types.indexOf === 'function') {
|
|
|
172 |
isHTML = (types.indexOf('text/html') > -1);
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
var content;
|
|
|
176 |
if (isHTML) {
|
|
|
177 |
// Get the clipboard content.
|
|
|
178 |
try {
|
|
|
179 |
content = event.clipboardData.getData('text/html');
|
|
|
180 |
} catch (error) {
|
|
|
181 |
// Something went wrong. Fallback.
|
|
|
182 |
delayedCleanup.cancel();
|
|
|
183 |
this.fallbackPasteCleanupDelayed();
|
|
|
184 |
return true;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
// Stop the original paste.
|
|
|
188 |
sourceEvent.preventDefault();
|
|
|
189 |
|
|
|
190 |
// Scrub the paste content.
|
|
|
191 |
content = this._cleanPasteHTML(content);
|
|
|
192 |
|
|
|
193 |
// Insert the content.
|
|
|
194 |
this.insertContentAtFocusPoint(content);
|
|
|
195 |
|
|
|
196 |
// Update the text area.
|
|
|
197 |
this.updateOriginal();
|
|
|
198 |
return false;
|
|
|
199 |
} else {
|
|
|
200 |
try {
|
|
|
201 |
// Plaintext clipboard content can only be retrieved this way.
|
|
|
202 |
content = event.clipboardData.getData('text');
|
|
|
203 |
} catch (error) {
|
|
|
204 |
// Something went wrong. Fallback.
|
|
|
205 |
// Due to poor cross browser clipboard compatibility, the failure to find html doesn't mean it isn't there.
|
|
|
206 |
// Wait for the clipboard event to finish then fallback clean the entire editor.
|
|
|
207 |
delayedCleanup.cancel();
|
|
|
208 |
this.fallbackPasteCleanupDelayed();
|
|
|
209 |
return true;
|
|
|
210 |
}
|
|
|
211 |
}
|
|
|
212 |
} else {
|
|
|
213 |
// If we reached a here, this probably means the browser has limited (or no) clipboard support.
|
|
|
214 |
// Wait for the clipboard event to finish then fallback clean the entire editor.
|
|
|
215 |
this.fallbackPasteCleanupDelayed();
|
|
|
216 |
return true;
|
|
|
217 |
}
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
// We should never get here - we must have received a non-paste event for some reason.
|
|
|
221 |
// Um, just call updateOriginalDelayed() - it's safe.
|
|
|
222 |
this.updateOriginalDelayed();
|
|
|
223 |
return true;
|
|
|
224 |
},
|
|
|
225 |
|
|
|
226 |
/**
|
|
|
227 |
* Calls postPasteCleanup on a short timer to allow the paste event handlers to complete, then deep clean the content.
|
|
|
228 |
*
|
|
|
229 |
* @method postPasteCleanupDelayed
|
|
|
230 |
* @return {object}
|
|
|
231 |
* @chainable
|
|
|
232 |
*/
|
|
|
233 |
postPasteCleanupDelayed: function() {
|
|
|
234 |
Y.soon(Y.bind(this.postPasteCleanup, this));
|
|
|
235 |
|
|
|
236 |
return this;
|
|
|
237 |
},
|
|
|
238 |
|
|
|
239 |
/**
|
|
|
240 |
* Do additional cleanup after the paste is complete.
|
|
|
241 |
*
|
|
|
242 |
* @method postPasteCleanup
|
|
|
243 |
* @return {object}
|
|
|
244 |
* @chainable
|
|
|
245 |
*/
|
|
|
246 |
postPasteCleanup: function() {
|
|
|
247 |
Y.log('Executing delayed post paste cleanup', 'debug', LOGNAME);
|
|
|
248 |
|
|
|
249 |
// Save the current selection (cursor position).
|
|
|
250 |
var selection = window.rangy.saveSelection();
|
|
|
251 |
|
|
|
252 |
// Get, clean, and replace the content in the editable.
|
|
|
253 |
var content = this.editor.get('innerHTML');
|
|
|
254 |
this.editor.set('innerHTML', this._cleanHTML(content, true));
|
|
|
255 |
|
|
|
256 |
// Update the textarea.
|
|
|
257 |
this.updateOriginal();
|
|
|
258 |
|
|
|
259 |
// Restore the selection (cursor position).
|
|
|
260 |
window.rangy.restoreSelection(selection);
|
|
|
261 |
|
|
|
262 |
return this;
|
|
|
263 |
},
|
|
|
264 |
|
|
|
265 |
/**
|
|
|
266 |
* Cleanup code after a paste event if we couldn't intercept the paste content.
|
|
|
267 |
*
|
|
|
268 |
* @method fallbackPasteCleanup
|
|
|
269 |
* @return {object}
|
|
|
270 |
* @chainable
|
|
|
271 |
*/
|
|
|
272 |
fallbackPasteCleanup: function() {
|
|
|
273 |
Y.log('Using fallbackPasteCleanup for atto cleanup', 'debug', LOGNAME);
|
|
|
274 |
|
|
|
275 |
// Save the current selection (cursor position).
|
|
|
276 |
var selection = window.rangy.saveSelection();
|
|
|
277 |
|
|
|
278 |
// Get, clean, and replace the content in the editable.
|
|
|
279 |
var content = this.editor.get('innerHTML');
|
|
|
280 |
this.editor.set('innerHTML', this._cleanHTML(this._cleanPasteHTML(content), true));
|
|
|
281 |
|
|
|
282 |
// Update the textarea.
|
|
|
283 |
this.updateOriginal();
|
|
|
284 |
|
|
|
285 |
// Restore the selection (cursor position).
|
|
|
286 |
window.rangy.restoreSelection(selection);
|
|
|
287 |
|
|
|
288 |
return this;
|
|
|
289 |
},
|
|
|
290 |
|
|
|
291 |
/**
|
|
|
292 |
* Calls fallbackPasteCleanup on a short timer to allow the paste event handlers to complete.
|
|
|
293 |
*
|
|
|
294 |
* @method fallbackPasteCleanupDelayed
|
|
|
295 |
* @chainable
|
|
|
296 |
*/
|
|
|
297 |
fallbackPasteCleanupDelayed: function() {
|
|
|
298 |
Y.soon(Y.bind(this.fallbackPasteCleanup, this));
|
|
|
299 |
|
|
|
300 |
return this;
|
|
|
301 |
},
|
|
|
302 |
|
|
|
303 |
/**
|
|
|
304 |
* Cleanup html that comes from WYSIWYG paste events. These are more likely to contain messy code that we should strip.
|
|
|
305 |
*
|
|
|
306 |
* @method _cleanPasteHTML
|
|
|
307 |
* @private
|
|
|
308 |
* @param {String} content The html content to clean
|
|
|
309 |
* @return {String} The cleaned HTML
|
|
|
310 |
*/
|
|
|
311 |
_cleanPasteHTML: function(content) {
|
|
|
312 |
// Return an empty string if passed an invalid or empty object.
|
|
|
313 |
if (!content || content.length === 0) {
|
|
|
314 |
return "";
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
// Rules that get rid of the real-nasties and don't care about normalize code (correct quotes, white spaces, etc).
|
|
|
318 |
var rules = [
|
|
|
319 |
// Stuff that is specifically from MS Word and similar office packages.
|
|
|
320 |
// Remove all garbage after closing html tag.
|
|
|
321 |
{regex: /<\s*\/html\s*>([\s\S]+)$/gi, replace: ""},
|
|
|
322 |
// Remove if comment blocks.
|
|
|
323 |
{regex: /<!--\[if[\s\S]*?endif\]-->/gi, replace: ""},
|
|
|
324 |
// Remove start and end fragment comment blocks.
|
|
|
325 |
{regex: /<!--(Start|End)Fragment-->/gi, replace: ""},
|
|
|
326 |
// Remove any xml blocks.
|
|
|
327 |
{regex: /<xml[^>]*>[\s\S]*?<\/xml>/gi, replace: ""},
|
|
|
328 |
// Remove any <?xml><\?xml> blocks.
|
|
|
329 |
{regex: /<\?xml[^>]*>[\s\S]*?<\\\?xml>/gi, replace: ""},
|
|
|
330 |
// Remove <o:blah>, <\o:blah>.
|
|
|
331 |
{regex: /<\/?\w+:[^>]*>/gi, replace: ""}
|
|
|
332 |
];
|
|
|
333 |
|
|
|
334 |
// Apply the first set of harsher rules.
|
|
|
335 |
content = this._filterContentWithRules(content, rules);
|
|
|
336 |
|
|
|
337 |
// Apply the standard rules, which mainly cleans things like headers, links, and style blocks.
|
|
|
338 |
content = this._cleanHTML(content);
|
|
|
339 |
|
|
|
340 |
// Check if the string is empty or only contains whitespace.
|
|
|
341 |
if (content.length === 0 || !content.match(/\S/)) {
|
|
|
342 |
return content;
|
|
|
343 |
}
|
|
|
344 |
|
|
|
345 |
// Now we let the browser normalize the code by loading it into the DOM and then get the html back.
|
|
|
346 |
// This gives us well quoted, well formatted code to continue our work on. Word may provide very poorly formatted code.
|
|
|
347 |
var holder = document.createElement('div');
|
|
|
348 |
holder.innerHTML = content;
|
|
|
349 |
content = holder.innerHTML;
|
|
|
350 |
// Free up the DOM memory.
|
|
|
351 |
holder.innerHTML = "";
|
|
|
352 |
|
|
|
353 |
// Run some more rules that care about quotes and whitespace.
|
|
|
354 |
rules = [
|
|
|
355 |
// Get all class attributes so we can work on them.
|
|
|
356 |
{regex: /(<[^>]*?class\s*?=\s*?")([^>"]*)(")/gi, replace: function(match, group1, group2, group3) {
|
|
|
357 |
// Remove MSO classes.
|
|
|
358 |
group2 = group2.replace(/(?:^|[\s])[\s]*MSO[_a-zA-Z0-9\-]*/gi, "");
|
|
|
359 |
// Remove Apple- classes.
|
|
|
360 |
group2 = group2.replace(/(?:^|[\s])[\s]*Apple-[_a-zA-Z0-9\-]*/gi, "");
|
|
|
361 |
return group1 + group2 + group3;
|
|
|
362 |
}},
|
|
|
363 |
// Remove OLE_LINK# anchors that may litter the code.
|
|
|
364 |
{regex: /<a [^>]*?name\s*?=\s*?"OLE_LINK\d*?"[^>]*?>\s*?<\/a>/gi, replace: ""}
|
|
|
365 |
];
|
|
|
366 |
|
|
|
367 |
// Clean all style attributes from the text.
|
|
|
368 |
content = this._cleanStyles(content);
|
|
|
369 |
|
|
|
370 |
// Apply the rules.
|
|
|
371 |
content = this._filterContentWithRules(content, rules);
|
|
|
372 |
|
|
|
373 |
// Reapply the standard cleaner to the content.
|
|
|
374 |
content = this._cleanHTML(content);
|
|
|
375 |
|
|
|
376 |
// Clean unused spans out of the content.
|
|
|
377 |
content = this._cleanSpans(content);
|
|
|
378 |
|
|
|
379 |
return content;
|
|
|
380 |
},
|
|
|
381 |
|
|
|
382 |
/**
|
|
|
383 |
* Clean all inline styles from pasted text.
|
|
|
384 |
*
|
|
|
385 |
* This code intentionally doesn't use YUI Nodes. YUI was quite a bit slower at this, so using raw DOM objects instead.
|
|
|
386 |
*
|
|
|
387 |
* @method _cleanStyles
|
|
|
388 |
* @private
|
|
|
389 |
* @param {String} content The content to clean
|
|
|
390 |
* @return {String} The cleaned HTML
|
|
|
391 |
*/
|
|
|
392 |
_cleanStyles: function(content) {
|
|
|
393 |
var holder = document.createElement('div');
|
|
|
394 |
holder.innerHTML = content;
|
|
|
395 |
var elementsWithStyle = holder.querySelectorAll('[style]');
|
|
|
396 |
var i = 0;
|
|
|
397 |
|
|
|
398 |
for (i = 0; i < elementsWithStyle.length; i++) {
|
|
|
399 |
elementsWithStyle[i].removeAttribute('style');
|
|
|
400 |
}
|
|
|
401 |
|
|
|
402 |
var elementsWithClass = holder.querySelectorAll('[class]');
|
|
|
403 |
for (i = 0; i < elementsWithClass.length; i++) {
|
|
|
404 |
elementsWithClass[i].removeAttribute('class');
|
|
|
405 |
}
|
|
|
406 |
|
|
|
407 |
return holder.innerHTML;
|
|
|
408 |
},
|
|
|
409 |
/**
|
|
|
410 |
* Clean empty or un-unused spans from passed HTML.
|
|
|
411 |
*
|
|
|
412 |
* This code intentionally doesn't use YUI Nodes. YUI was quite a bit slower at this, so using raw DOM objects instead.
|
|
|
413 |
*
|
|
|
414 |
* @method _cleanSpans
|
|
|
415 |
* @private
|
|
|
416 |
* @param {String} content The content to clean
|
|
|
417 |
* @return {String} The cleaned HTML
|
|
|
418 |
*/
|
|
|
419 |
_cleanSpans: function(content) {
|
|
|
420 |
// Return an empty string if passed an invalid or empty object.
|
|
|
421 |
if (!content || content.length === 0) {
|
|
|
422 |
return "";
|
|
|
423 |
}
|
|
|
424 |
// Check if the string is empty or only contains whitespace.
|
|
|
425 |
if (content.length === 0 || !content.match(/\S/)) {
|
|
|
426 |
return content;
|
|
|
427 |
}
|
|
|
428 |
|
|
|
429 |
var rules = [
|
|
|
430 |
// Remove unused class, style, or id attributes. This will make empty tag detection easier later.
|
|
|
431 |
{regex: /(<[^>]*?)(?:[\s]*(?:class|style|id)\s*?=\s*?"\s*?")+/gi, replace: "$1"}
|
|
|
432 |
];
|
|
|
433 |
// Apply the rules.
|
|
|
434 |
content = this._filterContentWithRules(content, rules);
|
|
|
435 |
|
|
|
436 |
// Reference: "http://stackoverflow.com/questions/8131396/remove-nested-span-without-id"
|
|
|
437 |
|
|
|
438 |
// This is better to run detached from the DOM, so the browser doesn't try to update on each change.
|
|
|
439 |
var holder = document.createElement('div');
|
|
|
440 |
holder.innerHTML = content;
|
|
|
441 |
var spans = holder.getElementsByTagName('span');
|
|
|
442 |
|
|
|
443 |
// Since we will be removing elements from the list, we should copy it to an array, making it static.
|
|
|
444 |
var spansarr = Array.prototype.slice.call(spans, 0);
|
|
|
445 |
|
|
|
446 |
spansarr.forEach(function(span) {
|
|
|
447 |
if (!span.hasAttributes()) {
|
|
|
448 |
// If no attributes (id, class, style, etc), this span is has no effect.
|
|
|
449 |
// Move each child (if they exist) to the parent in place of this span.
|
|
|
450 |
while (span.firstChild) {
|
|
|
451 |
span.parentNode.insertBefore(span.firstChild, span);
|
|
|
452 |
}
|
|
|
453 |
|
|
|
454 |
// Remove the now empty span.
|
|
|
455 |
span.parentNode.removeChild(span);
|
|
|
456 |
}
|
|
|
457 |
});
|
|
|
458 |
|
|
|
459 |
return holder.innerHTML;
|
|
|
460 |
},
|
|
|
461 |
|
|
|
462 |
/**
|
|
|
463 |
* This is a function that searches for, and attempts to correct certain issues with ul/ol html lists.
|
|
|
464 |
* This is needed because these lists are used heavily in page layout, and content with bad tags can
|
|
|
465 |
* lead to broke course pages.
|
|
|
466 |
*
|
|
|
467 |
* The theory of operation here is to linearly process the incoming content, counting the opening and closing
|
|
|
468 |
* of list tags, and determining when there is a mismatch.
|
|
|
469 |
*
|
|
|
470 |
* The specific issues this should be able to correct are:
|
|
|
471 |
* - Orphaned li elements will be wrapped in a set of ul tags.
|
|
|
472 |
* - li elements inside li elements.
|
|
|
473 |
* - An extra closing ul, or ol tag will be discarded.
|
|
|
474 |
* - An extra closing li tag will have an opening tag added if appropriate, or will be discarded.
|
|
|
475 |
* - If there is an unmatched list open tag, a matching close tag will be inserted.
|
|
|
476 |
*
|
|
|
477 |
* It does it's best to match the case of corrected tags. Even though not required by html spec,
|
|
|
478 |
* it seems like the safer route.
|
|
|
479 |
*
|
|
|
480 |
* A note on parent elements of li. This code assumes that li must have a ol or ul parent.
|
|
|
481 |
* There are two other potential other parents of li. They are menu and dir. The dir tag was deprecated in
|
|
|
482 |
* HTML4, and removed in HTML5. The menu tag is experimental as of this writing, and basically doesn't work
|
|
|
483 |
* in any browsers, even Firefox, which theoretically has limited support for it. If other parents of li
|
|
|
484 |
* become viable, they will need to be added to this code.
|
|
|
485 |
*
|
|
|
486 |
* @method _cleanHTMLLists
|
|
|
487 |
* @private
|
|
|
488 |
* @param {String} content The content to clean
|
|
|
489 |
* @return {String} The cleaned content
|
|
|
490 |
*/
|
|
|
491 |
_cleanHTMLLists: function(content) {
|
|
|
492 |
var output = '',
|
|
|
493 |
toProcess = content,
|
|
|
494 |
match = null,
|
|
|
495 |
openTags = [],
|
|
|
496 |
currentTag = null,
|
|
|
497 |
previousTag = null;
|
|
|
498 |
|
|
|
499 |
// Use a regular expression to find the next open or close li, ul, or ol tag.
|
|
|
500 |
// Keep going until there are no more matching tags left.
|
|
|
501 |
// This expression looks for whole words by employing the word boundary (\b) metacharacter.
|
|
|
502 |
while ((match = toProcess.match(/<(\/?)(li|ul|ol)\b[^>]*>/i))) {
|
|
|
503 |
currentTag = {
|
|
|
504 |
tag: match[2],
|
|
|
505 |
tagLowerCase: match[2].toLowerCase(),
|
|
|
506 |
fullTag: match[0],
|
|
|
507 |
isOpen: (match[1].length == 1) ? false : true
|
|
|
508 |
};
|
|
|
509 |
|
|
|
510 |
// Get the most recent open tag.
|
|
|
511 |
previousTag = (openTags.length) ? openTags[openTags.length - 1] : null;
|
|
|
512 |
|
|
|
513 |
// Slice up the content based on the match and add content before the match to output.
|
|
|
514 |
output += toProcess.slice(0, match.index);
|
|
|
515 |
toProcess = toProcess.slice(match.index + match[0].length);
|
|
|
516 |
|
|
|
517 |
// Now the full content is in output + currentTag.fullTag + toProcess. When making fixes, it is best to push the fix and
|
|
|
518 |
// fullTag back onto the front or toProcess, then restart the loop. This allows processing to follow the normal path
|
|
|
519 |
// most often. But sometimes we will need to modify output to insert or remove tags in the already complete code.
|
|
|
520 |
|
|
|
521 |
if (currentTag.isOpen) {
|
|
|
522 |
// We are at the opening phase of a tag.
|
|
|
523 |
// We have to do special processing for list items, as they can only be children of ul and ol tags.
|
|
|
524 |
if (currentTag.tagLowerCase === 'li') {
|
|
|
525 |
if (!previousTag) {
|
|
|
526 |
// This means we have are opening a li, but aren't in a list. This is not allowed!
|
|
|
527 |
|
|
|
528 |
// We are going to check for the count of open and close ol tags ahead to decide what to do.
|
|
|
529 |
var closeCount = (toProcess.match(/<\/(ol)[ >]/ig) || []).length;
|
|
|
530 |
var openCount = (toProcess.match(/<(ol)[ >]/ig) || []).length;
|
|
|
531 |
|
|
|
532 |
if (closeCount > openCount) {
|
|
|
533 |
// There are more close ol's ahead than opens ahead. So open the ol and try again.
|
|
|
534 |
Y.log('Adding an opening ol for orphan li', 'debug', LOGNAME);
|
|
|
535 |
toProcess = '<ol>' + currentTag.fullTag + toProcess;
|
|
|
536 |
continue;
|
|
|
537 |
}
|
|
|
538 |
|
|
|
539 |
// For the other cases, just open a ul and try again. Later the closing ul will get matched if it exists,
|
|
|
540 |
// or if it doesn't one will automatically get inserted.
|
|
|
541 |
Y.log('Adding an opening ul for orphan li', 'debug', LOGNAME);
|
|
|
542 |
toProcess = '<ul>' + currentTag.fullTag + toProcess;
|
|
|
543 |
continue;
|
|
|
544 |
}
|
|
|
545 |
|
|
|
546 |
if (previousTag.tagLowerCase === 'li') {
|
|
|
547 |
// You aren't allowed to nest li tags. Close the current one before starting the new one.
|
|
|
548 |
Y.log('Adding a closing ' + previousTag.tag + ' before opening a new one.', 'debug', LOGNAME);
|
|
|
549 |
toProcess = '</' + previousTag.tag + '>' + currentTag.fullTag + toProcess;
|
|
|
550 |
continue;
|
|
|
551 |
}
|
|
|
552 |
|
|
|
553 |
// Previous tag must be a list at this point, so we can continue.
|
|
|
554 |
}
|
|
|
555 |
|
|
|
556 |
// If we made it this far, record the tag to the open tags list.
|
|
|
557 |
openTags.push({
|
|
|
558 |
tag: currentTag.tag,
|
|
|
559 |
tagLowerCase: currentTag.tagLowerCase,
|
|
|
560 |
position: output.length,
|
|
|
561 |
length: currentTag.fullTag.length
|
|
|
562 |
});
|
|
|
563 |
} else {
|
|
|
564 |
// We are processing a closing tag.
|
|
|
565 |
|
|
|
566 |
if (openTags.length == 0) {
|
|
|
567 |
// We are closing a tag that isn't open. That's a problem. Just discarding should be safe.
|
|
|
568 |
Y.log('Discarding extra ' + currentTag.fullTag + ' tag.', 'debug', LOGNAME);
|
|
|
569 |
continue;
|
|
|
570 |
}
|
|
|
571 |
|
|
|
572 |
if (previousTag.tagLowerCase === currentTag.tagLowerCase) {
|
|
|
573 |
// Closing a tag that matches the open tag. This is the nominal case. Pop it off, and update previousTag.
|
|
|
574 |
if (currentTag.tag != previousTag.tag) {
|
|
|
575 |
// This would mean cases don't match between the opening and closing tag.
|
|
|
576 |
// We are going to swap them to match, even though not required.
|
|
|
577 |
currentTag.fullTag = currentTag.fullTag.replace(currentTag.tag, previousTag.tag);
|
|
|
578 |
}
|
|
|
579 |
|
|
|
580 |
openTags.pop();
|
|
|
581 |
previousTag = (openTags.length) ? openTags[openTags.length - 1] : null;
|
|
|
582 |
} else {
|
|
|
583 |
// We are closing a tag that isn't the most recent open one open, so we have a mismatch.
|
|
|
584 |
if (currentTag.tagLowerCase === 'li' && previousTag.liEnd && (previousTag.liEnd < output.length)) {
|
|
|
585 |
// We are closing an unopened li, but the parent list has complete li tags more than 0 chars ago.
|
|
|
586 |
// Assume we are missing an open li at the end of the previous li, and insert there.
|
|
|
587 |
Y.log('Inserting opening ' + currentTag.tag + ' after previous li.', 'debug', LOGNAME);
|
|
|
588 |
output = this._insertString(output, '<' + currentTag.tag + '>', previousTag.liEnd);
|
|
|
589 |
} else if (currentTag.tagLowerCase === 'li' && !previousTag.liEnd &&
|
|
|
590 |
((previousTag.position + previousTag.length) < output.length)) {
|
|
|
591 |
// We are closing an unopened li, and the parent has no previous lis in it, but opened more than 0
|
|
|
592 |
// chars ago. Assume we are missing a starting li, and insert it right after the list opened.
|
|
|
593 |
Y.log('Inserting opening ' + currentTag.tag + ' at start of parent.', 'debug', LOGNAME);
|
|
|
594 |
output = this._insertString(output, '<' + currentTag.tag + '>', previousTag.position + previousTag.length);
|
|
|
595 |
} else if (previousTag.tagLowerCase === 'li') {
|
|
|
596 |
// We must be trying to close a ul/ol while in a li. Just assume we are missing a closing li.
|
|
|
597 |
Y.log('Adding a closing ' + previousTag.tag + ' before closing ' + currentTag.tag + '.', 'debug', LOGNAME);
|
|
|
598 |
toProcess = '</' + previousTag.tag + '>' + currentTag.fullTag + toProcess;
|
|
|
599 |
continue;
|
|
|
600 |
} else {
|
|
|
601 |
// Here we must be trying to close a tag that isn't open, or is open higher up. Just discard.
|
|
|
602 |
// If there ends up being a missing close tag later on, that will get fixed separately.
|
|
|
603 |
Y.log('Discarding incorrect ' + currentTag.fullTag + '.', 'debug', LOGNAME);
|
|
|
604 |
continue;
|
|
|
605 |
}
|
|
|
606 |
}
|
|
|
607 |
|
|
|
608 |
// If we have a valid closing li tag, and a list, record where the li ended.
|
|
|
609 |
if (currentTag.tagLowerCase === 'li' && previousTag) {
|
|
|
610 |
previousTag.liEnd = output.length + currentTag.fullTag.length;
|
|
|
611 |
}
|
|
|
612 |
|
|
|
613 |
}
|
|
|
614 |
|
|
|
615 |
// Now we can add the tag to the output.
|
|
|
616 |
output += currentTag.fullTag;
|
|
|
617 |
}
|
|
|
618 |
|
|
|
619 |
// Add anything left in toProcess to the output.
|
|
|
620 |
output += toProcess;
|
|
|
621 |
|
|
|
622 |
// Anything still in the openTags list are extra and need to be dealt with.
|
|
|
623 |
if (openTags.length) {
|
|
|
624 |
// Work on the list in reverse order so positions stay correct.
|
|
|
625 |
while ((currentTag = openTags.pop())) {
|
|
|
626 |
if (currentTag.liEnd) {
|
|
|
627 |
// We have a position for the last list item in this element. Insert the closing it after that.
|
|
|
628 |
output = this._insertString(output, '</' + currentTag.tag + '>', currentTag.liEnd);
|
|
|
629 |
Y.log('Adding closing ' + currentTag.tag + ' based on last li location.', 'debug', LOGNAME);
|
|
|
630 |
} else {
|
|
|
631 |
// If there weren't any children list items, then we should just remove the tag where it started.
|
|
|
632 |
// This will also remote an open li tag that runs to the end of the content, since it has no children lis.
|
|
|
633 |
output = output.slice(0, currentTag.position) + output.slice(currentTag.position + currentTag.length);
|
|
|
634 |
Y.log('Removing opening ' + currentTag.fullTag + ' because it was missing closing.', 'debug', LOGNAME);
|
|
|
635 |
}
|
|
|
636 |
}
|
|
|
637 |
}
|
|
|
638 |
|
|
|
639 |
return output;
|
|
|
640 |
},
|
|
|
641 |
|
|
|
642 |
/**
|
|
|
643 |
* Insert a string in the middle of an existing string at the specified location.
|
|
|
644 |
*
|
|
|
645 |
* @method _insertString
|
|
|
646 |
* @param {String} content The subject of the insertion.
|
|
|
647 |
* @param {String} insert The string that will be inserted.
|
|
|
648 |
* @param {Number} position The location to make the insertion.
|
|
|
649 |
* @return {String} The string with the new content inserted.
|
|
|
650 |
*/
|
|
|
651 |
_insertString: function(content, insert, position) {
|
|
|
652 |
return content.slice(0, position) + insert + content.slice(position);
|
|
|
653 |
}
|
|
|
654 |
};
|
|
|
655 |
|
|
|
656 |
Y.Base.mix(Y.M.editor_atto.Editor, [EditorClean]);
|