Skip to content

Commit c166f2d

Browse files
nzakasCopilot
andauthored
fix: Allow nested at-rules to have nested rules (#77)
* fix: Allow nested at-rules to have nested rules fixes #66 * Update lib/syntax/node/Atrule.js Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 5b0722a commit c166f2d

File tree

5 files changed

+144
-6
lines changed

5 files changed

+144
-6
lines changed

fixtures/ast/rule/nesting.json

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,5 +1791,142 @@
17911791
]
17921792
}
17931793
}
1794+
},
1795+
"nested @media with nesting selector": {
1796+
"source": "slot { margin: 0; @media (max-width: 52rem) { display: inline-block; .page-nav-icon + & { display: none; } } }",
1797+
"generate": "slot{margin:0;@media (max-width:52rem){display:inline-block;.page-nav-icon+&{display:none}}}",
1798+
"ast": {
1799+
"type": "Rule",
1800+
"prelude": {
1801+
"type": "SelectorList",
1802+
"children": [
1803+
{
1804+
"type": "Selector",
1805+
"children": [
1806+
{
1807+
"type": "TypeSelector",
1808+
"name": "slot"
1809+
}
1810+
]
1811+
}
1812+
]
1813+
},
1814+
"block": {
1815+
"type": "Block",
1816+
"children": [
1817+
{
1818+
"type": "Declaration",
1819+
"important": false,
1820+
"property": "margin",
1821+
"value": {
1822+
"type": "Value",
1823+
"children": [
1824+
{
1825+
"type": "Number",
1826+
"value": "0"
1827+
}
1828+
]
1829+
}
1830+
},
1831+
{
1832+
"type": "Atrule",
1833+
"name": "media",
1834+
"prelude": {
1835+
"type": "AtrulePrelude",
1836+
"children": [
1837+
{
1838+
"type": "MediaQueryList",
1839+
"children": [
1840+
{
1841+
"type": "MediaQuery",
1842+
"modifier": null,
1843+
"mediaType": null,
1844+
"condition": {
1845+
"type": "Condition",
1846+
"kind": "media",
1847+
"children": [
1848+
{
1849+
"type": "Feature",
1850+
"kind": "media",
1851+
"name": "max-width",
1852+
"value": {
1853+
"type": "Dimension",
1854+
"value": "52",
1855+
"unit": "rem"
1856+
}
1857+
}
1858+
]
1859+
}
1860+
}
1861+
]
1862+
}
1863+
]
1864+
},
1865+
"block": {
1866+
"type": "Block",
1867+
"children": [
1868+
{
1869+
"type": "Declaration",
1870+
"important": false,
1871+
"property": "display",
1872+
"value": {
1873+
"type": "Value",
1874+
"children": [
1875+
{
1876+
"type": "Identifier",
1877+
"name": "inline-block"
1878+
}
1879+
]
1880+
}
1881+
},
1882+
{
1883+
"type": "Rule",
1884+
"prelude": {
1885+
"type": "SelectorList",
1886+
"children": [
1887+
{
1888+
"type": "Selector",
1889+
"children": [
1890+
{
1891+
"type": "ClassSelector",
1892+
"name": "page-nav-icon"
1893+
},
1894+
{
1895+
"type": "Combinator",
1896+
"name": "+"
1897+
},
1898+
{
1899+
"type": "NestingSelector"
1900+
}
1901+
]
1902+
}
1903+
]
1904+
},
1905+
"block": {
1906+
"type": "Block",
1907+
"children": [
1908+
{
1909+
"type": "Declaration",
1910+
"important": false,
1911+
"property": "display",
1912+
"value": {
1913+
"type": "Value",
1914+
"children": [
1915+
{
1916+
"type": "Identifier",
1917+
"name": "none"
1918+
}
1919+
]
1920+
}
1921+
}
1922+
]
1923+
}
1924+
}
1925+
]
1926+
}
1927+
}
1928+
]
1929+
}
1930+
}
17941931
}
17951932
}

lib/__tests/parse.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function createParseErrorTest(name, test, options) {
3232
}
3333

3434
describe('parse', () => {
35+
3536
describe('basic', () => {
3637
forEachAstTest((name, test) => {
3738
(test.skip ? it.skip : it)(name, () => {

lib/syntax/atrule/media.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ export default {
55
this.MediaQueryList()
66
);
77
},
8-
block(nested = false) {
9-
return this.Block(nested);
8+
block(nested = false, { allowNestedRules = false } = {}) {
9+
return this.Block(nested, { allowNestedRules });
1010
}
1111
}
1212
};

lib/syntax/node/Atrule.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const structure = {
3333
block: ['Block', null]
3434
};
3535

36-
export function parse(isDeclaration = false) {
36+
export function parse(isDeclaration = false, { allowNestedRules = false } = {}) {
3737
const start = this.tokenStart;
3838
let name;
3939
let nameLowerCase;
@@ -67,10 +67,10 @@ export function parse(isDeclaration = false) {
6767
case LeftCurlyBracket:
6868
if (hasOwnProperty.call(this.atrule, nameLowerCase) &&
6969
typeof this.atrule[nameLowerCase].block === 'function') {
70-
block = this.atrule[nameLowerCase].block.call(this, isDeclaration);
70+
block = this.atrule[nameLowerCase].block.call(this, isDeclaration, { allowNestedRules });
7171
} else {
7272
// TODO: should consume block content as Raw?
73-
block = this.Block(isDeclarationBlockAtrule.call(this));
73+
block = this.Block(isDeclaration || isDeclarationBlockAtrule.call(this), { allowNestedRules });
7474
}
7575

7676
break;

lib/syntax/node/Block.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export function parse(isStyleBlock, { allowNestedRules = false } = {}) {
9999
break;
100100

101101
case AtKeyword:
102-
children.push(this.parseWithFallback(this.Atrule.bind(this, isStyleBlock), consumeRaw));
102+
children.push(this.parseWithFallback(this.Atrule.bind(this, isStyleBlock, { allowNestedRules }), consumeRaw));
103103
break;
104104

105105
default:

0 commit comments

Comments
 (0)