Skip to content
This repository has been archived by the owner on Apr 30, 2022. It is now read-only.

Commit

Permalink
Merge pull request #550 from Magenic/Accessibility_FileManagement
Browse files Browse the repository at this point in the history
Accessibility: Create JS and CSS files instead of strings
  • Loading branch information
jason-edstrom authored Oct 15, 2021
2 parents 08c2b7c + 459ffc8 commit 7b69294
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 82 deletions.
2 changes: 1 addition & 1 deletion jmaqs-accessibility/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<dependency>
<groupId>com.deque.html.axe-core</groupId>
<artifactId>selenium</artifactId>
<version>4.2.1</version>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
Expand All @@ -21,8 +23,8 @@
import java.util.List;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.DataNode;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.openqa.selenium.OutputType;
Expand All @@ -39,6 +41,8 @@ public class HtmlReporter {

private static final String classString = "class";

private static final String resourcesFile = "../jmaqs-accessibility/src/main/resources/";

protected HtmlReporter() {
}

Expand Down Expand Up @@ -193,7 +197,7 @@ private static void createAxeHtmlReportFile(SearchContext context, Results resul
modal.appendChild(modalImage);

Element script = doc.select("script").first();
script.text(getJavascriptToString());
script.appendChild(new DataNode(getJavascriptFileToString()));

FileUtils.writeStringToFile(new File(destination), doc.outerHtml(), StandardCharsets.UTF_8);
}
Expand Down Expand Up @@ -294,41 +298,6 @@ private static void getReadableAxeResults(List<Rule> results, String type, Eleme
}
}

private static String getCss(SearchContext context) {
return ".thumbnail{" + "content: url('" + getDataImageString(context)
+ "; border: 1px solid black;margin-left:1em;margin-right:1em;width:auto;max-height:150px;"
+ "} .thumbnail:hover{border:2px solid black;}"
+ ".wrap .wrapTwo .wrapThree{margin:2px;max-width:70vw;}"
+ ".wrapOne {margin-left:1em;overflow-wrap:anywhere;}"
+ ".wrapTwo {margin-left:2em;overflow-wrap:anywhere;}"
+ ".wrapThree {margin-left:3em;overflow-wrap:anywhere;}"
+ ".emOne {margin-left:1em;margin-right:1em;overflow-wrap:anywhere;}"
+ ".emTwo {margin-left:2em;overflow-wrap:anywhere;}"
+ ".emThree {margin-left:3em;overflow-wrap:anywhere;}"
+ "#modal {display: none;position: fixed;z-index: 1;left: 0;top: 0;width: 100%;"
+ "height: 100%;overflow: auto;background-color: rgba(0, 0, 0, 0.9); flex-direction: column;}"
+ "#modalclose{font-family: Lucida Console; font-size: 35px; width: auto; "
+ "color: white; text-align: right; padding: 20px;"
+ "cursor: pointer; max-height: 10%}"
+ "#modalimage {margin: auto;display: block;max-width: 95%; padding: 10px; max-height: 90%}"
+ ".htmlTable{border-top:double lightgray;width:100%;display:table;}"
+ ".sectionbutton{background-color: #000000; color: #FFFFFF; cursor: pointer; padding: 18px; width: 100%;"
+ "text-align: left; outline: none; transition: 0.4s; border: 1px solid black;}"
+ ".sectionbutton:hover {background-color: #828282;}"
+ ".buttonInfoText {width: 50%; float: left;}"
+ ".buttonExpandoText {text-align: right; width: 50%; float: right;}"
+ ".majorSection{padding: 0 18px;background-color:white; overflow:hidden;"
+ "transition: max-height 0.2s ease-out;}"
+ ".findings{margin-top: 5px; border-top:1px solid black;}"
+ ".active {background-color: #474747; margin-bottom: 0px;}"
+ ".resultWrapper {margin: 5px}" + "#context {width: 50%;}"
+ "#image {width: 50%; height: 220px;}" + "#counts {width: 100%;}"
+ "#metadata {display: flex; flex-wrap: wrap;}"
+ "#results {display: flex; flex-direction: column;}"
+ "@media only screen and (max-width: 800px) {#metadata {flex-direction: column;}"
+ "#context {width: 100%;}" + "#image {width: 100%;}";
}

private static void getContextContent(Results results, Element element) throws ParseException {
element.text("Url: " + results.getUrl());
element.appendChild(new Element("br"));
Expand Down Expand Up @@ -385,58 +354,24 @@ private static void getCountContent(int violationCount, int incompleteCount, int
}
}

private static String getCss(SearchContext context) throws IOException {
String css = new String(Files.readAllBytes(
Paths.get(resourcesFile + "htmlReporter.css")));
return css.replace("url('", "url('" + getDataImageString(context));
}

private static String getDataImageString(SearchContext context) {
TakesScreenshot newScreen = (TakesScreenshot) context;
return "data:image/png;base64," + newScreen.getScreenshotAs(OutputType.BASE64) + "');";
return "data:image/png;base64," + newScreen.getScreenshotAs(OutputType.BASE64);
}

private static String getDateFormat(String timestamp) throws ParseException {
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(timestamp);
return new SimpleDateFormat("dd-MMM-yy HH:mm:ss Z").format(date);
}

