mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-25 11:55:03 +03:00
Fix FancyFirstChar nested tag content
Added some white space skipping and inner tag checking to the fancify method.
This commit is contained in:
parent
fa37a56922
commit
e56a0badb5
@ -1,16 +1,55 @@
|
|||||||
var util = require('util'),
|
var util = require('util'),
|
||||||
_ = require('underscore'),
|
_ = require('underscore'),
|
||||||
fancifyPlugin;
|
fancifyPlugin,
|
||||||
|
whiteSpace = [
|
||||||
|
'',
|
||||||
|
' ',
|
||||||
|
'\t',
|
||||||
|
'\n',
|
||||||
|
'\r'
|
||||||
|
];
|
||||||
|
|
||||||
fancifyPlugin = {
|
fancifyPlugin = {
|
||||||
|
|
||||||
// Fancify a single post body
|
// Fancify a single post body
|
||||||
fancify: function (originalContent) {
|
fancify: function (originalContent) {
|
||||||
var newContent,
|
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) === '<') {
|
return currIndex;
|
||||||
firstCharIndex = originalContent.indexOf('>') + 1;
|
},
|
||||||
|
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);
|
newContent = originalContent.substr(0, firstCharIndex);
|
||||||
|
@ -9,10 +9,13 @@ var testUtils = require('./testUtils'),
|
|||||||
// Stuff we are testing
|
// Stuff we are testing
|
||||||
plugins = require('../../server/plugins'),
|
plugins = require('../../server/plugins'),
|
||||||
GhostPlugin = plugins.GhostPlugin,
|
GhostPlugin = plugins.GhostPlugin,
|
||||||
loader = require('../../server/plugins/loader');
|
loader = require('../../server/plugins/loader'),
|
||||||
|
FancyFirstChar = require('../../../content/plugins/FancyFirstChar');
|
||||||
|
|
||||||
describe('Plugins', function () {
|
describe('Plugins', function () {
|
||||||
|
|
||||||
|
var sandbox;
|
||||||
|
|
||||||
before(function (done) {
|
before(function (done) {
|
||||||
testUtils.clearData().then(function () {
|
testUtils.clearData().then(function () {
|
||||||
done();
|
done();
|
||||||
@ -21,12 +24,16 @@ describe('Plugins', function () {
|
|||||||
|
|
||||||
beforeEach(function (done) {
|
beforeEach(function (done) {
|
||||||
this.timeout(5000);
|
this.timeout(5000);
|
||||||
|
sandbox = sinon.sandbox.create();
|
||||||
|
|
||||||
testUtils.initData().then(function () {
|
testUtils.initData().then(function () {
|
||||||
done();
|
done();
|
||||||
}, done);
|
}, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function (done) {
|
afterEach(function (done) {
|
||||||
|
sandbox.restore();
|
||||||
|
|
||||||
testUtils.clearData().then(function () {
|
testUtils.clearData().then(function () {
|
||||||
done();
|
done();
|
||||||
}, done);
|
}, done);
|
||||||
@ -95,8 +102,8 @@ describe('Plugins', function () {
|
|||||||
it("can initialize an array of plugins", function (done) {
|
it("can initialize an array of plugins", function (done) {
|
||||||
|
|
||||||
var fakeGhost = {
|
var fakeGhost = {
|
||||||
registerFilter: function () { return; },
|
registerFilter: sandbox.stub(),
|
||||||
unregisterFilter: function () { return; }
|
unregisterFilter: sandbox.stub()
|
||||||
},
|
},
|
||||||
installSpy = sinon.spy(loader, "installPluginByName"),
|
installSpy = sinon.spy(loader, "installPluginByName"),
|
||||||
activateSpy = sinon.spy(loader, "activatePluginByName");
|
activateSpy = sinon.spy(loader, "activatePluginByName");
|
||||||
@ -120,4 +127,107 @@ describe('Plugins', function () {
|
|||||||
}, done);
|
}, 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
Loading…
Reference in New Issue
Block a user