mediawiki.jqueryMsg: Implement `<nowiki>` support
authorBartosz Dziewoński <matma.rex@gmail.com>
Wed, 11 May 2016 22:21:34 +0000 (00:21 +0200)
committerBartosz Dziewoński <matma.rex@gmail.com>
Thu, 12 May 2016 11:45:49 +0000 (13:45 +0200)
Only `<nowiki>...</nowiki>` is supported (self-closing `<nowiki/>` is not).
Anything inside the tag is treated as plain text and returned as-is (but
escaped, if necessary, as usual).

Bug: T47173
Change-Id: I9b037907470595753ef19374c87a6513b631eef8

resources/src/mediawiki/mediawiki.jqueryMsg.js
tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js

index e905f69..8a89fbc 100644 (file)
                                return result;
                        }
 
+                       // <nowiki>...</nowiki> tag. The tags are stripped and the contents are returned unparsed.
+                       function nowiki() {
+                               var parsedResult, plainText,
+                                       result = null;
+
+                               parsedResult = sequence( [
+                                       makeStringParser( '<nowiki>' ),
+                                       // We use a greedy non-backtracking parser, so we must ensure here that we don't take too much
+                                       makeRegexParser( /^.*?(?=<\/nowiki>)/ ),
+                                       makeStringParser( '</nowiki>' )
+                               ] );
+                               if ( parsedResult !== null ) {
+                                       plainText = parsedResult[ 1 ];
+                                       result = [ 'CONCAT' ].concat( plainText );
+                               }
+
+                               return result;
+                       }
+
                        templateName = transform(
                                // see $wgLegalTitleChars
                                // not allowing : due to the need to catch "PLURAL:$1"
                                wikilink,
                                extlink,
                                replacement,
+                               nowiki,
                                html,
                                literal
                        ] );
index ee948bb..8c96697 100644 (file)
                );
        } );
 
+       QUnit.test( 'Nowiki', 3, function ( assert ) {
+               mw.messages.set( 'jquerymsg-nowiki-link', 'Foo <nowiki>[[bar]]</nowiki> baz.' );
+               assert.equal(
+                       formatParse( 'jquerymsg-nowiki-link' ),
+                       'Foo [[bar]] baz.',
+                       'Link inside nowiki is not parsed'
+               );
+
+               mw.messages.set( 'jquerymsg-nowiki-htmltag', 'Foo <nowiki><b>bar</b></nowiki> baz.' );
+               assert.equal(
+                       formatParse( 'jquerymsg-nowiki-htmltag' ),
+                       'Foo &lt;b&gt;bar&lt;/b&gt; baz.',
+                       'HTML inside nowiki is not parsed and escaped'
+               );
+
+               mw.messages.set( 'jquerymsg-nowiki-template', 'Foo <nowiki>{{bar}}</nowiki> baz.' );
+               assert.equal(
+                       formatParse( 'jquerymsg-nowiki-template' ),
+                       'Foo {{bar}} baz.',
+                       'Template inside nowiki is not parsed and does not cause a parse error'
+               );
+       } );
+
        QUnit.test( 'Behavior in case of invalid wikitext', 3, function ( assert ) {
                mw.messages.set( 'invalid-wikitext', '<b>{{FAIL}}</b>' );