Diff
checker
Text
Text
Images
Documents
Excel
Folders
Legal
Enterprise
Desktop
Pricing
Sign in
Download Diffchecker Desktop
Compare text
Find the difference between two text files
Tools
History
Real-time editor
Hide whitespace changes
Hide unchanged lines
Disable line wrap
Layout
Split
Unified
Diff precision
Smart
Word
Char
Text styles
Change appearance
Syntax highlighting
Choose syntax
Ignore
Transform text
Go to first change
Edit input
Diffchecker Desktop
The most secure way to run Diffchecker. Get the Diffchecker Desktop app: your diffs never leave your computer!
Get Desktop
Untitled diff
Created
9 years ago
Diff never expires
Clear
Export
Share
Explain
94 removals
Lines
Total
Removed
Characters
Total
Removed
To continue using this feature, upgrade to
Diff
checker
Pro
View Pricing
209 lines
Copy
57 additions
Lines
Total
Added
Characters
Total
Added
To continue using this feature, upgrade to
Diff
checker
Pro
View Pricing
174 lines
Copy
'use strict';
'use strict';
exports.type = 'full';
exports.type = 'full';
exports.active = true;
exports.active = true;
exports.description = 'removes unused IDs and minifies used';
exports.description = 'removes unused IDs and minifies used';
exports.params = {
exports.params = {
remove: true,
remove: true,
minify: true,
minify: true,
Copy
Copied
Copy
Copied
prefix: ''
prefix: ''
,
preserve: [],
force: false
};
};
Copy
Copied
Copy
Copied
var referencesProps =
require('./_collections').referencesProps
,
var referencesProps =
new Set(
require('./_collections').referencesProps
)
,
regReferencesUrl = /\burl\(("|')?#(.+?)\1\)/,
regReferencesUrl = /\burl\(("|')?#(.+?)\1\)/,
regReferencesHref = /^#(.+?)$/,
regReferencesHref = /^#(.+?)$/,
regReferencesBegin = /^(\w+?)\./,
regReferencesBegin = /^(\w+?)\./,
styleOrScript = ['style', 'script'],
styleOrScript = ['style', 'script'],
generateIDchars = [
generateIDchars = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
],
],
maxIDindex = generateIDchars.length - 1;
maxIDindex = generateIDchars.length - 1;
/**
/**
* Remove unused and minify used IDs
* Remove unused and minify used IDs
* (only if there are no any <style> or <script>).
* (only if there are no any <style> or <script>).
*
*
* @param {Object} item current iteration item
* @param {Object} item current iteration item
* @param {Object} params plugin params
* @param {Object} params plugin params
*
*
* @author Kir Belevich
* @author Kir Belevich
*/
*/
exports.fn = function(data, params) {
exports.fn = function(data, params) {
Copy
Copied
Copy
Copied
var currentID,
var currentID,
currentIDstring,
currentIDstring,
Copy
Copied
Copy
Copied
IDs =
Object.create(null
),
IDs =
new Map(
),
referencesIDs =
Object.create(null
),
referencesIDs =
new Map(
),
idPrefix = 'id-', // prefix IDs so that values like '__proto__' don't break the work
hasStyleOrScript = false
,
hasStyleOrScript = false
;
preserveIDs = new Set(Array.isArray(params.preserve) ? params.preserve : params.preserve ? [params.preserve] : []),
idValuePrefix = '#',
idValuePostfix = '.';
/**
/**
* Bananas!
* Bananas!
*
*
* @param {Array} items input items
* @param {Array} items input items
* @return {Array} output items
* @return {Array} output items
*/
*/
function monkeys(items) {
function monkeys(items) {
Copy
Copied
Copy
Copied
for (var i = 0; i < items.content.length && !hasStyleOrScript; i++) {
for (var i = 0; i < items.content.length && !hasStyleOrScript; i++) {
Copy
Copied
Copy
Copied
var item = items.content[i]
;
var item = items.content[i]
,
match
;
Copy
Copied
Copy
Copied
//
check
if <style> of <script> presents
//
quit
if <style> of <script> presents
('force' param prevents quitting)
if (
item.isElem(styleOrScript)) {
if (
!params.force &&
item.isElem(styleOrScript)) {
hasStyleOrScript = true;
hasStyleOrScript = true;
continue;
continue;
}
}
Copy
Copied
Copy
Copied
// …and don't remove any ID if yes
// …and don't remove any ID if yes
if (item.isElem()) {
if (item.isElem()) {
Copy
Copied
Copy
Copied
item.eachAttr(function(attr) {
item.eachAttr(function(attr) {
Copy
Copied
Copy
Copied
var key
;
var key
, match;
// save IDs
// save IDs
if (attr.name === 'id') {
if (attr.name === 'id') {
Copy
Copied
Copy
Copied
key =
idPrefix +
attr.value;
key =
attr.value;
if (
key in
IDs
) {
if (
IDs
.has(key)
) {
item.removeAttr('id');
item.removeAttr('id');
// remove repeated id
} else {
} else {
Copy
Copied
Copy
Copied
IDs
[
key
] =
item
;
IDs
.set(
key
,
item
);
}
}
// save IDs url() references
else if (referencesProps.indexOf(attr.name) > -1) {
match = attr.value.match(regReferencesUrl);
if (match) {
key = idPrefix + match[2];
if (referencesIDs[key]) {
referencesIDs[key].push(attr);
} else {
referencesIDs[key] = [attr];
}
}
}
Copy
Copied
Copy
Copied
return;
}
}
Copy
Copied
Copy
Copied
// save
references
// save
IDs href
references
if (referencesProps.has(attr.name) && (match = attr.value.match(regReferencesUrl))) {
else if (
key = match[2]; // url() reference
}
else if (
attr.local === 'href' && (match = attr.value.match(regReferencesHref)) ||
attr.local === 'href' && (match = attr.value.match(regReferencesHref)) ||
attr.name === 'begin' && (match = attr.value.match(regReferencesBegin))
attr.name === 'begin' && (match = attr.value.match(regReferencesBegin))
) {
) {
Copy
Copied
Copy
Copied
key =
idPrefix +
match[1];
key =
match[1];
// href reference
if (
referencesIDs[
key
]
) {
}
referencesIDs
[
key
]
.push(attr);
if (
key
) {
} else {
var ref =
referencesIDs
.get(
key
) || [];
referencesIDs
[
key
] = [attr];
ref
.push(attr);
}
referencesIDs
.set(
key
, ref);
}
}
});
});
Copy
Copied
Copy
Copied
}
}
Copy
Copied
Copy
Copied
// go deeper
// go deeper
if (item.content) {
if (item.content) {
monkeys(item);
monkeys(item);
}
}
}
}
Copy
Copied
Copy
Copied
return items;
return items;
Copy
Copied
Copy
Copied
}
}
data = monkeys(data);
data = monkeys(data);
if (hasStyleOrScript) {
if (hasStyleOrScript) {
return data;
return data;
}
}
Copy
Copied
Copy
Copied
var idKey;
for (var
ref of
referencesIDs) {
for (var
k in
referencesIDs) {
var key
=
ref[0];
if (IDs[k]) {
idKey
=
k;
if (IDs.has(key)) {
k = k.replace(idPrefix, '');
// replace referenced IDs with the minified ones
// replace referenced IDs with the minified ones
Copy
Copied
Copy
Copied
if (params.minify
) {
if (params.minify
&& !preserveIDs.has(key)
) {
currentIDstring = getIDstring(currentID = generateID(currentID), params);
currentIDstring = getIDstring(currentID = generateID(currentID), params);
Copy
Copied
Copy
Copied
IDs
[idKey]
.attr('id').value = currentIDstring;
IDs
.get(key)
.attr('id').value = currentIDstring;
referencesIDs[idKey].forEach(function(attr) {
attr.value = attr.value
.replace('#' + k, '#' + currentIDstring)
.replace(k + '.', currentIDstring + '.');
});
Copy
Copied
Copy
Copied
idKey = idPrefix + k;
for (var attr of ref[1]) {
attr.value = attr.value.includes(idValuePrefix) ?
attr.value.replace(idValuePrefix + key, idValuePrefix + currentIDstring) :
attr.value.replace(key + idValuePostfix, currentIDstring + idValuePostfix);
}
}
}
Copy
Copied
Copy
Copied
// don't remove referenced IDs
// don't remove referenced IDs
Copy
Copied
Copy
Copied
delete
IDs[idKey]
;
IDs.
delete
(key)
;
}
}
}
}
Copy
Copied
Copy
Copied
// remove non-referenced IDs attributes from elements
// remove non-referenced IDs attributes from elements
if (params.remove) {
if (params.remove) {
Copy
Copied
Copy
Copied
for(var
keyElem of
IDs) {
for(var
ID in
IDs) {
if (!preserveIDs.has(keyElem[0])) {
IDs[ID
].removeAttr('id');
keyElem[1
].removeAttr('id');
}
}
}
Copy
Copied
Copy
Copied
}
}
Copy
Copied
Copy
Copied
return data;
return data;
Copy
Copied
Copy
Copied
};
};
/**
/**
* Generate unique minimal ID.
* Generate unique minimal ID.
*
*
* @param {Array} [currentID] current ID
* @param {Array} [currentID] current ID
* @return {Array} generated ID array
* @return {Array} generated ID array
*/
*/
function generateID(currentID) {
function generateID(currentID) {
Copy
Copied
Copy
Copied
if (!currentID) return [0];
if (!currentID) return [0];
currentID[currentID.length - 1]++;
currentID[currentID.length - 1]++;
for(var i = currentID.length - 1; i > 0; i--) {
for(var i = currentID.length - 1; i > 0; i--) {
if (currentID[i] > maxIDindex) {
if (currentID[i] > maxIDindex) {
currentID[i] = 0;
currentID[i] = 0;
if (currentID[i - 1] !== undefined) {
if (currentID[i - 1] !== undefined) {
currentID[i - 1]++;
currentID[i - 1]++;
}
}
}
}
}
}
Copy
Copied
Copy
Copied
if (currentID[0] > maxIDindex) {
if (currentID[0] > maxIDindex) {
currentID[0] = 0;
currentID[0] = 0;
currentID.unshift(0);
currentID.unshift(0);
}
}
Copy
Copied
Copy
Copied
return currentID;
return currentID;
Copy
Copied
Copy
Copied
}
}
/**
/**
* Get string from generated ID array.
* Get string from generated ID array.
*
*
* @param {Array} arr input ID array
* @param {Array} arr input ID array
* @return {String} output ID string
* @return {String} output ID string
*/
*/
function getIDstring(arr, params) {
function getIDstring(arr, params) {
Copy
Copied
Copy
Copied
var str = params.prefix;
var str = params.prefix;
Copy
Copied
Copy
Copied
return
str +
arr.map(i =>
generateIDchars[i]
).join('');
arr.forEach(function(i) {
str +
=
generateIDchars[i]
;
});
return str;
}
}
Saved diffs
Original text
Open file
'use strict'; exports.type = 'full'; exports.active = true; exports.description = 'removes unused IDs and minifies used'; exports.params = { remove: true, minify: true, prefix: '' }; var referencesProps = require('./_collections').referencesProps, regReferencesUrl = /\burl\(("|')?#(.+?)\1\)/, regReferencesHref = /^#(.+?)$/, regReferencesBegin = /^(\w+?)\./, styleOrScript = ['style', 'script'], generateIDchars = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ], maxIDindex = generateIDchars.length - 1; /** * Remove unused and minify used IDs * (only if there are no any <style> or <script>). * * @param {Object} item current iteration item * @param {Object} params plugin params * * @author Kir Belevich */ exports.fn = function(data, params) { var currentID, currentIDstring, IDs = Object.create(null), referencesIDs = Object.create(null), idPrefix = 'id-', // prefix IDs so that values like '__proto__' don't break the work hasStyleOrScript = false; /** * Bananas! * * @param {Array} items input items * @return {Array} output items */ function monkeys(items) { for (var i = 0; i < items.content.length && !hasStyleOrScript; i++) { var item = items.content[i], match; // check if <style> of <script> presents if (item.isElem(styleOrScript)) { hasStyleOrScript = true; continue; } // …and don't remove any ID if yes if (item.isElem()) { item.eachAttr(function(attr) { var key; // save IDs if (attr.name === 'id') { key = idPrefix + attr.value; if (key in IDs) { item.removeAttr('id'); } else { IDs[key] = item; } } // save IDs url() references else if (referencesProps.indexOf(attr.name) > -1) { match = attr.value.match(regReferencesUrl); if (match) { key = idPrefix + match[2]; if (referencesIDs[key]) { referencesIDs[key].push(attr); } else { referencesIDs[key] = [attr]; } } } // save IDs href references else if ( attr.local === 'href' && (match = attr.value.match(regReferencesHref)) || attr.name === 'begin' && (match = attr.value.match(regReferencesBegin)) ) { key = idPrefix + match[1]; if (referencesIDs[key]) { referencesIDs[key].push(attr); } else { referencesIDs[key] = [attr]; } } }); } // go deeper if (item.content) { monkeys(item); } } return items; } data = monkeys(data); if (hasStyleOrScript) { return data; } var idKey; for (var k in referencesIDs) { if (IDs[k]) { idKey = k; k = k.replace(idPrefix, ''); // replace referenced IDs with the minified ones if (params.minify) { currentIDstring = getIDstring(currentID = generateID(currentID), params); IDs[idKey].attr('id').value = currentIDstring; referencesIDs[idKey].forEach(function(attr) { attr.value = attr.value .replace('#' + k, '#' + currentIDstring) .replace(k + '.', currentIDstring + '.'); }); idKey = idPrefix + k; } // don't remove referenced IDs delete IDs[idKey]; } } // remove non-referenced IDs attributes from elements if (params.remove) { for(var ID in IDs) { IDs[ID].removeAttr('id'); } } return data; }; /** * Generate unique minimal ID. * * @param {Array} [currentID] current ID * @return {Array} generated ID array */ function generateID(currentID) { if (!currentID) return [0]; currentID[currentID.length - 1]++; for(var i = currentID.length - 1; i > 0; i--) { if (currentID[i] > maxIDindex) { currentID[i] = 0; if (currentID[i - 1] !== undefined) { currentID[i - 1]++; } } } if (currentID[0] > maxIDindex) { currentID[0] = 0; currentID.unshift(0); } return currentID; } /** * Get string from generated ID array. * * @param {Array} arr input ID array * @return {String} output ID string */ function getIDstring(arr, params) { var str = params.prefix; arr.forEach(function(i) { str += generateIDchars[i]; }); return str; }
Changed text
Open file
'use strict'; exports.type = 'full'; exports.active = true; exports.description = 'removes unused IDs and minifies used'; exports.params = { remove: true, minify: true, prefix: '', preserve: [], force: false }; var referencesProps = new Set(require('./_collections').referencesProps), regReferencesUrl = /\burl\(("|')?#(.+?)\1\)/, regReferencesHref = /^#(.+?)$/, regReferencesBegin = /^(\w+?)\./, styleOrScript = ['style', 'script'], generateIDchars = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ], maxIDindex = generateIDchars.length - 1; /** * Remove unused and minify used IDs * (only if there are no any <style> or <script>). * * @param {Object} item current iteration item * @param {Object} params plugin params * * @author Kir Belevich */ exports.fn = function(data, params) { var currentID, currentIDstring, IDs = new Map(), referencesIDs = new Map(), hasStyleOrScript = false, preserveIDs = new Set(Array.isArray(params.preserve) ? params.preserve : params.preserve ? [params.preserve] : []), idValuePrefix = '#', idValuePostfix = '.'; /** * Bananas! * * @param {Array} items input items * @return {Array} output items */ function monkeys(items) { for (var i = 0; i < items.content.length && !hasStyleOrScript; i++) { var item = items.content[i]; // quit if <style> of <script> presents ('force' param prevents quitting) if (!params.force && item.isElem(styleOrScript)) { hasStyleOrScript = true; continue; } // …and don't remove any ID if yes if (item.isElem()) { item.eachAttr(function(attr) { var key, match; // save IDs if (attr.name === 'id') { key = attr.value; if (IDs.has(key)) { item.removeAttr('id'); // remove repeated id } else { IDs.set(key, item); } return; } // save references if (referencesProps.has(attr.name) && (match = attr.value.match(regReferencesUrl))) { key = match[2]; // url() reference } else if ( attr.local === 'href' && (match = attr.value.match(regReferencesHref)) || attr.name === 'begin' && (match = attr.value.match(regReferencesBegin)) ) { key = match[1]; // href reference } if (key) { var ref = referencesIDs.get(key) || []; ref.push(attr); referencesIDs.set(key, ref); } }); } // go deeper if (item.content) { monkeys(item); } } return items; } data = monkeys(data); if (hasStyleOrScript) { return data; } for (var ref of referencesIDs) { var key = ref[0]; if (IDs.has(key)) { // replace referenced IDs with the minified ones if (params.minify && !preserveIDs.has(key)) { currentIDstring = getIDstring(currentID = generateID(currentID), params); IDs.get(key).attr('id').value = currentIDstring; for (var attr of ref[1]) { attr.value = attr.value.includes(idValuePrefix) ? attr.value.replace(idValuePrefix + key, idValuePrefix + currentIDstring) : attr.value.replace(key + idValuePostfix, currentIDstring + idValuePostfix); } } // don't remove referenced IDs IDs.delete(key); } } // remove non-referenced IDs attributes from elements if (params.remove) { for(var keyElem of IDs) { if (!preserveIDs.has(keyElem[0])) { keyElem[1].removeAttr('id'); } } } return data; }; /** * Generate unique minimal ID. * * @param {Array} [currentID] current ID * @return {Array} generated ID array */ function generateID(currentID) { if (!currentID) return [0]; currentID[currentID.length - 1]++; for(var i = currentID.length - 1; i > 0; i--) { if (currentID[i] > maxIDindex) { currentID[i] = 0; if (currentID[i - 1] !== undefined) { currentID[i - 1]++; } } } if (currentID[0] > maxIDindex) { currentID[0] = 0; currentID.unshift(0); } return currentID; } /** * Get string from generated ID array. * * @param {Array} arr input ID array * @return {String} output ID string */ function getIDstring(arr, params) { var str = params.prefix; return str + arr.map(i => generateIDchars[i]).join(''); }
Find difference