All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sirius.web.templates.rythm.EscapeStringTransformer Maven / Gradle / Ivy

/*
 * Made with all the love in the world
 * by scireum in Remshalden, Germany
 *
 * Copyright by scireum GmbH
 * http://www.scireum.de - [email protected]
 */

package sirius.web.templates.rythm;

import org.rythmengine.extension.Transformer;
import sirius.kernel.commons.Strings;

import javax.annotation.Nullable;

/**
 * Escapes JS strings by only replacing single quotes not double quotes (this would otherwise result in invalid
 * JavaScript (which is produced by escape("JS")).
 */
@Transformer("")
public class EscapeStringTransformer {

    private EscapeStringTransformer() {
    }

    /**
     * In contrast to escape JS / JavaScript, this only escapes a single
     * quote, but not a double quote.
     *
     * @param value the string to escape
     * @return the escaped string
     */
    @Nullable
    public static String escapeString(@Nullable String value) {
        if (Strings.isEmpty(value)) {
            return value;
        }

        return value.replace("'", "\\'");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy