Mercurial Hosting > sceditor
comparison src/formats/bbcode.js @ 27:8165b83907af
remove bind()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 10 Aug 2022 23:48:47 -0600 |
parents | 160be99e1517 |
children | db061869f28f |
comparison
equal
deleted
inserted
replaced
26:160be99e1517 | 27:8165b83907af |
---|---|
2520 * | 2520 * |
2521 * @param {boolean} asFragment | 2521 * @param {boolean} asFragment |
2522 * @param {string} source | 2522 * @param {string} source |
2523 * @param {boolean} [legacyAsFragment] Used by fromBBCode() method | 2523 * @param {boolean} [legacyAsFragment] Used by fromBBCode() method |
2524 */ | 2524 */ |
2525 function toHtml(asFragment, source, legacyAsFragment) { | 2525 function toHtml(asFragment) { |
2526 var parser = newBBCodeParser(base.opts.parserOptions); | 2526 return function(source, legacyAsFragment) { |
2527 var html = parser.toHTML( | 2527 var parser = newBBCodeParser(base.opts.parserOptions); |
2528 base.opts.bbcodeTrim ? source.trim() : source | 2528 var html = parser.toHTML( |
2529 ); | 2529 base.opts.bbcodeTrim ? source.trim() : source |
2530 | 2530 ); |
2531 return (asFragment || legacyAsFragment) ? | 2531 |
2532 removeFirstLastDiv(html) : html; | 2532 return (asFragment || legacyAsFragment) ? |
2533 removeFirstLastDiv(html) : html; | |
2534 }; | |
2533 } | 2535 } |
2534 | 2536 |
2535 /** | 2537 /** |
2536 * Converts HTML into BBCode | 2538 * Converts HTML into BBCode |
2537 * | 2539 * |
2540 * @param {!Document} [context] | 2542 * @param {!Document} [context] |
2541 * @param {!HTMLElement} [parent] | 2543 * @param {!HTMLElement} [parent] |
2542 * @return {string} | 2544 * @return {string} |
2543 * @private | 2545 * @private |
2544 */ | 2546 */ |
2545 function toSource(asFragment, html, context, parent) { | 2547 function toSource(asFragment) { |
2546 context = context || document; | 2548 return function(html, context, parent) { |
2547 | 2549 context = context || document; |
2548 var bbcode, elements; | 2550 |
2549 var containerParent = context.createElement('div'); | 2551 var bbcode, elements; |
2550 var container = context.createElement('div'); | 2552 var containerParent = context.createElement('div'); |
2551 var parser = newBBCodeParser(base.opts.parserOptions); | 2553 var container = context.createElement('div'); |
2552 | 2554 var parser = newBBCodeParser(base.opts.parserOptions); |
2553 container.innerHTML = html; | 2555 |
2554 css(containerParent, 'visibility', 'hidden'); | 2556 container.innerHTML = html; |
2555 containerParent.appendChild(container); | 2557 css(containerParent, 'visibility', 'hidden'); |
2556 context.body.appendChild(containerParent); | 2558 containerParent.appendChild(container); |
2557 | 2559 context.body.appendChild(containerParent); |
2558 if (asFragment) { | 2560 |
2559 // Add text before and after so removeWhiteSpace doesn't remove | 2561 if (asFragment) { |
2560 // leading and trailing whitespace | 2562 // Add text before and after so removeWhiteSpace doesn't remove |
2561 containerParent.insertBefore( | 2563 // leading and trailing whitespace |
2562 context.createTextNode('#'), | 2564 containerParent.insertBefore( |
2563 containerParent.firstChild | 2565 context.createTextNode('#'), |
2564 ); | 2566 containerParent.firstChild |
2565 containerParent.appendChild(context.createTextNode('#')); | 2567 ); |
2566 } | 2568 containerParent.appendChild(context.createTextNode('#')); |
2567 | 2569 } |
2568 // Match parents white-space handling | 2570 |
2569 if (parent) { | 2571 // Match parents white-space handling |
2570 css(container, 'whiteSpace', css(parent, 'whiteSpace')); | 2572 if (parent) { |
2571 } | 2573 css(container, 'whiteSpace', css(parent, 'whiteSpace')); |
2572 | 2574 } |
2573 // Remove all nodes with sceditor-ignore class | 2575 |
2574 elements = container.getElementsByClassName('sceditor-ignore'); | 2576 // Remove all nodes with sceditor-ignore class |
2575 while (elements.length) { | 2577 elements = container.getElementsByClassName('sceditor-ignore'); |
2576 elements[0].parentNode.removeChild(elements[0]); | 2578 while (elements.length) { |
2577 } | 2579 elements[0].parentNode.removeChild(elements[0]); |
2578 | 2580 } |
2579 dom.removeWhiteSpace(containerParent); | 2581 |
2580 | 2582 dom.removeWhiteSpace(containerParent); |
2581 bbcode = elementToBbcode(container); | 2583 |
2582 | 2584 bbcode = elementToBbcode(container); |
2583 context.body.removeChild(containerParent); | 2585 |
2584 | 2586 context.body.removeChild(containerParent); |
2585 bbcode = parser.toBBCode(bbcode, true); | 2587 |
2586 | 2588 bbcode = parser.toBBCode(bbcode, true); |
2587 if (base.opts.bbcodeTrim) { | 2589 |
2588 bbcode = bbcode.trim(); | 2590 if (base.opts.bbcodeTrim) { |
2589 } | 2591 bbcode = bbcode.trim(); |
2590 | 2592 } |
2591 return bbcode; | 2593 |
2592 }; | 2594 return bbcode; |
2593 | 2595 }; |
2594 base.toHtml = toHtml.bind(null, false); | 2596 } |
2595 base.fragmentToHtml = toHtml.bind(null, true); | 2597 |
2596 base.toSource = toSource.bind(null, false); | 2598 base.toHtml = toHtml(false); |
2597 base.fragmentToSource = toSource.bind(null, true); | 2599 base.fragmentToHtml = toHtml(true); |
2600 base.toSource = toSource(false); | |
2601 base.fragmentToSource = toSource(true); | |
2598 | 2602 |
2599 return base; | 2603 return base; |
2600 }; | 2604 }; |
2601 | 2605 |
2602 /** | 2606 /** |