From dec48d73bbe0d0189ae203eb12431ffc5521e0a2 Mon Sep 17 00:00:00 2001 From: Kevin Ngo Date: Thu, 9 Feb 2017 16:22:50 -0800 Subject: [PATCH] add warning if component declared after the scene in HTML (fixes #2307) (#2310) --- src/core/component.js | 40 ++++++++++++++---- src/core/scene/a-scene.js | 9 +++- src/core/scene/scenes.js | 4 ++ src/index.js | 12 +++--- tests/core/component.test.js | 72 +++++++++++++++++++++++++++++++- tests/core/scene/a-scene.test.js | 34 ++++++++++++++- 6 files changed, 156 insertions(+), 15 deletions(-) create mode 100644 src/core/scene/scenes.js diff --git a/src/core/component.js b/src/core/component.js index 658e92c5dd0..555da93ea34 100644 --- a/src/core/component.js +++ b/src/core/component.js @@ -1,5 +1,6 @@ -/* global HTMLElement */ +/* global HTMLElement, Node */ var schema = require('./schema'); +var scenes = require('./scene/scenes'); var systems = require('./system'); var utils = require('../utils/'); @@ -11,6 +12,10 @@ var isSingleProp = schema.isSingleProperty; var stringifyProperties = schema.stringifyProperties; var stringifyProperty = schema.stringifyProperty; var styleParser = utils.styleParser; +var warn = utils.debug('core:component:warn'); + +var aframeScript = document.currentScript; +var upperCaseRegExp = new RegExp('[A-Z]+'); /** * Component class definition. @@ -261,6 +266,11 @@ Component.prototype = { } }; +// For testing. +if (window.debug) { + var registrationOrderWarnings = module.exports.registrationOrderWarnings = {}; +} + /** * Registers a component to A-Frame. * @@ -272,13 +282,29 @@ module.exports.registerComponent = function (name, definition) { var NewComponent; var proto = {}; - var testForUpperCase = new RegExp('[A-Z]+'); + // Warning if component is statically registered after the scene. + if (document.currentScript && document.currentScript !== aframeScript) { + scenes.forEach(function checkPosition (sceneEl) { + // Okay to register component after the scene at runtime. + if (sceneEl.hasLoaded) { return; } + + // Check that component is declared before the scene. + if (document.currentScript.compareDocumentPosition(sceneEl) === + Node.DOCUMENT_POSITION_FOLLOWING) { return; } + + warn('The component `' + name + '` was registered in a