package org.biouno.unochoice.util;

import com.google.common.base.Throwables;
import hudson.markup.BasicPolicy;
import hudson.markup.MarkupFormatter;
import org.owasp.html.HtmlPolicyBuilder;
import org.owasp.html.HtmlSanitizer;
import org.owasp.html.HtmlStreamRenderer;
import org.owasp.html.PolicyFactory;

import java.io.IOException;
import java.io.Writer;

public class SafeHtmlExtendedMarkupFormatter extends MarkupFormatter {

    public static final SafeHtmlExtendedMarkupFormatter INSTANCE = new SafeHtmlExtendedMarkupFormatter();

    /**
     * {@link BasicPolicy#POLICY_DEFINITION} is the policy used by {@link hudson.markup.RawHtmlMarkupFormatter}.
     * We start from that secure policy and then extend it to include required elements for this plugin.
     */
    private static final PolicyFactory POLICY = BasicPolicy.POLICY_DEFINITION.and(new HtmlPolicyBuilder()
        .allowElements("input", "textarea", "select", "option")
        .allowAttributes("id", "class", "style", "type", "name", "value", "placeholder", "disabled", "checked", "max", "maxlength", "min", "minlength", "multiple", "pattern", "readonly", "step").onElements("input")
        .allowAttributes("id", "class", "style", "maxlength", "name", "placeholder", "disabled", "readonly", "wrap", "rows", "cols").onElements("textarea")
        .allowAttributes("id", "class", "style", "disabled", "multiple", "name", "required", "size").onElements("select")
        .allowAttributes("id", "class", "style", "disabled", "label", "selected", "value").onElements("option")
        .toFactory());

    /**
     * Copied from {@link hudson.markup.RawHtmlMarkupFormatter#translate(String, Writer)}
     */
    @Override
    public void translate(String markup, Writer output) throws IOException {
        HtmlStreamRenderer renderer = HtmlStreamRenderer.create(
            output,
            // Receives notifications on a failure to write to the output.
            Throwables::propagate, // System.out suppresses IOExceptions
            // Our HTML parser is very lenient, but this receives notifications on
            // truly bizarre inputs.
            x -> {
                throw new Error(x);
            }
        );
        HtmlSanitizer.sanitize(markup, POLICY.apply(renderer));
    }
}

@@ -1,154 +1,1272 @@
-package org.biouno.unochoice.util;%0A%0Aimport com.google.common.base.Throwables;%0Aimport hudson.markup.BasicPolicy;%0Aimport hudson.markup.MarkupFormat
+/*%0A * The MIT License (MIT)%0A *%0A * Copyright (c) 2014-2020 Ioannis K. Moutsatsos, Bruno P. Kinoshita%0A *%0A * Permission is hereby granted, free of charge, to any person obtaining a copy%0A * of this software and associated documentation files (the %22Software%22), to deal%0A * in the Software without restriction, including without limitation the rights%0A * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell%0A * copies of the Software, and to permit persons to whom the Software is%0A * furnished to do so, subject to the following conditions:%0A *%0A * The above copyright notice and this permission notice shall be included in all%0A * copies or substantial portions of the Software.%0A *%0A * THE SOFTWARE IS PROVIDED %22AS IS%22, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR%0A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,%0A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE%0A * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER%0A * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,%0A * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE%0A * SOFTWARE.%0A */%0A%0Apackage org.biouno.unochoice.util;%0A%0Aimport java.io.IOException;%0Aimport java.io.Wri
 ter;%0A
+%0A
 impo
@@ -1430,47 +1430,111 @@
 ort 
-java.io.IOException;%0Aimport java.io.Wri
+com.google.common.base.Throwables;%0A%0Aimport hudson.markup.BasicPolicy;%0Aimport hudson.markup.MarkupFormat
 ter;

Add missing header, sort imports