// (C) Copyright 2015 Moodle Pty Ltd. // // 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. import { Injectable } from '@angular/core'; import { SafeUrl } from '@angular/platform-browser'; import { CoreAnyError, CoreError } from '@classes/errors/error'; import { makeSingleton } from '@singletons'; import { CoreWSFile } from '@services/ws'; import { CoreFileHelper } from '@services/file-helper'; import { CoreUrl } from '@singletons/url'; import { CoreDom } from '@singletons/dom'; import { CoreText } from '@singletons/text'; import { CoreViewer, CoreViewerTextOptions } from '@features/viewer/services/viewer'; import { CoreErrorHelper, CoreErrorObject } from '@services/error-helper'; /** * "Utils" service with helper functions for text. * * @deprecated since 4.5. Some of the functions have been moved to CoreText but not all of them, check function deprecation message. */ @Injectable({ providedIn: 'root' }) export class CoreTextUtilsProvider { /** * Add ending slash from a path or URL. * * @param text Text to treat. * @returns Treated text. * * @deprecated since 4.5. Use CoreText.addEndingSlash instead. */ addEndingSlash(text: string): string { return CoreText.addEndingSlash(text); } /** * Add some text to an error message. * * @param error Error message or object. * @param text Text to add. * @returns Modified error. * * @deprecated since 4.5. Use CoreErrorHelper.addTextToError instead. */ addTextToError( error: string | CoreError | CoreErrorObject | undefined | null, text: string, ): string | CoreErrorObject { return CoreErrorHelper.addTextToError(error, text); } /** * Add some title to an error message. * * @param error Error message or object. * @param title Title to add. * @returns Modified error. * * @deprecated since 4.5. Use CoreErrorHelper.addTitleToError instead. */ addTitleToError(error: string | CoreError | CoreErrorObject | undefined | null, title: string): CoreErrorObject { return CoreErrorHelper.addTitleToError(error, title); } /** * Given an address as a string, return a URL to open the address in maps. * * @param address The address. * @returns URL to view the address. * * @deprecated since 4.5. Use CoreUrl.buildAddressURL instead. */ buildAddressURL(address: string): SafeUrl { return CoreUrl.buildAddressURL(address); } /** * Given a list of sentences, build a message with all of them wrapped in
. * * @param messages Messages to show. * @returns Message with all the messages. * * @deprecated since 4.5. Use CoreText.buildMessage instead. */ buildMessage(messages: string[]): string { return CoreText.buildMessage(messages); } /** * Build a message with several paragraphs. * * @param paragraphs List of paragraphs. * @returns Built message. * * @deprecated since 4.5. Use CoreErrorHelper.buildSeveralParagraphsMessage instead. */ buildSeveralParagraphsMessage(paragraphs: (string | CoreErrorObject)[]): string { return CoreErrorHelper.buildSeveralParagraphsMessage(paragraphs); } /** * Convert size in bytes into human readable format * * @param bytes Number of bytes to convert. * @param precision Number of digits after the decimal separator. * @returns Size in human readable format. * * @deprecated since 4.5. Use CoreText.bytesToSize instead. */ bytesToSize(bytes: number, precision: number = 2): string { return CoreText.bytesToSize(bytes, precision); } /** * Process HTML string. * * @param text HTML string. * @param process Method to process the HTML. * @returns Processed HTML string. * * @deprecated since 4.5. Use CoreText.processHTML instead. */ processHTML(text: string, process: (element: HTMLElement) => unknown): string { return CoreText.processHTML(text, process); } /** * Clean HTML tags. * * @param text The text to be cleaned. * @param options Processing options. * @param options.singleLine True if new lines should be removed (all the text in a single line). * @param options.trim True if text should be trimmed. * @returns Clean text. * * @deprecated since 4.5. Use CoreText.cleanTags instead. */ cleanTags(text: string | undefined, options: { singleLine?: boolean; trim?: boolean } = {}): string { return CoreText.cleanTags(text, options); } /** * Count words in a text. * This function is based on Moodle's count_words. * * @param text Text to count. * @returns Number of words. * * @deprecated since 4.5. Use CoreText.countWords instead. */ countWords(text?: string | null): number { return CoreText.countWords(text); } /** * Decode an escaped HTML text. This implementation is based on PHP's htmlspecialchars_decode. * * @param text Text to decode. * @returns Decoded text. * * @deprecated since 4.5. Use CoreText.decodeHTML instead. */ decodeHTML(text: string | number): string { return CoreText.decodeHTML(text); } /** * Decode HTML entities in a text. Equivalent to PHP html_entity_decode. * * @param text Text to decode. * @returns Decoded text. * * @deprecated since 4.5. Use CoreText.decodeHTMLEntities instead. */ decodeHTMLEntities(text: string): string { return CoreText.decodeHTMLEntities(text); } /** * Same as Javascript's decodeURI, but if an exception is thrown it will return the original URI. * * @param uri URI to decode. * @returns Decoded URI, or original URI if an exception is thrown. * * @deprecated since 4.5. Use CoreUrl.decodeURI instead. */ decodeURI(uri: string): string { return CoreUrl.decodeURI(uri); } /** * Same as Javascript's decodeURIComponent, but if an exception is thrown it will return the original URI. * * @param uri URI to decode. * @returns Decoded URI, or original URI if an exception is thrown. * * @deprecated since 4.5. Use CoreUrl.decodeURIComponent instead. */ decodeURIComponent(uri: string): string { return CoreUrl.decodeURIComponent(uri); } /** * Escapes some characters in a string to be used as a regular expression. * * @param text Text to escape. * @returns Escaped text. * * @deprecated since 4.5. Use CoreText.escapeForRegex instead. */ escapeForRegex(text: string): string { return CoreText.escapeForRegex(text); } /** * Escape an HTML text. This implementation is based on PHP's htmlspecialchars. * * @param text Text to escape. * @param doubleEncode If false, it will not convert existing html entities. Defaults to true. * @returns Escaped text. * * @deprecated since 4.5. Use CoreText.escapeHTML instead. */ escapeHTML(text?: string | number | null, doubleEncode = true): string { return CoreText.escapeHTML(text, doubleEncode); } /** * Formats a text, in HTML replacing new lines by correct html new lines. * * @param text Text to format. * @returns Formatted text. * * @deprecated since 4.5. Use CoreText.formatHtmlLines instead. */ formatHtmlLines(text: string): string { return CoreText.formatHtmlLines(text); } /** * Get the error message from an error object. * * @param error Error. * @returns Error message, undefined if not found. * * @deprecated since 4.5. Use CoreErrorHelper.getErrorMessageFromError instead. */ getErrorMessageFromError(error?: CoreAnyError): string | undefined { return CoreErrorHelper.getErrorMessageFromError(error); } /** * Given some HTML code, return the HTML code inside
tags. If there are no body tags, return the whole HTML. * * @param html HTML text. * @returns Body HTML. * * @deprecated since 4.5. Use CoreDom.getHTMLBodyContent instead. */ getHTMLBodyContent(html: string): string { return CoreDom.getHTMLBodyContent(html); } /** * Get the pluginfile URL to replace @@PLUGINFILE@@ wildcards. * * @param files Files to extract the URL from. They need to have the URL in a 'url' or 'fileurl' attribute. * @returns Pluginfile URL, undefined if no files found. * * @deprecated since 4.5. Use CoreFileHelper.getTextPluginfileUrl instead. */ getTextPluginfileUrl(files: CoreWSFile[]): string | undefined { return CoreFileHelper.getTextPluginfileUrl(files); } /** * Check if a text contains HTML tags. * * @param text Text to check. * @returns Whether it has HTML tags. * * @deprecated since 4.5. Use CoreText.hasHTMLTags instead. */ hasHTMLTags(text: string): boolean { return CoreText.hasHTMLTags(text); } /** * Highlight all occurrences of a certain text inside another text. It will add some HTML code to highlight it. * * @param text Full text. * @param searchText Text to search and highlight. * @returns Highlighted text. * * @deprecated since 4.5. Use CoreText.highlightText instead. */ highlightText(text: string, searchText: string): string { return CoreText.highlightText(text, searchText); } /** * Check if HTML content is blank. * * @param content HTML content. * @returns True if the string does not contain actual content: text, images, etc. * * @deprecated since 4.5. Use CoreDom.htmlIsBlank instead. */ htmlIsBlank(content: string): boolean { return CoreDom.htmlIsBlank(content); } /** * Check if a text contains Unicode long chars. * Using as threshold Hex value D800 * * @param text Text to check. * @returns True if has Unicode chars, false otherwise. * * @deprecated since 4.5. Use CoreText.hasUnicode instead. */ hasUnicode(text: string): boolean { return CoreText.hasUnicode(text); } /** * Check if an object has any long Unicode char. * * @param data Object to be checked. * @returns If the data has any long Unicode char on it. * * @deprecated since 4.5. Use CoreText.hasUnicodeData instead. */ hasUnicodeData(data: Record