Skip to content

Commit

Permalink
Test custom parser
Browse files Browse the repository at this point in the history
  • Loading branch information
hudochenkov committed May 18, 2021
1 parent 49eec3a commit 54e2e52
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
8 changes: 8 additions & 0 deletions lib/document.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import Root, { RootProps } from './root.js'

export interface DocumentProps extends ContainerProps {
nodes?: Root[]

/**
* Information to generate byte-to-byte equal node string as it was
* in the origin input.
*
* Every parser saves its own properties.
*/
raws?: Record<string, any>
}

type ChildNode = Root
Expand Down
2 changes: 1 addition & 1 deletion lib/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Container from './container.js'

export type ChildNode = AtRule | Rule | Declaration | Comment

export type AnyNode = AtRule | Rule | Declaration | Comment | Root
export type AnyNode = AtRule | Rule | Declaration | Comment | Root | Document

export type ChildProps =
| AtRuleProps
Expand Down
2 changes: 1 addition & 1 deletion lib/root.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Document from './document.js'
import { ProcessOptions } from './postcss.js'
import Result from './result.js'

interface RootRaws {
interface RootRaws extends Record<string, any> {
/**
* The space symbols after the last child to the end of file.
*/
Expand Down
64 changes: 63 additions & 1 deletion test/processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import postcss, {
Node,
Root,
parse,
PluginCreator
PluginCreator,
Document,
Parser,
Stringifier
} from '../lib/postcss.js'
import LazyResult from '../lib/lazy-result.js'
import Processor from '../lib/processor.js'
import Rule from '../lib/rule.js'

afterEach(() => {
jest.resetAllMocks()
Expand Down Expand Up @@ -547,3 +551,61 @@ it('supports plugin creators returning processors', () => {
processor.use(other)
expect(processor.plugins).toEqual([a])
})

it('uses custom syntax for document', async () => {
// @ts-expect-error
let customParser: Parser = () => {
return new Document({
nodes: [
new Root({
raws: {
markupBefore: '<html>\n<head>\n<style id="id1">',
after: '\n\n\n'
},
nodes: [new Rule({ selector: 'a' })]
}),
new Root({
raws: {
markupBefore: '</style>\n<style id="id2">',
after: '\n',
markupAfter: '</style>\n</head>'
},
nodes: [new Rule({ selector: 'b' })]
})
]
})
}

let customStringifier: Stringifier = (doc, builder) => {
if (doc.type === 'document') {
for (let root of doc.nodes) {
if (root.raws.markupBefore) {
builder(root.raws.markupBefore, root)
}

builder(root.toString(), root)

if (root.raws.markupAfter) {
builder(root.raws.markupAfter, root)
}
}
}
}

let processor = new Processor([() => {}])
let result = await processor.process('a{}', {
syntax: {
parse: customParser,
stringify: customStringifier
},
from: undefined
})

expect(result.css).toEqual(
'<html>\n<head>\n<style id="id1">' +
'a {}\n\n\n' +
'</style>\n<style id="id2">' +
'b {}\n' +
'</style>\n</head>'
)
})

0 comments on commit 54e2e52

Please sign in to comment.