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 #1582 from hudochenkov/add-document-node-1498
Add Document node and related API (#1498)
- Loading branch information
Showing
24 changed files
with
953 additions
and
59 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
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,54 @@ | ||
import Container, { ContainerProps } from './container.js' | ||
import { ProcessOptions } from './postcss.js' | ||
import Result from './result.js' | ||
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 | ||
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. | ||
* | ||
* ```js | ||
* const document = htmlParser('<html><style>a{color:black}</style><style>b{z-index:2}</style>') | ||
* document.type //=> 'document' | ||
* document.nodes.length //=> 2 | ||
* ``` | ||
*/ | ||
export default class Document extends Container<Root> { | ||
type: 'document' | ||
parent: undefined | ||
|
||
constructor(defaults?: DocumentProps) | ||
|
||
/** | ||
* Returns a `Result` instance representing the document’s CSS roots. | ||
* | ||
* ```js | ||
* const root1 = postcss.parse(css1, { from: 'a.css' }) | ||
* const root2 = postcss.parse(css2, { from: 'b.css' }) | ||
* const document = postcss.document() | ||
* document.append(root1) | ||
* document.append(root2) | ||
* const result = document.toResult({ to: 'all.css', map: true }) | ||
* ``` | ||
* | ||
* @param opts Options. | ||
* @return Result with current document’s CSS. | ||
*/ | ||
toResult(options?: ProcessOptions): Result | ||
} |
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,33 @@ | ||
'use strict' | ||
|
||
let Container = require('./container') | ||
|
||
let LazyResult, Processor | ||
|
||
class Document extends Container { | ||
constructor(defaults) { | ||
// type needs to be passed to super, otherwise child roots won't be normalized correctly | ||
super({ type: 'document', ...defaults }) | ||
|
||
if (!this.nodes) { | ||
this.nodes = [] | ||
} | ||
} | ||
|
||
toResult(opts = {}) { | ||
let lazy = new LazyResult(new Processor(), this, opts) | ||
|
||
return lazy.stringify() | ||
} | ||
} | ||
|
||
Document.registerLazyResult = dependant => { | ||
LazyResult = dependant | ||
} | ||
|
||
Document.registerProcessor = dependant => { | ||
Processor = dependant | ||
} | ||
|
||
module.exports = Document | ||
Document.default = Document |
Oops, something went wrong.