diff --git a/grammars/mediawiki.js b/grammars/mediawiki.js index 2a97d36..fe374e1 100644 --- a/grammars/mediawiki.js +++ b/grammars/mediawiki.js @@ -294,3 +294,8 @@ var mediawiki = { rr.ZeroOrMore(rr.Ref('paragraph')), rr.EndOfText())) }; + + +var mediawiki_filters = { + 'bi': rr.SplitTagAndNest('b', 'i') +}; diff --git a/recentrunes.js b/recentrunes.js index 8885d00..0647545 100644 --- a/recentrunes.js +++ b/recentrunes.js @@ -716,10 +716,51 @@ rr.SingleLineText = function() { +/* ============ Filters ============ */ + + +rr.SplitTagAndNest = function() { + var hierarchy = Array.prototype.slice.call(arguments); + return function(node) { + var outerNode, innerNode; + for (var i = 0; i < hierarchy.length; i++) { + var newNode = document.createElement(hierarchy[i]); + if (i == 0) { + outerNode = innerNode = newNode; + } else { + innerNode.appendChild(newNode); + innerNode = newNode; + } + } + for (var i = 0; i < node.childNodes.length; i++) { + innerNode.appendChild(node.childNodes[i]); + } + node.parentNode.replaceChild(outerNode, node); + } +}; + + + /* ============ Scaffolding ============ */ +/** + * @param {Node} node + * @param {Object.} filters + */ +rr.ApplyFilters = function(node, filters) { + var filter = filters[node.nodeName.toLowerCase()]; + if (filter) { + filter(node); + } + for (var i = 0; i < node.childNodes.length; i++) { + rr.ApplyFilters(node.childNodes[i], filters); + } +}; + + + /** * @constructor * diff --git a/test.js b/test.js index ac7562a..279e886 100644 --- a/test.js +++ b/test.js @@ -39,12 +39,12 @@ QUnit.test('Base', function(assert) { var expected = [ '

This is a paragraph with many text styles. This is italic and ', - 'this \nis bold; this is both. This is underline ', - 'as is \nthis. This is underlined, bold and italic', - '. \nThis is strikethrough, as is this. Source ', - '\ncode looks like this. Fixed width text looks like \n', - 'this.

This sentence is inline pre-formatted, which stops \n',
-    "'''''this from being bold and italic.'''''
We can also \nstop ", + 'this \nis bold; this is both. This is underline', + ' as is \nthis. This is underlined, bold and italic', + '. \nThis is strikethrough, as is this. ', + 'Source \ncode looks like this. Fixed width text looks like ', + '\nthis.
This sentence is inline pre-formatted, which stops',
+    " \n'''''this from being bold and italic.'''''
We can also \nstop ", '<u>this from being underlined</u>, or just try \n', '<pre>interrupting cow style.</pre>
This is a ', 'blockquote

Header 2

Header 3 with ', @@ -61,7 +61,9 @@ QUnit.test('Base', function(assert) { var context = new rr.Context(mediawiki, content); var iterable = context.rules['wikidoc'].match(context); - assert.equal(iterable.next().value.nodes[0].innerHTML, expected); + var rootNode = iterable.next().value.nodes[0]; + rr.ApplyFilters(rootNode, mediawiki_filters); + assert.equal(rootNode.innerHTML, expected); }); QUnit.test('singleline-wikichunk', function(assert) {