Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace most loops in web/debugger.js with for...of loops #14742

Merged
merged 1 commit into from
Apr 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 35 additions & 52 deletions web/debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ var FontInspector = (function FontInspectorClosure() {
}
const fontName = e.target.dataset.fontName;
const selects = document.getElementsByTagName("input");
for (let i = 0; i < selects.length; ++i) {
const select = selects[i];
for (const select of selects) {
if (select.dataset.fontName !== fontName) {
continue;
}
Expand All @@ -65,7 +64,7 @@ var FontInspector = (function FontInspectorClosure() {
name: "Font Inspector",
panel: null,
manager: null,
init: function init(pdfjsLib) {
init(pdfjsLib) {
const panel = this.panel;
const tmp = document.createElement("button");
tmp.addEventListener("click", resetSelection);
Expand All @@ -75,7 +74,7 @@ var FontInspector = (function FontInspectorClosure() {
fonts = document.createElement("div");
panel.appendChild(fonts);
},
cleanup: function cleanup() {
cleanup() {
fonts.textContent = "";
},
enabled: false,
Expand All @@ -93,16 +92,16 @@ var FontInspector = (function FontInspectorClosure() {
}
},
// FontInspector specific functions.
fontAdded: function fontAdded(fontObj, url) {
fontAdded(fontObj, url) {
function properties(obj, list) {
const moreInfo = document.createElement("table");
for (let i = 0; i < list.length; i++) {
for (const entry of list) {
const tr = document.createElement("tr");
const td1 = document.createElement("td");
td1.textContent = list[i];
td1.textContent = entry;
tr.appendChild(td1);
const td2 = document.createElement("td");
td2.textContent = obj[list[i]].toString();
td2.textContent = obj[entry].toString();
tr.appendChild(td2);
moreInfo.appendChild(tr);
}
Expand Down Expand Up @@ -172,7 +171,7 @@ var StepperManager = (function StepperManagerClosure() {
name: "Stepper",
panel: null,
manager: null,
init: function init(pdfjsLib) {
init(pdfjsLib) {
const self = this;
stepperControls = document.createElement("div");
stepperChooser = document.createElement("select");
Expand All @@ -192,15 +191,15 @@ var StepperManager = (function StepperManagerClosure() {
opMap[pdfjsLib.OPS[key]] = key;
}
},
cleanup: function cleanup() {
cleanup() {
stepperChooser.textContent = "";
stepperDiv.textContent = "";
steppers = [];
},
enabled: false,
active: false,
// Stepper specific functions.
create: function create(pageIndex) {
create(pageIndex) {
const debug = document.createElement("div");
debug.id = "stepper" + pageIndex;
debug.hidden = true;
Expand All @@ -218,23 +217,19 @@ var StepperManager = (function StepperManagerClosure() {
}
return stepper;
},
selectStepper: function selectStepper(pageIndex, selectPanel) {
let i;
selectStepper(pageIndex, selectPanel) {
pageIndex |= 0;
if (selectPanel) {
this.manager.selectPanel(this);
}
for (i = 0; i < steppers.length; ++i) {
const stepper = steppers[i];
for (const stepper of steppers) {
stepper.panel.hidden = stepper.pageIndex !== pageIndex;
}
const options = stepperChooser.options;
for (i = 0; i < options.length; ++i) {
const option = options[i];
for (const option of stepperChooser.options) {
option.selected = (option.value | 0) === pageIndex;
}
},
saveBreakPoints: function saveBreakPoints(pageIndex, bps) {
saveBreakPoints(pageIndex, bps) {
breakPoints[pageIndex] = bps;
sessionStorage.setItem("pdfjsBreakPoints", JSON.stringify(breakPoints));
},
Expand Down Expand Up @@ -361,8 +356,7 @@ const Stepper = (function StepperClosure() {
const charCodeRow = c("tr");
const fontCharRow = c("tr");
const unicodeRow = c("tr");
for (let j = 0; j < glyphs.length; j++) {
const glyph = glyphs[j];
for (const glyph of glyphs) {
if (typeof glyph === "object" && glyph !== null) {
charCodeRow.appendChild(c("td", glyph.originalCharCode));
fontCharRow.appendChild(c("td", glyph.fontChar));
Expand Down Expand Up @@ -410,9 +404,9 @@ const Stepper = (function StepperClosure() {
this.breakPoints.sort(function (a, b) {
return a - b;
});
for (let i = 0; i < this.breakPoints.length; i++) {
if (this.breakPoints[i] > this.currentIdx) {
return this.breakPoints[i];
for (const breakPoint of this.breakPoints) {
if (breakPoint > this.currentIdx) {
return breakPoint;
}
}
return null;
Expand Down Expand Up @@ -444,8 +438,7 @@ const Stepper = (function StepperClosure() {

goTo(idx) {
const allRows = this.panel.getElementsByClassName("line");
for (let x = 0, xx = allRows.length; x < xx; ++x) {
const row = allRows[x];
for (const row of allRows) {
if ((row.dataset.idx | 0) === idx) {
row.style.backgroundColor = "rgb(251,250,207)";
row.scrollIntoView();
Expand All @@ -465,8 +458,8 @@ var Stats = (function Stats() {
node.textContent = ""; // Remove any `node` contents from the DOM.
}
function getStatIndex(pageNumber) {
for (let i = 0, ii = stats.length; i < ii; ++i) {
if (stats[i].pageNumber === pageNumber) {
for (const [i, stat] of stats.entries()) {
if (stat.pageNumber === pageNumber) {
return i;
}
}
Expand Down Expand Up @@ -505,8 +498,8 @@ var Stats = (function Stats() {
return a.pageNumber - b.pageNumber;
});
clear(this.panel);
for (let i = 0, ii = stats.length; i < ii; ++i) {
this.panel.appendChild(stats[i].div);
for (const entry of stats) {
this.panel.appendChild(entry.div);
}
},
cleanup() {
Expand All @@ -527,8 +520,7 @@ window.PDFBug = (function PDFBugClosure() {
enable(ids) {
const all = ids.length === 1 && ids[0] === "all";
const tools = this.tools;
for (let i = 0; i < tools.length; ++i) {
const tool = tools[i];
for (const tool of tools) {
if (all || ids.includes(tool.id)) {
tool.enabled = true;
}
Expand Down Expand Up @@ -570,22 +562,14 @@ window.PDFBug = (function PDFBugClosure() {
container.style.right = panelWidth + "px";

// Initialize all the debugging tools.
const tools = this.tools;
const self = this;
for (let i = 0; i < tools.length; ++i) {
const tool = tools[i];
for (const [i, tool] of this.tools.entries()) {
const panel = document.createElement("div");
const panelButton = document.createElement("button");
panelButton.textContent = tool.name;
panelButton.addEventListener(
"click",
(function (selected) {
return function (event) {
event.preventDefault();
self.selectPanel(selected);
};
})(i)
);
panelButton.addEventListener("click", event => {
event.preventDefault();
this.selectPanel(i);
});
controls.appendChild(panelButton);
panels.appendChild(panel);
tool.panel = panel;
Expand All @@ -602,9 +586,9 @@ window.PDFBug = (function PDFBugClosure() {
this.selectPanel(0);
},
cleanup() {
for (let i = 0, ii = this.tools.length; i < ii; i++) {
if (this.tools[i].enabled) {
this.tools[i].cleanup();
for (const tool of this.tools) {
if (tool.enabled) {
tool.cleanup();
}
}
},
Expand All @@ -616,12 +600,11 @@ window.PDFBug = (function PDFBugClosure() {
return;
}
activePanel = index;
const tools = this.tools;
for (let j = 0; j < tools.length; ++j) {
for (const [j, tool] of this.tools.entries()) {
const isActive = j === index;
buttons[j].classList.toggle("active", isActive);
tools[j].active = isActive;
tools[j].panel.hidden = !isActive;
tool.active = isActive;
tool.panel.hidden = !isActive;
}
},
};
Expand Down