Diff
checker
Testo
Testo
Immagini
Documenti
Excel
Cartelle
Legal
Enterprise
Applicazione per desktop
Prezzi
Accedi
Scarica Diffchecker Desktop
Confronta il testo
Trova la differenza tra due file di testo
Strumenti
Cronologia
Editor live
Nascondi spazi bianchi
Comprimi invariate
Senza a capo
Layout
Diviso
Unificato
Livello di dettaglio
Intelligente
Parola
Carattere
Stili testo
Modifica aspetto
Evidenziazione sintassi
Scegli sintassi
Ignora
Trasforma testo
Vai alla prima modifica
Modifica input
Diffchecker Desktop
Il modo più sicuro per usare Diffchecker. Ottieni l'app Diffchecker Desktop: i tuoi diff non lasciano mai il tuo computer!
Ottieni Desktop
Untitled diff
Creato
9 anni fa
Il diff non scade mai
Eliminare
Esporta
Condividere
Spiegare
94 rimozioni
Linee
Totale
Rimosso
Caratteri
Totale
Rimosso
Per continuare a utilizzare questa funzione, aggiorna a
Diff
checker
Pro
Visualizza prezzi
209 linee
Copia tutti
57 aggiunte
Linee
Totale
Aggiunto
Caratteri
Totale
Aggiunto
Per continuare a utilizzare questa funzione, aggiorna a
Diff
checker
Pro
Visualizza prezzi
174 linee
Copia tutti
'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,
Copia
Copiato
Copia
Copiato
prefix: ''
prefix: ''
,
preserve: [],
force: false
};
};
Copia
Copiato
Copia
Copiato
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) {
Copia
Copiato
Copia
Copiato
var currentID,
var currentID,
currentIDstring,
currentIDstring,
Copia
Copiato
Copia
Copiato
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) {
Copia
Copiato
Copia
Copiato
for (var i = 0; i < items.content.length && !hasStyleOrScript; i++) {
for (var i = 0; i < items.content.length && !hasStyleOrScript; i++) {
Copia
Copiato
Copia
Copiato
var item = items.content[i]
;
var item = items.content[i]
,
match
;
Copia
Copiato
Copia
Copiato
//
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;
}
}
Copia
Copiato
Copia
Copiato
// …and don't remove any ID if yes
// …and don't remove any ID if yes
if (item.isElem()) {
if (item.isElem()) {
Copia
Copiato
Copia
Copiato
item.eachAttr(function(attr) {
item.eachAttr(function(attr) {
Copia
Copiato
Copia
Copiato
var key
;
var key
, match;
// save IDs
// save IDs
if (attr.name === 'id') {
if (attr.name === 'id') {
Copia
Copiato
Copia
Copiato
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 {
Copia
Copiato
Copia
Copiato
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];
}
}
}
Copia
Copiato
Copia
Copiato
return;
}
}
Copia
Copiato
Copia
Copiato
// 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))
) {
) {
Copia
Copiato
Copia
Copiato
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);
}
}
});
});
Copia
Copiato
Copia
Copiato
}
}
Copia
Copiato
Copia
Copiato
// go deeper
// go deeper
if (item.content) {
if (item.content) {
monkeys(item);
monkeys(item);
}
}
}
}
Copia
Copiato
Copia
Copiato
return items;
return items;
Copia
Copiato
Copia
Copiato
}
}
data = monkeys(data);
data = monkeys(data);
if (hasStyleOrScript) {
if (hasStyleOrScript) {
return data;
return data;
}
}
Copia
Copiato
Copia
Copiato
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
Copia
Copiato
Copia
Copiato
if (params.minify
) {
if (params.minify
&& !preserveIDs.has(key)
) {
currentIDstring = getIDstring(currentID = generateID(currentID), params);
currentIDstring = getIDstring(currentID = generateID(currentID), params);
Copia
Copiato
Copia
Copiato
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 + '.');
});
Copia
Copiato
Copia
Copiato
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);
}
}
}
Copia
Copiato
Copia
Copiato
// don't remove referenced IDs
// don't remove referenced IDs
Copia
Copiato
Copia
Copiato
delete
IDs[idKey]
;
IDs.
delete
(key)
;
}
}
}
}
Copia
Copiato
Copia
Copiato
// remove non-referenced IDs attributes from elements
// remove non-referenced IDs attributes from elements
if (params.remove) {
if (params.remove) {
Copia
Copiato
Copia
Copiato
for(var
keyElem of
IDs) {
for(var
ID in
IDs) {
if (!preserveIDs.has(keyElem[0])) {
IDs[ID
].removeAttr('id');
keyElem[1
].removeAttr('id');
}
}
}
Copia
Copiato
Copia
Copiato
}
}
Copia
Copiato
Copia
Copiato
return data;
return data;
Copia
Copiato
Copia
Copiato
};
};
/**
/**
* 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) {
Copia
Copiato
Copia
Copiato
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]++;
}
}
}
}
}
}
Copia
Copiato
Copia
Copiato
if (currentID[0] > maxIDindex) {
if (currentID[0] > maxIDindex) {
currentID[0] = 0;
currentID[0] = 0;
currentID.unshift(0);
currentID.unshift(0);
}
}
Copia
Copiato
Copia
Copiato
return currentID;
return currentID;
Copia
Copiato
Copia
Copiato
}
}
/**
/**
* 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) {
Copia
Copiato
Copia
Copiato
var str = params.prefix;
var str = params.prefix;
Copia
Copiato
Copia
Copiato
return
str +
arr.map(i =>
generateIDchars[i]
).join('');
arr.forEach(function(i) {
str +
=
generateIDchars[i]
;
});
return str;
}
}
Diff salvati
Testo originale
Apri 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; }
Testo modificato
Apri 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(''); }
Trovare la differenza