@open-iframe-resizer/core 1.0.0-rc5 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md CHANGED
@@ -1,20 +1,20 @@
1
- Copyright (c) 2012-2024 Scott Chacon and others
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ Copyright (c) 2012-2024 Scott Chacon and others
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,18 +1,41 @@
1
- # Open Iframe Resizer
1
+ # Open Iframe Resizer
2
2
 
3
3
  ## Overview
4
4
 
5
- `iframe-resizer` is a modern, lightweight library for resizing iframes dynamically. It is shipped under the MIT license, making it usable in commercial projects.
5
+ A modern, lightweight library for resizing iframes dynamically. It is shipped under the MIT license, making it usable in commercial projects.
6
+
7
+ If you found this plugin helpful, please consider starring the repository!
6
8
 
7
9
  ## Getting Started
8
10
 
9
- ### Browser
10
- #### ES6 Modules
11
+ ### Browser (ES6 modules)
11
12
 
12
13
  ```html
13
14
  <script type="module">
14
- import { initialize } from "/dist/index.js";
15
+ import { initialize } from "https://cdn.jsdelivr.net/npm/@open-iframe-resizer/core@latest/dist/index.js";
15
16
 
16
- initialize({}, "#iframe-id");
17
+ initialize({}, "#my-iframe");
17
18
  </script>
18
19
  ```
20
+
21
+ You can found a working example [here](https://codesandbox.io/p/sandbox/open-iframe-resize-browser-m655zt)
22
+
23
+ ### Package
24
+ Note you can also install the package through npm:
25
+ ```bash
26
+ npm install @open-iframe-resizer/core
27
+ ```
28
+
29
+ ## Notes
30
+
31
+ ### Retro-compatibility
32
+ I decided to replicate the API from the well-known iframe-resizer library, as it may facilitate migration to this project (you only need to change the script).
33
+
34
+ ### Resize cross-origin iframes
35
+ To resize iframes from a different origin, you also need to load the script on your child page to enable messaging between the two windows (you do not need to call the initialize function in the child; loading the module is sufficient).
36
+
37
+ ## Browser support
38
+
39
+ | Chrome | Safari | Firefox | Opera | IE |
40
+ |--------|--------|---------|-------|-----------|
41
+ | 64+ | 13.1+ | 69+ | 51+ | 🙅‍♂️ |
package/dist/common.d.ts CHANGED
@@ -4,8 +4,8 @@ declare const isSameOriginIframe: (iframe: HTMLIFrameElement) => iframe is HTMLI
4
4
  contentDocument: Document;
5
5
  };
6
6
  declare const isHtmlIframeElement: (element: Element) => element is HTMLIFrameElement;
7
+ declare const deferWhenWindowIsLoaded: (_window: Window, executable: () => void) => void;
7
8
  declare const deferWhenIframeIsLoaded: (iframe: HTMLIFrameElement, executable: () => void) => void;
8
9
  declare const getDefaultSettings: () => Settings;
9
10
  declare const isIframeSameOrigin: (iframe: HTMLIFrameElement) => boolean;
