Skip to content

Commit

Permalink
Refactor and protect for..in loops (#209)
Browse files Browse the repository at this point in the history
Co-authored-by: Ben Roberts <ben.roberts.extern@joyn.de>
  • Loading branch information
palemieux and Ben Roberts authored Sep 13, 2021
1 parent ebeb24b commit 4ec7fd6
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 49 deletions.
43 changes: 29 additions & 14 deletions src/main/js/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@

for (var sid in estack[0].styles) {

if (! estack[0].styles.hasOwnProperty(sid)) continue;

mergeChainedStyles(estack[0], estack[0].styles[sid], errorHandler);

}
Expand Down Expand Up @@ -362,6 +364,8 @@
ini.initFromNode(node, errorHandler);

for (var qn in ini.styleAttrs) {

if (! ini.styleAttrs.hasOwnProperty(qn)) continue;

doc.head.styling.initials[qn] = ini.styleAttrs[qn];

Expand Down Expand Up @@ -608,9 +612,10 @@

for (var i in doc.head.layout.regions) {

hasRegions = true;

break;
if (doc.head.layout.regions.hasOwnProperty(i)) {
hasRegions = true;
break;
}

}

Expand All @@ -628,6 +633,8 @@

for (var region_i in doc.head.layout.regions) {

if (! doc.head.layout.regions.hasOwnProperty(region_i)) continue;

resolveTiming(doc, doc.head.layout.regions[region_i], null, null);

}
Expand Down Expand Up @@ -713,21 +720,25 @@

var s = null;

for (var set_i in element.sets) {
if ("sets" in element) {

for (var set_i = 0; set_i < element.sets.length; set_i++) {

resolveTiming(doc, element.sets[set_i], s, element);
resolveTiming(doc, element.sets[set_i], s, element);

if (element.timeContainer === "seq") {
if (element.timeContainer === "seq") {

implicit_end = element.sets[set_i].end;
implicit_end = element.sets[set_i].end;

} else {
} else {

implicit_end = Math.max(implicit_end, element.sets[set_i].end);
implicit_end = Math.max(implicit_end, element.sets[set_i].end);

}
}

s = element.sets[set_i];
s = element.sets[set_i];

}

}

Expand All @@ -749,9 +760,9 @@

}

} else {

for (var content_i in element.contents) {
} else if ("contents" in element) {
for (var content_i = 0; content_i < element.contents.length; content_i++) {

resolveTiming(doc, element.contents[content_i], s, element);

Expand Down Expand Up @@ -1270,6 +1281,8 @@

for (var qname in styles) {

if (! styles.hasOwnProperty(qname)) continue;

if (this.qname) {

reportError(errorHandler, "More than one style specified on set");
Expand Down Expand Up @@ -1748,6 +1761,8 @@

for (var sname in from_styles) {

if (! from_styles.hasOwnProperty(sname)) continue;

if (sname in into_styles)
continue;

Expand Down
30 changes: 20 additions & 10 deletions src/main/js/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,13 @@

element.appendChild(rootcontainer);

for (var i in isd.contents) {
if ("contents" in isd) {

processElement(context, rootcontainer, isd.contents[i], isd);
for (var i = 0; i < isd.contents.length; i++) {

processElement(context, rootcontainer, isd.contents[i], isd);

}

}

Expand Down Expand Up @@ -297,7 +301,7 @@

/* tranform TTML styles to CSS styles */

for (var i in STYLING_MAP_DEFS) {
for (var i = 0; i < STYLING_MAP_DEFS.length; i++) {

var sm = STYLING_MAP_DEFS[i];

Expand Down Expand Up @@ -447,9 +451,13 @@

/* process the children of the ISD element */

for (var k in isd_element.contents) {
if ("contents" in isd_element) {

for (var k = 0; k < isd_element.contents.length; k++) {

processElement(context, proc_e, isd_element.contents[k], isd_element);
processElement(context, proc_e, isd_element.contents[k], isd_element);

}

}

Expand Down Expand Up @@ -681,7 +689,9 @@

function applyLinePadding(lineList, lp, context) {

for (var i in lineList) {
if (lineList === null) return;

for (var i = 0; i < lineList.length; i++) {

var l = lineList[i].elements.length;

Expand Down Expand Up @@ -927,7 +937,7 @@

/* copy specified style properties from the sibling ruby container */

for(var k = 0; k < sib.style.length; k++) {
for (var k = 0; k < sib.style.length; k++) {

ruby.style.setProperty(sib.style.item(k), sib.style.getPropertyValue(sib.style.item(k)));

Expand Down Expand Up @@ -1379,7 +1389,7 @@

/* per IMSC1 */

for (var i in attr) {
for (var i = 0; i < attr.length; i++) {

if (attr[i] === "monospaceSerif") {

Expand Down Expand Up @@ -1673,7 +1683,7 @@

if (attr !== "none") {

for (var i in attr) {
for (var i = 0; i < attr.length; i++) {


s.push(attr[i].x_off.toUsedLength(context.w, context.h) + "px " +
Expand Down Expand Up @@ -1805,7 +1815,7 @@

var STYLMAP_BY_QNAME = {};

for (var i in STYLING_MAP_DEFS) {
for (var i = 0; i < STYLING_MAP_DEFS.length; i++) {

STYLMAP_BY_QNAME[STYLING_MAP_DEFS[i].qname] = STYLING_MAP_DEFS[i];
}
Expand Down
46 changes: 31 additions & 15 deletions src/main/js/isd.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,15 @@

/* apply set (animation) styling */

for (var i in elem.sets) {
if ("sets" in elem) {
for (var i = 0; i < elem.sets.length; i++) {

if (offset < elem.sets[i].begin || offset >= elem.sets[i].end)
continue;
if (offset < elem.sets[i].begin || offset >= elem.sets[i].end)
continue;

isd_element.styleAttrs[elem.sets[i].qname] = elem.sets[i].value;
isd_element.styleAttrs[elem.sets[i].qname] = elem.sets[i].value;

}
}

/*
Expand All @@ -215,6 +217,8 @@

for (var qname in isd_element.styleAttrs) {

if (! isd_element.styleAttrs.hasOwnProperty(qname)) continue;

spec_attr[qname] = true;

/* special rule for tts:writingMode (section 7.29.1 of XSL)
Expand Down Expand Up @@ -245,7 +249,7 @@

if (parent !== null) {

for (var j in imscStyles.all) {
for (var j = 0; j < imscStyles.all.length; j++) {

var sa = imscStyles.all[j];

Expand Down Expand Up @@ -344,8 +348,8 @@

/* initial value styling */

for (var k in imscStyles.all) {

for (var k = 0; k < imscStyles.all.length; k++) {
var ivs = imscStyles.all[k];

/* skip if value is already specified */
Expand Down Expand Up @@ -401,8 +405,8 @@
/* compute styles (only for non-inherited styles) */
/* TODO: get rid of spec_attr */

for (var z in imscStyles.all) {

for (var z = 0; z < imscStyles.all.length; z++) {
var cs = imscStyles.all[z];

if (!(cs.qname in spec_attr)) continue;
Expand Down Expand Up @@ -447,7 +451,7 @@

/* process contents of the element */

var contents;
var contents = null;

if (parent === null) {

Expand All @@ -473,7 +477,7 @@

}

for (var x in contents) {
for (var x = 0; contents !== null && x < contents.length; x++) {

var c = isdProcessContentElement(doc, offset, region, body, isd_element, associated_region_id, contents[x], errorHandler, context);

Expand All @@ -493,6 +497,7 @@
/* remove styles that are not applicable */

for (var qnameb in isd_element.styleAttrs) {
if (!isd_element.styleAttrs.hasOwnProperty(qnameb)) continue;

/* true if not applicable */

Expand Down Expand Up @@ -529,7 +534,12 @@
if (! na) {

var da = imscStyles.byQName[qnameb];
na = da.applies.indexOf(isd_element.kind) === -1;

if ("applies" in da){

na = da.applies.indexOf(isd_element.kind) === -1;

}

}

Expand Down Expand Up @@ -598,7 +608,7 @@

var element;

for(var i = 0; i < elist.length;) {
for (var i = 0; i < elist.length;) {

element = elist[i];

Expand Down Expand Up @@ -629,7 +639,7 @@

/* remove trailing LWSPs */

for(i = 0; i < elist.length; i++) {
for (i = 0; i < elist.length; i++) {

element = elist[i];

Expand All @@ -652,7 +662,11 @@

function constructSpanList(element, elist) {

for (var i in element.contents) {
if (! ("contents" in element)) {
return;
}

for (var i = 0; i < element.contents.length; i++) {

var child = element.contents[i];
var ruby = child.styleAttrs[imscStyles.byName.ruby.qname];
Expand Down Expand Up @@ -733,6 +747,8 @@

for (var sname in ttelem.styleAttrs) {

if (! ttelem.styleAttrs.hasOwnProperty(sname)) continue;

this.styleAttrs[sname] =
ttelem.styleAttrs[sname];
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/js/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
var ffs = str.split(",");
var rslt = [];

for (var i in ffs) {
for (var i = 0; i < ffs.length; i++) {

if (ffs[i].charAt(0) !== "'" && ffs[i].charAt(0) !== '"') {

Expand Down Expand Up @@ -460,7 +460,7 @@
if (s.length > 4)
return null;
var r = [];
for (var i in s) {
for (var i = 0; i < s.length; i++) {

var l = imscUtils.parseLength(s[i]);
if (!l)
Expand Down Expand Up @@ -538,7 +538,7 @@

var out = [];

for (var i in padding) {
for (var i = 0 ; i < padding.length; i++) {

if (padding[i].value === 0) {

Expand All @@ -550,9 +550,9 @@
padding[i].value,
padding[i].unit,
element.styleAttrs[imscStyles.byName.fontSize.qname],
i === "0" || i === "2" ? element.styleAttrs[imscStyles.byName.extent.qname].h : element.styleAttrs[imscStyles.byName.extent.qname].w,
i === "0" || i === "2" ? doc.cellLength.h : doc.cellLength.w,
i === "0" || i === "2" ? doc.pxLength.h: doc.pxLength.w
i === 0 || i === 2 ? element.styleAttrs[imscStyles.byName.extent.qname].h : element.styleAttrs[imscStyles.byName.extent.qname].w,
i === 0 || i === 2 ? doc.cellLength.h : doc.cellLength.w,
i === 0 || i === 2 ? doc.pxLength.h: doc.pxLength.w
);

if (out[i] === null) return null;
Expand Down Expand Up @@ -834,7 +834,7 @@

var rslt = {style: null, symbol: null, color: null, position: null};

for (var i in e) {
for (var i = 0; i < e.length; i++) {

if (e[i] === "none" || e[i] === "auto") {

Expand Down Expand Up @@ -989,7 +989,7 @@

var r = [];

for (var i in attr) {
for (var i = 0; i < attr.length; i++) {

var shadow = {};

Expand Down
Loading

0 comments on commit 4ec7fd6

Please sign in to comment.