-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #247 from marp-team/root-increasing-specificity
Add PostCSS root increasing specificity plugin
- Loading branch information
Showing
7 changed files
with
81 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** @module */ | ||
import postcss from 'postcss' | ||
|
||
export const pseudoClass = ':marpit-root' | ||
|
||
const matcher = new RegExp(`\\b${pseudoClass}\\b`, 'g') | ||
|
||
/** | ||
* Marpit PostCSS root increasing specificity plugin. | ||
* | ||
* Replace `:marpit-root` pseudo-class selector into `:not(\9)`, to increase | ||
* specificity. | ||
* | ||
* @alias module:postcss/root/increasing_specificity | ||
*/ | ||
const plugin = postcss.plugin( | ||
'marpit-postcss-root-increasing-specificity', | ||
() => (css) => | ||
css.walkRules((rule) => { | ||
rule.selectors = rule.selectors.map((selector) => | ||
selector.replace(matcher, ':not(\\9)') | ||
) | ||
}) | ||
) | ||
|
||
export default plugin |
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
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,21 @@ | ||
import postcss from 'postcss' | ||
import increasingSpecificity, { | ||
pseudoClass, | ||
} from '../../../src/postcss/root/increasing_specificity' | ||
import replace from '../../../src/postcss/root/replace' | ||
|
||
describe('Marpit PostCSS root increasing specificity plugin', () => { | ||
const run = (input) => | ||
postcss([replace({ pseudoClass }), increasingSpecificity]).process(input, { | ||
from: undefined, | ||
}) | ||
|
||
it('replaces specific pseudo-class into ":not(\\9)" to increase specificity', () => { | ||
expect(run(`section${pseudoClass} {}`).css).toBe('section:not(\\9) {}') | ||
|
||
// With replaced :root selector via root replace plugin | ||
expect(run(`:root {}`).css).toBe('section:not(\\9) {}') | ||
expect(run(`section :root {}`).css).toBe('section section:not(\\9) {}') | ||
expect(run(`:root.klass div {}`).css).toBe('section:not(\\9).klass div {}') | ||
}) | ||
}) |