-
Notifications
You must be signed in to change notification settings - Fork 309
/
Copy pathoembed.js
174 lines (142 loc) · 5.52 KB
/
oembed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import * as oembedUtils from './oembedUtils.js';
import cheerio from 'cheerio';
import * as entities from 'entities';
import * as URL from 'url';
import * as querystring from 'querystring';
const pxRe = /^([\d\.]+)(?:px)?$/;
const percentRe = /^([\d\.]+)%$/;
function fixOembedIframeAttributes(obj) {
var result = {};
for(var key in obj) {
var value = obj[key];
if (typeof value === 'string') {
var m = value.match(pxRe);
if (m) {
// Convert string numbers to int.
value = parseFloat(m[1]);
} else if (key === 'width' || key === 'height') {
m = value.match(percentRe);
if (m) {
// Skip % value for width of height.
continue;
}
}
}
result[key] = value;
}
return result;
}
function _getOembedIframe(oembed) {
if (typeof oembed._iframe === 'undefined') {
var _iframe = null;
if (oembed.html5 || oembed.html) {
// Allow encoded entities if they start from $lt;
var html = oembed.html5 || oembed.html;
if (/^<$/i.test(html)) {
html = entities.decodeHTML(html);
}
var $container = cheerio('<div>');
try {
$container.html(html);
} catch (ex) {}
var $iframe = $container.find('iframe');
if ($iframe.length === 2 && /<iframe>$/i.test(html)) {
// Forgive mis-closed iFrame tag
$iframe = $iframe.first();
}
if ($iframe.length === 1) {
_iframe = fixOembedIframeAttributes($iframe[0].attribs);
_iframe.placeholder = oembed.thumbnail_url;
}
// When oembed is in fact iframe from domain fallbacks on oEmbedError
} else if (oembed.src) {
_iframe = oembed;
}
if (_iframe && _iframe.src) {
var src = URL.parse(_iframe.src, true);
_iframe.host = src.host;
_iframe.pathname = src.pathname;
_iframe.path = src.path;
_iframe.query = src.query;
_iframe.replaceQuerystring = function(params) {
var qs = querystring.stringify({..._iframe.query, ...params});
return _iframe.src.replace(/\?.*$/, '') + (qs ? '?' + qs : '');
}
_iframe.assignQuerystring = function(params) {
var qs = querystring.stringify(params);
return oembed._iframe.src.replace(/\?.*$/, '') + (qs ? '?' + qs : '');
}
} else {
_iframe = null;
}
oembed._iframe = _iframe ? {..._iframe} : null;
}
return oembed._iframe;
}
function getOembedIframe(oembed) {
return function() {
return _getOembedIframe(oembed);
};
}
function getOembedIframeAttr(oembed) {
return function(attr) {
var iframe = _getOembedIframe(oembed);
if (iframe) {
return iframe[attr];
}
};
}
export default {
provides: ['self', 'oembedError', '__oembedError'],
getIframe: _getOembedIframe, // available via export for fallbacks as plugins['oembed'].getIframe(obj)
getData: function(url, oembedLinks, options, cb) {
var href = oembedLinks[0].href;
var skip = CONFIG.SKIP_OEMBED_RE_LIST
&& CONFIG.SKIP_OEMBED_RE_LIST.some(re => re.test(href)),
self_endpoint = CONFIG.SKIP_OEMBED_RE_LIST
&& CONFIG.SKIP_OEMBED_RE_LIST.some(re => re.test(href));
if (skip || self_endpoint) {
return cb(null);
}
oembedUtils.getOembed(href, options, function(error, oembed) {
if (error && !oembed) {
return cb('Oembed error "'+ oembedLinks[0].href + '": ' + error, {
oembedError: error,
__oembedError: error // To enable fallbacks to optional meta
});
} else if (error && oembed) { // via `options.parseErrorBody = true`
return cb(null, {
oembedError: {
code: error,
body: oembed
},
__oembedError: { // To enable fallbacks to optional meta
code: error,
body: oembed
},
});
}
var result = {
oembed: Object.assign({
getIframe: getOembedIframe(oembed),
getIframeAttr: getOembedIframeAttr(oembed)
}, oembed),
};
// If no oEmbed record for the domain - allow to be whitelisted by the oEmbed endpoint domian record.
if (options.getWhitelistRecord) {
var currentWhitelistRecord = options.getWhitelistRecord(url, {disableWildcard: true});
var oembedWhitelistRecord = options.getWhitelistRecord(href, {exclusiveRel: 'oembed'});
if (oembedWhitelistRecord
&& !oembedWhitelistRecord.isDefault
&& (!currentWhitelistRecord
|| !currentWhitelistRecord.oembed
|| currentWhitelistRecord.isDefault)
) {
result.whitelistRecord = oembedWhitelistRecord;
result.oembed.domain = oembedWhitelistRecord.domain;
}
}
cb(null, result);
});
}
};