| 1 | /* | |
| 2 | * Copyright (c) P. Ottlinger | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | package de.aikiit.mailversendala.template; | |
| 17 | ||
| 18 | import org.jsoup.nodes.Document; | |
| 19 | import org.jsoup.nodes.Element; | |
| 20 | import org.jsoup.parser.ParseError; | |
| 21 | import org.jsoup.parser.Parser; | |
| 22 | ||
| 23 | import java.io.IOException; | |
| 24 | import java.util.ArrayList; | |
| 25 | import java.util.List; | |
| 26 | ||
| 27 | /** Checks rendered HTML without changing the outgoing markup or fetching resources. */ | |
| 28 | public final class HtmlMarkupValidator { | |
| 29 | private static final int MAX_ERRORS = 20; | |
| 30 | ||
| 31 | private HtmlMarkupValidator() { } | |
| 32 | ||
| 33 | /** | |
| 34 | * Reject HTML parser errors and likely misspelled HTML tags before delivery. | |
| 35 | * Full documents and body fragments are accepted. Positions refer to rendered HTML. | |
| 36 | * @param html rendered HTML body. | |
| 37 | * @param source template filename and recipient for diagnostic messages. | |
| 38 | * @throws IOException if markup errors are found. | |
| 39 | */ | |
| 40 | public static void validate(String html, String source) throws IOException { | |
| 41 | Parser parser = Parser.htmlParser().setTrackErrors(MAX_ERRORS).setTrackPosition(true); | |
| 42 | Document document = parser.parseInput(html, ""); | |
| 43 | List<String> errors = new ArrayList<>(); | |
| 44 | for (ParseError error : parser.getErrors()) { | |
| 45 | errors.add("line/column " + error.getCursorPos() + ": " + error.getErrorMessage()); | |
| 46 | } | |
| 47 | for (Element element : document.getAllElements()) { | |
| 48 | String tag = element.normalName(); | |
| 49 | // Jsoup accepts unknown tags silently. Keep custom elements and namespaced | |
| 50 | // mail/SVG markup, but reject ordinary misspellings such as <spna>. | |
| 51 |
2
1. validate : negated conditional → KILLED 2. validate : negated conditional → KILLED |
if (!(element instanceof Document) && Parser.NamespaceHtml.equals(element.tag().namespace()) |
| 52 |
3
1. validate : negated conditional → KILLED 2. validate : negated conditional → KILLED 3. validate : negated conditional → KILLED |
&& !element.tag().isKnownTag() && !tag.contains("-") && !tag.contains(":")) { |
| 53 |
1
1. validate : negated conditional → KILLED |
if (errors.size() == MAX_ERRORS) { |
| 54 | break; | |
| 55 | } | |
| 56 | errors.add("line/column " + element.sourceRange().start().lineNumber() + ":" | |
| 57 | + element.sourceRange().start().columnNumber() + ": Unknown HTML tag <" + tag + ">"); | |
| 58 | } | |
| 59 | } | |
| 60 |
1
1. validate : negated conditional → KILLED |
if (!errors.isEmpty()) { |
| 61 | throw new IOException("Invalid HTML markup in " + source + " (up to " + MAX_ERRORS | |
| 62 | + " errors shown):\n" + String.join("\n", errors)); | |
| 63 | } | |
| 64 | } | |
| 65 | } | |
Mutations | ||
| 51 |
1.1 2.2 |
|
| 52 |
1.1 2.2 3.3 |
|
| 53 |
1.1 |
|
| 60 |
1.1 |