Diff
checker
Texte
Texte
Images
Documents
Excel
Dossiers
Legal
Enterprise
Application de bureau
Prix
Se connecter
Télécharger Diffchecker Desktop
Comparer le texte
Trouver la différence entre deux fichiers texte
Outils
Historique
Éditeur live
Masquer les espaces
Cacher identiques
Sans retour à la ligne
Vue
Divisé
Unifié
Niveau de précision
Intelligent
Mot
Caractère
Styles de texte
Modifier l’apparence
Coloration syntaxique
Choisir la syntaxe
Ignorer
Transformer le texte
Aller au premier écart
Modifier l'entrée
Diffchecker Desktop
La façon la plus sécurisée d'utiliser Diffchecker. Obtenez l'application Diffchecker Desktop : vos diffs ne quittent jamais votre ordinateur !
Obtenir Desktop
Untitled diff
Créé
il y a 9 ans
Le diff n'expire jamais
Effacer
Exporter
Partager
Expliquer
94 suppressions
Lignes
Total
Supprimé
Caractères
Total
Supprimé
Pour continuer à utiliser cette fonctionnalité, passez à
Diff
checker
Pro
Voir les prix
209 lignes
Copier tout
57 ajouts
Lignes
Total
Ajouté
Caractères
Total
Ajouté
Pour continuer à utiliser cette fonctionnalité, passez à
Diff
checker
Pro
Voir les prix
174 lignes
Copier tout
'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,
Copier
Copié
Copier
Copié
prefix: ''
prefix: ''
,
preserve: [],
force: false
};
};
Copier
Copié
Copier
Copié
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) {
Copier
Copié
Copier
Copié
var currentID,
var currentID,
currentIDstring,
currentIDstring,
Copier
Copié
Copier
Copié
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) {
Copier
Copié
Copier
Copié
for (var i = 0; i < items.content.length && !hasStyleOrScript; i++) {
for (var i = 0; i < items.content.length && !hasStyleOrScript; i++) {
Copier
Copié
Copier
Copié
var item = items.content[i]
;
var item = items.content[i]
,
match
;
Copier
Copié
Copier
Copié
//
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;
}
}
Copier
Copié
Copier
Copié
// …and don't remove any ID if yes
// …and don't remove any ID if yes
if (item.isElem()) {
if (item.isElem()) {
Copier
Copié
Copier
Copié
item.eachAttr(function(attr) {
item.eachAttr(function(attr) {
Copier
Copié
Copier
Copié
var key
;
var key
, match;
// save IDs
// save IDs
if (attr.name === 'id') {
if (attr.name === 'id') {
Copier
Copié
Copier
Copié
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 {
Copier
Copié
Copier
Copié
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];
}
}
}
Copier
Copié
Copier
Copié
return;
}
}
Copier
Copié
Copier
Copié
// 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))
) {
) {
Copier
Copié
Copier
Copié
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);
}
}
});
});
Copier
Copié
Copier
Copié
}
}
Copier
Copié
Copier
Copié
// go deeper
// go deeper
if (item.content) {
if (item.content) {
monkeys(item);
monkeys(item);
}
}
}
}
Copier
Copié
Copier
Copié
return items;
return items;
Copier
Copié
Copier
Copié
}
}
data = monkeys(data);
data = monkeys(data);
if (hasStyleOrScript) {
if (hasStyleOrScript) {
return data;
return data;
}
}
Copier
Copié
Copier
Copié
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
Copier
Copié
Copier
Copié
if (params.minify
) {
if (params.minify
&& !preserveIDs.has(key)
) {
currentIDstring = getIDstring(currentID = generateID(currentID), params);
currentIDstring = getIDstring(currentID = generateID(currentID), params);
Copier
Copié
Copier
Copié
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 + '.');
});
Copier
Copié
Copier
Copié
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);
}
}
}
Copier
Copié
Copier
Copié
// don't remove referenced IDs
// don't remove referenced IDs
Copier
Copié
Copier
Copié
delete
IDs[idKey]
;
IDs.
delete
(key)
;
}
}
}
}
Copier
Copié
Copier
Copié
// remove non-referenced IDs attributes from elements
// remove non-referenced IDs attributes from elements
if (params.remove) {
if (params.remove) {
Copier
Copié
Copier
Copié
for(var
keyElem of
IDs) {
for(var
ID in
IDs) {
if (!preserveIDs.has(keyElem[0])) {
IDs[ID
].removeAttr('id');
keyElem[1
].removeAttr('id');
}
}
}
Copier
Copié
Copier
Copié
}
}
Copier
Copié
Copier
Copié
return data;
return data;
Copier
Copié
Copier
Copié
};
};
/**
/**
* 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) {
Copier
Copié
Copier
Copié
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]++;
}
}
}
}
}
}
Copier
Copié
Copier
Copié
if (currentID[0] > maxIDindex) {
if (currentID[0] > maxIDindex) {
currentID[0] = 0;
currentID[0] = 0;
currentID.unshift(0);
currentID.unshift(0);
}
}
Copier
Copié
Copier
Copié
return currentID;
return currentID;
Copier
Copié
Copier
Copié
}
}
/**
/**
* 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) {
Copier
Copié
Copier
Copié
var str = params.prefix;
var str = params.prefix;
Copier
Copié
Copier
Copié
return
str +
arr.map(i =>
generateIDchars[i]
).join('');
arr.forEach(function(i) {
str +
=
generateIDchars[i]
;
});
return str;
}
}
Différences enregistrées
Texte d'origine
Ouvrir un fichier
'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; }
Texte modifié
Ouvrir un fichier
'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(''); }
Trouver la différence