Skip to content

Commit

Permalink
Use codeBefore instead of markupBefore
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed May 19, 2021
1 parent e09414a commit 7b3c872
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
7 changes: 5 additions & 2 deletions lib/document.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ type ChildProps = RootProps
/**
* Represents a file and contains all its parsed nodes.
*
* **Experimental:** some aspects of this node could change within minor or patch version releases.
* **Experimental:** some aspects of this node could change within minor
* or patch version releases.
*
* ```js
* const document = htmlParser('<html><style>a{color:black}</style><style>b{z-index:2}</style>')
* const document = htmlParser(
* '<html><style>a{color:black}</style><style>b{z-index:2}</style>'
* )
* document.type //=> 'document'
* document.nodes.length //=> 2
* ```
Expand Down
21 changes: 20 additions & 1 deletion lib/root.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,33 @@ interface RootRaws extends Record<string, any> {
*/
after?: string

/**
* Non-CSS code before `Root`, when `Root` is inside `Document`.
*
* **Experimental:** some aspects of this node could change within minor
* or patch version releases.
*/
codeBefore?: string

/**
* Non-CSS code after `Root`, when `Root` is inside `Document`.
*
* **Experimental:** some aspects of this node could change within minor
* or patch version releases.
*/
codeAfter?: string

/**
* Is the last child has an (optional) semicolon.
*/
semicolon?: boolean
}

export interface RootProps extends ContainerProps {
/** Information used to generate byte-to-byte equal node string as it was in the origin input. */
/**
* Information used to generate byte-to-byte equal node string
* as it was in the origin input.
* */
raws?: RootRaws
}

Expand Down
14 changes: 7 additions & 7 deletions test/processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,16 +558,16 @@ it('uses custom syntax for document', async () => {
nodes: [
new Root({
raws: {
markupBefore: '<html>\n<head>\n<style id="id1">',
codeBefore: '<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">',
codeBefore: '</style>\n<style id="id2">',
after: '\n',
markupAfter: '</style>\n</head>'
codeAfter: '</style>\n</head>'
},
nodes: [new Rule({ selector: 'b' })]
})
Expand All @@ -578,14 +578,14 @@ it('uses custom syntax for document', async () => {
let customStringifier: Stringifier = (doc, builder) => {
if (doc.type === 'document') {
for (let root of doc.nodes) {
if (root.raws.markupBefore) {
builder(root.raws.markupBefore, root)
if (root.raws.codeBefore) {
builder(root.raws.codeBefore, root)
}

builder(root.toString(), root)

if (root.raws.markupAfter) {
builder(root.raws.markupAfter, root)
if (root.raws.codeAfter) {
builder(root.raws.codeAfter, root)
}
}
}
Expand Down

0 comments on commit 7b3c872

Please sign in to comment.