diff --git a/app/renderer/lib/domUtil.js b/app/renderer/lib/domUtil.js new file mode 100644 index 00000000000..fc0d372c92a --- /dev/null +++ b/app/renderer/lib/domUtil.js @@ -0,0 +1,7 @@ +module.exports.createWebView = () => { + return document.createElement('webview') +} + +module.exports.appendChild = (element, child) => { + element.appendChild(child) +} diff --git a/js/components/frame.js b/js/components/frame.js index c4fb11e8383..07141b7a891 100644 --- a/js/components/frame.js +++ b/js/components/frame.js @@ -33,6 +33,7 @@ const UrlUtil = require('../lib/urlutil') const cx = require('../lib/classSet') const urlParse = require('../../app/common/urlParse') const contextMenus = require('../contextMenus') +const domUtil = require('../../app/renderer/lib/domUtil') const { aboutUrls, isSourceMagnetUrl, @@ -191,7 +192,7 @@ class Frame extends React.Component { // Create the webview dynamically because React doesn't whitelist all // of the attributes we need if (this.shouldCreateWebview()) { - this.webview = document.createElement('webview') + this.webview = domUtil.createWebView() this.webview.setAttribute('data-frame-key', this.props.frameKey) this.addEventListeners() @@ -210,7 +211,7 @@ class Frame extends React.Component { this.webview.setAttribute('partition', frameStateUtil.getPartition(this.frame)) this.webview.setAttribute('src', newSrc) } - this.webviewContainer.appendChild(this.webview) + domUtil.appendChild(this.webviewContainer, this.webview) } else { cb && cb(prevProps) } @@ -355,13 +356,13 @@ class Frame extends React.Component { // This can happen for pages which don't load properly. // Some examples are basic http auth and bookmarklets. // In this case both the user display and the user think they're on this.props.location. - if (this.tab.get('url') !== this.props.location && + if (this.props.tabUrl !== this.props.location && !this.isAboutPage() && !isTorrentViewerURL(this.props.location)) { this.webview.loadURL(this.props.location) } else if (this.isIntermediateAboutPage() && - this.tab.get('url') !== this.props.location && - this.tab.get('url') !== this.props.aboutDetailsUrl) { + this.props.tabUrl !== this.props.location && + this.props.tabUrl !== this.props.aboutDetailsUrl) { appActions.loadURLRequested(this.props.aboutDetailsUrl, this.props.aboutDetailsFrameKey) } else { @@ -384,7 +385,7 @@ class Frame extends React.Component { this.zoomReset() break case 'view-source': - const sourceLocation = UrlUtil.getViewSourceUrlFromUrl(this.tab.get('url')) + const sourceLocation = UrlUtil.getViewSourceUrlFromUrl(this.props.tabUrl) if (sourceLocation !== null) { appActions.createTabRequested({ url: sourceLocation, @@ -398,8 +399,8 @@ class Frame extends React.Component { break case 'save': const downloadLocation = getSetting(settings.PDFJS_ENABLED) - ? UrlUtil.getLocationIfPDF(this.tab.get('url')) - : this.tab.get('url') + ? UrlUtil.getLocationIfPDF(this.props.tabUrl) + : this.props.tabUrl // TODO: Sometimes this tries to save in a non-existent directory this.webview.downloadURL(downloadLocation, true) break @@ -410,7 +411,7 @@ class Frame extends React.Component { windowActions.setFindbarShown(this.props.frameKey, true) break case 'fill-password': - let currentUrl = urlParse(this.tab.get('url')) + let currentUrl = urlParse(this.props.tabUrl) if (currentUrl && [currentUrl.protocol, currentUrl.host].join('//') === this.props.shortcutDetailsOrigin) { this.webview.send(messages.GOT_PASSWORD, @@ -1032,7 +1033,8 @@ class Frame extends React.Component { props.unloaded = frame.get('unloaded') props.isWidevineEnabled = state.get('widevine') && state.getIn(['widevine', 'enabled']) props.siteZoomLevel = frameSiteSettings && frameSiteSettings.get('zoomLevel') - props.allSiteSettings = allSiteSettings // TODO remove + props.allSiteSettings = allSiteSettings // TODO (nejc) can be improved even more + props.tabUrl = tab && tab.get('url') return Object.assign({}, ownProps, props) } diff --git a/test/unit/js/components/frameTest.js b/test/unit/js/components/frameTest.js index 267e7ed2bcb..fe0dfeeebba 100644 --- a/test/unit/js/components/frameTest.js +++ b/test/unit/js/components/frameTest.js @@ -47,7 +47,26 @@ describe('Frame component unit tests', function () { const fakeWindowActions = { frameShortcutChanged: () => {}, setFindbarShown: () => {}, - setActiveFrame: () => {} + setActiveFrame: () => {}, + setLastZoomPercentage: () => {} + } + + const domUtil = { + appendChild: () => {}, + createWebView: () => { + return { + setAttribute: () => {}, + addEventListener: () => {}, + removeEventListener: () => {}, + attachGuest: () => {}, + zoomIn: () => {}, + zoomOut: () => {}, + zoomReset: () => {}, + executeScriptInTab: () => {}, + focus: () => {}, + getZoomPercent: () => {} + } + } } before(function () { @@ -64,6 +83,7 @@ describe('Frame component unit tests', function () { mockery.registerMock('../../extensions/brave/img/caret_down_grey.svg', 'caret_down_grey.svg') mockery.registerMock('electron', require('../../lib/fakeElectron')) mockery.registerMock('../actions/windowActions', fakeWindowActions) + mockery.registerMock('../../app/renderer/lib/domUtil', domUtil) // the version in frame.js mockery.registerMock('../stores/appStoreRenderer', fakeAppStoreRenderer) // the version in reduxComponent.js