Skip to content

Commit d837b86

Browse files
Split long chained selectors
1 parent 37bc6a7 commit d837b86

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

css-html-prettify.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
from tempfile import gettempdir
2727
from time import sleep
2828

29-
from bs4 import BeautifulSoup
29+
try:
30+
from bs4 import BeautifulSoup
31+
except ImportError:
32+
print("BeautifulSoup4 Not Found!, use: sudo apt-get install python3-bs4")
3033

3134
try:
3235
from urllib import request
@@ -171,8 +174,10 @@ def _props_grouper(props, pgs):
171174
log.debug("Grouping all CSS / SCSS Properties.")
172175
if not props:
173176
return props
174-
props = sorted([_ if _.strip().endswith(";")
175-
else _.rstrip() + ";\n" for _ in props])
177+
props = sorted([
178+
_ if _.strip().endswith(";")
179+
and not _.strip().endswith("*/") and not _.strip().endswith("/*")
180+
else _.rstrip() + ";\n" for _ in props])
176181
props_pg = zip(map(lambda prop: _prioritify(prop, pgs), props), props)
177182
props_pg = sorted(props_pg, key=lambda item: item[0][1])
178183
props_by_groups = map(
@@ -227,7 +232,7 @@ def remove_empty_rules(css):
227232
def condense_zero_units(css):
228233
"""Replace `0(px, em, %, etc)` with `0`."""
229234
log.debug("Condensing all zeroes on values.")
230-
return re.sub(r"([\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)", r"\1\2", css)
235+
return re.sub(r"([\s:])(0)(px|em|rem|%|in|cm|mm|pc|pt|ex)", r"\1\2", css)
231236

232237

233238
def condense_semicolons(css):
@@ -266,11 +271,11 @@ def normalize_whitespace(css):
266271
css = re.sub(r"\n{3}", "\n\n\n", css) # if 3 new lines,make them 2
267272
css = re.sub(r"\n{5}", "\n\n\n\n\n", css) # if 5 new lines, make them 4
268273
h_line = "/* {} */".format("-" * 72) # if >6 new lines, horizontal line
269-
css = re.sub(r"\n{6,}", "\n\n\n\n\n{}\n\n\n\n\n".format(h_line), css)
274+
css = re.sub(r"\n{6,}", "\n\n\n{}\n\n\n".format(h_line), css)
270275
css = css.replace(" ;\n", ";\n").replace("{\n", " {\n")
271276
css = re.sub("\s{2,}{\n", " {\n", css)
272277
log.debug("Finished Normalize white spaces on CSS.")
273-
return css.replace("\t", " ").rstrip("\n") + "\n\n"
278+
return css.replace("\t", " ").rstrip() + "\n"
274279

275280

276281
def justify_right(css):
@@ -304,16 +309,39 @@ def justify_right(css):
304309
return right_justified_css if max_indent > 1 else css
305310

306311

312+
def split_long_selectors(css):
313+
"""Split too large CSS Selectors chained with commas if > 80 chars."""
314+
log.debug("Splitting too long chained selectors on CSS / SCSS.")
315+
result = ""
316+
for line in css.splitlines():
317+
cond_1 = len(line) > 80 and "," in line and line.strip().endswith("{")
318+
cond_2 = line.startswith(("*", ".", "#"))
319+
if cond_1 and cond_2:
320+
result += line.replace(", ", ",").replace(",", ",\n").replace(
321+
"{", "{\n")
322+
else:
323+
result += line + "\n"
324+
return result
325+
326+
327+
def simple_replace(css):
328+
"""dumb simple replacements on CSS."""
329+
return css.replace("}\n#", "}\n\n#").replace(
330+
"}\n.", "}\n\n.").replace("}\n*", "}\n\n*")
331+
332+
307333
def css_prettify(css, justify=False):
308334
"""Prettify CSS main function."""
309335
log.info("Prettify CSS / SCSS...")
310336
css = sort_properties(css)
311337
css = condense_zero_units(css)
312338
css = wrap_css_lines(css, 80)
339+
css = split_long_selectors(css)
313340
css = condense_semicolons(css)
314341
css = normalize_whitespace(css)
315342
css = justify_right(css) if justify else css
316343
css = add_encoding(css)
344+
css = simple_replace(css)
317345
log.info("Finished Prettify CSS / SCSS !.")
318346
return css
319347

0 commit comments

Comments
 (0)