|
1 |
| -import { NodeTransform } from '@vue/compiler-core' |
| 1 | +import { |
| 2 | + createCompoundExpression, |
| 3 | + createSimpleExpression, |
| 4 | + NodeTransform, |
| 5 | + NodeTypes, |
| 6 | + SimpleExpressionNode |
| 7 | +} from '@vue/compiler-core' |
| 8 | +import { parseUrl } from './templateUtils' |
2 | 9 |
|
3 |
| -export const transformSrcset: NodeTransform = () => { |
4 |
| - // TODO |
| 10 | +const srcsetTags = ['img', 'source'] |
| 11 | + |
| 12 | +interface ImageCandidate { |
| 13 | + url: string |
| 14 | + descriptor: string |
| 15 | +} |
| 16 | + |
| 17 | +// http://w3c.github.io/html/semantics-embedded-content.html#ref-for-image-candidate-string-5 |
| 18 | +const escapedSpaceCharacters = /( |\\t|\\n|\\f|\\r)+/g |
| 19 | + |
| 20 | +export const transformSrcset: NodeTransform = (node, context) => { |
| 21 | + if (node.type === NodeTypes.ELEMENT) { |
| 22 | + if (srcsetTags.includes(node.tag) && node.props.length) { |
| 23 | + node.props.forEach((attr, index) => { |
| 24 | + if (attr.name === 'srcset' && attr.type === NodeTypes.ATTRIBUTE) { |
| 25 | + if (!attr.value) return |
| 26 | + // same logic as in transform-require.js |
| 27 | + const value = attr.value.content |
| 28 | + |
| 29 | + const imageCandidates: ImageCandidate[] = value.split(',').map(s => { |
| 30 | + // The attribute value arrives here with all whitespace, except |
| 31 | + // normal spaces, represented by escape sequences |
| 32 | + const [url, descriptor] = s |
| 33 | + .replace(escapedSpaceCharacters, ' ') |
| 34 | + .trim() |
| 35 | + .split(' ', 2) |
| 36 | + return { url, descriptor } |
| 37 | + }) |
| 38 | + |
| 39 | + const compoundExpression = createCompoundExpression([], attr.loc) |
| 40 | + imageCandidates.forEach(({ url, descriptor }, index) => { |
| 41 | + const { path } = parseUrl(url) |
| 42 | + let exp: SimpleExpressionNode |
| 43 | + if (path) { |
| 44 | + const importsArray = Array.from(context.imports) |
| 45 | + const existingImportsIndex = importsArray.findIndex( |
| 46 | + i => i.path === path |
| 47 | + ) |
| 48 | + if (existingImportsIndex > -1) { |
| 49 | + exp = createSimpleExpression( |
| 50 | + `_imports_${existingImportsIndex}`, |
| 51 | + false, |
| 52 | + attr.loc, |
| 53 | + true |
| 54 | + ) |
| 55 | + } else { |
| 56 | + exp = createSimpleExpression( |
| 57 | + `_imports_${importsArray.length}`, |
| 58 | + false, |
| 59 | + attr.loc, |
| 60 | + true |
| 61 | + ) |
| 62 | + context.imports.add({ exp, path }) |
| 63 | + } |
| 64 | + compoundExpression.children.push(exp) |
| 65 | + } |
| 66 | + const isNotLast = imageCandidates.length - 1 > index |
| 67 | + if (descriptor && isNotLast) { |
| 68 | + compoundExpression.children.push(` + '${descriptor}, ' + `) |
| 69 | + } else if (descriptor) { |
| 70 | + compoundExpression.children.push(` + '${descriptor}'`) |
| 71 | + } else if (isNotLast) { |
| 72 | + compoundExpression.children.push(` + ', ' + `) |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | + node.props[index] = { |
| 77 | + type: NodeTypes.DIRECTIVE, |
| 78 | + name: 'bind', |
| 79 | + arg: createSimpleExpression('srcset', true, attr.loc), |
| 80 | + exp: context.hoist(compoundExpression), |
| 81 | + modifiers: [], |
| 82 | + loc: attr.loc |
| 83 | + } |
| 84 | + } |
| 85 | + }) |
| 86 | + } |
| 87 | + } |
5 | 88 | }
|
0 commit comments