comparison src/formats/bbcode.js @ 36:21090996a131

clean up TokenizeToken
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 16 Aug 2022 13:50:36 -0600
parents cd02cd04bc9d
children 0c4e4b5ef40d
comparison
equal deleted inserted replaced
35:cd02cd04bc9d 36:21090996a131
968 * @param {string} attrib Any attributes. Only set on 968 * @param {string} attrib Any attributes. Only set on
969 * TOKEN_TYPE_OPEN tokens 969 * TOKEN_TYPE_OPEN tokens
970 * @param {array} children Any children of this token 970 * @param {array} children Any children of this token
971 * @param {TokenizeToken} closing This tokens closing tag. 971 * @param {TokenizeToken} closing This tokens closing tag.
972 * Only set on TOKEN_TYPE_OPEN tokens 972 * Only set on TOKEN_TYPE_OPEN tokens
973 * @class {TokenizeToken}
974 * @name {TokenizeToken} 973 * @name {TokenizeToken}
975 * @memberOf BBCodeParser.prototype
976 */ 974 */
977 // eslint-disable-next-line max-params 975 // eslint-disable-next-line max-params
978 function TokenizeToken(type, name, val, attrib, children, closing) { 976 function newTokenizeToken(type, name, val, attrib, children, closing) {
979 var base = this; 977 let base = {};
980 978
981 base.type = type; 979 base.type = type;
982 base.name = name; 980 base.name = name;
983 base.val = val; 981 base.val = val;
984 base.attrib = attrib; 982 base.attrib = attrib;
985 base.children = children || []; 983 base.children = children || [];
986 base.closing = closing || null; 984 base.closing = closing || null;
987 }; 985
988 986 base.clone = function () {
989 TokenizeToken.prototype = { 987 return newTokenizeToken(
990 /** @lends BBCodeParser.prototype.TokenizeToken */
991 /**
992 * Clones this token
993 *
994 * @return {TokenizeToken}
995 */
996 clone: function () {
997 var base = this;
998
999 return new TokenizeToken(
1000 base.type, 988 base.type,
1001 base.name, 989 base.name,
1002 base.val, 990 base.val,
1003 base.attrib, 991 base.attrib,
1004 [], 992 [],
1005 base.closing ? base.closing.clone() : null 993 base.closing ? base.closing.clone() : null
1006 ); 994 );
1007 }, 995 };
1008 /** 996
1009 * Splits this token at the specified child 997 base.splitAt = function (splitAt) {
1010 *
1011 * @param {TokenizeToken} splitAt The child to split at
1012 * @return {TokenizeToken} The right half of the split token or
1013 * empty clone if invalid splitAt lcoation
1014 */
1015 splitAt: function (splitAt) {
1016 var offsetLength;
1017 var base = this;
1018 var clone = base.clone(); 998 var clone = base.clone();
1019 var offset = base.children.indexOf(splitAt); 999 var offset = base.children.indexOf(splitAt);
1020 1000
1021 if (offset > -1) { 1001 if (offset > -1) {
1022 // Work out how many items are on the right side of the split 1002 // Work out how many items are on the right side of the split
1023 // to pass to splice() 1003 // to pass to splice()
1024 offsetLength = base.children.length - offset; 1004 let offsetLength = base.children.length - offset;
1025 clone.children = base.children.splice(offset, offsetLength); 1005 clone.children = base.children.splice(offset, offsetLength);
1026 } 1006 }
1027 1007
1028 return clone; 1008 return clone;
1029 } 1009 };
1030 }; 1010
1031 1011 return base;
1012 }
1032 1013
1033 /** 1014 /**
1034 * SCEditor BBCode parser class 1015 * SCEditor BBCode parser class
1035 * 1016 *
1036 * @param {Object} options 1017 * @param {Object} options
1052 * before it. For that the tokens should be passed to the 1033 * before it. For that the tokens should be passed to the
1053 * parse function. 1034 * parse function.
1054 * 1035 *
1055 * @param {string} str 1036 * @param {string} str
1056 * @return {array} 1037 * @return {array}
1057 * @memberOf BBCodeParser.prototype
1058 */ 1038 */
1059 base.tokenize = function (str) { 1039 base.tokenize = function (str) {
1060 var matches, type, i; 1040 var matches, type, i;
1061 var tokens = []; 1041 var tokens = [];
1062 // The token types in reverse order of precedence 1042 // The token types in reverse order of precedence
1155 1135
1156 type = TOKEN_CONTENT; 1136 type = TOKEN_CONTENT;
1157 name = '#'; 1137 name = '#';
1158 } 1138 }
1159 1139
1160 return new TokenizeToken(type, name, val, attrib); 1140 return newTokenizeToken(type, name, val, attrib);
1161 } 1141 }
1162 1142
1163 /** 1143 /**
1164 * Parses a string into an array of BBCodes 1144 * Parses a string into an array of BBCodes
1165 * 1145 *
1166 * @param {string} str 1146 * @param {string} str
1167 * @param {boolean} preserveNewLines If to preserve all new lines, not 1147 * @param {boolean} preserveNewLines If to preserve all new lines, not
1168 * strip any based on the passed 1148 * strip any based on the passed
1169 * formatting options 1149 * formatting options
1170 * @return {array} Array of BBCode objects 1150 * @return {array} Array of BBCode objects
1171 * @memberOf BBCodeParser.prototype
1172 */ 1151 */
1173 base.parse = function (str, preserveNewLines) { 1152 base.parse = function (str, preserveNewLines) {
1174 var ret = parseTokens(base.tokenize(str)); 1153 var ret = parseTokens(base.tokenize(str));
1175 var opts = base.opts; 1154 var opts = base.opts;
1176 1155
1735 * @param {string} str 1714 * @param {string} str
1736 * @param {boolean} preserveNewLines If to preserve all new lines, not 1715 * @param {boolean} preserveNewLines If to preserve all new lines, not
1737 * strip any based on the passed 1716 * strip any based on the passed
1738 * formatting options 1717 * formatting options
1739 * @return {string} 1718 * @return {string}
1740 * @memberOf BBCodeParser.prototype
1741 */ 1719 */
1742 base.toHTML = function (str, preserveNewLines) { 1720 base.toHTML = function (str, preserveNewLines) {
1743 return convertToHTML(base.parse(str, preserveNewLines), true); 1721 return convertToHTML(base.parse(str, preserveNewLines), true);
1744 }; 1722 };
1745 1723
1854 * @param {string} str 1832 * @param {string} str
1855 * @param {boolean} preserveNewLines If to preserve all new lines, not 1833 * @param {boolean} preserveNewLines If to preserve all new lines, not
1856 * strip any based on the passed 1834 * strip any based on the passed
1857 * formatting options 1835 * formatting options
1858 * @return {string} 1836 * @return {string}
1859 * @memberOf BBCodeParser.prototype
1860 */ 1837 */
1861 base.toBBCode = function (str, preserveNewLines) { 1838 base.toBBCode = function (str, preserveNewLines) {
1862 return convertToBBCode(base.parse(str, preserveNewLines)); 1839 return convertToBBCode(base.parse(str, preserveNewLines));
1863 }; 1840 };
1864 1841
2373 * the innermost element and working backwards 2350 * the innermost element and working backwards
2374 * 2351 *
2375 * @private 2352 * @private
2376 * @param {HTMLElement} element 2353 * @param {HTMLElement} element
2377 * @return {string} BBCode 2354 * @return {string} BBCode
2378 * @memberOf SCEditor.plugins.bbcode.prototype
2379 */ 2355 */
2380 function elementToBbcode(element) { 2356 function elementToBbcode(element) {
2381 var toBBCode = function (node, vChildren) { 2357 var toBBCode = function (node, vChildren) {
2382 var ret = ''; 2358 var ret = '';
2383 2359