-
-
Notifications
You must be signed in to change notification settings - Fork 986
Fix no-invalid-position-declaration false positives for embedded styles
#8701
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
Merged
jeddy3
merged 11 commits into
stylelint:main
from
sw1tch3roo:fix/no-invalid-position-declaration-in-style-attribute
Aug 7, 2025
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d04247d
fix `no-invalid-position-declaration` false positives for embedded st…
sw1tch3roo 556584f
Add accept test case for `<style>--custom-property: blue;</style>`
sw1tch3roo 1da579e
changes after review
sw1tch3roo a623517
Merge branch 'main' into fix/no-invalid-position-declaration-in-style…
sw1tch3roo 4a9e6dc
changes after review
sw1tch3roo e947322
Restore original `isInDocument` to handle CSS-in-JS ASTs correctly
sw1tch3roo 80a203a
add test for `isInDocument` util
sw1tch3roo 5379f6a
Merge branch 'main' into fix/no-invalid-position-declaration-in-style…
sw1tch3roo 48c4bd9
Merge branch 'main' into fix/no-invalid-position-declaration-in-style…
sw1tch3roo ce24f1c
add a comment that describes the code relies on the unofficial docume…
sw1tch3roo fc67a96
add `instanceof Node` to document node
sw1tch3roo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| "stylelint": patch | ||
| --- | ||
|
|
||
| Fixed: `no-invalid-position-declaration` false positives for embedded styles |
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,234 @@ | ||
| import postcss from 'postcss'; | ||
| import postcssHtml from 'postcss-html'; | ||
|
|
||
| import isInDocument from '../isInDocument.mjs'; | ||
| import naiveCssInJs from '../../__tests__/fixtures/postcss-naive-css-in-js.cjs'; | ||
|
|
||
| describe('isInDocument', () => { | ||
| it('returns false for nodes not in a document', () => { | ||
| const root = postcss.parse('a { color: red; }'); | ||
| const { | ||
| first: { first: decl }, | ||
| } = root; | ||
|
|
||
| expect(isInDocument(decl)).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false for root nodes', () => { | ||
| const root = postcss.parse('a { color: red; }'); | ||
|
|
||
| expect(isInDocument(root)).toBe(false); | ||
| }); | ||
|
|
||
| it('returns true for nodes directly in a document', () => { | ||
| const document = postcss.document(); | ||
| const root = postcss.parse('a { color: red; }'); | ||
|
|
||
| document.append(root); | ||
|
|
||
| expect(isInDocument(root)).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true for nested nodes in a document', () => { | ||
| const document = postcss.document(); | ||
| const root = postcss.parse('a { color: red; --custom: blue; }'); | ||
|
|
||
| document.append(root); | ||
|
|
||
| const decls = []; | ||
|
|
||
| root.walkDecls((decl) => decls.push(decl)); | ||
|
|
||
| expect(decls).toHaveLength(2); | ||
| expect(isInDocument(decls[0])).toBe(true); // color: red | ||
| expect(isInDocument(decls[1])).toBe(true); // --custom: blue | ||
| }); | ||
|
|
||
| it('returns true for deeply nested nodes in a document', () => { | ||
| const document = postcss.document(); | ||
| const root = postcss.parse(` | ||
| @media (min-width: 600px) { | ||
| a { | ||
| color: red; | ||
| @supports (display: grid) { | ||
| display: grid; | ||
| } | ||
| } | ||
| } | ||
| `); | ||
|
|
||
| document.append(root); | ||
|
|
||
| const decls = []; | ||
|
|
||
| root.walkDecls((decl) => decls.push(decl)); | ||
|
|
||
| expect(decls).toHaveLength(2); | ||
| expect(isInDocument(decls[0])).toBe(true); // color: red | ||
| expect(isInDocument(decls[1])).toBe(true); // display: grid | ||
| }); | ||
|
|
||
| it('returns true for CSS-in-JS with naiveCssInJs parser', () => { | ||
| const document = naiveCssInJs.parse('css` color: red; `;'); | ||
| const decls = []; | ||
|
|
||
| document.walkDecls((decl) => decls.push(decl)); | ||
|
|
||
| expect(decls).toHaveLength(1); | ||
| expect(isInDocument(decls[0])).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true for multiple CSS-in-JS blocks', () => { | ||
| const document = naiveCssInJs.parse(` | ||
| css\` color: red; \`; | ||
| css\` font-size: 14px; --custom: blue; \`; | ||
| `); | ||
| const decls = []; | ||
|
|
||
| document.walkDecls((decl) => decls.push(decl)); | ||
|
|
||
| expect(decls).toHaveLength(3); | ||
| expect(isInDocument(decls[0])).toBe(true); // color: red | ||
| expect(isInDocument(decls[1])).toBe(true); // font-size: 14px | ||
| expect(isInDocument(decls[2])).toBe(true); // --custom: blue | ||
| }); | ||
|
|
||
| it('returns false for nodes without parent', () => { | ||
| const decl = postcss.decl({ prop: 'color', value: 'red' }); | ||
|
|
||
| expect(isInDocument(decl)).toBe(false); | ||
| }); | ||
|
|
||
| it('returns true for nodes with document property', () => { | ||
| const document = postcss.document(); | ||
| const mockNode = postcss.decl({ prop: 'color', value: 'blue' }); | ||
|
|
||
| // Mock a node that has a document property | ||
| mockNode.document = document; | ||
|
|
||
| expect(isInDocument(mockNode)).toBe(true); | ||
| }); | ||
|
|
||
| it('returns false for nodes with non-document document property', () => { | ||
| const mockNode = postcss.decl({ prop: 'color', value: 'blue' }); | ||
|
|
||
| // Mock a node that has a document property but it's not a Document | ||
| mockNode.document = 'not-a-document'; | ||
|
|
||
| expect(isInDocument(mockNode)).toBe(false); | ||
| }); | ||
|
|
||
| it('returns true for document nodes themselves', () => { | ||
| const document = postcss.document(); | ||
|
|
||
| expect(isInDocument(document)).toBe(true); | ||
| }); | ||
|
|
||
| it('handles complex nested document structures', () => { | ||
| const outerDocument = postcss.document(); | ||
| const innerDocument = postcss.document(); | ||
| const root = postcss.parse('a { color: red; }'); | ||
|
|
||
| innerDocument.append(root); | ||
| outerDocument.append(innerDocument); | ||
|
|
||
| const decl = root.first.first; | ||
|
|
||
| expect(isInDocument(decl)).toBe(true); | ||
| }); | ||
|
|
||
| describe('with postcss-html parser', () => { | ||
| it('returns true for declarations in HTML style tags', () => { | ||
| const document = postcssHtml.parse('<style>a { color: red; --custom: blue; }</style>'); | ||
| const decls = []; | ||
|
|
||
| document.walkDecls((decl) => decls.push(decl)); | ||
|
|
||
| expect(decls).toHaveLength(2); | ||
| expect(isInDocument(decls[0])).toBe(true); // color: red | ||
| expect(isInDocument(decls[1])).toBe(true); // --custom: blue | ||
| }); | ||
|
|
||
| it('returns true for declarations in HTML style attributes', () => { | ||
| const document = postcssHtml.parse('<div style="color: red; font-size: 14px;">content</div>'); | ||
| const decls = []; | ||
|
|
||
| document.walkDecls((decl) => decls.push(decl)); | ||
|
|
||
| expect(decls).toHaveLength(2); | ||
| expect(isInDocument(decls[0])).toBe(true); // color: red | ||
| expect(isInDocument(decls[1])).toBe(true); // font-size: 14px | ||
| }); | ||
|
|
||
| it('returns true for declarations in multiple HTML style sources', () => { | ||
| const document = postcssHtml.parse(` | ||
| <style>body { margin: 0; }</style> | ||
| <div style="color: red;">content</div> | ||
| <style>.class { padding: 10px; }</style> | ||
| `); | ||
| const decls = []; | ||
|
|
||
| document.walkDecls((decl) => decls.push(decl)); | ||
|
|
||
| expect(decls).toHaveLength(3); | ||
| expect(isInDocument(decls[0])).toBe(true); // margin: 0 | ||
| expect(isInDocument(decls[1])).toBe(true); // color: red | ||
| expect(isInDocument(decls[2])).toBe(true); // padding: 10px | ||
| }); | ||
|
|
||
| it('returns true for deeply nested declarations in HTML', () => { | ||
| const document = postcssHtml.parse(` | ||
| <style> | ||
| @media (min-width: 600px) { | ||
| .container { | ||
| display: grid; | ||
| @supports (display: grid) { | ||
| grid-template-columns: 1fr 1fr; | ||
| } | ||
| } | ||
| } | ||
| </style> | ||
| `); | ||
| const decls = []; | ||
|
|
||
| document.walkDecls((decl) => decls.push(decl)); | ||
|
|
||
| expect(decls).toHaveLength(2); | ||
| expect(isInDocument(decls[0])).toBe(true); // display: grid | ||
| expect(isInDocument(decls[1])).toBe(true); // grid-template-columns | ||
| }); | ||
|
|
||
| it('returns true for custom properties in HTML style attributes', () => { | ||
| const document = postcssHtml.parse( | ||
| '<div style="--theme-color: #ff0000; color: var(--theme-color);">content</div>', | ||
| ); | ||
| const decls = []; | ||
|
|
||
| document.walkDecls((decl) => decls.push(decl)); | ||
|
|
||
| expect(decls).toHaveLength(2); | ||
| expect(isInDocument(decls[0])).toBe(true); // --theme-color | ||
| expect(isInDocument(decls[1])).toBe(true); // color | ||
| }); | ||
|
|
||
| it('handles edge case where root.parent might be undefined', () => { | ||
| // Test cases where HTML parsers may not properly set parent relationships | ||
| // Some parsers create Document nodes but don't always link Root.parent to Document | ||
| const document = postcssHtml.parse('<style>a { color: red; }</style>'); | ||
| const root = document.first; | ||
| const decl = root.first.first; | ||
|
|
||
| // Verify the expected AST structure: Document -> Root -> Rule -> Declaration | ||
| expect(document.type).toBe('document'); | ||
| expect(root.type).toBe('root'); | ||
|
|
||
| // In some HTML parsing scenarios, root.parent may be undefined | ||
| // despite the root being contained within a document | ||
| expect(root.parent).toBeUndefined(); | ||
|
|
||
| // Our function should still correctly detect that the declaration | ||
| // is within a document context through alternative means | ||
| expect(isInDocument(decl)).toBe(true); | ||
| }); | ||
| }); | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,27 @@ | ||
| import { isDocument } from './typeGuards.mjs'; | ||
|
|
||
| /** | ||
| * @param {import('postcss').Node} node | ||
sw1tch3roo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @returns {boolean} | ||
| */ | ||
| export default function isInDocument(node) { | ||
| let current = node; | ||
|
|
||
| while (current) { | ||
| if (isDocument(current)) return true; | ||
|
|
||
| // Check for unofficial 'document' property from parsers like postcss-html | ||
| if ( | ||
| 'document' in current && | ||
| current.document && | ||
| isDocument(/** @type {import('postcss').Document} */ (current.document)) | ||
sw1tch3roo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
| return true; | ||
|
|
||
| if (!current.parent) break; | ||
|
|
||
| current = current.parent; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.