From 011eb3beee0a4506df1c6bdad82e9cf31cf227d9 Mon Sep 17 00:00:00 2001 From: David Verhasselt Date: Sat, 22 Apr 2017 13:48:24 +0300 Subject: [PATCH] Recursively replace other parts of already processed names --- lib/main.js | 21 ++++++++++++++++++--- test/main.js | 40 ++++++++++++++++++++++------------------ 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/lib/main.js b/lib/main.js index c8e005c..20d52a2 100644 --- a/lib/main.js +++ b/lib/main.js @@ -19,17 +19,32 @@ function HTMLUglify(config) { } HTMLUglify.prototype.checkForStandardPointer = function(lookups, type, value) { - return lookups[type] && lookups[type][value]; + if (value.length == 1) { + return value; + } + else { + return lookups[type] && lookups[type][value]; + } }; HTMLUglify.prototype.checkForAttributePointer = function(lookups, type, value) { var typeLookups = lookups[type] || {}; var keys = Object.keys(typeLookups); - var pointer; + var that = this; + var pointer, bits; keys.some(function(key) { if (value.indexOf(key) !== -1) { - pointer = value.replace(key, typeLookups[key]); + bits = value.split(key) + .map(function(bit) { + if (bit == "") { + return ""; + } else { + return that.pointer(type, bit, lookups); + } + }); + + pointer = bits.join(typeLookups[key]); return true; } return false; diff --git a/test/main.js b/test/main.js index 3becbef..1985f57 100644 --- a/test/main.js +++ b/test/main.js @@ -73,7 +73,7 @@ describe('HTMLUglify', function() { var value = 'somethingElse'; var pointer = htmlUglify.checkForAttributePointer(lookups, 'class', value); - assert.equal(pointer, 'zzzElse'); + assert.equal(pointer, 'zzzwk'); }); }); describe('#generatePointer', function() { @@ -443,12 +443,16 @@ describe('HTMLUglify', function() { }); it('uglifies attribute selectors correctly towards the end of a stylesheet', function() { var html = htmlUglify.process("

"); - assert.equal(html, '

'); + assert.equal(html, '

'); }); it('uglifies attribute selectors with spaced classes', function() { var html = htmlUglify.process("

"); - assert.equal(html, '

'); + assert.equal(html, '

'); }); + it('uglifies class names with replaced names', function() { + var html = htmlUglify.process("

"); + assert.equal(html, '

'); + }) }); describe('attribute selectors', function() { @@ -467,59 +471,59 @@ describe('HTMLUglify', function() { describe('anywhere selector', function() { it('uglifies in the middle of a string', function() { var html = htmlUglify.process('
'); - assert.equal(html, '
'); + assert.equal(html, '
'); html = htmlUglify.process('
'); - assert.equal(html, '
'); + assert.equal(html, '
'); html = htmlUglify.process('
'); - assert.equal(html, '
'); + assert.equal(html, '
'); }); it('uglifies at the start of a string', function() { var html = htmlUglify.process('
'); - assert.equal(html, '
'); + assert.equal(html, '
'); html = htmlUglify.process('
'); - assert.equal(html, '
'); + assert.equal(html, '
'); html = htmlUglify.process('
'); - assert.equal(html, '
'); + assert.equal(html, '
'); }); it('uglifies at the end of a string', function() { var html = htmlUglify.process('
'); - assert.equal(html, '
'); + assert.equal(html, '
'); html = htmlUglify.process('
'); - assert.equal(html, '
'); + assert.equal(html, '
'); html = htmlUglify.process('
'); - assert.equal(html, '
'); + assert.equal(html, '
'); }); }); describe('begins with selector', function() { it('uglifies at the start of a string', function() { var html = htmlUglify.process('
'); - assert.equal(html, '
'); + assert.equal(html, '
'); html = htmlUglify.process('
'); - assert.equal(html, '
'); + assert.equal(html, '
'); html = htmlUglify.process('
'); - assert.equal(html, '
'); + assert.equal(html, '
'); }); }); describe('ends with selector', function() { it('uglifies at the end of a string', function() { var html = htmlUglify.process('
'); - assert.equal(html, '
'); + assert.equal(html, '
'); html = htmlUglify.process('
'); - assert.equal(html, '
'); + assert.equal(html, '
'); html = htmlUglify.process('
'); - assert.equal(html, '
'); + assert.equal(html, '
'); }); }); });