-
-
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 #104 from marp-team/fix-printable-selector
Fix over-scoped selectors injected by printable plugin
- Loading branch information
Showing
6 changed files
with
106 additions
and
29 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
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,24 @@ | ||
export function find(from, cond) { | ||
let found | ||
return ( | ||
from.some(rule => { | ||
for (const key of Object.keys(cond)) { | ||
if (rule[key] === undefined) return false | ||
if (typeof cond[key] === 'function') { | ||
if (!cond[key](rule[key])) return false | ||
} else if (rule[key] !== cond[key]) { | ||
return false | ||
} | ||
} | ||
found = rule | ||
return true | ||
}) && found | ||
) | ||
} | ||
|
||
export const findAtRule = (f, cond) => find(f, { type: 'atrule', ...cond }) | ||
export const findComment = (f, cond) => find(f, { type: 'comment', ...cond }) | ||
export const findDecl = (f, cond) => find(f, { type: 'decl', ...cond }) | ||
export const findRule = (f, cond) => find(f, { type: 'rule', ...cond }) | ||
|
||
export default find |
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 |
---|---|---|
@@ -1,36 +1,49 @@ | ||
import dedent from 'dedent' | ||
import postcss from 'postcss' | ||
import printable from '../../src/postcss/printable' | ||
import { findAtRule, findDecl, findRule } from '../_supports/postcss_finder' | ||
import printable, { postprocess } from '../../src/postcss/printable' | ||
|
||
describe('Marpit PostCSS printable plugin', () => { | ||
const run = (input, opts) => | ||
postcss([printable(opts)]).process(input, { from: undefined }) | ||
postcss([printable(opts), postprocess]).process(input, { from: undefined }) | ||
|
||
it('prepends style for printing', () => { | ||
const css = 'section.theme { background: #fff; }' | ||
const css = dedent` | ||
section.theme { background: #fff; } | ||
@media marpit-print { | ||
/* Declarations for internal will remove */ | ||
html { margin: 10px; } | ||
} | ||
` | ||
|
||
const opts = { width: '640px', height: '480px' } | ||
|
||
return run(css, opts).then(result => { | ||
const rules = [] | ||
result.root.walk(node => { | ||
if ( | ||
node.type === 'atrule' || | ||
(node.type === 'rule' && node.selector === 'section.theme') | ||
) | ||
rules.push(node) | ||
return run(css, opts).then(({ root }) => { | ||
const page = findAtRule(root, { name: 'page' }) | ||
const print = findAtRule(root, { name: 'media', params: 'print' }) | ||
const marpitPrint = findAtRule(root, { | ||
name: 'media', | ||
params: 'marpit-print', | ||
}) | ||
|
||
expect(rules).toStrictEqual([ | ||
expect.objectContaining({ type: 'atrule', name: 'page' }), | ||
expect.objectContaining({ type: 'atrule', name: 'media' }), | ||
expect.objectContaining({ type: 'rule', selector: 'section.theme' }), | ||
]) | ||
expect(page).toBeTruthy() | ||
expect(print).toBeTruthy() | ||
expect(marpitPrint).toBeFalsy() | ||
|
||
let sizeDecl | ||
result.root.walkDecls('size', rule => { | ||
sizeDecl = rule | ||
}) | ||
// @page at-rule | ||
const pageSizeDecl = findDecl(page, { prop: 'size' }) | ||
expect(pageSizeDecl.value).toBe('640px 480px') | ||
|
||
// @print at-rule for print | ||
for (const tag of ['html', 'body']) { | ||
const r = findRule(print, { selectors: s => s.includes(tag) }) | ||
expect(findDecl(r, { prop: 'page-break-inside' }).value).toBe('avoid') | ||
expect(findDecl(r, { prop: 'margin' }).value).toBe('0') | ||
} | ||
|
||
expect(sizeDecl.value).toBe('640px 480px') | ||
// Original CSS | ||
expect(findRule(root, { selector: 'section.theme' })).toBeTruthy() | ||
}) | ||
}) | ||
}) |
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