1
- import postcss from ' postcss' ;
2
- import detectContainerDefinition from ' ./detectContainerDefinition' ;
3
- import getConditionsFromQueryParams from ' ./getConditionsFromQueryParams' ;
4
- import getStylesObjectFromNode from ' ./getStylesObjectFromNode' ;
5
- import isEmptyObject from ' ./isEmptyObject' ;
1
+ import postcss from " postcss" ;
2
+ import detectContainerDefinition from " ./detectContainerDefinition" ;
3
+ import getConditionsFromQueryParams from " ./getConditionsFromQueryParams" ;
4
+ import getStylesObjectFromNode from " ./getStylesObjectFromNode" ;
5
+ import isEmptyObject from " ./isEmptyObject" ;
6
6
import { DEFINE_CONTAINER_NAME } from "../constants" ;
7
- import saveJSON from ' ./saveJSON' ;
7
+ import saveJSON from " ./saveJSON" ;
8
8
9
- function addStylesToDefaultQuery ( defaultElementRef , styles , keepValues = false ) {
9
+ function addStylesToDefaultQuery (
10
+ defaultElementRef ,
11
+ styles ,
12
+ keepValues = false
13
+ ) {
10
14
for ( let prop in styles ) {
11
- if ( typeof defaultElementRef . styles [ prop ] !== ' undefined' ) {
15
+ if ( typeof defaultElementRef . styles [ prop ] !== " undefined" ) {
12
16
continue ;
13
17
}
14
18
15
- defaultElementRef . styles [ prop ] = keepValues ? styles [ prop ] : '' ;
19
+ defaultElementRef . styles [ prop ] = keepValues ? styles [ prop ] : "" ;
16
20
}
17
21
}
18
22
@@ -21,33 +25,29 @@ function addStylesToDefaultQuery (defaultElementRef, styles, keepValues = false)
21
25
*
22
26
* @param {Node } node
23
27
*/
24
- function shouldProcessNode ( node ) {
25
- return (
26
- node . parent . type === 'root' ||
27
- (
28
- node . parent . type === 'atrule' &&
29
- [ 'container' , 'media' ] . indexOf ( node . parent . name ) !== - 1
30
- )
31
- ) ;
28
+ function shouldProcessNode ( node ) {
29
+ return node . parent . type === "root" ||
30
+ ( node . parent . type === "atrule" &&
31
+ [ "container" , "media" ] . indexOf ( node . parent . name ) !== - 1 ) ;
32
32
}
33
33
34
34
/**
35
35
* @param {{ getJSON: function } } options
36
36
*/
37
- function containerQuery ( options = { } ) {
37
+ function containerQuery ( options = { } ) {
38
38
const getJSON = options . getJSON || saveJSON ;
39
39
40
- return function ( root ) {
40
+ return function ( root ) {
41
41
let containers = { } ;
42
42
let currentContainerSelector = null ;
43
43
let currentDefaultQuery = null ;
44
44
let currentDefaultQueryMap = null ;
45
45
46
- function getElementRefBySelector ( selector ) {
47
- if ( typeof currentDefaultQueryMap [ selector ] === ' undefined' ) {
46
+ function getElementRefBySelector ( selector ) {
47
+ if ( typeof currentDefaultQueryMap [ selector ] === " undefined" ) {
48
48
let elementRef = {
49
49
selector : selector ,
50
- styles : { } ,
50
+ styles : { }
51
51
} ;
52
52
53
53
currentDefaultQuery . elements . push ( elementRef ) ;
@@ -57,15 +57,17 @@ function containerQuery (options = {}) {
57
57
return currentDefaultQueryMap [ selector ] ;
58
58
}
59
59
60
- function flushCurrentContainerData ( newContainer = null ) {
60
+ function flushCurrentContainerData ( newContainer = null ) {
61
61
// Prepend the default query to the previously processed container
62
62
if ( currentContainerSelector !== null ) {
63
- containers [ currentContainerSelector ] . queries . unshift ( currentDefaultQuery ) ;
63
+ containers [ currentContainerSelector ] . queries . unshift (
64
+ currentDefaultQuery
65
+ ) ;
64
66
}
65
67
if ( newContainer !== null ) {
66
68
containers [ newContainer ] = {
67
69
selector : newContainer ,
68
- queries : [ ] ,
70
+ queries : [ ]
69
71
} ;
70
72
}
71
73
currentDefaultQuery = { elements : [ ] } ;
@@ -79,47 +81,49 @@ function containerQuery (options = {}) {
79
81
return ;
80
82
}
81
83
82
- if ( node . type === ' rule' ) {
84
+ if ( node . type === " rule" ) {
83
85
// Check if there's a new container declared in the rule node
84
86
const newContainer = detectContainerDefinition ( node ) ;
85
87
if ( newContainer !== null ) {
86
88
flushCurrentContainerData ( newContainer ) ;
87
89
}
88
90
89
- const isContainer = newContainer !== null || node . selector === currentContainerSelector ;
91
+ const isContainer = newContainer !== null ||
92
+ node . selector === currentContainerSelector ;
90
93
91
94
if ( currentContainerSelector !== null ) {
92
95
// Process potential container unit usages to the default query
93
96
addStylesToDefaultQuery (
94
97
getElementRefBySelector ( node . selector ) ,
95
- getStylesObjectFromNode (
96
- node ,
97
- isContainer ,
98
- true ,
99
- true
100
- ) ,
98
+ getStylesObjectFromNode ( node , isContainer , true , true ) ,
101
99
true
102
100
) ;
103
101
}
104
- } else if ( node . type === ' atrule' && node . name === ' container' ) {
102
+ } else if ( node . type === " atrule" && node . name === " container" ) {
105
103
if ( currentContainerSelector === null ) {
106
- throw node . error ( `A @container query was found, without a preceding @${ DEFINE_CONTAINER_NAME } declaration.` ) ;
104
+ throw node . error (
105
+ `A @container query was found, without a preceding @${ DEFINE_CONTAINER_NAME } declaration.`
106
+ ) ;
107
107
}
108
108
109
109
let query = {
110
110
conditions : getConditionsFromQueryParams ( node . params ) ,
111
- elements : [ ] ,
111
+ elements : [ ]
112
112
} ;
113
113
114
- node . nodes . forEach ( ( elementRule ) => {
115
- if ( elementRule . type !== ' rule' ) {
114
+ node . nodes . forEach ( elementRule => {
115
+ if ( elementRule . type !== " rule" ) {
116
116
return ;
117
117
}
118
118
119
- const isContainer = elementRule . selector === currentContainerSelector ;
119
+ const isContainer = elementRule . selector ===
120
+ currentContainerSelector ;
120
121
let element = {
121
122
selector : elementRule . selector ,
122
- styles : getStylesObjectFromNode ( elementRule , isContainer ) ,
123
+ styles : getStylesObjectFromNode (
124
+ elementRule ,
125
+ isContainer
126
+ )
123
127
} ;
124
128
125
129
if ( ! isEmptyObject ( element . styles ) ) {
@@ -144,7 +148,6 @@ function containerQuery (options = {}) {
144
148
145
149
getJSON ( root . source . input . file , containers ) ;
146
150
} ;
147
-
148
151
}
149
152
150
- export default postcss . plugin ( ' postcss-container-query' , containerQuery ) ;
153
+ export default postcss . plugin ( " postcss-container-query" , containerQuery ) ;
0 commit comments