Framework for applying filters, and one filter to start with.
This commit is contained in:
@@ -294,3 +294,8 @@ var mediawiki = {
|
||||
rr.ZeroOrMore(rr.Ref('paragraph')),
|
||||
rr.EndOfText()))
|
||||
};
|
||||
|
||||
|
||||
var mediawiki_filters = {
|
||||
'bi': rr.SplitTagAndNest('b', 'i')
|
||||
};
|
||||
|
||||
@@ -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.<string, rr.typeFilter>} 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
|
||||
*
|
||||
|
||||
16
test.js
16
test.js
@@ -39,12 +39,12 @@ QUnit.test('Base', function(assert) {
|
||||
|
||||
var expected = [
|
||||
'<p>This is a paragraph with many text styles. This is <i>italic</i> and ',
|
||||
'this \nis <b>bold</b>; this is <bi>both</bi>. This is <u>underline</u> ',
|
||||
'as is \n<u>this</u>. This is <u><bi>underlined, bold and italic</bi>',
|
||||
'</u>. \nThis is <del>strikethrough</del>, as is <del>this</del>. Source ',
|
||||
'\ncode looks like <code>this</code>. Fixed width text looks like \n',
|
||||
'<tt>this</tt>. <pre>This sentence is inline pre-formatted, which stops \n',
|
||||
"'''''this from being bold and italic.'''''</pre> We can also \nstop ",
|
||||
'this \nis <b>bold</b>; this is <b><i>both</i></b>. This is <u>underline',
|
||||
'</u> as is \n<u>this</u>. This is <u><b><i>underlined, bold and italic',
|
||||
'</i></b></u>. \nThis is <del>strikethrough</del>, as is <del>this</del>. ',
|
||||
'Source \ncode looks like <code>this</code>. Fixed width text looks like ',
|
||||
'\n<tt>this</tt>. <pre>This sentence is inline pre-formatted, which stops',
|
||||
" \n'''''this from being bold and italic.'''''</pre> We can also \nstop ",
|
||||
'<u>this from being underlined</u>, or just try \n',
|
||||
'<pre>interrupting cow style.</pre><blockquote>This is a ',
|
||||
'blockquote</blockquote></p><p><h2>Header 2</h2><h3>Header 3 <i>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) {
|
||||
|
||||
Reference in New Issue
Block a user