private static String getJavascriptToString() {
return StringEscapeUtils.escapeEcmaScript(
"var buttons = document.getElementsByClassName(\"sectionbutton\");\n"
+ " var i;\n"
+ "\n"
+ " for (i = 0; i < buttons.length; i++) \n"
+ " {\n"
+ " buttons[i].addEventListener(\"click\", function() \n"
+ " {\n"
+ " var expandoText = this.getElementsByClassName(\"buttonExpandoText\")[0];\n"
+ " \n"
+ " this.classList.toggle(\"active\");\n"
+ "\n"
+ " var content = this.nextElementSibling;\n"
+ " if (expandoText.innerHTML == \"-\") \n"
+ " {\n"
+ " content.style.maxHeight = 0;\n"
+ " expandoText.innerHTML = \"+\";\n"
+ " } \n"
+ " else \n"
+ " {\n"
+ " content.style.maxHeight = content.scrollHeight + \"px\";\n"
+ " expandoText.innerHTML = \"-\";\n"
+ " }\n"
+ " })\n"
+ " }\n"
+ "\n"
+ " var thumbnail = document.getElementById(\"screenshotThumbnail\");\n"
+ " var thumbnailStyle = getComputedStyle(thumbnail); \n"
+ " var modal = document.getElementById(\"modal\");\n"
+ " var modalimg = modal.getElementsByTagName(\"img\")[0]\n"
+ "\n"
+ " modal.addEventListener('click',function(){\n"
+ " modal.style.display = \"none\";\n"
+ " modalimg.style.content = \"\";\n"
+ " modalimg.alt = \"\";\n"
+ " })\n"
+ "\n"
+ " thumbnail.addEventListener('click',function(){\n"
+ " modal.style.display = \"flex\";\n"
+ " modalimg.style.content = thumbnailStyle.content;\n"
+ " modalimg.alt = thumbnail.alt;\n"
+ " })");
private static String getJavascriptFileToString() throws IOException {
return new String(Files.readAllBytes(
Paths.get(resourcesFile + "htmlReporterElements.js")));
}
}
165 changes: 165 additions & 0 deletions jmaqs-accessibility/src/main/resources/htmlReporter.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
.thumbnail {
content: url('');
border: 1px solid black;
margin-left: 1em;
margin-right: 1em;
width: auto;
max-height: 150px;
}

.thumbnail:hover {
border: 2px solid black;
}

.wrap .wrapTwo .wrapThree {
margin: 2px;
max-width: 70vw;
}

.wrapOne {
margin-left: 1em;
}

.wrapTwo {
margin-left: 2em;
}

.wrapThree {
margin-left: 3em;
}

.emOne {
margin-left: 1em;
margin-right: 1em;
}

.emTwo {
margin-left: 2em;
}

.emThree {
margin-left: 3em;
}

#modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0, 0, 0, 0.9);
flex-direction: column;
}

#modalclose {
font-family: Lucida Console;
font-size: 35px;
width: auto;
color: white;
text-align: right;
padding: 20px;
cursor: pointer;
max-height: 10%;
}

#modalimage {
margin: auto;
display: block;
max-width: 95%;
padding: 10px;
max-height: 90%;
}

.htmlTable {
border-top: double lightgray;
width: 100%;
display: table;
}

.sectionbutton {
background-color: #000000;
color: #ffffff;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
outline: none;
transition: 0.4s;
border: 1px solid black;
}

.sectionbutton:hover {
background-color: #828282;
}

.buttonInfoText {
width: 50%;
float: left;
}

.buttonExpandoText {
text-align: right;
width: 50%;
float: right;
}

.majorSection {
padding: 0 18px;
background-color: white;
overflow: hidden;
transition: max-height 0.2s ease-out;
}

.findings {
margin-top: 5px;
border-top: 1px solid black;
}

.active {
background-color: #474747;
margin-bottom: 0px;
}

.resultWrapper {
margin: 5px;
}

#context {
width: 50%;
}

#image {
width: 50%;
height: 220px;
}

#counts {
width: 100%;
}

#metadata {
display: flex;
flex-wrap: wrap;
}

#results {
display: flex;
flex-direction: column;
}

@media only screen and (max-width: 800px) {
#metadata {
flex-direction: column;
}

#context {
width: 100%;
}

#image {
width: 100%;
}
}
36 changes: 36 additions & 0 deletions jmaqs-accessibility/src/main/resources/htmlReporterElements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var buttons = document.getElementsByClassName("sectionbutton");
var i;

for (i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", function () {
var expandoText = this.getElementsByClassName("buttonExpandoText")[0];

this.classList.toggle("active");

var content = this.nextElementSibling;
if (expandoText.innerHTML === "-") {
content.style.maxHeight = 0;
expandoText.innerHTML = "+";
} else {
content.style.maxHeight = content.scrollHeight + "px";
expandoText.innerHTML = "-";
}
});
}

var thumbnail = document.getElementById("screenshotThumbnail");
var thumbnailStyle = getComputedStyle(thumbnail);
var modal = document.getElementById("modal");
var modalimg = modal.getElementsByTagName("img")[0];

modal.addEventListener("click", function () {
modal.style.display = "none";
modalimg.style.content = "";
modalimg.alt = "";
});

thumbnail.addEventListener("click", function () {
modal.style.display = "flex";
modalimg.style.content = thumbnailStyle.content;
modalimg.alt = thumbnail.alt;
});

0 comments on commit 7b69294

Please sign in to comment.