safevalues
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -52,2 +52,10 @@ /// <reference types="trusted-types" /> | ||
/** | ||
* Creates a new TrustedScriptURL based on an existing one but with the | ||
* addition of a fragment (the part after `#`). If the URL already has a | ||
* fragment, it is replaced with the new one. | ||
* @param fragment The fragment to add to the URL, verbatim, without the leading | ||
* `#`. No additional escaping is applied. | ||
*/ | ||
export declare function replaceFragment(trustedUrl: TrustedScriptURL, fragment: string): TrustedScriptURL; | ||
/** | ||
* Creates a `TrustedScriptURL` by generating a `Blob` from a | ||
@@ -54,0 +62,0 @@ * `TrustedScript` and then calling `URL.createObjectURL` with that `Blob`. |
@@ -178,3 +178,23 @@ /* | ||
} | ||
const BEFORE_FRAGMENT_REGEXP = /[^#]*/; | ||
/** | ||
* Creates a new TrustedScriptURL based on an existing one but with the | ||
* addition of a fragment (the part after `#`). If the URL already has a | ||
* fragment, it is replaced with the new one. | ||
* @param fragment The fragment to add to the URL, verbatim, without the leading | ||
* `#`. No additional escaping is applied. | ||
*/ | ||
export function replaceFragment(trustedUrl, fragment) { | ||
const urlString = unwrapScriptUrlAsString(trustedUrl); | ||
if ('URL' in window && typeof URL === 'function') { | ||
const url = new URL(urlString); | ||
url.hash = fragment; | ||
return createScriptUrl(url.toString()); | ||
} | ||
else { | ||
// Fallback for IE | ||
return createScriptUrl(BEFORE_FRAGMENT_REGEXP.exec(urlString)[0] + '#' + fragment); | ||
} | ||
} | ||
/** | ||
* Creates a `TrustedScriptURL` by generating a `Blob` from a | ||
@@ -181,0 +201,0 @@ * `TrustedScript` and then calling `URL.createObjectURL` with that `Blob`. |
@@ -29,5 +29,5 @@ /// <reference types="trusted-types" /> | ||
* Also ensures to return the right string value for `TrustedScriptURL` objects | ||
* if the `toString function has been overwritten on the object. | ||
* if the `toString` function has been overwritten on the object. | ||
*/ | ||
export declare function unwrapScriptUrlAsString(value: TrustedScriptURL): string; | ||
//# sourceMappingURL=script_url_impl.d.ts.map |
@@ -75,3 +75,3 @@ /* | ||
* Also ensures to return the right string value for `TrustedScriptURL` objects | ||
* if the `toString function has been overwritten on the object. | ||
* if the `toString` function has been overwritten on the object. | ||
*/ | ||
@@ -78,0 +78,0 @@ export function unwrapScriptUrlAsString(value) { |
@@ -52,8 +52,19 @@ /* | ||
if (trustedTypesPolicy === undefined) { | ||
trustedTypesPolicy = | ||
(_b = (_a = getTrustedTypes()) === null || _a === void 0 ? void 0 : _a.createPolicy(trustedTypesPolicyName, { | ||
createHTML: (s) => s, | ||
createScript: (s) => s, | ||
createScriptURL: (s) => s | ||
})) !== null && _b !== void 0 ? _b : null; | ||
try { | ||
trustedTypesPolicy = | ||
(_b = (_a = getTrustedTypes()) === null || _a === void 0 ? void 0 : _a.createPolicy(trustedTypesPolicyName, { | ||
createHTML: (s) => s, | ||
createScript: (s) => s, | ||
createScriptURL: (s) => s | ||
})) !== null && _b !== void 0 ? _b : null; | ||
} | ||
catch (e) { | ||
// In Chromium versions before 81, trustedTypes.createPolicy throws if | ||
// called with a name that is already registered, even if no CSP is set. | ||
// Until users have largely migrated to 81 or above, catch the error not | ||
// to break the applications functionally. In such case, the code will | ||
// fall back to using regular Safe Types. | ||
trustedTypesPolicy = null; | ||
console.log(e); | ||
} | ||
} | ||
@@ -60,0 +71,0 @@ return trustedTypesPolicy; |
@@ -6,5 +6,5 @@ /** | ||
*/ | ||
export { htmlEscape } from './builders/html_builders'; | ||
export { script } from './builders/script_builders'; | ||
export { scriptUrl } from './builders/script_url_builders'; | ||
export { concatHtmls, createScriptSrc, htmlEscape } from './builders/html_builders'; | ||
export { concatScripts, script, scriptFromJson, scriptWithArgs } from './builders/script_builders'; | ||
export { appendParams, blobUrlFromScript, replaceFragment, scriptUrl } from './builders/script_url_builders'; | ||
/** Reexport the public type (but not the Impl). */ | ||
@@ -11,0 +11,0 @@ export { EMPTY_HTML, unwrapHtmlForSink } from './implementation/html_impl'; |
@@ -22,5 +22,5 @@ /* | ||
*/ | ||
export { htmlEscape } from './builders/html_builders'; | ||
export { script } from './builders/script_builders'; | ||
export { scriptUrl } from './builders/script_url_builders'; | ||
export { concatHtmls, createScriptSrc, htmlEscape } from './builders/html_builders'; | ||
export { concatScripts, script, scriptFromJson, scriptWithArgs } from './builders/script_builders'; | ||
export { appendParams, blobUrlFromScript, replaceFragment, scriptUrl } from './builders/script_url_builders'; | ||
/** Reexport the public type (but not the Impl). */ | ||
@@ -27,0 +27,0 @@ export { EMPTY_HTML, unwrapHtmlForSink } from './implementation/html_impl'; |
{ | ||
"name": "safevalues", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Safe builders for Trusted Types values", | ||
@@ -13,2 +13,3 @@ "repository": "https://github.com/google/safevalues", | ||
"types": "index.d.ts", | ||
"sideEffects": false, | ||
"files": [ | ||
@@ -15,0 +16,0 @@ "/index.*", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
140853
6
1312