Skip to content

Commit 257f5d6

Browse files
d4rekanguokpieh
authored andcommitted
fix(gatsby-remark-responsive-iframe): iframe ratio is NaN if d… (#18328)
* simplify width/height value check, ratio calculation doesnt return NaN * update test snapshot * modernize the code * update test snapshot (remove extra colon, extra space) * convert: add comments, trim off white space
1 parent 08dc747 commit 257f5d6

File tree

4 files changed

+78
-98
lines changed

4 files changed

+78
-98
lines changed

packages/gatsby-remark-responsive-iframe/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
},
99
"dependencies": {
1010
"@babel/runtime": "^7.6.2",
11-
"bluebird": "^3.7.0",
1211
"cheerio": "^1.0.0-rc.3",
1312
"common-tags": "^1.8.0",
1413
"lodash": "^4.17.15",

packages/gatsby-remark-responsive-iframe/src/__tests__/__snapshots__/index.js.snap

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`gatsby-remark-responsive-iframe can copy JSX images 1`] = `"<div class=\\"gatsby-resp-iframe-wrapper\\" style=\\"padding-bottom: NaN%; position: relative; height: 0; overflow: hidden;\\" > <iframe url=\\"http://www.example.com/\\" style=\\"border:0; position: absolute; top: 0; left: 0; width: 100%; height: 100%; \\"></iframe> </div>"`;
3+
exports[`gatsby-remark-responsive-iframe can copy JSX images 1`] = `"<div class=\\"gatsby-resp-iframe-wrapper\\" style=\\"padding-bottom: 66.66666666666666%; position: relative; height: 0; overflow: hidden; \\" > <iframe url=\\"http://www.example.com/\\" style=\\"border:0; position: absolute; top: 0; left: 0; width: 100%; height: 100%; \\"></iframe> </div>"`;
44
55
exports[`gatsby-remark-responsive-iframe doesn't transform an iframe with dimensions: '100%' '100' 1`] = `
66
" <iframe url=\\"http://www.example.com/\\" width=\\"100%\\" height=\\"100\\"></iframe>
@@ -42,18 +42,18 @@ exports[`gatsby-remark-responsive-iframe doesn't transform an object with dimens
4242
"
4343
`;
4444
45-
exports[`gatsby-remark-responsive-iframe transforms an iframe and maintains existing styles 1`] = `"<div class=\\"gatsby-resp-iframe-wrapper\\" style=\\"padding-bottom: NaN%; position: relative; height: 0; overflow: hidden;\\" > <iframe url=\\"http://www.example.com/\\" style=\\"border:0; position: absolute; top: 0; left: 0; width: 100%; height: 100%; \\"></iframe> </div>"`;
45+
exports[`gatsby-remark-responsive-iframe transforms an iframe and maintains existing styles 1`] = `"<div class=\\"gatsby-resp-iframe-wrapper\\" style=\\"padding-bottom: 66.66666666666666%; position: relative; height: 0; overflow: hidden; \\" > <iframe url=\\"http://www.example.com/\\" style=\\"border:0; position: absolute; top: 0; left: 0; width: 100%; height: 100%; \\"></iframe> </div>"`;
4646
47-
exports[`gatsby-remark-responsive-iframe transforms an iframe and maintains existing styles when a semicolon exists 1`] = `"<div class=\\"gatsby-resp-iframe-wrapper\\" style=\\"padding-bottom: NaN%; position: relative; height: 0; overflow: hidden;\\" > <iframe url=\\"http://www.example.com/\\" style=\\"border:0; position: absolute; top: 0; left: 0; width: 100%; height: 100%; \\"></iframe> </div>"`;
47+
exports[`gatsby-remark-responsive-iframe transforms an iframe and maintains existing styles when a semicolon exists 1`] = `"<div class=\\"gatsby-resp-iframe-wrapper\\" style=\\"padding-bottom: 66.66666666666666%; position: relative; height: 0; overflow: hidden; \\" > <iframe url=\\"http://www.example.com/\\" style=\\"border:0; position: absolute; top: 0; left: 0; width: 100%; height: 100%; \\"></iframe> </div>"`;
4848
49-
exports[`gatsby-remark-responsive-iframe transforms an iframe with pixel width and height 1`] = `"<div class=\\"gatsby-resp-iframe-wrapper\\" style=\\"padding-bottom: NaN%; position: relative; height: 0; overflow: hidden;\\" > <iframe url=\\"http://www.example.com/\\" style=\\" position: absolute; top: 0; left: 0; width: 100%; height: 100%; \\"></iframe> </div>"`;
49+
exports[`gatsby-remark-responsive-iframe transforms an iframe with pixel width and height 1`] = `"<div class=\\"gatsby-resp-iframe-wrapper\\" style=\\"padding-bottom: 66.66666666666666%; position: relative; height: 0; overflow: hidden; \\" > <iframe url=\\"http://www.example.com/\\" style=\\" position: absolute; top: 0; left: 0; width: 100%; height: 100%; \\"></iframe> </div>"`;
5050
51-
exports[`gatsby-remark-responsive-iframe transforms an iframe with unitless width and height 1`] = `"<div class=\\"gatsby-resp-iframe-wrapper\\" style=\\"padding-bottom: 66.66666666666666%; position: relative; height: 0; overflow: hidden;\\" > <iframe url=\\"http://www.example.com/\\" style=\\" position: absolute; top: 0; left: 0; width: 100%; height: 100%; \\"></iframe> </div>"`;
51+
exports[`gatsby-remark-responsive-iframe transforms an iframe with unitless width and height 1`] = `"<div class=\\"gatsby-resp-iframe-wrapper\\" style=\\"padding-bottom: 66.66666666666666%; position: relative; height: 0; overflow: hidden; \\" > <iframe url=\\"http://www.example.com/\\" style=\\" position: absolute; top: 0; left: 0; width: 100%; height: 100%; \\"></iframe> </div>"`;
5252
53-
exports[`gatsby-remark-responsive-iframe transforms an object and maintains existing styles 1`] = `"<div class=\\"gatsby-resp-iframe-wrapper\\" style=\\"padding-bottom: NaN%; position: relative; height: 0; overflow: hidden;\\" > <object url=\\"http://www.example.com/\\" style=\\" position: absolute; top: 0; left: 0; width: 100%; height: 100%; \\"></object> </div>"`;
53+
exports[`gatsby-remark-responsive-iframe transforms an object and maintains existing styles 1`] = `"<div class=\\"gatsby-resp-iframe-wrapper\\" style=\\"padding-bottom: 66.66666666666666%; position: relative; height: 0; overflow: hidden; \\" > <object url=\\"http://www.example.com/\\" style=\\" position: absolute; top: 0; left: 0; width: 100%; height: 100%; \\"></object> </div>"`;
5454
55-
exports[`gatsby-remark-responsive-iframe transforms an object and maintains existing styles when a semicolon exists 1`] = `"<div class=\\"gatsby-resp-iframe-wrapper\\" style=\\"padding-bottom: NaN%; position: relative; height: 0; overflow: hidden;\\" > <object url=\\"http://www.example.com/\\" style=\\" position: absolute; top: 0; left: 0; width: 100%; height: 100%; \\"></object> </div>"`;
55+
exports[`gatsby-remark-responsive-iframe transforms an object and maintains existing styles when a semicolon exists 1`] = `"<div class=\\"gatsby-resp-iframe-wrapper\\" style=\\"padding-bottom: 66.66666666666666%; position: relative; height: 0; overflow: hidden; \\" > <object url=\\"http://www.example.com/\\" style=\\" position: absolute; top: 0; left: 0; width: 100%; height: 100%; \\"></object> </div>"`;
5656
57-
exports[`gatsby-remark-responsive-iframe transforms an object with pixel width and height 1`] = `"<div class=\\"gatsby-resp-iframe-wrapper\\" style=\\"padding-bottom: NaN%; position: relative; height: 0; overflow: hidden;\\" > <object url=\\"http://www.example.com/\\" style=\\" position: absolute; top: 0; left: 0; width: 100%; height: 100%; \\"></object> </div>"`;
57+
exports[`gatsby-remark-responsive-iframe transforms an object with pixel width and height 1`] = `"<div class=\\"gatsby-resp-iframe-wrapper\\" style=\\"padding-bottom: 66.66666666666666%; position: relative; height: 0; overflow: hidden; \\" > <object url=\\"http://www.example.com/\\" style=\\" position: absolute; top: 0; left: 0; width: 100%; height: 100%; \\"></object> </div>"`;
5858
59-
exports[`gatsby-remark-responsive-iframe transforms an object with unitless width and height 1`] = `"<div class=\\"gatsby-resp-iframe-wrapper\\" style=\\"padding-bottom: 66.66666666666666%; position: relative; height: 0; overflow: hidden;\\" > <object url=\\"http://www.example.com/\\" style=\\" position: absolute; top: 0; left: 0; width: 100%; height: 100%; \\"></object> </div>"`;
59+
exports[`gatsby-remark-responsive-iframe transforms an object with unitless width and height 1`] = `"<div class=\\"gatsby-resp-iframe-wrapper\\" style=\\"padding-bottom: 66.66666666666666%; position: relative; height: 0; overflow: hidden; \\" > <object url=\\"http://www.example.com/\\" style=\\" position: absolute; top: 0; left: 0; width: 100%; height: 100%; \\"></object> </div>"`;
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,85 @@
11
const visit = require(`unist-util-visit`)
22
const cheerio = require(`cheerio`)
3-
const Promise = require(`bluebird`)
43
const { oneLine } = require(`common-tags`)
54
const _ = require(`lodash`)
65

