|
| 1 | +import { RESOLVE_FILTER } from '../runtimeHelpers' |
| 2 | +import { |
| 3 | + AttributeNode, |
| 4 | + DirectiveNode, |
| 5 | + NodeTransform, |
| 6 | + NodeTypes, |
| 7 | + SimpleExpressionNode, |
| 8 | + toValidAssetId, |
| 9 | + TransformContext |
| 10 | +} from '@vue/compiler-core' |
| 11 | +import { |
| 12 | + CompilerDeprecationTypes, |
| 13 | + isCompatEnabled, |
| 14 | + warnDeprecation |
| 15 | +} from './compatConfig' |
| 16 | +import { ExpressionNode } from '../ast' |
| 17 | + |
| 18 | +const validDivisionCharRE = /[\w).+\-_$\]]/ |
| 19 | + |
| 20 | +export const transformFilter: NodeTransform = (node, context) => { |
| 21 | + if (!isCompatEnabled(CompilerDeprecationTypes.COMPILER_FILTERS, context)) { |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + if (node.type === NodeTypes.INTERPOLATION) { |
| 26 | + // filter rewrite is applied before expression transform so only |
| 27 | + // simple expressions are possible at this stage |
| 28 | + rewriteFilter(node.content, context) |
| 29 | + } |
| 30 | + |
| 31 | + if (node.type === NodeTypes.ELEMENT) { |
| 32 | + node.props.forEach((prop: AttributeNode | DirectiveNode) => { |
| 33 | + if ( |
| 34 | + prop.type === NodeTypes.DIRECTIVE && |
| 35 | + prop.name !== 'for' && |
| 36 | + prop.exp |
| 37 | + ) { |
| 38 | + rewriteFilter(prop.exp, context) |
| 39 | + } |
| 40 | + }) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +function rewriteFilter(node: ExpressionNode, context: TransformContext) { |
| 45 | + if (node.type === NodeTypes.SIMPLE_EXPRESSION) { |
| 46 | + parseFilter(node, context) |
| 47 | + } else { |
| 48 | + for (let i = 0; i < node.children.length; i++) { |
| 49 | + const child = node.children[i] |
| 50 | + if (typeof child !== 'object') continue |
| 51 | + if (child.type === NodeTypes.SIMPLE_EXPRESSION) { |
| 52 | + parseFilter(child, context) |
| 53 | + } else if (child.type === NodeTypes.COMPOUND_EXPRESSION) { |
| 54 | + rewriteFilter(node, context) |
| 55 | + } else if (child.type === NodeTypes.INTERPOLATION) { |
| 56 | + rewriteFilter(child.content, context) |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function parseFilter(node: SimpleExpressionNode, context: TransformContext) { |
| 63 | + const exp = node.content |
| 64 | + let inSingle = false |
| 65 | + let inDouble = false |
| 66 | + let inTemplateString = false |
| 67 | + let inRegex = false |
| 68 | + let curly = 0 |
| 69 | + let square = 0 |
| 70 | + let paren = 0 |
| 71 | + let lastFilterIndex = 0 |
| 72 | + let c, |
| 73 | + prev, |
| 74 | + i: number, |
| 75 | + expression, |
| 76 | + filters: string[] = [] |
| 77 | + |
| 78 | + for (i = 0; i < exp.length; i++) { |
| 79 | + prev = c |
| 80 | + c = exp.charCodeAt(i) |
| 81 | + if (inSingle) { |
| 82 | + if (c === 0x27 && prev !== 0x5c) inSingle = false |
| 83 | + } else if (inDouble) { |
| 84 | + if (c === 0x22 && prev !== 0x5c) inDouble = false |
| 85 | + } else if (inTemplateString) { |
| 86 | + if (c === 0x60 && prev !== 0x5c) inTemplateString = false |
| 87 | + } else if (inRegex) { |
| 88 | + if (c === 0x2f && prev !== 0x5c) inRegex = false |
| 89 | + } else if ( |
| 90 | + c === 0x7c && // pipe |
| 91 | + exp.charCodeAt(i + 1) !== 0x7c && |
| 92 | + exp.charCodeAt(i - 1) !== 0x7c && |
| 93 | + !curly && |
| 94 | + !square && |
| 95 | + !paren |
| 96 | + ) { |
| 97 | + if (expression === undefined) { |
| 98 | + // first filter, end of expression |
| 99 | + lastFilterIndex = i + 1 |
| 100 | + expression = exp.slice(0, i).trim() |
| 101 | + } else { |
| 102 | + pushFilter() |
| 103 | + } |
| 104 | + } else { |
| 105 | + switch (c) { |
| 106 | + case 0x22: |
| 107 | + inDouble = true |
| 108 | + break // " |
| 109 | + case 0x27: |
| 110 | + inSingle = true |
| 111 | + break // ' |
| 112 | + case 0x60: |
| 113 | + inTemplateString = true |
| 114 | + break // ` |
| 115 | + case 0x28: |
| 116 | + paren++ |
| 117 | + break // ( |
| 118 | + case 0x29: |
| 119 | + paren-- |
| 120 | + break // ) |
| 121 | + case 0x5b: |
| 122 | + square++ |
| 123 | + break // [ |
| 124 | + case 0x5d: |
| 125 | + square-- |
| 126 | + break // ] |
| 127 | + case 0x7b: |
| 128 | + curly++ |
| 129 | + break // { |
| 130 | + case 0x7d: |
| 131 | + curly-- |
| 132 | + break // } |
| 133 | + } |
| 134 | + if (c === 0x2f) { |
| 135 | + // / |
| 136 | + let j = i - 1 |
| 137 | + let p |
| 138 | + // find first non-whitespace prev char |
| 139 | + for (; j >= 0; j--) { |
| 140 | + p = exp.charAt(j) |
| 141 | + if (p !== ' ') break |
| 142 | + } |
| 143 | + if (!p || !validDivisionCharRE.test(p)) { |
| 144 | + inRegex = true |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + if (expression === undefined) { |
| 151 | + expression = exp.slice(0, i).trim() |
| 152 | + } else if (lastFilterIndex !== 0) { |
| 153 | + pushFilter() |
| 154 | + } |
| 155 | + |
| 156 | + function pushFilter() { |
| 157 | + filters.push(exp.slice(lastFilterIndex, i).trim()) |
| 158 | + lastFilterIndex = i + 1 |
| 159 | + } |
| 160 | + |
| 161 | + if ( |
| 162 | + filters.length && |
| 163 | + warnDeprecation( |
| 164 | + CompilerDeprecationTypes.COMPILER_FILTERS, |
| 165 | + context, |
| 166 | + node.loc |
| 167 | + ) |
| 168 | + ) { |
| 169 | + for (i = 0; i < filters.length; i++) { |
| 170 | + expression = wrapFilter(expression, filters[i], context) |
| 171 | + } |
| 172 | + node.content = expression |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +function wrapFilter( |
| 177 | + exp: string, |
| 178 | + filter: string, |
| 179 | + context: TransformContext |
| 180 | +): string { |
| 181 | + context.helper(RESOLVE_FILTER) |
| 182 | + const i = filter.indexOf('(') |
| 183 | + if (i < 0) { |
| 184 | + context.filters!.add(filter) |
| 185 | + return `${toValidAssetId(filter, 'filter')}(${exp})` |
| 186 | + } else { |
| 187 | + const name = filter.slice(0, i) |
| 188 | + const args = filter.slice(i + 1) |
| 189 | + context.filters!.add(name) |
| 190 | + return `${toValidAssetId(name, 'filter')}(${exp}${ |
| 191 | + args !== ')' ? ',' + args : args |
| 192 | + }` |
| 193 | + } |
| 194 | +} |
0 commit comments