Skip to content

Commit

Permalink
Merge pull request #1837 from Fdawgs/chore/tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs authored Apr 15, 2024
2 parents cd4e980 + 369dc0a commit 56fe808
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/config/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ describe("Configuration", () => {
HTTPS_SSL_KEY_PATH: "./test_resources/test_ssl_cert/error.key",
},
},
])("Throws error if $testName", async ({ envVariables }) => {
])("Throws an error if $testName", async ({ envVariables }) => {
const HOST = "0.0.0.0";
const PORT = 443;
const HTTPS_SSL_KEY_PATH = envVariables.HTTPS_SSL_KEY_PATH || "";
Expand Down
1 change: 0 additions & 1 deletion src/plugins/docx-to-html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ async function plugin(server) {
/**
* Mammoth does not wrap the results inside <html> and <body> tags itself, so
* do that here.
*
* `fixUtf8` function replaces most common incorrectly converted
* Windows-1252 to UTF-8 results with HTML equivalents.
* @see {@link https://i18nqa.com/debug/utf8-debug.html | UTF-8 Encoding Debugging Chart}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/embed-html-images/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe("Embed-HTML-Images plugin", () => {
expect(response.statusCode).toBe(200);
});

it("Throws error if it cannot find images to embed in specified directory", async () => {
it("Throws an error if it cannot find images to embed in specified directory", async () => {
server.post("/", async (req) => server.embedHtmlImages(req.body));
await server.register(plugin, config.poppler).ready();

Expand Down
7 changes: 7 additions & 0 deletions src/plugins/embed-html-images/plugin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ exports[`Embed-HTML-Images plugin Embeds images into HTML 1`] = `
.ft12{font-size:14px;font-family:Times;color:#000000;}
.ft13{font-size:14px;font-family:Times;color:#0000ff;}
.ft14{font-size:14px;line-height:19px;font-family:Times;color:#000000;}
@media print {
.ft11 {font-size: 12px}
.ft12 {font-size: 12px}
.ft13 {font-size: 12px}
.ft14 {font-size: 12px}
}
-->
/*]]>*/
</style>
Expand Down
20 changes: 9 additions & 11 deletions src/plugins/image-to-txt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,15 @@ async function plugin(server, options) {

// Procedurally create workers based on number passed
await Promise.all(
Array(options.workers)
.fill(0)
.map(async () => {
const worker = await createWorker(
options.languages,
1,
workerConfig
);
await worker.setParameters(workerParams);
return scheduler.addWorker(worker);
})
Array.from({ length: options.workers }, async () => {
const worker = await createWorker(
options.languages,
1,
workerConfig
);
await worker.setParameters(workerParams);
return scheduler.addWorker(worker);
})
);

server.log.info(
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/pdf-to-html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ async function plugin(server, options) {
* Overwrite content of remaining title element with temp file id,
* as Poppler reveals directory structure in title.
*/
titles[0].innerHTML = id;
titles[0].textContent = id;

/**
* `fixUtf8` function replaces most common incorrectly converted
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/pdf-to-txt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ async function plugin(server, options) {

try {
await poppler.pdfToCairo(req.body, tempFile, {
resolutionXYAxis: 300,
grayscaleFile: true,
pngFile: true,
resolutionXYAxis: 300,
...query,
});
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/rtf-to-html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ async function plugin(server, options) {
*/
req.conversionResults.docLocation = {
directory,
rtf: tempFile,
id,
rtf: tempFile,
};

try {
Expand All @@ -119,7 +119,7 @@ async function plugin(server, options) {
meta.content = "text/html; charset=utf-8";
meta.httpEquiv = "content-type";
const title = dom.window.document.createElement("title");
title.innerHTML = id;
title.textContent = id;
dom.window.document.head.prepend(meta, title);

/**
Expand Down
17 changes: 10 additions & 7 deletions src/plugins/tidy-css/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function plugin(server) {
// Create style element inside head if none already exist
if (styles.length === 0 && (newFonts || newBackgroundColor)) {
const element = dom.window.document.createElement("style");
element.innerHTML = "div {}";
element.textContent = "div {}";
dom.window.document.head.append(element);

styles = dom.window.document.querySelectorAll("style");
Expand All @@ -49,12 +49,13 @@ async function plugin(server) {
// Combine style elements into single element
const combinedStyle = dom.window.document.createElement("style");
styles.forEach((style) => {
const element = style;
combinedStyle.innerHTML += element.innerHTML;
element.remove();
// @ts-ignore: textContent will never be null
combinedStyle.textContent += style.textContent;
style.remove();
});

const styleObj = cssomParse(combinedStyle.innerHTML);
// @ts-ignore: textContent will never be null
const styleObj = cssomParse(combinedStyle.textContent);

styleObj.cssRules.forEach((rule) => {
if (rule instanceof CSSStyleRule) {
Expand Down Expand Up @@ -113,10 +114,12 @@ async function plugin(server) {
* Minifies output whilst also removing HTML comment tags
* wrapping CSS, and redundant semi-colons, generated by Poppler.
*/
combinedStyle.innerHTML = cssCleaner.minify(styleObj.toString()).styles;
combinedStyle.textContent = cssCleaner.minify(
styleObj.toString()
).styles;

// Stop empty <style> element being added
if (combinedStyle.innerHTML !== "") {
if (combinedStyle.textContent !== "") {
dom.window.document.head.append(combinedStyle);
}

Expand Down
10 changes: 5 additions & 5 deletions src/plugins/tidy-css/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ describe("Tidy-CSS plugin", () => {
// Check CSS is combined into one style tag
expect(dom.window.document.querySelectorAll("style")).toHaveLength(1);
// Check font-family is set to expected value
expect(style?.innerHTML).toMatch(expected?.fonts || /./u);
expect(style?.textContent).toMatch(expected?.fonts || /./u);
// Check background-color is set to expected value
expect(style?.innerHTML).toMatch(expected?.backgroundColor || /./u);
expect(style?.textContent).toMatch(expected?.backgroundColor || /./u);
// Check page-break-inside is set to avoid
expect(style?.innerHTML).toMatch(/page-break-inside:avoid/u);
expect(style?.textContent).toMatch(/page-break-inside:avoid/u);
// Check CSS is tidied and minified
expect(style?.innerHTML).not.toMatch(/;\}|<!--|--!?>|\n|\r/u);
expect(style?.textContent).not.toMatch(/;\}|<!--|--!?>|\n|\r/u);
expect(response.statusCode).toBe(200);
});

Expand Down Expand Up @@ -146,7 +146,7 @@ describe("Tidy-CSS plugin", () => {
// Check CSS is combined into one style tag
expect(dom.window.document.querySelectorAll("style")).toHaveLength(1);
// Check CSS is tidied and minified
expect(style?.innerHTML).not.toMatch(/;\}|<!--|--!?>|\n|\r/u);
expect(style?.textContent).not.toMatch(/;\}|<!--|--!?>|\n|\r/u);
expect(response.statusCode).toBe(200);
});

Expand Down
10 changes: 5 additions & 5 deletions src/plugins/tidy-css/plugin.test.js.snap

Large diffs are not rendered by default.

27 changes: 17 additions & 10 deletions src/plugins/tidy-html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,33 +99,40 @@ async function plugin(server) {
const { document } = hidden.window;
const styles = document.querySelectorAll("style");

/** @type {string[]} */
const selectorsToRemove = [];

styles.forEach((style) => {
const styleElement = style;
const styleObj = cssomParse(styleElement.innerHTML);
// @ts-ignore: textContent will never be null
const styleObj = cssomParse(styleElement.textContent);
const cssRulesLength = styleObj.cssRules.length;

for (let i = 0; i < cssRulesLength; i += 1) {
// Iterate over CSS rules in reverse to avoid index issues
for (let i = cssRulesLength - 1; i >= 0; i -= 1) {
const rule = styleObj.cssRules[i];
if (rule instanceof CSSStyleRule) {
if (
rule.style.display === "none" ||
rule.style.visibility === "hidden"
) {
document
.querySelectorAll(rule.selectorText)
.forEach((element) => {
element.parentNode.removeChild(element);
});
selectorsToRemove.push(rule.selectorText);
// Remove rule from style tag
styleObj.deleteRule(i);
// Decrement the counter as the length of rules has changed
i -= 1;
}
}
}

styleElement.innerHTML = styleObj.toString();
styleElement.textContent = styleObj.toString();
});

// Remove all elements that match the selectors
selectorsToRemove.forEach((selector) => {
document.querySelectorAll(selector).forEach((element) => {
element.remove();
});
});

result = hidden.serialize();
}

Expand Down
31 changes: 30 additions & 1 deletion src/plugins/tidy-html/plugin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ exports[`Tidy-HTML plugin Tidies HTML 1`] = `
.ft12{font-size:14px;font-family:Times;color:#000000;}
.ft13{font-size:14px;font-family:Times;color:#0000ff;}
.ft14{font-size:14px;line-height:19px;font-family:Times;color:#000000;}
@media print {
.ft11 {font-size: 12px}
.ft12 {font-size: 12px}
.ft13 {font-size: 12px}
.ft14 {font-size: 12px}
}
-->
/*]]>*/</style><style type="text/css">/*<![CDATA[*/
Expand Down Expand Up @@ -464,7 +471,8 @@ exports[`Tidy-HTML plugin Tidies HTML and removes elements with \`display: none\
.ft11 {font-size: 14px; font-family: Times; color: #000000;}
.ft12 {font-size: 14px; font-family: Times; color: #000000;}
.ft13 {font-size: 14px; font-family: Times; color: #0000ff;}
.ft14 {font-size: 14px; line-height: 19px; font-family: Times; color: #000000;}</style><style type="text/css"><!--
.ft14 {font-size: 14px; line-height: 19px; font-family: Times; color: #000000;}
@media print {.ft11 {font-size: 12px;}.ft12 {font-size: 12px;}.ft13 {font-size: 12px;}.ft14 {font-size: 12px;}}</style><style type="text/css"><!--
p {margin: 0; padding: 0;}
.ft20 {font-size: 14px; font-family: Times; color: #000000;}
.ft21 {font-size: 12px; font-family: Times; color: #000000;}
Expand Down Expand Up @@ -553,6 +561,13 @@ exports[`Tidy-HTML plugin Tidies HTML and sets img alt attributes to empty strin
.ft12{font-size:14px;font-family:Times;color:#000000;}
.ft13{font-size:14px;font-family:Times;color:#0000ff;}
.ft14{font-size:14px;line-height:19px;font-family:Times;color:#000000;}
@media print {
.ft11 {font-size: 12px}
.ft12 {font-size: 12px}
.ft13 {font-size: 12px}
.ft14 {font-size: 12px}
}
-->
/*]]>*/</style><style type="text/css">/*<![CDATA[*/
Expand Down Expand Up @@ -1010,6 +1025,13 @@ exports[`Tidy-HTML plugin Tidies HTML and sets language 1`] = `
.ft12{font-size:14px;font-family:Times;color:#000000;}
.ft13{font-size:14px;font-family:Times;color:#0000ff;}
.ft14{font-size:14px;line-height:19px;font-family:Times;color:#000000;}
@media print {
.ft11 {font-size: 12px}
.ft12 {font-size: 12px}
.ft13 {font-size: 12px}
.ft14 {font-size: 12px}
}
-->
/*]]>*/</style><style type="text/css">/*<![CDATA[*/
Expand Down Expand Up @@ -1467,6 +1489,13 @@ exports[`Tidy-HTML plugin Tidies HTML, sets img alt attributes to empty string,
.ft12{font-size:14px;font-family:Times;color:#000000;}
.ft13{font-size:14px;font-family:Times;color:#0000ff;}
.ft14{font-size:14px;line-height:19px;font-family:Times;color:#000000;}
@media print {
.ft11 {font-size: 12px}
.ft12 {font-size: 12px}
.ft13 {font-size: 12px}
.ft14 {font-size: 12px}
}
-->
/*]]>*/</style><style type="text/css">/*<![CDATA[*/
Expand Down
6 changes: 3 additions & 3 deletions src/utils/core-count/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ function coreCount() {

switch (platform()) {
case "darwin":
result = parseInt(
result = Number.parseInt(
execSync("sysctl -n hw.physicalcpu_max", config),
10
);
break;
case "linux":
result = parseInt(
result = Number.parseInt(
execSync(
'lscpu -p | egrep -v "^#" | sort -u -t, -k 2,4 | wc -l',
config
Expand All @@ -35,7 +35,7 @@ function coreCount() {
case "win32":
result = execSync("WMIC CPU Get NumberOfCores", config)
.split(EOL)
.map((line) => parseInt(line, 10))
.map((line) => Number.parseInt(line, 10))
.filter((value) => !Number.isNaN(Number(value)))
.reduce((sum, number) => sum + number, 0);
break;
Expand Down
7 changes: 7 additions & 0 deletions test_resources/test_files/html_valid_bullet_issues.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
.ft12{font-size:14px;font-family:Times;color:#000000;}
.ft13{font-size:14px;font-family:Times;color:#0000ff;}
.ft14{font-size:14px;line-height:19px;font-family:Times;color:#000000;}

@media print {
.ft11 {font-size: 12px}
.ft12 {font-size: 12px}
.ft13 {font-size: 12px}
.ft14 {font-size: 12px}
}
-->
/*]]>*/
</style>
Expand Down

0 comments on commit 56fe808

Please sign in to comment.