UNPKG

@esri/solution-common

Version:

Provides general helper functions for @esri/solution.js.

105 lines 3.55 kB
/** @license * Copyright 2020 Esri * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Provides connectors to third-party helper functions. */ import JSZip from "jszip"; import { blobToFile } from "./generalHelpers"; import { Sanitizer } from "@esri/arcgis-html-sanitizer"; export { Sanitizer } from "@esri/arcgis-html-sanitizer"; //#region JSZip ----------------------------------------------------------------------------------------------------- // /** * Creates a zip File from a collection of Files. * * @param zipFilename Name to use for zip File * @param files List of files to add to zip File * @param folder Folder to contain the files * @returns Promise resolving to a zip File */ export function createZip(zipFilename, files, folder) { return new Promise((resolve, reject) => { const zip = new JSZip(); let container = zip; if (folder) { container = zip.folder(folder); } // Add the files files.forEach((file) => container.file(file.name, file, { binary: true })); // Create the ZIP zip .generateAsync({ type: "blob" }) .then((content) => resolve(blobToFile(content, zipFilename, "application/zip")), reject); }); } /** * Sanitizes html. * * @param HTML Text to sanitize * @param sanitizer Instance of Sanitizer class * @returns Sanitized version of `html` * @see https://github.com/esri/arcgis-html-sanitizer#basic-usage */ export function sanitizeHTML(html, sanitizer) { if (!sanitizer) { sanitizer = new Sanitizer(); } return sanitizer.sanitize(html); } /** * Sanitizes JSON. * * @param json JSON to sanitize * @param sanitizer Instance of Sanitizer class * @returns Sanitized version of `json` * @see https://github.com/esri/arcgis-html-sanitizer#sanitize-json */ export function sanitizeJSON(json, sanitizer) { if (!sanitizer) { sanitizer = new Sanitizer(); } return sanitizer.sanitize(json); } /** * Sanitizes the protocol in a URL. * * @param url URL to sanitize * @param sanitizer Instance of Sanitizer class * @returns Sanitized version of `url` * @see https://github.com/esri/arcgis-html-sanitizer#sanitize-urls */ export function sanitizeURLProtocol(url, sanitizer) { if (!sanitizer) { sanitizer = new Sanitizer(); } return sanitizer.sanitizeUrl(url); } /** * Checks if a string contains invalid HTML. * * @param html HTML to check * @param sanitizer Instance of Sanitizer class * @returns An object containing a flag indicating if `html` is valid (i.e., contains no invalid HTML) * as well as the sanitized version of `html` * @see https://github.com/esri/arcgis-html-sanitizer#basic-usage */ export function validateHTML(html, sanitizer) { if (!sanitizer) { sanitizer = new Sanitizer(); } return sanitizer.validate(html); } //#endregion ---------------------------------------------------------------------------------------------------------// //# sourceMappingURL=libConnectors.js.map