Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #25 Option to retain class names #44

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions js/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
combineSameRulesInput = $('#combine-same-rules'),
fixHTMLIndentationInput = $('#fix-html-indentation'),
includeAncestors = $('#include-ancestors'),
retainClasses = $('#retain-classes'),
idPrefix = $('#id-prefix'),

htmlTextarea = $('#html'),
Expand Down Expand Up @@ -67,6 +68,7 @@
fixHTMLIndentationInput.on('change', persistSettingAndProcessSnapshot);
combineSameRulesInput.on('change', persistSettingAndProcessSnapshot);
includeAncestors.on('change', persistSettingAndProcessSnapshot);
retainClasses.on('change', persistSettingAndProcessSnapshot);

createButton.on('click', makeSnapshot);

Expand Down Expand Up @@ -175,13 +177,39 @@

var styles = lastSnapshot.css,
html = lastSnapshot.html,
prefix = "";
prefix = "",
htmlCleanOptions = {
removeAttrs: ['class'],
allowedAttributes: [
['id'],
['placeholder', ['input', 'textarea']],
['disabled', ['input', 'textarea', 'select', 'option', 'button']],
['value', ['input', 'button']],
['readonly', ['input', 'textarea', 'option']],
['label', ['option']],
['selected', ['option']],
['checked', ['input']]
],
format: true,
replace: [],
replaceStyles: [],
allowComments: true
};

if (includeAncestors.is(':checked')) {
styles = lastSnapshot.ancestorCss.concat(styles);
html = lastSnapshot.leadingAncestorHtml + html + lastSnapshot.trailingAncestorHtml;
}

if (retainClasses.is(':checked')) {
for (var index in htmlCleanOptions.removeAttrs) {
var value = htmlCleanOptions.removeAttrs[index];
if (value === 'class') {
htmlCleanOptions.removeAttrs.splice(index, 1);
}
}
}

loader.addClass('processing');

if (removeDefaultValuesInput.is(':checked')) {
Expand All @@ -199,23 +227,7 @@
}

if (fixHTMLIndentationInput.is(':checked')) {
html = $.htmlClean(html, {
removeAttrs: ['class'],
allowedAttributes: [
['id'],
['placeholder', ['input', 'textarea']],
['disabled', ['input', 'textarea', 'select', 'option', 'button']],
['value', ['input', 'button']],
['readonly', ['input', 'textarea', 'option']],
['label', ['option']],
['selected', ['option']],
['checked', ['input']]
],
format: true,
replace: [],
replaceStyles: [],
allowComments: true
});
html = $.htmlClean(html, htmlCleanOptions);
}

styles = cssStringifier.process(styles);
Expand Down
22 changes: 13 additions & 9 deletions js/tools/CSSStringifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ function CSSStringifier() {
return output.join(', ');
}

function generateStyle(selector, node) {
var output = "";

output += selector + ' {\n';
output += propertiesToString(node);
output += '}/*' + selector + '*/\n\n';

return output;
}

this.process = function (styles) {
var i, l,
style,
Expand All @@ -48,20 +58,14 @@ function CSSStringifier() {
for (i = 0, l = styles.length; i < l; i++) {
style = styles[i];

output += printIDs(style.id) + ' {\n';
output += propertiesToString(style.node);
output += '}/*' + printIDs(style.id) + '*/\n\n';
output += generateStyle(printIDs(style.id), style.node);

if (style.after) {
output += printIDs(style.id, ':after') + ' {\n';
output += propertiesToString(style.after);
output += '}/*' + printIDs(style.id, ':after') + '*/\n\n';
output += generateStyle(printIDs(style.id, ':after'), style.after);
}

if (style.before) {
output += printIDs(style.id, ':before') + ' {\n';
output += propertiesToString(style.before);
output += '}/*' + printIDs(style.id, ':before') + '*/\n\n';
output += generateStyle(printIDs(style.id, ':before'), style.before);
}
}

Expand Down
3 changes: 3 additions & 0 deletions panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ <h4 class="panel-title">
<li><label class="checkbox">
<input type='checkbox' id='include-ancestors'/> Include ancestor elements in the snippet
</label></li>
<li><label class="checkbox">
<input type='checkbox' id='retain-classes'/> Retain class names when you copy this snippet
</label></li>
<li>
<div class="form-group">
Prefix all CSS IDs with <input type='text' class="form-control" id='id-prefix' placeholder="prefix_"/>
Expand Down