Skip to content

Commit

Permalink
Feat/add_passed_items (#366)
Browse files Browse the repository at this point in the history
* edit logic and add function to create passed items
  • Loading branch information
LeeYiXuan21 authored Jun 18, 2024
1 parent d96a935 commit 8581e0c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 20 deletions.
24 changes: 10 additions & 14 deletions src/mergeAxeResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type PageInfo = {
url?: string;
pageImagePath?: string;
pageIndex?: number;
metadata: string;
};

type RuleInfo = {
Expand Down Expand Up @@ -82,6 +83,7 @@ type AllIssues = {

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
let allIssues;

const extractFileNames = async (directory: string): Promise<string[]> =>
fs
Expand Down Expand Up @@ -221,31 +223,27 @@ const base64Encode = data => {
}
};

const writeQueryString = async (allIssues, storagePath, htmlFilename = 'report.html') => {
// Spread the data
const writeBase64 = async (allIssues, storagePath, htmlFilename = 'report.html') => {

const { items, ...rest } = allIssues;

// Encode the data
const encodedScanItems = base64Encode(items);
const encodedScanData = base64Encode(rest);

// Path to the file where the encoded data will be saved
const filePath = path.join(storagePath, 'reports', 'reportScanData.csv');
const filePath = path.join(storagePath, 'reports', 'scanDetails.csv');

// Ensure directory existence
const directoryPath = path.dirname(filePath);
if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath, { recursive: true });
}

// Write the encoded scan data to the file
await fs.promises.writeFile(filePath, `scanData_base64,scanItems_base64\n${encodedScanData},${encodedScanItems}`);

// Read the existing HTML file
const htmlFilePath = path.join(storagePath, 'reports', htmlFilename);
let htmlContent = fs.readFileSync(htmlFilePath, 'utf8');

// Find the position to insert the script tag in the head section
const allIssuesJson = JSON.stringify(allIssues);

const headIndex = htmlContent.indexOf('</head>');
const injectScript = `
<script>
Expand All @@ -260,19 +258,16 @@ const writeQueryString = async (allIssues, storagePath, htmlFilename = 'report.h
// Decode the encoded data
scanData = base64Decode('${encodedScanData}');
scanItems = base64Decode('${encodedScanItems}');
allIssues = ${allIssuesJson};
</script>
`;

if (headIndex !== -1) {
// If </head> tag is found, insert the script tag before it
htmlContent = htmlContent.slice(0, headIndex) + injectScript + htmlContent.slice(headIndex);
} else {
// If </head> tag is not found, append the script tag at the end of the file
htmlContent += injectScript;
}

// Write the updated HTML content back to the file
fs.writeFileSync(htmlFilePath, htmlContent);
};

Expand Down Expand Up @@ -689,8 +684,9 @@ export const generateArtifacts = async (

await writeCsv(allIssues, storagePath);
await writeHTML(allIssues, storagePath);
await writeBase64(allIssues, storagePath);
await writeSummaryHTML(allIssues, storagePath);
await writeQueryString(allIssues, storagePath);
await retryFunction(() => writeSummaryPdf(storagePath), 1);
return createRuleIdJson(allIssues);
};

6 changes: 3 additions & 3 deletions src/static/ejs/partials/components/scanAbout.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
id="pagesScannedModalToggle"
data-bs-toggle="modal"
data-bs-target="#pagesScannedModal"
>NA
><span id="pagesScannedModalToggletxt">NA</span>
<% if (totalPagesNotScanned > 0) { %>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none">
<path d="M12 19.875C9.91142 19.875 7.90838 19.0453 6.43153 17.5685C4.95468 16.0916 4.125 14.0886 4.125 12C4.125 9.91142 4.95468 7.90838 6.43153 6.43153C7.90838 4.95468 9.91142 4.125 12 4.125C14.0886 4.125 16.0916 4.95468 17.5685 6.43153C19.0453 7.90838 19.875 9.91142 19.875 12C19.875 14.0886 19.0453 16.0916 17.5685 17.5685C16.0916 19.0453 14.0886 19.875 12 19.875ZM12 21C14.3869 21 16.6761 20.0518 18.364 18.364C20.0518 16.6761 21 14.3869 21 12C21 9.61305 20.0518 7.32387 18.364 5.63604C16.6761 3.94821 14.3869 3 12 3C9.61305 3 7.32387 3.94821 5.63604 5.63604C3.94821 7.32387 3 9.61305 3 12C3 14.3869 3.94821 16.6761 5.63604 18.364C7.32387 20.0518 9.61305 21 12 21Z" fill="#D7260F"/>
Expand Down Expand Up @@ -196,8 +196,8 @@
</clipPath>
</defs>
</svg>
<span id="items">
NA
<span>
<span id="items">NA</span>
<svg
tabindex="-1"
id="passedItemsTooltip"
Expand Down
49 changes: 46 additions & 3 deletions src/static/ejs/report.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,15 @@
document.getElementById('urlScanned').innerHTML = scanData.urlScanned;
document.getElementById('urlScanned').href = scanData.urlScanned;
document.getElementById('viewport').innerHTML = scanData.viewport.startsWith("CustomWidth") ? `${scanData.viewport.split("_")[1]} width viewport` : scanData.viewport + ' viewport';
document.getElementById('pagesScannedModalToggle').innerHTML = scanData.isCustomFlow ? scanData.customFlowLabel + ' (' + scanData.totalPagesScanned + ' ' + (scanData.totalPagesScanned === 1 ? 'page' : 'pages') + ')' : scanData.scanType + ' crawl' + ' (' + scanData.totalPagesScanned + ' ' + (scanData.totalPagesScanned === 1 ? 'page' : 'pages') + ')';
document.getElementById('pagesScannedModalToggletxt').innerHTML = scanData.isCustomFlow ? scanData.customFlowLabel + ' (' + scanData.totalPagesScanned + ' ' + (scanData.totalPagesScanned === 1 ? 'page' : 'pages') + ')' : scanData.scanType + ' crawl' + ' (' + scanData.totalPagesScanned + ' ' + (scanData.totalPagesScanned === 1 ? 'page' : 'pages') + ')';
var itemsElement = document.getElementById('items');
var failedItems = scanItems.mustFix.totalItems + scanItems.goodToFix.totalItems;
var passedItems = scanItems.passed.totalItems;
var itemsContent = `${failedItems} ${failedItems === 1 ? 'occurrence' : 'occurrences'} failed,<br>${passedItems} ${passedItems === 1 ? 'occurrence' : 'occurrences'} passed`;
var itemsContent = `${failedItems} ${failedItems === 1 ? 'occurrence' : 'occurrences'} failed,<br>
<a href="#" id="createPassedItemsFile">${passedItems} ${passedItems === 1 ? 'occurrence' : 'occurrences'} passed</a>`;
itemsElement.innerHTML = itemsContent;
var phAppVersionElement = document.getElementById('phAppVersion');
var versionContent = 'Purple A11y Version ' + scanData.phAppVersion;
phAppVersionElement.innerHTML = versionContent;
Expand Down Expand Up @@ -196,6 +197,48 @@
document.getElementById('pagesScannedModalLabel').innerHTML = scanData.isCustomFlow ? scanData.customFlowLabel + ' (' + scanData.totalPagesScanned + ' ' + (scanData.totalPagesScanned === 1 ? 'page' : 'pages') + ')' : scanData.scanType + ' crawl' + ' (' + scanData.totalPagesScanned + ' ' + (scanData.totalPagesScanned === 1 ? 'page' : 'pages') + ')';
const createPassedItemsFile = async () => {
const passedItemsJson = {};
scanItems.passed.rules.forEach(r => {
passedItemsJson[r.description] = {
totalOccurrencesInScan: r.totalItems,
totalPages: r.pagesAffected.length,
pages: r.pagesAffected.map(p => ({
pageTitle: p.pageTitle,
url: p.url,
totalOccurrencesInPage: p.items.length,
occurrences: p.items,
metadata: p.metadata
})),
};
});
const jsonString = JSON.stringify(passedItemsJson, null, 4);
const blob = new Blob([jsonString], { type: "application/json" });
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
storagePath = scanData.storagePath;
link.download = `passed_items.json.txt`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
document.getElementById('createPassedItemsFile').addEventListener('click', async (e) => {
e.preventDefault();
await createPassedItemsFile();
console.log('click');
});
};
Expand Down

0 comments on commit 8581e0c

Please sign in to comment.