Fix #3807: parsing the data attributes.

This commit is contained in:
Ben 2024-02-16 19:26:01 +08:00
parent e36f13c595
commit 740b89258d
2 changed files with 27 additions and 13 deletions

View File

@ -145,6 +145,7 @@
string integrity = ""; string integrity = "";
string crossorigin = ""; string crossorigin = "";
string type = ""; string type = "";
var dataAttributes = new Dictionary<string, string>();
foreach (var attribute in attributes) foreach (var attribute in attributes)
{ {
if (attribute.Contains("=")) if (attribute.Contains("="))
@ -167,6 +168,12 @@
case "type": case "type":
type = value[1]; type = value[1];
break; break;
default:
if(!string.IsNullOrWhiteSpace(value[0]) && value[0].StartsWith("data-"))
{
dataAttributes.Add(value[0], value[1]);
}
break;
} }
} }
} }
@ -174,7 +181,7 @@
if (!string.IsNullOrEmpty(src)) if (!string.IsNullOrEmpty(src))
{ {
src = (src.Contains("://")) ? src : PageState.Alias.BaseUrl + src; src = (src.Contains("://")) ? src : PageState.Alias.BaseUrl + src;
scripts.Add(new { href = src, bundle = "", integrity = integrity, crossorigin = crossorigin, es6module = (type == "module"), location = location.ToString().ToLower() }); scripts.Add(new { href = src, bundle = "", integrity = integrity, crossorigin = crossorigin, es6module = (type == "module"), location = location.ToString().ToLower(), dataAttributes = dataAttributes });
} }
else else
{ {

View File

@ -206,21 +206,28 @@ Oqtane.Interop = {
returnPromise: true, returnPromise: true,
before: function (path, element) { before: function (path, element) {
for (let s = 0; s < scripts.length; s++) { for (let s = 0; s < scripts.length; s++) {
if (path === scripts[s].href && scripts[s].integrity !== '') { if (path === scripts[s].href) {
if (scripts[s].integrity !== '') {
element.integrity = scripts[s].integrity; element.integrity = scripts[s].integrity;
} }
if (path === scripts[s].href && scripts[s].crossorigin !== '') { if (scripts[s].crossorigin !== '') {
element.crossOrigin = scripts[s].crossorigin; element.crossOrigin = scripts[s].crossorigin;
} }
if (path === scripts[s].href && scripts[s].es6module === true) { if (scripts[s].es6module === true) {
element.type = "module"; element.type = "module";
} }
if (path === scripts[s].href && scripts[s].location === 'body') { if (typeof scripts[s].dataAttributes !== "undefined" && scripts[s].dataAttributes !== null) {
for (var key in scripts[s].dataAttributes) {
element.setAttribute(key, scripts[s].dataAttributes[key]);
}
}
if (scripts[s].location === 'body') {
document.body.appendChild(element); document.body.appendChild(element);
return false; // return false to bypass default DOM insertion mechanism return false; // return false to bypass default DOM insertion mechanism
} }
} }
} }
}
}) })
.then(function () { resolve(true) }) .then(function () { resolve(true) })
.catch(function (pathsNotFound) { reject(false) }); .catch(function (pathsNotFound) { reject(false) });