diff --git a/polygerrit-ui/app/elements/diff/gr-syntax-lib-loader/gr-syntax-lib-loader.js b/polygerrit-ui/app/elements/diff/gr-syntax-lib-loader/gr-syntax-lib-loader.js index bfd8e90b1c..ca3cf62293 100644 --- a/polygerrit-ui/app/elements/diff/gr-syntax-lib-loader/gr-syntax-lib-loader.js +++ b/polygerrit-ui/app/elements/diff/gr-syntax-lib-loader/gr-syntax-lib-loader.js @@ -26,6 +26,7 @@ // NOTE: intended singleton. value: { + configured: false, loading: false, callbacks: [], }, @@ -60,12 +61,13 @@ }, _getHighlightLib() { - return window.hljs; - }, + const lib = window.hljs; + if (lib && !this._state.configured) { + this._state.configured = true; - _configureHighlightLib() { - this._getHighlightLib().configure( - {classPrefix: 'gr-diff gr-syntax gr-syntax-'}); + lib.configure({classPrefix: 'gr-diff gr-syntax gr-syntax-'}); + } + return lib; }, _getLibRoot() { @@ -93,10 +95,8 @@ } script.src = src; - script.onload = function() { - this._configureHighlightLib(); - resolve(); - }.bind(this); + script.onload = resolve; + script.onerror = reject; Polymer.dom(document.head).appendChild(script); }); }, diff --git a/polygerrit-ui/app/elements/diff/gr-syntax-lib-loader/gr-syntax-lib-loader_test.html b/polygerrit-ui/app/elements/diff/gr-syntax-lib-loader/gr-syntax-lib-loader_test.html index 6ddde4698d..6e88ed1d38 100644 --- a/polygerrit-ui/app/elements/diff/gr-syntax-lib-loader/gr-syntax-lib-loader_test.html +++ b/polygerrit-ui/app/elements/diff/gr-syntax-lib-loader/gr-syntax-lib-loader_test.html @@ -55,6 +55,7 @@ limitations under the License. loadStub.restore(); // Because the element state is a singleton, clean it up. + element._state.configured = false; element._state.loading = false; element._state.callbacks = []; }); @@ -88,8 +89,13 @@ limitations under the License. }); suite('preloaded', () => { + let hljsStub; + setup(() => { - window.hljs = 'test-object'; + hljsStub = { + configure: sinon.stub(), + }; + window.hljs = hljsStub; }); teardown(() => { @@ -101,7 +107,14 @@ limitations under the License. element.get().then(firstCallHandler); flush(() => { assert.isTrue(firstCallHandler.called); - assert.isTrue(firstCallHandler.calledWith('test-object')); + assert.isTrue(firstCallHandler.calledWith(hljsStub)); + done(); + }); + }); + + test('configures hljs', done => { + element.get().then(() => { + assert.isTrue(window.hljs.configure.calledOnce); done(); }); });