-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
141 lines (123 loc) · 5.99 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!DOCTYPE html>
<html lang="en-US" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://ogp.me/ns/fb"
class="en js csstransforms csstransforms3d csstransitions">
<head>
<meta http-equiv="body-Type" body="text/html; charset=UTF-8">
<title> Custom Element - Accessibility Checker</title>
<!-- Kentico Custom elements API-->
<link rel="stylesheet" href="custom-element.css" />
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<script src="https://app.kontent.ai/js-api/custom-element/v1/custom-element.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.7/semantic.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/rxjs@6.4.0/bundles/rxjs.umd.min.js"></script>
<script type="text/javascript"
src="https://cdn.jsdelivr.net/npm/@kontent-ai/delivery-sdk@latest/dist/bundles/kontent-delivery.umd.min.js"></script>
<style></style>
</head>
<body>
<br />
<div class="ui container">
<div class="ui grid">
<div class="ui sixteen column">
<div id="body" style="display:none">No body found</div>
<p id="result"> </p>
</div>
</div>
</div>
<script>
var h1included = false;
$(document).ready(function () {
function processData(response) {
const text = response.data.item.elements.content.value;
document.getElementById('body').innerHTML = text;
document.getElementById('result').innerHTML = checkheadings();
CustomElement.setHeight(300);
}
CustomElement.init((element, _context) => {
config = element.config || {};
elementName = config.fieldname;
h1included = config.h1included;
previewApiKey = config.previewApiKey;
const Kk = window['kontentDelivery'];
const deliveryClient = new Kk.DeliveryClient({
environmentId: _context.projectId,
previewApiKey: previewApiKey,
defaultQueryConfig: {
usePreviewMode: true
}
});
// Retrieves the landing page from your project
deliveryClient
.item(_context.item.codename)
.languageParameter(_context.variant.codename)
.elementsParameter(["content"])
.toPromise()
.then(response => {
processData(response)
});
CustomElement.observeElementChanges(["content"], (changedElementCodenames) => {
setTimeout(function () {
deliveryClient
.item(_context.item.codename)
.languageParameter(_context.variant.codename)
.elementsParameter(["content"])
.toPromise()
.then(response => {
processData(response)
});
}, 3000);
});
});
});
function getHeadingLevel(heading) {
return parseInt(heading.tagName[1]);
}
function createError(expectedHeadingLevel, previousHeadingLevel, previousHeadingText, currentHeadingLevel, currentHeadingText) {
var err = {};
err.previousHeadingLevel = (previousHeadingLevel == 0) ? "no previous headings" : previousHeadingLevel;
err.seenHeadingLevel = currentHeadingLevel;
err.expectedHeadingLevel = expectedHeadingLevel;
err.seenHeadingText = currentHeadingText;
err.previousHeadingText = previousHeadingText;
return err;
}
function checkheadings() {
var cont = document.querySelector('#body');
var headings = cont.querySelectorAll("h1, h2, h3, h4, h5, h6");
var errors = [];
var previousHeadingLevel = h1included ? 1 : 0;
var previousHeadingText = "No headings yet";
for (x = 0; x < headings.length; x++) {
var currentHeadingLevel = getHeadingLevel(headings[x]);
var currentHeadingText = headings[x].textbody;
var expectedHeadingLevel = previousHeadingLevel + 1;
if (currentHeadingLevel > expectedHeadingLevel) {
errors.push(createError(expectedHeadingLevel, previousHeadingLevel, previousHeadingText, currentHeadingLevel, currentHeadingText));
continue;
}
previousHeadingLevel = currentHeadingLevel;
previousHeadingText = currentHeadingText;
}
result = "<strong>" + errors.length + " accessibility issues found:</strong></br>"
errors.forEach((error, i) => result +=
"Error number " + (i + 1) + "</br>" +
"Error on heading : <i>H" + error.seenHeadingLevel + " " + error.seenHeadingText + "</i></br>" +
"Previous heading: <i>H" + error.previousHeadingLevel + " " + error.seenHeadingText + "</i></br>" +
"Expected heading level: <i>H" + error.expectedHeadingLevel + "</i></br></br>"
);
if (errors.length == 0) {
result = "No accessibility issues found"
CustomElement.setValue("Validated - No headings issues");
}
else {
CustomElement.setValue(null);
}
return result;
}
</script>
</body>
</html>