mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vweb.assets: add option for custom href and src attribute values (#17703)
This commit is contained in:
parent
9275161d0f
commit
37af8bbd27
@ -24,6 +24,8 @@ pub mut:
|
||||
struct Asset {
|
||||
file_path string
|
||||
last_modified time.Time
|
||||
mut:
|
||||
include_name string
|
||||
}
|
||||
|
||||
// new_manager returns a new AssetManager
|
||||
@ -36,11 +38,33 @@ pub fn (mut am AssetManager) add_css(file string) bool {
|
||||
return am.add('css', file)
|
||||
}
|
||||
|
||||
// add_css_as adds a css asset with a custom href
|
||||
pub fn (mut am AssetManager) add_css_as(file string, href string) bool {
|
||||
if am.add('css', file) {
|
||||
// set name of added asset
|
||||
am.css.last().include_name = href
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// add_js adds a js asset
|
||||
pub fn (mut am AssetManager) add_js(file string) bool {
|
||||
return am.add('js', file)
|
||||
}
|
||||
|
||||
// add_js_as adds a js asset with a custom src
|
||||
pub fn (mut am AssetManager) add_js_as(file string, src string) bool {
|
||||
if am.add('js', file) {
|
||||
// set name of added asset
|
||||
am.js.last().include_name = src
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// combine_css returns the combined css as a string when to_file is false
|
||||
// when to_file is true it combines the css to disk and returns the path of the file
|
||||
pub fn (am AssetManager) combine_css(to_file bool) string {
|
||||
@ -126,7 +150,12 @@ fn (am AssetManager) include(asset_type string, combine bool) string {
|
||||
return '<link rel="stylesheet" href="${file}">\n'
|
||||
}
|
||||
for asset in assets {
|
||||
out += '<link rel="stylesheet" href="${asset.file_path}">\n'
|
||||
mut href := asset.file_path
|
||||
if asset.include_name.len > 0 {
|
||||
href = asset.include_name
|
||||
}
|
||||
|
||||
out += '<link rel="stylesheet" href="${href}">\n'
|
||||
}
|
||||
}
|
||||
if asset_type == 'js' {
|
||||
@ -135,7 +164,12 @@ fn (am AssetManager) include(asset_type string, combine bool) string {
|
||||
return '<script type="text/javascript" src="${file}"></script>\n'
|
||||
}
|
||||
for asset in assets {
|
||||
out += '<script type="text/javascript" src="${asset.file_path}"></script>\n'
|
||||
mut src := asset.file_path
|
||||
if asset.include_name.len > 0 {
|
||||
src = asset.include_name
|
||||
}
|
||||
|
||||
out += '<script type="text/javascript" src="${src}"></script>\n'
|
||||
}
|
||||
}
|
||||
return out
|
||||
|
@ -65,6 +65,16 @@ fn test_add_css() {
|
||||
// assert am.add_css(get_test_file_path('test1.js')) == false // TODO: test extension on add
|
||||
}
|
||||
|
||||
fn test_add_css_as() {
|
||||
mut am := assets.new_manager()
|
||||
file_name := '/custom/path/test1.css'
|
||||
assert am.add_css_as(get_test_file_path('test1.css'), file_name) == true
|
||||
|
||||
expected := '<link rel="stylesheet" href="${file_name}">\n'
|
||||
actual := am.include_css(false)
|
||||
assert actual == expected
|
||||
}
|
||||
|
||||
fn test_add_js() {
|
||||
mut am := assets.new_manager()
|
||||
assert am.add_js('testx.js') == false
|
||||
@ -72,6 +82,16 @@ fn test_add_js() {
|
||||
// assert am.add_css(get_test_file_path('test1.css')) == false // TODO: test extension on add
|
||||
}
|
||||
|
||||
fn test_add_js_as() {
|
||||
mut am := assets.new_manager()
|
||||
file_name := '/custom/path/test1.js'
|
||||
assert am.add_js_as(get_test_file_path('test1.js'), file_name) == true
|
||||
|
||||
expected := '<script type="text/javascript" src="${file_name}"></script>\n'
|
||||
actual := am.include_js(false)
|
||||
assert expected == actual
|
||||
}
|
||||
|
||||
fn test_combine_css() {
|
||||
mut am := assets.new_manager()
|
||||
am.cache_dir = cache_dir('test_combine_css')
|
||||
|
Loading…
Reference in New Issue
Block a user