Skip to content
Browse files

Added a better fix for the removal of internal elements and some unit…

… tests for it.
  • Loading branch information...
1 parent 9694169 commit 9404a3fa8ab67660020a591d7e34d230ccb46b84 @spocke spocke committed
View
13 jscripts/tiny_mce/classes/html/SaxParser.js
@@ -82,7 +82,7 @@
* @param {String} html Html string to sax parse.
*/
self.parse = function(html) {
- var self = this, matches, index = 0, value, endRegExp, stack = [], attrList, i, text, name,
+ var self = this, matches, index = 0, value, endRegExp, stack = [], attrList, i, text, name, isInternalElement, removeInternalElements,
shortEndedElements, fillAttrsMap, isShortEnded, validate, elementRule, isValidElement, attr, attribsValue,
validAttributesMap, validAttributePatterns, attributesRequired, attributesDefault, attributesForced, selfClosing,
tokenRegExp, attrRegExp, specialElements, attrValue, idCount = 0, decode = tinymce.html.Entities.decode, fixSelfClosing;
@@ -134,6 +134,7 @@
selfClosing = schema.getSelfClosingElements();
fillAttrsMap = schema.getBoolAttrs();
validate = settings.validate;
+ removeInternalElements = settings.remove_internals;
fixSelfClosing = settings.fix_self_closing;
while (matches = tokenRegExp.exec(html)) {
@@ -163,6 +164,12 @@
// Parse attributes
if (attribsValue = matches[8]) {
+ isInternalElement = attribsValue.indexOf('data-mce-type') !== -1; // Check if the element is an internal element
+
+ // If the element has internal attributes then remove it if we are told to do so
+ if (isInternalElement && removeInternalElements)
+ isValidElement = false;
+
attrList = [];
attrList.map = {};
@@ -173,7 +180,7 @@
value = name in fillAttrsMap ? name : decode(value || val2 || val3 || ''); // Handle boolean attribute than value attribute
// Validate name and value
- if (validate && name.indexOf('data-') !== 0) {
+ if (validate && !isInternalElement && name.indexOf('data-') !== 0) {
attrRule = validAttributesMap[name];
// Find rule by pattern matching
@@ -212,7 +219,7 @@
}
// Process attributes if validation is enabled
- if (validate) {
+ if (validate && !isInternalElement) {
attributesRequired = elementRule.attributesRequired;
attributesDefault = elementRule.attributesDefault;
attributesForced = elementRule.attributesForced;
View
5 jscripts/tiny_mce/classes/html/Schema.js
@@ -493,11 +493,8 @@
addValidChildren('+ol[ul|ol],+ul[ul|ol]');
// If the user didn't allow span only allow internal spans
- // Todo: This doesn't solve the case if the user decided to disable specific
- // attributes used internally like span[myattr] but that might be an edge case
- if (!getElementRule('span')) {
+ if (!getElementRule('span'))
addValidElements('span[!data-mce-type|*]');
- }
// Delete invalid elements
if (settings.invalid_elements) {
View
45 tests/tinymce.html.SaxParser.html
@@ -474,6 +474,51 @@
parser.parse('<ul><li>1<li><b>2</b><li><em><b>3</b></em></ul>');
equals(writer.getContent(), '<ul><li>1</li><li><b>2</b></li><li><em><b>3</b></em></li></ul>', 'Parse list with self closing items.');
});
+
+test('Preserve internal elements', function() {
+ expect(2);
+
+ var schema = new tinymce.html.Schema({valid_elements : 'b'});
+ var counter = createCounter(writer);
+ counter.validate = true;
+ var parser = new tinymce.html.SaxParser(counter, schema);
+ writer.reset();
+ parser.parse('<span id="id"><b>text</b></span><span id="id" data-mce-type="something"></span>');
+ equals(writer.getContent(), '<b>text</b><span id="id" data-mce-type="something"></span>', 'Preserve internal span element without any span schema rule.');
+
+ var schema = new tinymce.html.Schema({valid_elements : 'b,span[class]'});
+ var counter = createCounter(writer);
+ counter.validate = true;
+ var parser = new tinymce.html.SaxParser(counter, schema);
+ writer.reset();
+ parser.parse('<span id="id" class="class"><b>text</b></span><span id="id" data-mce-type="something"></span>');
+ equals(writer.getContent(), '<span class="class"><b>text</b></span><span id="id" data-mce-type="something"></span>', 'Preserve internal span element with a span schema rule.');
+});
+
+test('Remove internal elements', function() {
+ expect(2);
+
+ var schema = new tinymce.html.Schema({valid_elements : 'b'});
+ var counter = createCounter(writer);
+ counter.validate = true;
+ counter.remove_internals = true;
+ var parser = new tinymce.html.SaxParser(counter, schema);
+ writer.reset();
+ parser.parse('<span id="id"><b>text</b></span><span id="id" data-mce-type="something"></span>');
+ equals(writer.getContent(), '<b>text</b>', 'Remove internal span element without any span schema rule.');
+
+ var schema = new tinymce.html.Schema({valid_elements : 'b,span[class]'});
+ var counter = createCounter(writer);
+ counter.validate = true;
+ counter.remove_internals = true;
+ var parser = new tinymce.html.SaxParser(counter, schema);
+ writer.reset();
+ parser.parse('<span id="id" class="class"><b>text</b></span><span id="id" data-mce-type="something"></span>');
+ equals(writer.getContent(), '<span class="class"><b>text</b></span>', 'Remove internal span element with a span schema rule.');
+
+ // Reset
+ counter.remove_internals = false;
+});
</script>
</head>
<body>

0 comments on commit 9404a3f

Please sign in to comment.
Something went wrong with that request. Please try again.