diff --git a/core/server/helpers/content.js b/core/server/helpers/content.js index ce29b42e60..9c7f25faed 100644 --- a/core/server/helpers/content.js +++ b/core/server/helpers/content.js @@ -9,7 +9,6 @@ var hbs = require('express-hbs'), _ = require('lodash'), downsize = require('downsize'), - downzero = require('../utils/downzero'), content; content = function (options) { @@ -20,13 +19,6 @@ content = function (options) { }); if (truncateOptions.hasOwnProperty('words') || truncateOptions.hasOwnProperty('characters')) { - // Legacy function: {{content words="0"}} should return leading tags. - if (truncateOptions.hasOwnProperty('words') && truncateOptions.words === 0) { - return new hbs.handlebars.SafeString( - downzero(this.html) - ); - } - return new hbs.handlebars.SafeString( downsize(this.html, truncateOptions) ); diff --git a/core/server/utils/downzero.js b/core/server/utils/downzero.js deleted file mode 100644 index 95e01a0a74..0000000000 --- a/core/server/utils/downzero.js +++ /dev/null @@ -1,109 +0,0 @@ -// Functions to imitate the behavior of Downsize@0.0.5 with 'words: "0"' (heavily based on Downsize) - -var stack, tagName, tagBuffer, truncatedText, parseState, pointer, - states = {unitialized: 0, tag_commenced: 1, tag_string: -1, tag_string_single: -2, comment: -3}, - voidElements = ['area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', - 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr']; - -function getTagName(tag) { - var tagName = (tag || '').match(/<\/*([a-z0-9\:\-\_]+)/i); - return tagName ? tagName[1] : null; -} - -function closeTag(openingTag) { - var tagName = (getTagName(openingTag)) ? '' : ''; - return tagName; -} - -function downzero(text) { - stack = []; - tagName = ''; - tagBuffer = ''; - truncatedText = ''; - parseState = 0; - pointer = 0; - - for (; pointer < text.length; pointer += 1) { - if (parseState !== states.unitialized) { - tagBuffer += text[pointer]; - } - - switch (text[pointer]) { - case '<': - if (parseState === states.unitialized && text[pointer + 1].match(/[a-z0-9\-\_\/\!]/)) { - parseState = states.tag_commenced; - tagBuffer += text[pointer]; - } - - break; - case '!': - if (parseState === states.tag_commenced && text[pointer - 1] === '<') { - parseState = states.comment; - } - - break; - case '\"': - if (parseState === states.tag_string) { - parseState = states.tag_commenced; - } else if (parseState === states.tag_string_single) { - // if double quote is found in a single quote string, ignore it and let the string finish - break; - } else if (parseState !== states.unitialized) { - parseState = states.tag_string; - } - - break; - case '\'': - if (parseState === states.tag_string_single) { - parseState = states.tag_commenced; - } else if (parseState === states.tag_string) { - break; - } else if (parseState !== states.unitialized) { - parseState = states.tag_string_single; - } - - break; - case '>': - if (parseState === states.tag_commenced) { - parseState = states.unitialized; - truncatedText += tagBuffer; - tagName = getTagName(tagBuffer); - - if (tagBuffer.match(/<\s*\//) && getTagName(stack[stack.length - 1]) === tagName) { - stack.pop(); - } else if (voidElements.indexOf(tagName) < 0 && !tagBuffer.match(/\/\s*>$/)) { - stack.push(tagBuffer); - } - tagBuffer = ''; - - continue; - } - - if (parseState === states.comment && text.substring(pointer - 2, pointer) === '--') { - parseState = states.unitialized; - truncatedText += tagBuffer; - tagBuffer = ''; - - continue; - } - - break; - case '-': - break; - } - - if (!parseState) { - break; - } - } - - truncatedText += tagBuffer; - - while (stack.length) { - truncatedText += closeTag(stack.pop()); - } - - return truncatedText; -} - -module.exports = downzero; diff --git a/core/test/unit/server_helpers/content_spec.js b/core/test/unit/server_helpers/content_spec.js index ffbe3c26dc..95a148c849 100644 --- a/core/test/unit/server_helpers/content_spec.js +++ b/core/test/unit/server_helpers/content_spec.js @@ -48,120 +48,7 @@ describe('{{content}} helper', function () { ); should.exist(rendered); - rendered.string.should.equal('

'); - }); - - it('can truncate html to 0 words, leaving image tag if it is first', function () { - var html = '

Hello World! It\'s me!

', - rendered = ( - helpers.content - .call( - {html: html}, - {hash: {words: '0'}} - ) - ); - - should.exist(rendered); - rendered.string.should.equal('

'); - }); - - it('can truncate html to 0 words, leaving image tag with attributes', function () { - var html = '

Alternative

', - rendered = ( - helpers.content - .call( - {html: html}, - {hash: {words: '0'}} - ) - ); - - should.exist(rendered); - rendered.string.should.equal('

Alternative

'); - }); - - it('can truncate html to 0 words, leaving first image tag & if alt text has a single quote', function () { - var html = '

It\'s me!Hello World! It\'s me!

', - rendered = ( - helpers.content - .call( - {html: html}, - {hash: {words: '0'}} - ) - ); - - should.exist(rendered); - rendered.string.should.equal('

It\'s me!

'); - }); - - it('can truncate html to 0 words, leaving first image tag & if alt text has a double quote', function () { - var html = '

A double quote is \'' + - 'Hello World! It\'s me!

', - rendered = ( - helpers.content - .call( - {html: html}, - {hash: {words: '0'}} - ) - ); - - should.exist(rendered); - rendered.string.should.equal('

A double quote is \'

'); - }); - - it('can truncate html to 0 words, leaving first image tag if it contains > & <', function () { - var html = '

', - rendered = ( - helpers.content - .call( - {html: html}, - {hash: {words: '0'}} - ) - ); - - should.exist(rendered); - rendered.string.should.equal('

'); - }); - - it('can truncate html to 0 words, leaving first two image tags', function () { - var html = '

Hi

', - rendered = ( - helpers.content - .call( - {html: html}, - {hash: {words: '0'}} - ) - ); - - should.exist(rendered); - rendered.string.should.equal('

'); - }); - - it('can truncate html to 0 words, removing image if text comes first', function () { - var html = '

BliBlob

', - rendered = ( - helpers.content - .call( - {html: html}, - {hash: {words: '0'}} - ) - ); - - should.exist(rendered); - rendered.string.should.equal('

'); - }); - - it('can truncate html to 0 words, leaving video tag', function () { - var html = '

', - rendered = ( - helpers.content - .call( - {html: html}, - {hash: {words: '0'}} - ) - ); - - should.exist(rendered); - rendered.string.should.equal('

'); + rendered.string.should.equal(''); }); it('can truncate html by character', function () {