@@ -157,7 +157,7 @@ render_dependencies <- function(dependencies, local = TRUE, prefix=NULL) {
157
157
# as in Dash for Python
158
158
if (" script" %in% names(dep ) && tools :: file_ext(dep [[" script" ]]) != " map" ) {
159
159
if (! (is_local ) & ! (is.null(dep $ src $ href ))) {
160
- html <- generate_js_dist_html(href = dep $ src $ href )
160
+ html <- generate_js_dist_html(tagdata = dep $ src $ href )
161
161
} else {
162
162
script_mtime <- file.mtime(getDependencyPath(dep ))
163
163
modtime <- as.integer(script_mtime )
@@ -172,7 +172,7 @@ render_dependencies <- function(dependencies, local = TRUE, prefix=NULL) {
172
172
" &m=" ,
173
173
modified )
174
174
175
- html <- generate_js_dist_html(href = dep [[" script" ]], as_is = TRUE )
175
+ html <- generate_js_dist_html(tagdata = dep [[" script" ]], as_is = TRUE )
176
176
}
177
177
} else if (! (is_local ) & " stylesheet" %in% names(dep ) & src == " href" ) {
178
178
html <- generate_css_dist_html(href = paste(dep [[" src" ]][[" href" ]],
@@ -192,20 +192,20 @@ render_dependencies <- function(dependencies, local = TRUE, prefix=NULL) {
192
192
" ?v=" ,
193
193
dep $ version )
194
194
195
- html <- generate_css_dist_html(href = sheetpath , as_is = TRUE )
195
+ html <- generate_css_dist_html(tagdata = sheetpath , as_is = TRUE )
196
196
} else {
197
197
sheetpath <- paste0(dep [[" src" ]][[" file" ]],
198
198
dep [[" stylesheet" ]],
199
199
" ?v=" ,
200
200
dep $ version )
201
201
202
- html <- generate_css_dist_html(href = sheetpath , as_is = TRUE )
202
+ html <- generate_css_dist_html(tagdata = sheetpath , as_is = TRUE )
203
203
}
204
204
205
205
} else {
206
206
sheetpath <- paste0(dep [[" src" ]][[" file" ]],
207
207
dep [[" stylesheet" ]])
208
- html <- generate_css_dist_html(href = sheetpath , as_is = TRUE )
208
+ html <- generate_css_dist_html(tagdata = sheetpath , as_is = TRUE )
209
209
}
210
210
}
211
211
})
@@ -536,51 +536,98 @@ get_mimetype <- function(filename) {
536
536
empty = " application/octet-stream" ))
537
537
}
538
538
539
- generate_css_dist_html <- function (href ,
539
+ generate_css_dist_html <- function (tagdata ,
540
540
local = FALSE ,
541
541
local_path = NULL ,
542
542
prefix = NULL ,
543
543
as_is = FALSE ) {
544
+ attribs <- names(tagdata )
545
+ allowed_attribs <- c(" as" ,
546
+ " crossorigin" ,
547
+ " disabled" ,
548
+ " href" ,
549
+ " hreflang" ,
550
+ " importance" ,
551
+ " integrity" ,
552
+ " media" ,
553
+ " referrerpolicy" ,
554
+ " rel" ,
555
+ " sizes" ,
556
+ " title" ,
557
+ " type" ,
558
+ " methods" ,
559
+ " prefetch" ,
560
+ " target" ,
561
+ " charset" ,
562
+ " rev" )
563
+ if (! all(attribs %in% allowed_attribs )) {
564
+ stop(sprintf(" The following specified stylesheet attributes are invalid: " ,
565
+ paste0(setdiff(attribs , allowed_attribs ), collapse = " , " )), call. = FALSE )
566
+ }
544
567
if (! (local )) {
545
- if (grepl(" ^(?:http(s)?:\\ /\\ /)?[\\ w.-]+(?:\\ .[\\ w\\ .-]+)+[\\ w\\ -\\ ._~:/?#[\\ ]@!\\ $&'\\ (\\ )\\ *\\ +,;=.]+$" ,
546
- href ,
547
- perl = TRUE ) || as_is ) {
548
- sprintf(" <link href=\" %s\" rel=\" stylesheet\" >" , href )
568
+ if (any(grepl(" ^(?:http(s)?:\\ /\\ /)?[\\ w.-]+(?:\\ .[\\ w\\ .-]+)+[\\ w\\ -\\ ._~:/?#[\\ ]@!\\ $&'\\ (\\ )\\ *\\ +,;=.]+$" ,
569
+ tagdata ,
570
+ perl = TRUE )) || as_is ) {
571
+ if (is.list(tagdata ))
572
+ glue :: glue(' <link ' , glue :: glue_collapse(glue :: glue(' {attribs}="{tagdata}"' ), sep = " " ), ' rel="stylesheet">' )
573
+ else
574
+ glue :: glue(' <link ' , glue :: glue(' href="{tagdata}"' ), ' rel="stylesheet">' )
549
575
}
550
576
else
551
577
stop(sprintf(" Invalid URL supplied in external_stylesheets. Please check the syntax used for this parameter." ), call. = FALSE )
552
578
} else {
553
579
# strip leading slash from href if present
580
+ if (is.list(tagdata ))
581
+ href <- tagdata $ src
582
+ else
583
+ href <- tagdata
554
584
href <- sub(" ^/" , " " , href )
555
585
modified <- as.integer(file.mtime(local_path ))
556
- sprintf(" <link href=\" %s%s?m=%s\" rel=\" stylesheet\" >" ,
557
- prefix ,
558
- href ,
559
- modified )
586
+ glue :: glue(' <link href="{prefix}{href}?m={modified}" rel="stylesheet">' )
560
587
}
561
588
}
562
589
563
- generate_js_dist_html <- function (href ,
590
+ generate_js_dist_html <- function (tagdata ,
564
591
local = FALSE ,
565
592
local_path = NULL ,
566
593
prefix = NULL ,
567
594
as_is = FALSE ) {
595
+ attribs <- names(tagdata )
596
+ allowed_attribs <- c(" async" ,
597
+ " crossorigin" ,
598
+ " defer" ,
599
+ " integrity" ,
600
+ " nomodule" ,
601
+ " nonce" ,
602
+ " referrerpolicy" ,
603
+ " src" ,
604
+ " type" ,
605
+ " charset" ,
606
+ " language" )
607
+ if (! all(attribs %in% allowed_attribs )) {
608
+ stop(sprintf(" The following specified script attributes are invalid: " ,
609
+ paste0(setdiff(attribs , allowed_attribs ), collapse = " , " )), call. = FALSE )
610
+ }
568
611
if (! (local )) {
569
- if (grepl(" ^(?:http(s)?:\\ /\\ /)?[\\ w.-]+(?:\\ .[\\ w\\ .-]+)+[\\ w\\ -\\ ._~:/?#[\\ ]@!\\ $&'\\ (\\ )\\ *\\ +,;=.]+$" ,
570
- href ,
571
- perl = TRUE ) || as_is ) {
572
- sprintf(" <script src=\" %s\" ></script>" , href )
573
- }
612
+ if (any(grepl(" ^(?:http(s)?:\\ /\\ /)?[\\ w.-]+(?:\\ .[\\ w\\ .-]+)+[\\ w\\ -\\ ._~:/?#[\\ ]@!\\ $&'\\ (\\ )\\ *\\ +,;=.]+$" ,
613
+ tagdata ,
614
+ perl = TRUE )) || as_is ) {
615
+ if (is.list(tagdata ))
616
+ glue :: glue(' <script ' , glue :: glue_collapse(glue :: glue(' {attribs}="{tagdata}"' ), sep = " " ), ' ></script>' )
617
+ else
618
+ glue :: glue(' <script ' , glue :: glue(' src="{tagdata}"' ), ' ></script>' )
619
+ }
574
620
else
575
621
stop(sprintf(" Invalid URL supplied. Please check the syntax used for this parameter." ), call. = FALSE )
576
622
} else {
577
623
# strip leading slash from href if present
624
+ if (is.list(tagdata ))
625
+ href <- tagdata $ src
626
+ else
627
+ href <- tagdata
578
628
href <- sub(" ^/" , " " , href )
579
629
modified <- as.integer(file.mtime(local_path ))
580
- sprintf(" <script src=\" %s%s?m=%s\" ></script>" ,
581
- prefix ,
582
- href ,
583
- modified )
630
+ glue :: glue(' <script src="{prefix}{href}?m={modified}" ></script>' )
584
631
}
585
632
}
586
633
0 commit comments