10
- declare function debounce<T extends (...args: any[]) => any>(f: T, delay: number): (...args: unknown[]) => void;
11
- export { isInIframe, isSameOriginIframe, isHtmlIframeElement, deferWhenIframeIsLoaded, isIframeSameOrigin, debounce, getDefaultSettings };
11
+ export { isInIframe, isSameOriginIframe, isHtmlIframeElement, deferWhenIframeIsLoaded, isIframeSameOrigin, deferWhenWindowIsLoaded, getDefaultSettings };
package/dist/index.js CHANGED
@@ -1,17 +1,19 @@
1
1
  const g = () => window && window.self !== window.top, l = (e) => e instanceof HTMLIFrameElement, h = (e, n) => {
2
+ e.document.readyState === "complete" ? n() : e.addEventListener("load", n);
3
+ }, w = (e, n) => {
2
4
  var i, o;
3
- const t = ((i = e.contentWindow) == null ? void 0 : i.document.readyState) === "complete", r = e.src !== "about:blank" && ((o = e.contentWindow) == null ? void 0 : o.location.href) !== "about:blank";
4
- return console.log("test", r, t), r && t ? n() : e.addEventListener("load", n);
5
- }, w = () => ({ offsetSize: 0, checkOrigin: !0 }), b = (e) => {
5
+ const t = ((i = e.contentWindow) == null ? void 0 : i.document.readyState) === "complete";
6
+ return e.src !== "about:blank" && ((o = e.contentWindow) == null ? void 0 : o.location.href) !== "about:blank" && t ? n() : e.addEventListener("load", n);
7
+ }, b = () => ({ offsetSize: 0, checkOrigin: !0 }), y = (e) => {
6
8
  try {
7
9
  return new URL(e.src, window.location.origin).origin === window.location.origin;
8
10
  } catch {
9
11
  return !1;
10
12
  }
11
- }, u = I(), a = [], R = (e, n) => {
12
- const t = { ...w(), ...e }, r = y(n), i = O(t, r);
13
+ }, u = E(), a = [], k = (e, n) => {
14
+ const t = { ...b(), ...e }, r = O(n), i = v(t, r);
13
15
  return r.map((o) => {
14
- const s = a.push({ iframe: o, settings: t }), c = z(o, t, i);
16
+ const s = a.push({ iframe: o, settings: t }), c = L(o, t, i);
15
17
  return {
16
18
  unsubscribe: () => {
17
19
  c(), a.splice(s - 1, 1);
@@ -19,22 +21,22 @@ const g = () => window && window.self !== window.top, l = (e) => e instanceof HT
19
21
  };
20
22
  });
21
23
  };
22
- function y(e) {
24
+ function O(e) {
23
25
  return typeof e == "string" ? Array.from(document.querySelectorAll(e)).filter(l) : e ? l(e) ? [e] : [] : Array.from(document.getElementsByTagName("iframe"));
24
26
  }
25
- function O(e, n) {
27
+ function v(e, n) {
26
28
  if (Array.isArray(e.checkOrigin))
27
29
  return e.checkOrigin;
28
30
  if (!e.checkOrigin)
29
31
  return [];
30
32
  const t = [];
31
33
  for (const r of n) {
32
- const i = v(r);
34
+ const i = z(r);
33
35
  i && t.push(i);
34
36
  }
35
37
  return t;
36
38
  }
37
- function v(e) {
39
+ function z(e) {
38
40
  try {
39
41
  const n = new URL(e.src).origin;
40
42
  if (n !== "about:blank")
@@ -43,10 +45,10 @@ function v(e) {
43
45
  }
44
46
  return null;
45
47
  }
46
- function z(e, n, t) {
47
- return b(e) ? p(e) : L(e, n, t);
48
- }
49
48
  function L(e, n, t) {
49
+ return y(e) ? I(e) : p(e, n, t);
50
+ }
51
+ function p(e, n, t) {
50
52
  const r = (i) => {
51
53
  var s;
52
54
  if ((!n.checkOrigin || t.includes(i.origin)) && ((s = i.data) == null ? void 0 : s.type) === "iframe-resized" && e.contentWindow === i.source) {
@@ -56,7 +58,7 @@ function L(e, n, t) {
56
58
  };
57
59
  return window.addEventListener("message", r, !1), () => window.removeEventListener("message", r, !1);
58
60
  }
59
- function p(e) {
61
+ function I(e) {
60
62
  const n = () => {
61
63
  var r;
62
64
  const t = (r = e.contentDocument) == null ? void 0 : r.body;
@@ -66,12 +68,12 @@ function p(e) {
66
68
  }
67
69
  u.observe(t);
68
70
  };
69
- return h(e, n), () => {
71
+ return w(e, n), () => {
70
72
  var t;
71
73
  (t = e.contentDocument) != null && t.body && u.unobserve(e.contentDocument.body), e.removeEventListener("load", n);
72
74
  };
73
75
  }
74
- function I() {
76
+ function E() {
75
77
  const e = ({ target: n }) => {
76
78
  var c;
77
79
  const t = a.find((m) => {
@@ -88,9 +90,9 @@ function I() {
88
90
  function f({ height: e, iframe: n, settings: t }) {
89
91
  n.style.height = `${e + t.offsetSize}px`;
90
92
  }
91
- g() && E();
92
- function E() {
93
- window.addEventListener("load", () => {
93
+ g() && R();
94
+ function R() {
95
+ h(window, () => {
94
96
  const e = () => {
95
97
  const t = {
96
98
  type: "iframe-resized",
@@ -103,6 +105,6 @@ function E() {
103
105
  });
104
106
  }
105
107
  export {
106
- R as initialize,
107
- E as initializeChildListener
108
+ k as initialize,
109
+ R as initializeChildListener
108
110
  };
@@ -1 +1 @@
1
- (function(s,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(s=typeof globalThis<"u"?globalThis:s||self,a(s.iframeResizer={}))})(this,function(s){"use strict";const a=()=>window&&window.self!==window.top,u=e=>e instanceof HTMLIFrameElement,b=(e,n)=>{var r,o;const t=((r=e.contentWindow)==null?void 0:r.document.readyState)==="complete",i=e.src!=="about:blank"&&((o=e.contentWindow)==null?void 0:o.location.href)!=="about:blank";return console.log("test",i,t),i&&t?n():e.addEventListener("load",n)},w=()=>({offsetSize:0,checkOrigin:!0}),y=e=>{try{return new URL(e.src,window.location.origin).origin===window.location.origin}catch{return!1}},f=R(),l=[],O=(e,n)=>{const t={...w(),...e},i=z(n),r=p(t,i);return i.map(o=>{const c=l.push({iframe:o,settings:t}),d=L(o,t,r);return{unsubscribe:()=>{d(),l.splice(c-1,1)}}})};function z(e){return typeof e=="string"?Array.from(document.querySelectorAll(e)).filter(u):e?u(e)?[e]:[]:Array.from(document.getElementsByTagName("iframe"))}function p(e,n){if(Array.isArray(e.checkOrigin))return e.checkOrigin;if(!e.checkOrigin)return[];const t=[];for(const i of n){const r=v(i);r&&t.push(r)}return t}function v(e){try{const n=new URL(e.src).origin;if(n!=="about:blank")return n}catch{}return null}function L(e,n,t){return y(e)?E(e):I(e,n,t)}function I(e,n,t){const i=r=>{var c;if((!n.checkOrigin||t.includes(r.origin))&&((c=r.data)==null?void 0:c.type)==="iframe-resized"&&e.contentWindow===r.source){const{height:d}=r.data;m({height:d,iframe:e,settings:n})}};return window.addEventListener("message",i,!1),()=>window.removeEventListener("message",i,!1)}function E(e){const n=()=>{var i;const t=(i=e.contentDocument)==null?void 0:i.body;if(!t){console.error("Unable to observe the iframe content document body");return}f.observe(t)};return b(e,n),()=>{var t;(t=e.contentDocument)!=null&&t.body&&f.unobserve(e.contentDocument.body),e.removeEventListener("load",n)}}function R(){const e=({target:n})=>{var d;const t=l.find(S=>{var h;return((h=S.iframe.contentDocument)==null?void 0:h.body)===n});if(!t)return;const{iframe:i,settings:r}=t,{scrollHeight:o,scrollWidth:c}=((d=i.contentDocument)==null?void 0:d.documentElement)??{};o!==void 0&&c!==void 0&&m({height:o,iframe:i,settings:r})};return new ResizeObserver(n=>n.forEach(e))}function m({height:e,iframe:n,settings:t}){n.style.height=`${e+t.offsetSize}px`}a()&&g();function g(){window.addEventListener("load",()=>{const e=()=>{const t={type:"iframe-resized",width:document.documentElement.scrollWidth,height:document.documentElement.scrollHeight};window.parent.postMessage(t,"*")};new ResizeObserver(e).observe(document.body)})}s.initialize=O,s.initializeChildListener=g,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
1
+ (function(s,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(s=typeof globalThis<"u"?globalThis:s||self,a(s.iframeResizer={}))})(this,function(s){"use strict";const a=()=>window&&window.self!==window.top,l=e=>e instanceof HTMLIFrameElement,b=(e,n)=>{e.document.readyState==="complete"?n():e.addEventListener("load",n)},w=(e,n)=>{var r,o;const t=((r=e.contentWindow)==null?void 0:r.document.readyState)==="complete";return e.src!=="about:blank"&&((o=e.contentWindow)==null?void 0:o.location.href)!=="about:blank"&&t?n():e.addEventListener("load",n)},y=()=>({offsetSize:0,checkOrigin:!0}),O=e=>{try{return new URL(e.src,window.location.origin).origin===window.location.origin}catch{return!1}},f=S(),u=[],p=(e,n)=>{const t={...y(),...e},i=z(n),r=v(t,i);return i.map(o=>{const c=u.push({iframe:o,settings:t}),d=I(o,t,r);return{unsubscribe:()=>{d(),u.splice(c-1,1)}}})};function z(e){return typeof e=="string"?Array.from(document.querySelectorAll(e)).filter(l):e?l(e)?[e]:[]:Array.from(document.getElementsByTagName("iframe"))}function v(e,n){if(Array.isArray(e.checkOrigin))return e.checkOrigin;if(!e.checkOrigin)return[];const t=[];for(const i of n){const r=L(i);r&&t.push(r)}return t}function L(e){try{const n=new URL(e.src).origin;if(n!=="about:blank")return n}catch{}return null}function I(e,n,t){return O(e)?R(e):E(e,n,t)}function E(e,n,t){const i=r=>{var c;if((!n.checkOrigin||t.includes(r.origin))&&((c=r.data)==null?void 0:c.type)==="iframe-resized"&&e.contentWindow===r.source){const{height:d}=r.data;m({height:d,iframe:e,settings:n})}};return window.addEventListener("message",i,!1),()=>window.removeEventListener("message",i,!1)}function R(e){const n=()=>{var i;const t=(i=e.contentDocument)==null?void 0:i.body;if(!t){console.error("Unable to observe the iframe content document body");return}f.observe(t)};return w(e,n),()=>{var t;(t=e.contentDocument)!=null&&t.body&&f.unobserve(e.contentDocument.body),e.removeEventListener("load",n)}}function S(){const e=({target:n})=>{var d;const t=u.find(k=>{var h;return((h=k.iframe.contentDocument)==null?void 0:h.body)===n});if(!t)return;const{iframe:i,settings:r}=t,{scrollHeight:o,scrollWidth:c}=((d=i.contentDocument)==null?void 0:d.documentElement)??{};o!==void 0&&c!==void 0&&m({height:o,iframe:i,settings:r})};return new ResizeObserver(n=>n.forEach(e))}function m({height:e,iframe:n,settings:t}){n.style.height=`${e+t.offsetSize}px`}a()&&g();function g(){b(window,()=>{const e=()=>{const t={type:"iframe-resized",width:document.documentElement.scrollWidth,height:document.documentElement.scrollHeight};window.parent.postMessage(t,"*")};new ResizeObserver(e).observe(document.body)})}s.initialize=p,s.initializeChildListener=g,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
package/dist/type.d.ts CHANGED
@@ -1,21 +1,27 @@
1
1
  interface Window {
2
- iframeResizer: {
3
- initialize: InitializeFunction;
4
- };
2
+ iframeResizer: {
3
+ initialize: InitializeFunction;
4
+ };
5
5
  }
6
6
 
7
+ /**
8
+ * Automatically resizes the selected iframes.
9
+ * @param settings The settings for the selected iframes. The default settings properties are picked if empty.
10
+ * @param selector The selector for the iframe(s) or the HTMLIFrameElement to be resized. If empty, all document iframe elements will be selected.
11
+ * @returns A result array, which can be used to clean up the listeners if you often remove iframes from the document.
12
+ */
7
13
  export type InitializeFunction = (settings?: Partial<Settings>, selector?: string | HTMLIFrameElement) => InitializeResult[];
8
14
  export type InitializeResult = { unsubscribe: () => void };
9
15
 
10
16
  export type Settings = {
11
- offsetSize: number;
12
- checkOrigin: string[] | boolean;
17
+ offsetSize: number;
18
+ checkOrigin: string[] | boolean;
13
19
  };
14
20
 
15
21
  export type IframeResizeEventData = {
16
- type: "iframe-resized";
17
- width: number;
18
- height: number;
22
+ type: "iframe-resized";
23
+ width: number;
24
+ height: number;
19
25
  };
20
26
 
21
27
  export type IframeResizeEvent = MessageEvent<IframeResizeEventData>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@open-iframe-resizer/core",
3
3
  "private": false,
4
- "version": "1.0.0-rc5",
4
+ "version": "1.0.0",
5
5
  "description": "Open-source modern iframe resizer",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -11,7 +11,18 @@
11
11
  "engines": {
12
12
  "node": ">=20.0.0"
13
13
  },
14
- "keywords": ["iframe", "Resizing", "Resizer", "resize", "cross", "same", "domain", "width", "height", "responsive"],
14
+ "keywords": [
15
+ "iframe",
16
+ "Resizing",
17
+ "Resizer",
18
+ "resize",
19
+ "cross",
20
+ "same",
21
+ "domain",
22
+ "width",
23
+ "height",
24
+ "responsive"
25
+ ],
15
26
  "type": "module",
16
27
  "main": "./dist/index.cjs",
17
28
  "module": "./dist/index.js",
@@ -23,7 +34,10 @@
23
34
  "require": "./dist/index.cjs"
24
35
  }
25
36
  },
26
- "files": ["dist", "README.md", "LICENSE.md"],
37
+ "files": [
38
+ "dist",
39
+ "README.md"
40
+ ],
27
41
  "scripts": {
28
42
  "docs": "npm run typedoc",
29
43
  "lint": "biome lint ./src",
@@ -34,9 +48,10 @@
34
48
  "serve": "concurrently \"vite --port 5550\" \"vite --port 5551\"",
35
49
  "test": "npm run test:unit && npm run test:e2e",
36
50
  "test:e2e": "npx playwright install --with-deps && npx playwright test",
37
- "test:unit": "vitest run"
51
+ "test:unit": "vitest run",
52
+ "prepare": "copyfiles -V ../../README.md packages/core"
38
53
  },
39
54
  "devDependencies": {
40
- "@microsoft/tsdoc": "^0.14.1"
55
+ "@microsoft/tsdoc": "^0.15.0"
41
56
  }
42
57
  }