-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new plugin: remove unused IDs (close #76)
- Loading branch information
deepsweet
committed
Dec 11, 2012
1 parent
b3d8240
commit 7797f68
Showing
9 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
'use strict'; | ||
|
||
var referencesProps = require('./_collections').referencesProps, | ||
regReferencesUrl = /^url\(#(.+?)\)$/, | ||
regReferencesHref = /^#(.+?)$/, | ||
styleOrScript = ['style', 'script'], | ||
hasStyleOrScript = false; | ||
|
||
/** | ||
* Remove unused IDs | ||
* (only if there are no any <style> or <script>). | ||
* | ||
* @param {Object} item current iteration item | ||
* @return {Boolean} if false, item will be filtered out | ||
* | ||
* @author Kir Belevich | ||
*/ | ||
exports.removeUnusedIDs = function(data) { | ||
|
||
var IDs = {}, | ||
referencesIDs = []; | ||
|
||
/** | ||
* Bananas! | ||
* | ||
* @param {Array} items input items | ||
* @return {Array} output items | ||
*/ | ||
function monkeys(items) { | ||
|
||
var i = 0, | ||
length = items.content.length; | ||
|
||
while(i < length) { | ||
|
||
var item = items.content[i], | ||
match; | ||
|
||
// check if <style> of <script> presents | ||
if (item.isElem(styleOrScript)) { | ||
hasStyleOrScript = true; | ||
} | ||
|
||
// …and don't remove any ID if yes | ||
if (!hasStyleOrScript) { | ||
|
||
if (item.isElem()) { | ||
|
||
item.eachAttr(function(attr) { | ||
// save IDs | ||
if (attr.name === 'id') { | ||
IDs[item.attr('id').value] = item; | ||
} | ||
|
||
// save IDs url() references | ||
else if (referencesProps.indexOf(attr.name) > -1) { | ||
match = attr.value.match(regReferencesUrl); | ||
|
||
if (match && referencesIDs.indexOf(match[1]) === -1) { | ||
referencesIDs.push(match[1]); | ||
} | ||
} | ||
|
||
// save IDs href references | ||
else if (attr.name === 'xlink:href') { | ||
match = attr.value.match(regReferencesHref); | ||
|
||
if (match && referencesIDs.indexOf(match[1]) === -1) { | ||
referencesIDs.push(match[1]); | ||
} | ||
} | ||
}); | ||
|
||
} | ||
|
||
// go deeper | ||
if (item.content) { | ||
monkeys(item); | ||
} | ||
|
||
} | ||
|
||
i++; | ||
|
||
} | ||
|
||
return items; | ||
|
||
} | ||
|
||
data = monkeys(data); | ||
|
||
if (!hasStyleOrScript) { | ||
|
||
// don't remove referenced IDs | ||
if (referencesIDs.length) { | ||
referencesIDs.forEach(function(referencesID) { | ||
delete IDs[referencesID]; | ||
}); | ||
} | ||
|
||
// remove not referenced IDs from elements | ||
if (Object.keys(IDs).length) { | ||
for(var ID in IDs) { | ||
IDs[ID].removeAttr('id'); | ||
} | ||
} | ||
|
||
} | ||
|
||
return data; | ||
|
||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.