7-
const isPixelNumber = n => /\d+px$/.test(n)
8-
9-
const isUnitlessNumber = n => {
10-
const nToNum = _.toNumber(n)
11-
return _.isFinite(nToNum)
12-
}
6+
const needsSemicolon = str => !str.endsWith(`;`)
137

14-
const isUnitlessOrPixelNumber = n =>
15-
n && (isUnitlessNumber(n) || isPixelNumber(n))
8+
/**
9+
* Convert anything to number, except for % value.
10+
* We don't have to check for other values (em, vw, etc.)
11+
* because the browsers will treat them as px anyway.
12+
* @param {*} n something to be converted to number
13+
* @returns {number}
14+
*/
15+
const convert = n =>
16+
typeof n === `string` && n.trim().endsWith(`%`) ? NaN : parseInt(n, 10)
1617

17-
const needsSemicolon = str => !str.endsWith(`;`)
18+
/**
19+
* Check whether all passed in arguments are valid number or not
20+
* @param {...number} args dimension to check
21+
* @returns {boolean}
22+
*/
23+
const isValidDimensions = (...args) => args.every(n => _.isFinite(n))
1824

19-
// Aspect ratio can only be determined if both width and height are unitless or
20-
// pixel values. Any other values mean the responsive wrapper is not applied.
21-
const acceptedDimensions = (width, height) =>
22-
isUnitlessOrPixelNumber(width) && isUnitlessOrPixelNumber(height)
25+
module.exports = async ({ markdownAST }, pluginOptions = {}) => {
26+
const defaults = {
27+
wrapperStyle: ``,
28+
}
29+
const options = _.defaults(pluginOptions, defaults)
30+
visit(markdownAST, [`html`, `jsx`], node => {
31+
const $ = cheerio.load(node.value)
32+
const iframe = $(`iframe, object`)
33+
if (iframe.length === 0) {
34+
return
35+
}
2336

24-
module.exports = ({ markdownAST }, pluginOptions = {}) =>
25-
new Promise(resolve => {
26-
const defaults = {
27-
wrapperStyle: ``,
37+
const width = convert(iframe.attr(`width`))
38+
const height = convert(iframe.attr(`height`))
39+
if (!isValidDimensions(width, height)) {
40+
return
2841
}
29-
const options = _.defaults(pluginOptions, defaults)
30-
visit(markdownAST, [`html`, `jsx`], node => {
31-
const $ = cheerio.load(node.value)
32-
const iframe = $(`iframe, object`)
33-
if (iframe.length) {
34-
const width = iframe.attr(`width`)
35-
const height = iframe.attr(`height`)
3642

37-
if (acceptedDimensions(width, height)) {
38-
const existingStyle = $(`iframe`).attr(`style`) // Other plugins might set border: 0
39-
// so we make sure that we maintain those existing styles. If other styles like height or
40-
// width are already defined they will be overridden anyway.
43+
let fullStyle = $(`iframe`).attr(`style`) || `` // Other plugins might set border: 0
44+
// so we make sure that we maintain those existing styles. If other styles like height or
45+
// width are already defined they will be overridden anyway.
4146

42-
let fullStyle = ``
43-
if (existingStyle && needsSemicolon(existingStyle)) {
44-
fullStyle = `${existingStyle};`
45-
} else if (existingStyle) {
46-
fullStyle = existingStyle
47-
}
47+
if (fullStyle.length > 0 && needsSemicolon(fullStyle)) {
48+
fullStyle = `${fullStyle};`
49+
}
4850

49-
$(`iframe, object`).attr(
50-
`style`,
51-
`${fullStyle}
52-
position: absolute;
53-
top: 0;
54-
left: 0;
55-
width: 100%;
56-
height: 100%;
57-
`
58-
)
59-
$(`iframe, object`)
60-
.attr(`width`, null)
61-
.attr(`height`, null)
62-
const newIframe = $(`body`).html() // fix for cheerio v1
51+
$(`iframe, object`)
52+
.attr(
53+
`style`,
54+
`${fullStyle}
55+
position: absolute;
56+
top: 0;
57+
left: 0;
58+
width: 100%;
59+
height: 100%;
60+
`
61+
)
62+
.attr(`width`, null)
63+
.attr(`height`, null)
6364

64-
// TODO add youtube preview image as background-image.
65+
const newIframe = $(`body`).html() // fix for cheerio v1
6566

66-
const rawHTML = oneLine`
67-
<div
68-
class="gatsby-resp-iframe-wrapper"
69-
style="padding-bottom: ${(height / width) *
70-
100}%; position: relative; height: 0; overflow: hidden;${
71-
options.wrapperStyle
72-
}"
73-
>
74-
${newIframe}
75-
</div>
76-
`
67+
// TODO add youtube preview image as background-image.
7768

78-
node.type = `html`
79-
node.value = rawHTML
80-
}
81-
}
82-
})
69+
const rawHTML = oneLine`
70+
<div
71+
class="gatsby-resp-iframe-wrapper"
72+
style="padding-bottom: ${(height / width) *
73+
100}%; position: relative; height: 0; overflow: hidden;
74+
${options.wrapperStyle}"
75+
>
76+
${newIframe}
77+
</div>
78+
`
8379

84-
return resolve(markdownAST)
80+
node.type = `html`
81+
node.value = rawHTML
8582
})
83+
84+
return markdownAST
85+
}

yarn.lock

+2-21
Original file line numberDiff line numberDiff line change
@@ -19784,7 +19784,7 @@ type-of@^2.0.1:
1978419784
version "2.0.1"
1978519785
resolved "https://registry.yarnpkg.com/type-of/-/type-of-2.0.1.tgz#e72a1741896568e9f628378d816d6912f7f23972"
1978619786

19787-
typedarray-to-buffer@^3.1.5, typedarray-to-buffer@~3.1.5:
19787+
typedarray-to-buffer@~3.1.5:
1978819788
version "3.1.5"
1978919789
resolved "https://registry.yarnpkg.com/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz#a97ee7a9ff42691b9f783ff1bc5112fe3fca9080"
1979019790
integrity sha512-zdu8XMNEDepKKR+XYOXAVPtWui0ly0NtohUscw+UmaHiAWT8hrV1rr//H6V+0DvJ3OQ19S979M0laLfX8rm82Q==
@@ -21119,7 +21119,7 @@ wrappy@1:
2111921119
version "1.0.2"
2112021120
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
2112121121

21122-
[email protected], write-file-atomic@^2.0.0, write-file-atomic@^2.3.0:
21122+
[email protected], write-file-atomic@^2.0.0, write-file-atomic@^2.3.0, write-file-atomic@^2.4.2, write-file-atomic@^3.0.0:
2112321123
version "2.4.1"
2112421124
resolved "https://registry.yarnpkg.com/write-file-atomic/-/write-file-atomic-2.4.1.tgz#d0b05463c188ae804396fd5ab2a370062af87529"
2112521125
integrity sha512-TGHFeZEZMnv+gBFRfjAcxL5bPHrsGKtnb4qsFAws7/vlh+QfwAaySIw4AXP9ZskTTh5GWu3FLuJhsWVdiJPGvg==
@@ -21128,25 +21128,6 @@ [email protected], write-file-atomic@^2.0.0, write-file-atomic@^2.3.0:
2112821128
imurmurhash "^0.1.4"
2112921129
signal-exit "^3.0.2"
2113021130

21131-
write-file-atomic@^2.4.2:
21132-
version "2.4.3"
21133-
resolved "https://registry.yarnpkg.com/write-file-atomic/-/write-file-atomic-2.4.3.tgz#1fd2e9ae1df3e75b8d8c367443c692d4ca81f481"
21134-
integrity sha512-GaETH5wwsX+GcnzhPgKcKjJ6M2Cq3/iZp1WyY/X1CSqrW+jVNM9Y7D8EC2sM4ZG/V8wZlSniJnCKWPmBYAucRQ==
21135-
dependencies:
21136-
graceful-fs "^4.1.11"
21137-
imurmurhash "^0.1.4"
21138-
signal-exit "^3.0.2"
21139-
21140-
write-file-atomic@^3.0.0:
21141-
version "3.0.0"
21142-
resolved "https://registry.yarnpkg.com/write-file-atomic/-/write-file-atomic-3.0.0.tgz#1b64dbbf77cb58fd09056963d63e62667ab4fb21"
21143-
integrity sha512-EIgkf60l2oWsffja2Sf2AL384dx328c0B+cIYPTQq5q2rOYuDV00/iPFBOUiDKKwKMOhkymH8AidPaRvzfxY+Q==
21144-
dependencies:
21145-
imurmurhash "^0.1.4"
21146-
is-typedarray "^1.0.0"
21147-
signal-exit "^3.0.2"
21148-
typedarray-to-buffer "^3.1.5"
21149-
2115021131
2115121132
version "0.0.2"
2115221133
resolved "https://registry.yarnpkg.com/write-file-stdout/-/write-file-stdout-0.0.2.tgz#c252d7c7c5b1b402897630e3453c7bfe690d9ca1"

0 commit comments

Comments
 (0)