Skip to content

supports uri fragment in static style transformed require #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
58 changes: 53 additions & 5 deletions lib/templateCompilerModules/assetUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,26 @@ export default (userOptions?: AssetURLOptions) => {
}

function transform(node: ASTNode, options: AssetURLOptions) {
if (node.__assetUrlTransformed) {
return
}
for (const tag in options) {
if ((tag === '*' || node.tag === tag) && node.attrs) {
const attributes = options[tag]
if (tag === '*' || node.tag === tag) {
let attributes = options[tag]
if (typeof attributes === 'string') {
node.attrs.some(attr => rewrite(attr, attributes))
} else if (Array.isArray(attributes)) {
attributes.forEach(item => node.attrs.some(attr => rewrite(attr, item)))
attributes = [attributes]
}
if (node.staticStyle && attributes.indexOf('style') > -1) {
node.staticStyle = rewriteStaticStyle(node.staticStyle)
}
if (node.attrs) {
attributes.filter(attr => attr !== 'style').forEach(attrName => {
node.attrs.some(attr => rewrite(attr, attrName))
})
}
}
}
node.__assetUrlTransformed = true
}

function rewrite(attr: Attr, name: string) {
Expand All @@ -49,3 +59,41 @@ function rewrite(attr: Attr, name: string) {
}
return false
}

function rewriteStaticStyle(style: string): string {
const styleObj = JSON.parse(style)

// A marker which won't appear in target string
let MARKER: string = Math.random()
.toString(16)
.slice(2, 10)
while (style.indexOf(MARKER) !== -1) {
MARKER = `$${MARKER}$`
}
let id = -1
const expressions: string[] = []

let result: string = JSON.stringify(styleObj, (key, value) => {
if (typeof value !== 'string') {
return value
}
let transformed: string = value.replace(
/url\((['"])?(.*?)\1\)/g,
(_0, _1, url) => {
// outer quotes would be added later
return `url(' + ${urlToRequire(url)} + ')`
}
)
if (transformed !== value) {
// add outer quotes
transformed = `'${transformed}'`
expressions.push(transformed)
id++
return MARKER + id
}
return value
})
const MARKER_RE = new RegExp(`"${MARKER}(\\d+)"`, 'g')
result = result.replace(MARKER_RE, (_, id) => expressions[id])
return result
}
2 changes: 2 additions & 0 deletions lib/templateCompilerModules/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface Attr {
export interface ASTNode {
tag: string
attrs: Attr[]
staticStyle?: string
Copy link
Member

@znck znck Oct 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not convinced in changing the interface of ASTNode, it's not necessary to surface staticStyle from attrs. Also now codegen has to modified to treat styles differently.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not convinced in changing the interface of ASTNode, it's not necessary to surface staticStyle from attrs.

since attrs doesn't contain style

Also now codegen has to modified to treat styles differently.

Agree

__assetUrlTransformed?: boolean
}

import { UrlWithStringQuery, parse as uriParse } from 'url'
Expand Down
33 changes: 33 additions & 0 deletions test/__snapshots__/compileTemplate.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`supports uri fragment in static style transformed require 1`] = `
"var render = function() {
var _vm = this
var _h = _vm.$createElement
var _c = _vm._self._c || _h
return _c(\\"div\\", [
_c(\\"img\\", { attrs: { src: require(\\"./image.jpg\\") } }),
_c(\\"div\\", {
staticStyle: {
color: \\"yellow\\",
background: \\"#f00 url(\\" + require(\\"./image/bg0.jpg\\") + \\") repeat\\"
}
}),
_c(\\"div\\", {
staticStyle: {
background:
\\"url(\\" +
require(\\"./image/bg1.jpg\\") +
\\"), url(\\" +
require(\\"@/image/bg2.jpg\\") +
\\"), url(\\" +
\\"http://vuejs.org/logo.png\\" +
\\")\\"
}
})
])
}
var staticRenderFns = []
render._withStripped = true
"
`;
23 changes: 23 additions & 0 deletions test/compileTemplate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ test('when too short uri then empty require', () => {
expect(result.code).toMatch(/href: require\(""\)/)
})

/**
* Support uri fragment in static style in transformed require
*/
test('supports uri fragment in static style transformed require', () => {
const source =
'<div>' +
'<img src="./image.jpg"/>' +
'<div style="color: yellow;background: #f00 url(./image/bg0.jpg) repeat"></div>' +
'<div style="background: url(./image/bg1.jpg), url(~@/image/bg2.jpg), url(http://vuejs.org/logo.png)"></div>' +
'</div>'
const result = compileTemplate({
filename: 'inline-style.html',
source: source,
transformAssetUrls: {
use: 'href',
'*': 'style'
},
compiler: compiler
})
expect(result.errors.length).toBe(0)
expect(result.code).toMatchSnapshot()
})

test('warn missing preprocessor', () => {
const template = parse({
source: '<template lang="unknownLang">\n' + '</template>\n',
Expand Down