Skip to content

Commit 1bdf841

Browse files
author
Kaushik Shetty
committed
fix: set a preferred body tag for iframeRef
1 parent b44f13e commit 1bdf841

File tree

10 files changed

+46
-8
lines changed

10 files changed

+46
-8
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/extension.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ declare class Extension {
4444
}>;
4545
getCurrentLocation: () => ILocation;
4646
/**
47-
* Conditionally gets and returns the app version if not present already
47+
* Conditionally retrieves and returns the app version if not present already
4848
* @returns version of the app currently running.
4949
*/
5050
getAppVersion: () => Promise<number | null>;

dist/src/modal.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/types.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/utils/utils.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export declare function onData<Data extends Record<string, any>>(data: {
44
}): Promise<Data>;
55
export declare function onError(error: Error): Promise<never>;
66
export declare function formatAppRegion(region: string): Region;
7+
export declare function getPreferredBodyElement(nodeCollection: HTMLCollection): Element;
78
//# sourceMappingURL=utils.d.ts.map

dist/src/utils/utils.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ class Extension {
374374
};
375375

376376
/**
377-
* Conditionally gets and returns the app version if not present already
377+
* Conditionally retrieves and returns the app version if not present already
378378
* @returns version of the app currently running.
379379
*/
380380
getAppVersion = async (): Promise<number | null> => {

src/modal.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
import { getPreferredBodyElement } from "./utils/utils";
2+
13
class Modal {
24
constructor() {
35
if (!Object.prototype.hasOwnProperty.call(window, "iframeRef")) {
4-
window["iframeRef"] = document?.body?.children[0];
6+
const rootElement = getPreferredBodyElement(
7+
document?.body?.children
8+
);
9+
window["iframeRef"] = rootElement;
510
}
611
}
12+
713
setBackgroundElement(element: HTMLElement) {
814
window["iframeRef"] = element;
915
}

src/utils/utils.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,34 @@ export function formatAppRegion(region: string): Region {
2525
return Region.UNKNOWN;
2626
}
2727
}
28+
29+
export function getPreferredBodyElement(nodeCollection: HTMLCollection) {
30+
let rootElementIndex = Infinity;
31+
let rootElement: Element | undefined;
32+
const elementPreferenceList = [
33+
"HEADER",
34+
"NAV",
35+
"MAIN",
36+
"SECTION",
37+
"ARTICLE",
38+
"ASIDE",
39+
"FOOTER",
40+
"DIV",
41+
];
42+
const nonRenderingTags = ["SCRIPT", "NOSCRIPT", "STYLE", "TEMPLATE"];
43+
// ? choose higher preference semantic HTML tags
44+
Array.from(nodeCollection).forEach((el) => {
45+
const elIndex = elementPreferenceList.indexOf(el.nodeName);
46+
if (elIndex >= 0 && elIndex < rootElementIndex) {
47+
rootElementIndex = elIndex;
48+
rootElement = el;
49+
}
50+
});
51+
// ? choose the first rendering HTML tag found if no semantic tags found
52+
if (!rootElement) {
53+
rootElement = Array.from(nodeCollection).find(
54+
(el) => !nonRenderingTags.includes(el.nodeName)
55+
);
56+
}
57+
return rootElement || nodeCollection[0];
58+
}

0 commit comments

Comments
 (0)