From f9bc0ac97d5dabde667aacc8a81f849a63e1abf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Estev=C3=A3o=20Soares=20dos=20Santos?= Date: Mon, 25 Apr 2022 07:31:32 +0100 Subject: [PATCH] add simple event trigger tests --- karma.browserstack.js | 1 + karma.conf.js | 3 +- src/converter.js | 35 +-- src/showdown.js | 8 + src/subParsers/makehtml/blockGamut.js | 2 +- src/subParsers/makehtml/emoji.js | 9 +- src/subParsers/makehtml/emphasisAndStrong.js | 2 +- src/subParsers/makehtml/horizontalRule.js | 59 +++- src/subParsers/makehtml/image.js | 26 +- src/subParsers/makehtml/{lists.js => list.js} | 8 +- src/subParsers/makehtml/paragraphs.js | 3 +- src/subParsers/makehtml/underline.js | 10 +- test/bootstrap.js | 18 +- test/mocks/md-to-trigger-all-events.md | 82 +++++ test/optionswp.js | 2 +- test/unit/events.js | 15 - test/unit/showdown.events.js | 293 ++++++++++++++++++ 17 files changed, 493 insertions(+), 83 deletions(-) rename src/subParsers/makehtml/{lists.js => list.js} (98%) create mode 100644 test/mocks/md-to-trigger-all-events.md delete mode 100644 test/unit/events.js create mode 100644 test/unit/showdown.events.js diff --git a/karma.browserstack.js b/karma.browserstack.js index 341e42c..a8daa51 100644 --- a/karma.browserstack.js +++ b/karma.browserstack.js @@ -70,6 +70,7 @@ module.exports = function (config) { files: [ { pattern: '.build/showdown.js'}, { pattern: 'src/options.js'}, + { pattern: 'test/mocks/md-to-trigger-all-events.ms', watched: false, included: false, served: true, nocache: false}, // tests { pattern: 'test/unit/showdown*.js' } //{ pattern: 'test/functional/showdown*.js' }, diff --git a/karma.conf.js b/karma.conf.js index ade836f..a3778f1 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -15,7 +15,8 @@ module.exports = function (config) { { pattern: 'src/options.js'}, // tests { pattern: 'test/unit/showdown*.js' }, - { pattern: 'test/functional/showdown*.js' }, + { pattern: 'test/unit/showdown.events.js' }, + //{ pattern: 'test/functional/makehtml/testsuite.*.js' }, ], reporters: ['progress'], port: 9876, // karma web server port diff --git a/src/converter.js b/src/converter.js index d5266a9..b34a20b 100644 --- a/src/converter.js +++ b/src/converter.js @@ -11,7 +11,7 @@ showdown.Converter = function (converterOptions) { 'use strict'; - var + let /** * Options used by this converter * @private @@ -207,7 +207,7 @@ showdown.Converter = function (converterOptions) { } function rTrimInputText (text) { - var rsp = text.match(/^\s*/)[0].length, + let rsp = text.match(/^\s*/)[0].length, rgx = new RegExp('^\\s{0,' + rsp + '}', 'gm'); return text.replace(rgx, ''); } @@ -236,37 +236,6 @@ showdown.Converter = function (converterOptions) { return event; }; - /** - * - * @param {string} evtName Event name - * @param {string} text Text - * @param {{}} options Converter Options - * @param {{}} globals Converter globals - * @param {{}} [pParams] extra params for event - * @returns showdown.Event - * @private - */ - this._dispatch = function dispatch (evtName, text, options, globals, pParams) { - evtName = evtName.toLowerCase(); - text = text || ''; - var params = pParams || {}; - params.converter = this; - params.input = text; - params.options = options; - params.globals = globals; - var event = new showdown.Event(evtName, text, params); - - if (listeners.hasOwnProperty(evtName)) { - for (var ei = 0; ei < listeners[evtName].length; ++ei) { - var nText = listeners[evtName][ei](event); - if (nText && typeof nText !== 'undefined') { - event.output = nText; - } - } - } - return event; - }; - /** * Listen to an event * @param {string} name diff --git a/src/showdown.js b/src/showdown.js index 76a6e68..a361b93 100644 --- a/src/showdown.js +++ b/src/showdown.js @@ -183,6 +183,14 @@ showdown.subParser = function (name, func) { } }; +/** + * + * @returns {{}} + */ +showdown.getSubParserList = function () { + return parsers; +}; + /** * Gets or registers an extension * @static diff --git a/src/subParsers/makehtml/blockGamut.js b/src/subParsers/makehtml/blockGamut.js index eb4eff6..2bb38d5 100644 --- a/src/subParsers/makehtml/blockGamut.js +++ b/src/subParsers/makehtml/blockGamut.js @@ -29,7 +29,7 @@ showdown.subParser('makehtml.blockGamut', function (text, options, globals) { // Do Horizontal Rules: text = showdown.subParser('makehtml.horizontalRule')(text, options, globals); - text = showdown.subParser('makehtml.lists')(text, options, globals); + text = showdown.subParser('makehtml.list')(text, options, globals); text = showdown.subParser('makehtml.codeBlock')(text, options, globals); text = showdown.subParser('makehtml.table')(text, options, globals); diff --git a/src/subParsers/makehtml/emoji.js b/src/subParsers/makehtml/emoji.js index 5ad80bf..7a868a1 100644 --- a/src/subParsers/makehtml/emoji.js +++ b/src/subParsers/makehtml/emoji.js @@ -25,9 +25,12 @@ showdown.subParser('makehtml.emoji', function (text, options, globals) { startEvent = globals.converter.dispatch(startEvent); text = startEvent.output; - let pattern = /:([\S]+?):/g; + let pattern = /:(\S+?):/g; text = text.replace(pattern, function (wholeMatch, emojiCode) { + if (!showdown.helper.emojis.hasOwnProperty(emojiCode)) { + return wholeMatch; + } let otp = ''; let captureStartEvent = new showdown.Event('makehtml.emoji.onCapture', emojiCode); captureStartEvent @@ -45,10 +48,8 @@ showdown.subParser('makehtml.emoji', function (text, options, globals) { // if something was passed as output, it takes precedence and will be used as output if (captureStartEvent.output && captureStartEvent.output !== '') { otp = captureStartEvent.output; - } else if (showdown.helper.emojis.hasOwnProperty(emojiCode)) { - otp = showdown.helper.emojis[emojiCode]; } else { - otp = wm; + otp = showdown.helper.emojis[emojiCode]; } let beforeHashEvent = new showdown.Event('makehtml.emoji.onHash', otp); diff --git a/src/subParsers/makehtml/emphasisAndStrong.js b/src/subParsers/makehtml/emphasisAndStrong.js index bef8b02..73ceff5 100644 --- a/src/subParsers/makehtml/emphasisAndStrong.js +++ b/src/subParsers/makehtml/emphasisAndStrong.js @@ -102,7 +102,7 @@ showdown.subParser('makehtml.emphasisAndStrong', function (text, options, global } } - let beforeHashEvent = new showdown.Event('makehtml.' + subEventName + '.onHash', otp); + let beforeHashEvent = new showdown.Event('makehtml.emphasisAndStrong.' + subEventName + '.onHash', otp); beforeHashEvent .setOutput(otp) ._setGlobals(globals) diff --git a/src/subParsers/makehtml/horizontalRule.js b/src/subParsers/makehtml/horizontalRule.js index 1adec0d..0c03087 100644 --- a/src/subParsers/makehtml/horizontalRule.js +++ b/src/subParsers/makehtml/horizontalRule.js @@ -20,10 +20,21 @@ showdown.subParser('makehtml.horizontalRule', function (text, options, globals) startEvent = globals.converter.dispatch(startEvent); text = startEvent.output; - let key = showdown.subParser('makehtml.hashBlock')('
', options, globals); - text = text.replace(/^ {0,2}( ?-){3,}[ \t]*$/gm, key); - text = text.replace(/^ {0,2}( ?\*){3,}[ \t]*$/gm, key); - text = text.replace(/^ {0,2}( ?_){3,}[ \t]*$/gm, key); + + const rgx1 = /^ {0,2}( ?-){3,}[ \t]*$/gm; + text = text.replace(/^ {0,2}( ?-){3,}[ \t]*$/gm, function (wholeMatch) { + return parse(rgx1, wholeMatch); + }); + + const rgx2 = /^ {0,2}( ?\*){3,}[ \t]*$/gm; + text = text.replace(/^ {0,2}( ?\*){3,}[ \t]*$/gm, function (wholeMatch) { + return parse(rgx2, wholeMatch); + }); + + const rgx3 = /^ {0,2}( ?\*){3,}[ \t]*$/gm; + text = text.replace(/^ {0,2}( ?_){3,}[ \t]*$/gm, function (wholeMatch) { + return parse(rgx3, wholeMatch); + }); let afterEvent = new showdown.Event('makehtml.horizontalRule.onEnd', text); afterEvent @@ -32,4 +43,44 @@ showdown.subParser('makehtml.horizontalRule', function (text, options, globals) ._setOptions(options); afterEvent = globals.converter.dispatch(afterEvent); return afterEvent.output; + + /** + * + * @param {RegExp} pattern + * @param {string} wholeMatch + * @returns {string} + */ + function parse (pattern, wholeMatch) { + let otp; + let captureStartEvent = new showdown.Event('makehtml.horizontalRule.onCapture', wholeMatch); + captureStartEvent + .setOutput(null) + ._setGlobals(globals) + ._setOptions(options) + .setRegexp(pattern) + .setMatches({ + _whoteMatch: wholeMatch + }) + .setAttributes({}); + captureStartEvent = globals.converter.dispatch(captureStartEvent); + + // if something was passed as output, it takes precedence + // and will be used as output + if (captureStartEvent.output && captureStartEvent.output !== '') { + otp = captureStartEvent.output; + } else { + otp = ''; + } + + let beforeHashEvent = new showdown.Event('makehtml.horizontalRule.onHash', otp); + beforeHashEvent + .setOutput(otp) + ._setGlobals(globals) + ._setOptions(options); + beforeHashEvent = globals.converter.dispatch(beforeHashEvent); + otp = beforeHashEvent.output; + otp = showdown.subParser('makehtml.hashBlock')(otp, options, globals); + return otp; + } + }); diff --git a/src/subParsers/makehtml/image.js b/src/subParsers/makehtml/image.js index d53d587..b596cd0 100644 --- a/src/subParsers/makehtml/image.js +++ b/src/subParsers/makehtml/image.js @@ -80,19 +80,19 @@ showdown.subParser('makehtml.image', function (text, options, globals) { function writeImageTag (subEvtName, pattern, wholeMatch, altText, url, linkId, width, height, title) { let gUrls = globals.gUrls, - gTitles = globals.gTitles, - gDims = globals.gDimensions, - matches = { - _wholeMatch: wholeMatch, - _altText: altText, - _linkId: linkId, - _url: url, - _width: width, - _height: height, - _title: title - }, - otp, - attributes = {}; + gTitles = globals.gTitles, + gDims = globals.gDimensions, + matches = { + _wholeMatch: wholeMatch, + _altText: altText, + _linkId: linkId, + _url: url, + _width: width, + _height: height, + _title: title + }, + otp, + attributes = {}; linkId = (linkId) ? linkId.toLowerCase() : null; diff --git a/src/subParsers/makehtml/lists.js b/src/subParsers/makehtml/list.js similarity index 98% rename from src/subParsers/makehtml/lists.js rename to src/subParsers/makehtml/list.js index 1c8958d..6f11cb2 100644 --- a/src/subParsers/makehtml/lists.js +++ b/src/subParsers/makehtml/list.js @@ -1,5 +1,5 @@ //// -// makehtml/lists.js +// makehtml/list.js // Copyright (c) 2022 ShowdownJS // // Transforms MD lists into `