tailwindcss/tests/match-components.test.js
Robin Malfait b661614265
Enable optimize universal defaults by default (#5635)
* enabled `optimizeUniversalDefaults` by default

This PR is done in a way so that the default is set to `true`, but you
can still disable it if it causes issues. In this case we do appreciate
an issue in that case 😅.

* update tests to use optimized universal selector

* update integration tests

* add dedicated tests for the optimized universal selector

* improve minimumImpactSelector algorithm

I think I cracked the algorithm, but I will probably need another pair
of eyes on the subject.

The current implementation works like this:

Prerequisites:

- The selector should already have been parsed using the selectorParser
  from 'postcss-selector-parser'.

Algorithm:

1. Remove all of the pseudo classes from the list of nodes.
  1.1. We do want to keep pseudo elements (E.g.: `::before`, `::first-line`, ...)
  1.2. We do want to keep pseudo classes that contain nodes (E.g.:
    `:not(...)`)
2. Reverse the list of nodes.
  This will make it easier to search from the end to the start. For
  example `.group:hover .group-hover` should result in `.group-hover`
  not `.group`.
  2.1. Find the index of the best match (class, id, attribute), and
    convert the node if required. (E.g.: `span#app` -> `#app` => `[id="app"]`)
  2.2. Remove the rest of the selector that is not important anymore
  2.3. Re-join the left-over nodes together

* update tests using new algorithm

* also look for `tag` types

* take `tag` into account

* simplify logic

* add test to prove `rest.reverse()` in first case is required

In case we don't find a match (idx === -1), we use `rest.reverse()`.
However, it looks like you can just use `nodes` instead.
This is not entirely true, because the `rest` variable will contain only
the nodes that are not pseudo elements.

`*:hover` would result in `*:hover` instead of just `*`

* replace all nodes after > with a single universal selector
2021-10-06 17:45:26 +02:00

94 lines
2.2 KiB
JavaScript

import { run, html, css } from './util/run'
it('should be possible to matchComponents', () => {
let config = {
content: [
{
raw: html`<div class="card-[#0088cc] hover:card-[#f0f]">
<div class="card-header font-bold"></div>
<div class="shadow"></div>
<div class="card-footer text-center"></div>
</div>`,
},
],
corePlugins: {
preflight: false,
},
plugins: [
function ({ matchComponents }) {
matchComponents({
card: (value) => {
return [
{ color: value },
{
'.card-header': {
borderTopWidth: 3,
borderTopColor: value,
},
},
{
'.card-footer': {
borderBottomWidth: 3,
borderBottomColor: value,
},
},
]
},
})
},
],
}
return run('@tailwind base; @tailwind components; @tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.shadow {
--tw-ring-offset-shadow: 0 0 #0000;
--tw-ring-shadow: 0 0 #0000;
--tw-shadow: 0 0 #0000;
}
.card-\\[\\#0088cc\\] {
color: #0088cc;
}
.card-\\[\\#0088cc\\] .card-header {
border-top-width: 3px;
border-top-color: #0088cc;
}
.card-\\[\\#0088cc\\] .card-footer {
border-bottom-width: 3px;
border-bottom-color: #0088cc;
}
.text-center {
text-align: center;
}
.font-bold {
font-weight: 700;
}
.shadow {
--tw-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px 0 rgb(0 0 0 / 0.06);
box-shadow: var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000),
var(--tw-shadow);
}
.hover\\:card-\\[\\#f0f\\]:hover {
color: #f0f;
}
.hover\\:card-\\[\\#f0f\\]:hover .hover\\:card-header:hover {
border-top-width: 3px;
border-top-color: #f0f;
}
.hover\\:card-\\[\\#f0f\\]:hover .hover\\:card-footer:hover {
border-bottom-width: 3px;
border-bottom-color: #f0f;
}
`)
})
})