red-perfume / red-perfume-css

Library for atomizing strings of CSS
https://red-perfume.github.io
MIT License
3 stars 0 forks source link

Handle Nested Selectors (CSS) #2

Open TheJaredWilcurt opened 3 years ago

TheJaredWilcurt commented 3 years ago

After much consideration, and a few design revisions this is the solution for nesting I've come up with that seems to have the fewest "gotchas" (AKA: allows for supporting most any ). Previous attempts had smaller outputs, but caused many edge cases. I settled on retaining the original class names in the Markup, and embedding them in the atomized class to reduce styling outcomes that differ from source files.

/* input */
.cow .moo {
    margin: 4px;
}
.dog .woof {
    margin: 4px;
}
.vehicle .car .horn {
    margin: 4px;
}
/* verbose output */
.cow .rp____---CLASS_cow______-margin__--COLON4px { margin: 4px }
.dog .rp____---CLASS_dog______-margin__--COLON4px { margin: 4px }
.vehicle .car .rp____---CLASS_vehicle______-__---CLASS_car______-margin__--COLON4px { margin: 4px; }

Note: The class name encoding breakdown is:

So the un-encoded atomic class name .vehicle .car margin:4px gets encoded to rp____---CLASS_vehicle______-__---CLASS_car______-margin__--COLON4px.

/* uglified output */
.cow .rp__0 { margin: 4px }
.dog .rp__1 { margin: 4px }
.vehicle .car .rp__2 { margin: 4px; }
<div class="cow">
  <div class="moo"></div>
</div>
<div class="dog">
  <div class="woof"></div>
</div>
<div class="vehicle">
  <div class="car">
    <div class="horn"></div>
  </div>
</div>
<div class="cow">
  <div class="rp____--DOT_cow______-margin__--COLON4px"></div>
</div>
<div class="dog">
  <div class="rp____--DOT_dog______-margin__--COLON4px"></div>
</div>
<div class="vehicle">
  <div class="car">
    <div class="rp____--DOT_vehicle______-__--DOT_car______-margin__--COLON4px"></div>
  </div>
</div>
<div class="cow">
  <div class="rp__0"></div>
</div>
<div class="dog">
  <div class="rp__1"></div>
</div>
<div class="vehicle">
  <div class="car">
    <div class="rp__2"></div>
  </div>
</div>

classMap

{
  '.cow': ['.cow'],
  '.moo': ['.rp____---CLASS_cow______-margin__--COLON4px'],
  '.dog': ['.dog'],
  '.woof': ['.rp____---CLASS_dog______-margin__--COLON4px'],
  '.vehicle': ['.vehicle'],
  '.car': ['.car'],
  '.horn': ['.rp____---CLASS_vehicle______-__---CLASS_car______-margin__--COLON4px']
}
{
  '.cow': ['.cow'],
  '.moo': ['.rp__0'],
  '.dog': ['.dog'],
  '.woof': ['.rp__1'],
  '.vehicle': ['.vehicle'],
  '.car': ['.car'],
  '.horn': ['.rp__2']
}