Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Input#document #1996

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/input.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ declare class Input_ {
*/
css: string

/**
* Input source with support for non-CSS documents.
*
* ```js
* const input = postcss.parse('a{}', { from: file, document: '<style>a {}</style>' }).input
* input.document //=> "<style>a {}</style>"
* input.css //=> "a{}"
* ```
*/
document: string

/**
* The absolute path to the CSS source file defined
* with the `from` option.
Expand Down
3 changes: 3 additions & 0 deletions lib/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class Input {
this.hasBOM = false
}

this.document = this.css
if (opts.document) this.document = opts.document.toString()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose to use opts.document and add document to ProcessOptions.

Alternatively it could also be less exposed and only exists as a field on Input.

let root = parse('a {} b {}')
root.source.input.document = '<style>a {} b {}</style>';

We would still keep this.document = this.css to make sure that there is always a value.


if (opts.from) {
if (
!pathAvailable ||
Expand Down
16 changes: 8 additions & 8 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ class Node {
if (opts.index) {
pos = this.positionInside(opts.index)
} else if (opts.word) {
let stringRepresentation = this.source.input.css.slice(
sourceOffset(this.source.input.css, this.source.start),
sourceOffset(this.source.input.css, this.source.end)
let stringRepresentation = this.source.input.document.slice(
sourceOffset(this.source.input.document, this.source.start),
sourceOffset(this.source.input.document, this.source.end)
)
let index = stringRepresentation.indexOf(opts.word)
if (index !== -1) pos = this.positionInside(index)
Expand All @@ -222,11 +222,11 @@ class Node {
positionInside(index) {
let column = this.source.start.column
let line = this.source.start.line
let offset = sourceOffset(this.source.input.css, this.source.start)
let offset = sourceOffset(this.source.input.document, this.source.start)
let end = offset + index

for (let i = offset; i < end; i++) {
if (this.source.input.css[i] === '\n') {
if (this.source.input.document[i] === '\n') {
column = 1
line += 1
} else {
Expand Down Expand Up @@ -259,9 +259,9 @@ class Node {
}

if (opts.word) {
let stringRepresentation = this.source.input.css.slice(
sourceOffset(this.source.input.css, this.source.start),
sourceOffset(this.source.input.css, this.source.end)
let stringRepresentation = this.source.input.document.slice(
sourceOffset(this.source.input.document, this.source.start),
sourceOffset(this.source.input.document, this.source.end)
)
let index = stringRepresentation.indexOf(opts.word)
if (index !== -1) {
Expand Down
7 changes: 6 additions & 1 deletion lib/postcss.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ declare namespace postcss {
export interface Parser<RootNode = Document | Root> {
(
css: { toString(): string } | string,
opts?: Pick<ProcessOptions, 'from' | 'map'>
opts?: Pick<ProcessOptions, 'document' | 'from' | 'map'>
): RootNode
}

Expand Down Expand Up @@ -315,6 +315,11 @@ declare namespace postcss {
}

export interface ProcessOptions<RootNode = Document | Root> {
/**
* Input file if it is not simple CSS file, but HTML with <style> or JS with CSS-in-JS blocks.
*/
document?: string

/**
* The path of the CSS source file. You should always set `from`,
* because it is used in source map generation and syntax error messages.
Expand Down
86 changes: 86 additions & 0 deletions test/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,32 @@ test('positionInside() returns position after AST mutations', () => {
equal(two.positionInside(1), { column: 3, line: 3 })
})

test('positionInside() supports multi-root documents', () => {
let root = parse('a {} b {}', { document: '<style>a {} b {}</style>' })
is(root.source?.input.css, 'a {} b {}')
is(root.source?.input.document, '<style>a {} b {}</style>')

let a = root.first as Rule

// Offset the source location of `a` to mimic syntaxes like `postcss-html`
a.source = {
end: {
column: 12,
line: 1,
offset: 12
},
input: a.source!.input,
start: {
column: 8,
line: 1,
offset: 7
}
}

equal(a.positionInside(0), { column: 8, line: 1 })
equal(a.positionInside(1), { column: 9, line: 1 })
})

test('positionBy() returns position for word', () => {
let css = parse('a { one: X }')
let a = css.first as Rule
Expand Down Expand Up @@ -473,6 +499,33 @@ test('positionBy() returns position for index after AST mutations', () => {
equal(two.positionBy({ index: 1 }), { column: 3, line: 3 })
})

test('positionBy() supports multi-root documents', () => {
let root = parse('a {} b {}', { document: '<style>a {} b {}</style>' })
is(root.source?.input.css, 'a {} b {}')
is(root.source?.input.document, '<style>a {} b {}</style>')

let a = root.first as Rule

// Offset the source location of `a` to mimic syntaxes like `postcss-html`
a.source = {
end: {
column: 12,
line: 1,
offset: 12
},
input: a.source!.input,
start: {
column: 8,
line: 1,
offset: 7
}
}

equal(a.positionBy({ index: 0 }), { column: 8, line: 1, offset: 7 }) // `offset` is present because the `0` index returns `source.start`
equal(a.positionBy({ index: 1 }), { column: 9, line: 1 })
equal(a.positionBy({ word: 'a' }), { column: 8, line: 1 })
})

test('rangeBy() returns range for word', () => {
let css = parse('a { one: X }')
let a = css.first as Rule
Expand Down Expand Up @@ -615,4 +668,37 @@ test('rangeBy() returns range for index and endIndex after AST mutations', () =>
})
})

test('rangeBy() supports multi-root documents', () => {
let root = parse('a {} b {}', { document: '<style>a {} b {}</style>' })
is(root.source?.input.css, 'a {} b {}')
is(root.source?.input.document, '<style>a {} b {}</style>')

let a = root.first as Rule

// Offset the source location of `a` to mimic syntaxes like `postcss-html`
a.source = {
end: {
column: 12,
line: 1,
offset: 12
},
input: a.source!.input,
start: {
column: 8,
line: 1,
offset: 7
}
}

equal(a.rangeBy({ endIndex: 1, index: 0 }), {
end: { column: 9, line: 1 },
start: { column: 8, line: 1 }
})

equal(a.rangeBy({ word: 'a' }), {
end: { column: 9, line: 1 },
start: { column: 8, line: 1 }
})
})

test.run()
Loading