Comparing version 0.3.1 to 0.4.0
@@ -23,3 +23,3 @@ 'use strict'; | ||
, node = /tag|script/ | ||
, structural = /pre|textarea/ | ||
, structural = /pre|textarea|code/ | ||
, interpunction = /[\.,!%;:\-\?]/; | ||
@@ -46,2 +46,5 @@ | ||
if (options) config = util.mixin(config, options); | ||
// History of elements that require structure. | ||
this.ancestor = []; | ||
} | ||
@@ -68,3 +71,6 @@ | ||
*/ | ||
Helpers.prototype.tag = function tag (element, ancestor, data) { | ||
Helpers.prototype.tag = function tag (element, data) { | ||
// Check if the current element requires structure, store for later reference. | ||
if (this.structure(element)) this.ancestor.push(element); | ||
return (this.isInline(element) && (flow.test(data) || interpunction.test(data)) | ||
@@ -83,2 +89,4 @@ ? ' <' | ||
Helpers.prototype.close = function close (element) { | ||
if (this.structure(element)) this.ancestor.pop(); | ||
return node.test(element.type) && !~singular.indexOf(element.name) | ||
@@ -122,3 +130,2 @@ ? '</' + element.name + '>' | ||
* @param {Object} element | ||
* @param {Object} ancestor wrapping parent | ||
* @param {String} data minified content | ||
@@ -128,6 +135,11 @@ * @return {String} text | ||
*/ | ||
Helpers.prototype.text = function text (element, ancestor, data) { | ||
Helpers.prototype.text = function text (element, data) { | ||
element = element.data.trim(); | ||
if (!this.structure(ancestor)) element = element.replace(/\n/g, '').replace(/\s+/g, ' '); | ||
// If we have ancestors stored do not remove structure. | ||
if (!this.ancestor.length) { | ||
element = element.replace(/\n/g, '').replace(/\s+/g, ' '); | ||
} | ||
// Check if the text requires flowing based on last output and interpunction. | ||
if (flow.test(data) && !(new RegExp('^' + interpunction.source)).test(element)) { | ||
@@ -134,0 +146,0 @@ element = ' ' + element; |
@@ -10,4 +10,3 @@ 'use strict'; | ||
) | ||
, helpers | ||
, ancestor; | ||
, helpers; | ||
@@ -49,7 +48,4 @@ /** | ||
function walk (html, element) { | ||
html += helpers[element.type](element, ancestor, html); | ||
html += helpers[element.type](element, html); | ||
// Store reference to ancestor. | ||
if (element.children) ancestor = element; | ||
return (element.children | ||
@@ -56,0 +52,0 @@ ? traverse(element.children, html) |
{ | ||
"name": "minimize", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Minimize HTML", | ||
@@ -5,0 +5,0 @@ "main": "./lib/minimize", |
@@ -5,2 +5,3 @@ { | ||
, "interpunction": "<h3>Become a partner</h3>\n <p>\n Interested in being part of the solution?\n <a href=\"/company/contact\">Contact Nodejitsu to discuss</a>.\n </p>" | ||
, "code": "<code class=\"copy\">\n<span>var http = require('http');\nhttp.createServer(function (req, res) {\n res.writeHead(200, {'Content-Type': 'text/plain'});\n res.end('hello, i know nodejitsu');\n})listen(8080);</span><a href=\"#\"><s class=\"ss-layers\" role=\"presentation\"></s> copy</a></code>" | ||
, "doctype": { | ||
@@ -7,0 +8,0 @@ "raw": "!doctype html" |
@@ -83,4 +83,16 @@ 'use strict'; | ||
describe('function tag', function () { | ||
var structure; | ||
beforeEach(function () { | ||
structure = sinon.spy(helpers, 'structure'); | ||
}); | ||
afterEach(function () { | ||
structure.restore(); | ||
}); | ||
it('returns a string wrapped with < >', function () { | ||
expect(helpers.tag(html.doctype)).to.be.equal('<!doctype html>'); | ||
expect(structure).to.be.calledOnce; | ||
}); | ||
@@ -100,2 +112,4 @@ | ||
}); | ||
expect(structure).to.be.calledThrice; | ||
}); | ||
@@ -108,2 +122,4 @@ | ||
); | ||
expect(structure).to.be.calledOnce; | ||
}); | ||
@@ -115,2 +131,4 @@ | ||
); | ||
expect(structure).to.be.calledOnce; | ||
}); | ||
@@ -135,2 +153,12 @@ }); | ||
describe('function close', function () { | ||
var structure; | ||
beforeEach(function () { | ||
structure = sinon.spy(helpers, 'structure'); | ||
}); | ||
afterEach(function () { | ||
structure.restore(); | ||
}); | ||
it('only generates closing element for tags and scripts', function () { | ||
@@ -142,2 +170,3 @@ var result = helpers.close(html.doctype); | ||
expect(result.length).to.equal(0); | ||
expect(structure).to.be.calledOnce; | ||
}); | ||
@@ -150,2 +179,3 @@ | ||
expect(result).to.be.a('string'); | ||
expect(structure).to.be.calledOnce; | ||
}); | ||
@@ -159,2 +189,3 @@ | ||
expect(result.length).to.equal(0); | ||
expect(structure).to.be.calledOnce; | ||
}); | ||
@@ -197,3 +228,3 @@ }); | ||
it('returns true if element is pre or textarea', function () { | ||
it('returns true if element is textarea', function () { | ||
expect(helpers.structure(html.structure)).to.be.true; | ||
@@ -225,2 +256,3 @@ }); | ||
html.text.data = text; | ||
helpers.ancestor = []; | ||
}); | ||
@@ -230,3 +262,3 @@ | ||
html.text.data += ' '; | ||
var result = helpers.text(html.text, html.inline, ''); | ||
var result = helpers.text(html.text, ''); | ||
@@ -237,3 +269,3 @@ expect(result).to.be.equal(text); | ||
it('replaces whitelines and spaces in non structural elements', function () { | ||
var result = helpers.text(html.multiline, html.inline, ''); | ||
var result = helpers.text(html.multiline, ''); | ||
@@ -246,12 +278,11 @@ expect(result).to.be.equal( | ||
it('retains structure if element requires structure', function () { | ||
var structure = sinon.spy(helpers, 'structure'); | ||
helpers.ancestor = [ 'pre' ]; | ||
expect(helpers.text(html.multiline, html.script, '')).to.be.equal( | ||
expect(helpers.text(html.multiline, '')).to.be.equal( | ||
'some additional lines\n\n. some random text, and alot of spaces' | ||
); | ||
expect(structure).to.be.calledOnce; | ||
}); | ||
it('prepends space if current HTML ends with closing tag', function () { | ||
var result = helpers.text(html.text, html.inline, 'some HTML</strong>'); | ||
var result = helpers.text(html.text, 'some HTML</strong>'); | ||
@@ -261,4 +292,4 @@ expect(result).to.be.equal(' ' + text); | ||
it('prepends space if current HTML ends with word boundart', function () { | ||
var result = helpers.text(html.text, html.inline, 'some HTML'); | ||
it('prepends space if current HTML ends with word boundary', function () { | ||
var result = helpers.text(html.text, 'some HTML'); | ||
@@ -270,3 +301,3 @@ expect(result).to.be.equal(' ' + text); | ||
html.text.data = '. ' + html.text.data; | ||
var result = helpers.text(html.text, html.inline, 'some HTML'); | ||
var result = helpers.text(html.text, 'some HTML'); | ||
@@ -306,2 +337,3 @@ expect(result).to.be.equal(html.text.data); | ||
expect(helpers.structural.test('textarea')).to.be.true; | ||
expect(helpers.structural.test('code')).to.be.true; | ||
}); | ||
@@ -308,0 +340,0 @@ }); |
@@ -88,8 +88,10 @@ 'use strict'; | ||
it('should be configurable to retain CDATA', function (done) { | ||
minimize.minimize(html.comment, function (result) { | ||
expect(result).to.equal('<div class=\"slide nodejs\"><h3>100% Node.js</h3><p>We are Node.js experts and the first hosting platform to build our full stack in node. We understand your node application better than anyone.</p></div>'); | ||
it('should leave structural elements (like scripts and code) intact', function (done) { | ||
minimize.minimize(html.code, function (result) { | ||
expect(result).to.equal("<code class=\"copy\"><span>var http = require('http');\nhttp.createServer(function (req, res) {\n res.writeHead(200, {'Content-Type': 'text/plain'});\n res.end('hello, i know nodejitsu');\n})listen(8080);</span> <a href=\"#\"><s class=\"ss-layers\" role=\"presentation\"></s> copy</a></code>"); | ||
done(); | ||
}, { cdata: true }); | ||
}); | ||
}); | ||
it('should be configurable to retain CDATA'); | ||
}); | ||
@@ -96,0 +98,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28427
728