comparison src/formats/bbcode.js @ 41:7491026f7623

remove isSelfClosing
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 19 Aug 2022 19:27:03 -0600
parents 0c4e4b5ef40d
children ab2dc8736d88
comparison
equal deleted inserted replaced
40:eaecca025287 41:7491026f7623
146 }, 146 },
147 table: { 147 table: {
148 txtExec: ['[table][tr][td]', '[/td][/tr][/table]'] 148 txtExec: ['[table][tr][td]', '[/td][/tr][/table]']
149 }, 149 },
150 horizontalrule: { 150 horizontalrule: {
151 txtExec: ['[hr]'] 151 txtExec: ['[hr][/hr]']
152 }, 152 },
153 code: { 153 code: {
154 txtExec: ['[code]', '[/code]'] 154 txtExec: ['[code]', '[/code]']
155 }, 155 },
156 image: { 156 image: {
527 hr: { 527 hr: {
528 tags: { 528 tags: {
529 hr: null 529 hr: null
530 }, 530 },
531 allowsEmpty: true, 531 allowsEmpty: true,
532 isSelfClosing: true,
533 isInline: false, 532 isInline: false,
534 format: '[hr]{0}', 533 format: '[hr][/hr]',
535 html: '<hr />' 534 html: '<hr />'
536 }, 535 },
537 // END_COMMAND 536 // END_COMMAND
538 537
539 // START_COMMAND: Image 538 // START_COMMAND: Image
1292 } 1291 }
1293 1292
1294 addTag(token); 1293 addTag(token);
1295 bbcode = bbcodeHandlers[token.name]; 1294 bbcode = bbcodeHandlers[token.name];
1296 1295
1297 // If this tag is not self closing and it has a closing 1296 // If this tag has a closing
1298 // tag then it is open and has children so add it to the 1297 // tag then it is open and has children so add it to the
1299 // list of open tags. If has the closedBy property then 1298 // list of open tags. If has the closedBy property then
1300 // it is closed by other tags so include everything as 1299 // it is closed by other tags so include everything as
1301 // it's children until one of those tags is reached. 1300 // it's children until one of those tags is reached.
1302 if (bbcode && !bbcode.isSelfClosing && 1301 if (bbcode &&
1303 (bbcode.closedBy || 1302 (bbcode.closedBy ||
1304 hasTag(token.name, TOKEN_CLOSE, toks))) { 1303 hasTag(token.name, TOKEN_CLOSE, toks))) {
1305 openTags.push(token); 1304 openTags.push(token);
1306 } else if (!bbcode || !bbcode.isSelfClosing) { 1305 } else {
1307 token.type = TOKEN_CONTENT; 1306 token.type = TOKEN_CONTENT;
1308 } 1307 }
1309 break; 1308 break;
1310 1309
1311 case TOKEN_CLOSE: 1310 case TOKEN_CLOSE:
1465 right = i < childrenLength - 1 ? children[i + 1] : null; 1464 right = i < childrenLength - 1 ? children[i + 1] : null;
1466 remove = false; 1465 remove = false;
1467 1466
1468 // Handle the start and end new lines 1467 // Handle the start and end new lines
1469 // e.g. [tag]\n and \n[/tag] 1468 // e.g. [tag]\n and \n[/tag]
1470 if (!onlyRemoveBreakAfter && parentBBCode && 1469 if (!onlyRemoveBreakAfter && parentBBCode) {
1471 parentBBCode.isSelfClosing !== true) {
1472 // First child of parent so must be opening line break 1470 // First child of parent so must be opening line break
1473 // (breakStartBlock, breakStart) e.g. [tag]\n 1471 // (breakStartBlock, breakStart) e.g. [tag]\n
1474 if (!left) { 1472 if (!left) {
1475 if (parentBBCode.isInline === false && 1473 if (parentBBCode.isInline === false &&
1476 base.opts.breakStartBlock && 1474 base.opts.breakStartBlock &&
1699 1697
1700 // Remove any empty children of this tag first so that if they 1698 // Remove any empty children of this tag first so that if they
1701 // are all removed this one doesn't think it's not empty. 1699 // are all removed this one doesn't think it's not empty.
1702 removeEmpty(token.children); 1700 removeEmpty(token.children);
1703 1701
1704 if (isTokenWhiteSpace(token.children) && bbcode && 1702 if (isTokenWhiteSpace(token.children) && bbcode && !bbcode.allowsEmpty) {
1705 !bbcode.isSelfClosing && !bbcode.allowsEmpty) {
1706 tokens.splice.apply(tokens, [i, 1].concat(token.children)); 1703 tokens.splice.apply(tokens, [i, 1].concat(token.children));
1707 } 1704 }
1708 } 1705 }
1709 } 1706 }
1710 1707
1847 * @param {array} toks Array of parsed tokens from base.parse() 1844 * @param {array} toks Array of parsed tokens from base.parse()
1848 * @return {string} 1845 * @return {string}
1849 * @private 1846 * @private
1850 */ 1847 */
1851 function convertToBBCode(toks) { 1848 function convertToBBCode(toks) {
1852 var token, attr, bbcode, isBlock, isSelfClosing, quoteType, 1849 var token, attr, bbcode, isBlock, quoteType,
1853 breakBefore, breakStart, breakEnd, breakAfter, 1850 breakBefore, breakStart, breakEnd, breakAfter,
1854 ret = ''; 1851 ret = '';
1855 1852
1856 while (toks.length > 0) { 1853 while (toks.length > 0) {
1857 if (!(token = toks.shift())) { 1854 if (!(token = toks.shift())) {
1858 continue; 1855 continue;
1859 } 1856 }
1860 // TODO: tidy this 1857 // TODO: tidy this
1861 bbcode = bbcodeHandlers[token.name]; 1858 bbcode = bbcodeHandlers[token.name];
1862 isBlock = !(!bbcode || bbcode.isInline !== false); 1859 isBlock = !(!bbcode || bbcode.isInline !== false);
1863 isSelfClosing = bbcode && bbcode.isSelfClosing;
1864 1860
1865 breakBefore = (isBlock && base.opts.breakBeforeBlock && 1861 breakBefore = (isBlock && base.opts.breakBeforeBlock &&
1866 bbcode.breakBefore !== false) || 1862 bbcode.breakBefore !== false) ||
1867 (bbcode && bbcode.breakBefore); 1863 (bbcode && bbcode.breakBefore);
1868 1864
1869 breakStart = (isBlock && !isSelfClosing && 1865 breakStart = (isBlock &&
1870 base.opts.breakStartBlock && 1866 base.opts.breakStartBlock &&
1871 bbcode.breakStart !== false) || 1867 bbcode.breakStart !== false) ||
1872 (bbcode && bbcode.breakStart); 1868 (bbcode && bbcode.breakStart);
1873 1869
1874 breakEnd = (isBlock && base.opts.breakEndBlock && 1870 breakEnd = (isBlock && base.opts.breakEndBlock &&
1916 if (token.children) { 1912 if (token.children) {
1917 ret += convertToBBCode(token.children); 1913 ret += convertToBBCode(token.children);
1918 } 1914 }
1919 1915
1920 // add closing tag if not self closing 1916 // add closing tag if not self closing
1921 if (!isSelfClosing && !bbcode.excludeClosing) { 1917 if (!bbcode.excludeClosing) {
1922 if (breakEnd) { 1918 if (breakEnd) {
1923 ret += '\n'; 1919 ret += '\n';
1924 } 1920 }
1925 1921
1926 ret += '[/' + token.name + ']'; 1922 ret += '[/' + token.name + ']';
1927 } 1923 }
1928 1924
1929 if (breakAfter) { 1925 if (breakAfter) {
1930 ret += '\n'; 1926 ret += '\n';
1931 }
1932
1933 // preserve whatever was recognized as the
1934 // closing tag if it is a self closing tag
1935 if (token.closing && isSelfClosing) {
1936 ret += token.closing.val;
1937 } 1927 }
1938 } else { 1928 } else {
1939 ret += token.val; 1929 ret += token.val;
1940 } 1930 }
1941 } 1931 }