diff --git a/parser_rules/advanced.js b/parser_rules/advanced.js index 2e8178b8..0ec63bbb 100644 --- a/parser_rules/advanced.js +++ b/parser_rules/advanced.js @@ -36,6 +36,7 @@ var wysihtml5ParserRules = { /** * CSS Class white-list * Following CSS classes won't be removed when parsed by the wysihtml5 HTML parser + "classes": { "*":1 } will allow all classes to be carried over, otheriwse only those whitelisted */ "classes": { "wysiwyg-clear-both": 1, @@ -85,7 +86,7 @@ var wysihtml5ParserRules = { * - clear_br: converts clear attribute values left/right/all/both to their corresponding css class "wysiwyg-clear-*" *
... becomes ...
* - align_img: converts align attribute values (right/left) on to their corresponding css class "wysiwyg-float-*" - * + * * - remove: removes the element and its content * * - rename_tag: renames the element to the given tag diff --git a/src/dom/parse.js b/src/dom/parse.js index d2bdc998..29c4aade 100644 --- a/src/dom/parse.js +++ b/src/dom/parse.js @@ -50,7 +50,7 @@ * // => '

foo

bar

' */ wysihtml5.dom.parse = (function() { - + /** * It's not possible to use a XMLParser/DOMParser as HTML5 is not always well-formed XML * new DOMParser().parseFromString('') will cause a parseError since the @@ -67,27 +67,27 @@ wysihtml5.dom.parse = (function() { WHITE_SPACE_REG_EXP = /\s+/, defaultRules = { tags: {}, classes: {} }, currentRules = {}; - + /** * Iterates over all childs of the element, recreates them, appends them into a document fragment * which later replaces the entire body content */ function parse(elementOrHtml, rules, context, cleanUp) { wysihtml5.lang.object(currentRules).merge(defaultRules).merge(rules).get(); - + context = context || elementOrHtml.ownerDocument || document; var fragment = context.createDocumentFragment(), isString = typeof(elementOrHtml) === "string", element, newNode, firstChild; - + if (isString) { element = wysihtml5.dom.getAsDom(elementOrHtml, context); } else { element = elementOrHtml; } - + while (element.firstChild) { firstChild = element.firstChild; element.removeChild(firstChild); @@ -96,16 +96,16 @@ wysihtml5.dom.parse = (function() { fragment.appendChild(newNode); } } - + // Clear element contents element.innerHTML = ""; - + // Insert new DOM tree element.appendChild(fragment); - + return isString ? wysihtml5.quirks.getCorrectInnerHTML(element) : element; } - + function _convert(oldNode, cleanUp) { var oldNodeType = oldNode.nodeType, oldChilds = oldNode.childNodes, @@ -114,20 +114,20 @@ wysihtml5.dom.parse = (function() { i = 0, newNode, newChild; - + newNode = method && method(oldNode); - + if (!newNode) { return null; } - + for (i=0; i elements if (cleanUp && newNode.childNodes.length <= 1 && @@ -135,10 +135,10 @@ wysihtml5.dom.parse = (function() { !newNode.attributes.length) { return newNode.firstChild; } - + return newNode; } - + function _handleElement(oldNode) { var rule, newNode, @@ -146,7 +146,7 @@ wysihtml5.dom.parse = (function() { tagRules = currentRules.tags, nodeName = oldNode.nodeName.toLowerCase(), scopeName = oldNode.scopeName; - + /** * We already parsed that element * ignore it! (yes, this sometimes happens in IE8 when the html is invalid) @@ -155,11 +155,11 @@ wysihtml5.dom.parse = (function() { return null; } oldNode._wysihtml5 = 1; - + if (oldNode.className === "wysihtml5-temp") { return null; } - + /** * IE is the only browser who doesn't include the namespace in the * nodeName, that's why we have to prepend it by ourselves @@ -169,7 +169,7 @@ wysihtml5.dom.parse = (function() { if (scopeName && scopeName != "HTML") { nodeName = scopeName + ":" + nodeName; } - + /** * Repair node * IE is a bit bitchy when it comes to invalid nested markup which includes unclosed tags @@ -182,13 +182,13 @@ wysihtml5.dom.parse = (function() { nodeName = "div"; } } - + if (nodeName in tagRules) { rule = tagRules[nodeName]; if (!rule || rule.remove) { return null; } - + rule = typeof(rule) === "string" ? { rename_tag: rule } : rule; } else if (oldNode.firstChild) { rule = { rename_tag: DEFAULT_NODE_NAME }; @@ -196,14 +196,14 @@ wysihtml5.dom.parse = (function() { // Remove empty unknown elements return null; } - + newNode = oldNode.ownerDocument.createElement(rule.rename_tag || nodeName); _handleAttributes(oldNode, newNode, rule); - + oldNode = null; return newNode; } - + function _handleAttributes(oldNode, newNode, rule) { var attributes = {}, // fresh new set of attributes to set on newNode setClass = rule.set_class, // classes to set @@ -223,11 +223,11 @@ wysihtml5.dom.parse = (function() { attributeName, newAttributeValue, method; - + if (setAttributes) { attributes = wysihtml5.lang.object(setAttributes).clone(); } - + if (checkAttributes) { for (attributeName in checkAttributes) { method = attributeCheckMethods[checkAttributes[attributeName]]; @@ -240,11 +240,11 @@ wysihtml5.dom.parse = (function() { } } } - + if (setClass) { classes.push(setClass); } - + if (addClass) { for (attributeName in addClass) { method = addClassMethods[addClass[attributeName]]; @@ -257,10 +257,10 @@ wysihtml5.dom.parse = (function() { } } } - + // make sure that wysihtml5 temp class doesn't get stripped out allowedClasses["_wysihtml5-temp-placeholder"] = 1; - + // add old classes last oldClasses = oldNode.getAttribute("class"); if (oldClasses) { @@ -269,11 +269,11 @@ wysihtml5.dom.parse = (function() { classesLength = classes.length; for (; i hasAttribute = outerHTML.indexOf(" " + attributeName + "=") != -1; - + return hasAttribute ? node.getAttribute(attributeName) : null; } else{ return node.getAttribute(attributeName); } } - + /** * Check whether the given node is a proper loaded image * FIXME: Returns undefined when unknown (Chrome, Safari) @@ -351,12 +351,12 @@ wysihtml5.dom.parse = (function() { } } } - + function _handleText(oldNode) { return oldNode.ownerDocument.createTextNode(oldNode.data); } - - + + // ------------ attribute checks ------------ \\ var attributeCheckMethods = { url: (function() { @@ -394,7 +394,7 @@ wysihtml5.dom.parse = (function() { }); }; })(), - + alt: (function() { var REG_EXP = /[^ a-z0-9_\-]/gi; return function(attributeValue) { @@ -404,7 +404,7 @@ wysihtml5.dom.parse = (function() { return attributeValue.replace(REG_EXP, ""); }; })(), - + numbers: (function() { var REG_EXP = /\D/g; return function(attributeValue) { @@ -413,7 +413,7 @@ wysihtml5.dom.parse = (function() { }; })() }; - + // ------------ class converter (converts an html attribute to a class name) ------------ \\ var addClassMethods = { align_img: (function() { @@ -425,7 +425,7 @@ wysihtml5.dom.parse = (function() { return mapping[String(attributeValue).toLowerCase()]; }; })(), - + align_text: (function() { var mapping = { left: "wysiwyg-text-align-left", @@ -437,7 +437,7 @@ wysihtml5.dom.parse = (function() { return mapping[String(attributeValue).toLowerCase()]; }; })(), - + clear_br: (function() { var mapping = { left: "wysiwyg-clear-left", @@ -449,7 +449,7 @@ wysihtml5.dom.parse = (function() { return mapping[String(attributeValue).toLowerCase()]; }; })(), - + size_font: (function() { var mapping = { "1": "wysiwyg-font-size-xx-small", @@ -467,6 +467,6 @@ wysihtml5.dom.parse = (function() { }; })() }; - + return parse; })();