Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ function tokenize(cssString) {
// match[2] 为条件(如果存在)
tokens.push({
type: match[1], // 'if'、'elif'、'else' 或 'endif'
condition: match[2] ? match[2].trim() : null
condition: match[2] ? match[2].trim() : null,
rawValue: match[0]
})
lastIndex = regex.lastIndex
}
Expand All @@ -54,6 +55,7 @@ function parse(cssString) {
currentChildren.push(node)
} else if (token.type === 'if') {
const node = new Node('If', token.condition)
node.rawValue = token.rawValue || ''
currentChildren.push(node)
nodeStack.push(currentChildren)
currentChildren = node.children
Expand All @@ -63,6 +65,7 @@ function parse(cssString) {
}
currentChildren = nodeStack[nodeStack.length - 1]
const node = new Node('ElseIf', token.condition)
node.rawValue = token.rawValue || ''
currentChildren.push(node)
currentChildren = node.children
} else if (token.type === 'else') {
Expand All @@ -71,12 +74,16 @@ function parse(cssString) {
}
currentChildren = nodeStack[nodeStack.length - 1]
const node = new Node('Else')
node.rawValue = token.rawValue || ''
currentChildren.push(node)
currentChildren = node.children
} else if (token.type === 'endif') {
const node = new Node('EndIf')
node.rawValue = token.rawValue || ''
if (nodeStack.length > 0) {
currentChildren = nodeStack.pop()
}
currentChildren.push(node)
}
})
return ast
Expand Down Expand Up @@ -105,17 +112,22 @@ function traverseAndEvaluate(ast, defs) {
} else if (node.type === 'If') {
// 直接判断 If 节点
batchedIf = false
output += node.rawValue || ''
if (evaluateCondition(node.condition, defs)) {
traverse(node.children)
batchedIf = true
}
} else if (node.type === 'ElseIf' && !batchedIf) {
output += node.rawValue || ''
if (evaluateCondition(node.condition, defs)) {
traverse(node.children)
batchedIf = true
}
} else if (node.type === 'Else' && !batchedIf) {
output += node.rawValue || ''
traverse(node.children)
} else if (node.type === 'EndIf') {
output += node.rawValue || ''
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@
.selector3
color green /** comment 5 */


/* @mpx-if (!isMobile) *//* @mpx-else */
.layout-nested-else
background #fff
color blue

/* @mpx-endif */

.text-ellipsis {

/* @mpx-if (__mpx_mode__ !== 'ios' && __mpx_mode__ !== 'android' && __mpx_mode__ !== 'harmony') *//* @mpx-endif */
}


/* @mpx-if (__mpx_mode__ !== 'ios' && __mpx_mode__ !== 'android' && __mpx_mode__ !== 'harmony') *//* @mpx-endif */

.layout
background red
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@

.layout
background red

/* @mpx-if (isMobile) */
color yellow
.driver
color blue

/* @mpx-endif */


.wrapper
background red

/* @mpx-if (isMobile) */
color yellow
.child
color blue

/* @mpx-endif */

```
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
## Result

```css

/*@mpx-if(isMobile)*/
.mobile {
display: block;
}
/*@mpx-endif*/



/*@mpx-if(showHeader)*/
.header {
height: 100px;
}

/*@mpx-endif*/

```
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@

```css
body { margin: 0; }

/*@mpx-if(isMobile)*/
.mobile {
display: block;

/*@mpx-if(hasFeature)*/
.feature { color: red; }

/*@mpx-endif*/
}

/*@mpx-endif*/
header { color: red }
```
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

```css
header {}

/*@mpx-if(isMobile)*/
.mobile { display: block; }

/*@mpx-endif */
body {}
```
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

```stylus


/* @mpx-if (isMobile) *//* @mpx-else */
.desktop { display: block; }
/* @mpx-endif */




/*@mpx-if(!isMobile)*/
.mobile { display: block; }

/* @mpx-endif */

```
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

```stylus


/* @mpx-if (isMobile) */
.mobile { display: block; }
/* @mpx-endif */




/*@mpx-if(!isMobile)*//* @mpx-else */
.desktop { display: block; }

/* @mpx-endif */

```
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
```stylus

.selector

/* @mpx-if (isMobile) *//* @mpx-endif */
```
Loading