Merge pull request #640 from jgable/fixFancyPlugin

Fix FancyFirstChar nested tag content
This commit is contained in:
Hannah Wolfe 2013-09-07 15:57:58 -07:00
commit 8a5f96b635
2 changed files with 156 additions and 7 deletions

View File

@ -1,16 +1,55 @@
var util = require('util'),
_ = require('underscore'),
fancifyPlugin;
fancifyPlugin,
whiteSpace = [
'',
' ',
'\t',
'\n',
'\r'
];
fancifyPlugin = {
// Fancify a single post body
fancify: function (originalContent) {
var newContent,
firstCharIndex = 0;
firstCharIndex = 0,
firstChar,
getIndexOfNextCharacter = function (beginFrom) {
var currIndex = beginFrom,
nextChar;
nextChar = originalContent.substr(currIndex, 1);
while (_.contains(whiteSpace, nextChar) && currIndex !== originalContent.length) {
currIndex += 1;
nextChar = originalContent.substr(currIndex, 1);
}
if (originalContent.substr(0, 1) === '<') {
firstCharIndex = originalContent.indexOf('>') + 1;
return currIndex;
},
getAfterNextClosingTag = function (beginFrom) {
return originalContent.indexOf('>', beginFrom) + 1;
};
// Skip any leading white space until we get a character
firstCharIndex = getIndexOfNextCharacter(firstCharIndex);
firstChar = originalContent.substr(firstCharIndex, 1);
while (firstChar === '<') {
// Get after the close of the tag
firstCharIndex = getAfterNextClosingTag(firstCharIndex);
// Skip any white space until we get a character
firstCharIndex = getIndexOfNextCharacter(firstCharIndex);
// Grab the character
firstChar = originalContent.substr(firstCharIndex, 1);
}
// Do nothing if we found no characters
if (firstCharIndex === originalContent.length) {
return originalContent;
}
newContent = originalContent.substr(0, firstCharIndex);

View File

@ -9,10 +9,13 @@ var testUtils = require('./testUtils'),
// Stuff we are testing
plugins = require('../../server/plugins'),
GhostPlugin = plugins.GhostPlugin,
loader = require('../../server/plugins/loader');
loader = require('../../server/plugins/loader'),
FancyFirstChar = require('../../../content/plugins/FancyFirstChar');
describe('Plugins', function () {
var sandbox;
before(function (done) {
testUtils.clearData().then(function () {
done();
@ -21,12 +24,16 @@ describe('Plugins', function () {
beforeEach(function (done) {
this.timeout(5000);
sandbox = sinon.sandbox.create();
testUtils.initData().then(function () {
done();
}, done);
});
afterEach(function (done) {
sandbox.restore();
testUtils.clearData().then(function () {
done();
}, done);
@ -95,8 +102,8 @@ describe('Plugins', function () {
it("can initialize an array of plugins", function (done) {
var fakeGhost = {
registerFilter: function () { return; },
unregisterFilter: function () { return; }
registerFilter: sandbox.stub(),
unregisterFilter: sandbox.stub()
},
installSpy = sinon.spy(loader, "installPluginByName"),
activateSpy = sinon.spy(loader, "activatePluginByName");
@ -120,4 +127,107 @@ describe('Plugins', function () {
}, done);
});
describe("FancyFirstChar", function () {
it("has install and uninstall handlers", function () {
should.exist(FancyFirstChar.install);
should.exist(FancyFirstChar.uninstall);
});
it("activates and deactivates properly", function () {
var fakeGhost = {
registerFilter: sandbox.stub(),
unregisterFilter: sandbox.stub()
};
FancyFirstChar.activate(fakeGhost);
fakeGhost.registerFilter.called.should.equal(true);
FancyFirstChar.deactivate(fakeGhost);
fakeGhost.unregisterFilter.called.should.equal(true);
});
it("fancifies simple text", function () {
var original = "Some text to fancify",
expect = '<span class="fancyChar">S</span>ome text to fancify',
result;
result = FancyFirstChar.fancify(original);
result.should.equal(expect);
});
it("fancifies in single tag", function () {
var original = "<p>Some text to fancify</p>",
expect = '<p><span class="fancyChar">S</span>ome text to fancify</p>',
result;
result = FancyFirstChar.fancify(original);
result.should.equal(expect);
});
it("fancifies in nested tag", function () {
var original = "<p><strong>Some text</strong> to fancify</p>",
expect = '<p><strong><span class="fancyChar">S</span>ome text</strong> to fancify</p>',
result;
result = FancyFirstChar.fancify(original);
result.should.equal(expect);
});
it("fancifies in nested nested tag", function () {
var original = "<p><strong><em>Some</em> text</strong> to fancify</p>",
expect = '<p><strong><em><span class="fancyChar">S</span>ome</em> text</strong> to fancify</p>',
result;
result = FancyFirstChar.fancify(original);
result.should.equal(expect);
});
it("fancifies with tags before first text", function () {
var original = "<div class='someSpecialDiv'><img src='/kitty.jpg' alt='Kitteh' title='Kitteh'></div><p><strong>Some text</strong> to fancify</p>",
expect = "<div class='someSpecialDiv'><img src='/kitty.jpg' alt='Kitteh' title='Kitteh'></div><p><strong><span class=\"fancyChar\">S</span>ome text</strong> to fancify</p>",
result;
result = FancyFirstChar.fancify(original);
result.should.equal(expect);
});
it("does nothing if no text found", function () {
var original = "<div class='someSpecialDiv'><img src='/kitty.jpg' alt='Kitteh' title='Kitteh'></div>",
expect = "<div class='someSpecialDiv'><img src='/kitty.jpg' alt='Kitteh' title='Kitteh'></div>",
result;
result = FancyFirstChar.fancify(original);
result.should.equal(expect);
});
it("skips leading white space", function () {
var original = "\n\t <p>Some text to fancify</p>",
expect = '\n\t <p><span class="fancyChar">S</span>ome text to fancify</p>',
result;
result = FancyFirstChar.fancify(original);
result.should.equal(expect);
});
it("skips white space in inner tags", function () {
var original = "\n\t <p>\n\t\t Some text to fancify</p>",
expect = '\n\t <p>\n\t\t <span class="fancyChar">S</span>ome text to fancify</p>',
result;
result = FancyFirstChar.fancify(original);
result.should.equal(expect);
});
});
});