From aabb047050ed66e2d451373b29a2587de7945be6 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Tue, 7 Jul 2015 19:40:55 +0200 Subject: [PATCH] Added missing JSDoc --- .../files/js/WoltLab/WCF/BBCode/FromHtml.js | 161 ++++++++++++++++-- .../files/js/WoltLab/WCF/BBCode/Parser.js | 53 +++++- .../files/js/WoltLab/WCF/BBCode/ToHtml.js | 149 +++++++++++++++- 3 files changed, 344 insertions(+), 19 deletions(-) diff --git a/wcfsetup/install/files/js/WoltLab/WCF/BBCode/FromHtml.js b/wcfsetup/install/files/js/WoltLab/WCF/BBCode/FromHtml.js index f05b2bef0a..34c7ecbd94 100644 --- a/wcfsetup/install/files/js/WoltLab/WCF/BBCode/FromHtml.js +++ b/wcfsetup/install/files/js/WoltLab/WCF/BBCode/FromHtml.js @@ -1,3 +1,11 @@ +/** + * Converts a message containing HTML tags into BBCodes. + * + * @author Alexander Ebert + * @copyright 2001-2015 WoltLab GmbH + * @license GNU Lesser General Public License + * @module WoltLab/WCF/BBCode/FromHtml + */ define(['EventHandler', 'StringUtil', 'DOM/Traverse'], function(EventHandler, StringUtil, DOMTraverse) { "use strict"; @@ -5,6 +13,13 @@ define(['EventHandler', 'StringUtil', 'DOM/Traverse'], function(EventHandler, St var _inlineConverter = {}; var _sourceConverter = []; + /** + * Returns true if a whitespace should be inserted before or after the smiley. + * + * @param {Element} element image element + * @param {boolean} before evaluate previous node + * @return {boolean} true if a whitespace should be inserted + */ function addSmileyPadding(element, before) { var target = element[(before ? 'previousSibling' : 'nextSibling')]; if (target === null || target.nodeType !== Node.TEXT_NODE || !/\s$/.test(target.textContent)) { @@ -14,7 +29,16 @@ define(['EventHandler', 'StringUtil', 'DOM/Traverse'], function(EventHandler, St return false; } + /** + * @module WoltLab/WCF/BBCode/FromHtml + */ var BBCodeFromHtml = { + /** + * Converts a message containing HTML elements into BBCodes. + * + * @param {string} message message containing HTML elements + * @return {string} message containing BBCodes + */ convert: function(message) { if (message.length) this._setup(); @@ -25,22 +49,38 @@ define(['EventHandler', 'StringUtil', 'DOM/Traverse'], function(EventHandler, St var elements = container.getElementsByTagName('P'); while (elements.length) elements[0].outerHTML = elements[0].innerHTML; - var elements = container.getElementsByTagName('BR'); + elements = container.getElementsByTagName('BR'); while (elements.length) elements[0].outerHTML = "\n"; + // prevent conversion taking place inside source bbcodes var sourceElements = this._preserveSourceElements(container); + EventHandler.fire('com.woltlab.wcf.bbcode.fromHtml', 'beforeConvert', { container: container }); + for (var i = 0, length = _converter.length; i < length; i++) { this._convert(container, _converter[i]); } + EventHandler.fire('com.woltlab.wcf.bbcode.fromHtml', 'afterConvert', { container: container }); + this._restoreSourceElements(container, sourceElements); + // remove remaining HTML elements + elements = container.getElementsByTagName('*'); + while (elements.length) elements[0].outerHTML = elements[0].innerHTML; + message = this._convertSpecials(container.innerHTML); return message; }, + /** + * Replaces HTML elements mapping to source BBCodes to avoid + * them being handled by other converters. + * + * @param {Element} container container element + * @return {array} list of source elements and their placeholder + */ _preserveSourceElements: function(container) { var elements, sourceElements = [], tmp; @@ -58,6 +98,32 @@ define(['EventHandler', 'StringUtil', 'DOM/Traverse'], function(EventHandler, St return sourceElements; }, + /** + * Replaces an element with a placeholder. + * + * @param {Element} element target element + * @param {array} list of removed elements and their placeholders + */ + _preserveSourceElement: function(element, sourceElements) { + var placeholder = document.createElement('var'); + placeholder.setAttribute('data-source', 'wcf'); + element.parentNode.insertBefore(placeholder, element); + + var fragment = document.createDocumentFragment(); + fragment.appendChild(element); + + sourceElements.push({ + fragment: fragment, + placeholder: placeholder + }); + }, + + /** + * Reinserts source elements for parsing. + * + * @param {Element} container container element + * @param {array} sourceElements list of removed elements and their placeholders + */ _restoreSourceElements: function(container, sourceElements) { var element, elements, placeholder; for (var i = 0, length = sourceElements.length; i < length; i++) { @@ -80,6 +146,12 @@ define(['EventHandler', 'StringUtil', 'DOM/Traverse'], function(EventHandler, St } }, + /** + * Converts special entities. + * + * @param {string} message HTML message + * @return {string} HTML message + */ _convertSpecials: function(message) { message = message.replace(/&/g, '&'); message = message.replace(/</g, '<'); @@ -88,6 +160,9 @@ define(['EventHandler', 'StringUtil', 'DOM/Traverse'], function(EventHandler, St return message; }, + /** + * Sets up converters applied to elements in linear order. + */ _setup: function() { if (_converter.length) { return; @@ -139,6 +214,12 @@ define(['EventHandler', 'StringUtil', 'DOM/Traverse'], function(EventHandler, St }); }, + /** + * Converts an element into a raw string. + * + * @param {Element} container container element + * @param {object} converter converter object + */ _convert: function(container, converter) { if (typeof converter === 'function') { converter(container); @@ -158,6 +239,11 @@ define(['EventHandler', 'StringUtil', 'DOM/Traverse'], function(EventHandler, St } }, + /** + * Converts
into [quote]. + * + * @param {Element} element target element + */ _convertBlockquote: function(element) { var author = element.getAttribute('data-author') || ''; var link = element.getAttribute('cite') || ''; @@ -184,6 +270,11 @@ define(['EventHandler', 'StringUtil', 'DOM/Traverse'], function(EventHandler, St element.outerHTML = open + element.innerHTML.replace(/^\n*/, '').replace(/\n*$/, '') + '[/quote]\n'; }, + /** + * Converts into smilies, [attach] or [img]. + * + * @param {Element} element target element + */ _convertImage: function(element) { if (element.classList.contains('smiley')) { // smiley @@ -224,6 +315,11 @@ define(['EventHandler', 'StringUtil', 'DOM/Traverse'], function(EventHandler, St } }, + /** + * Converts
    and