diff options
author | rdevlin.cronin <rdevlin.cronin@chromium.org> | 2016-02-03 16:50:29 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-02-04 00:52:10 +0000 |
commit | dc8497a3cfc680e9bfe4fbc36c84dd17d2b092ff (patch) | |
tree | 03f3ef9241ccd5f3ab08b9628678ccda54a2a263 /ui/webui | |
parent | ec5af95c1086f8b675e67fbc278b83114c22f55c (diff) | |
download | chromium_src-dc8497a3cfc680e9bfe4fbc36c84dd17d2b092ff.zip chromium_src-dc8497a3cfc680e9bfe4fbc36c84dd17d2b092ff.tar.gz chromium_src-dc8497a3cfc680e9bfe4fbc36c84dd17d2b092ff.tar.bz2 |
Update util.js getElementById alias and assesrtInstanceof
Since util.js $() asserts that an element is an HTMLElement, we need a way of
getting an SVGElement from the document (and for symmetry and presubmit check
reasons, it makes sense to add another function in util.js). Also update
assertInstanceof to only construct the error message if the element is not of
the proper type, given the frequency with which it is called.
BUG=
Review URL: https://codereview.chromium.org/1666723003
Cr-Commit-Position: refs/heads/master@{#373402}
Diffstat (limited to 'ui/webui')
-rw-r--r-- | ui/webui/resources/js/assert.js | 8 | ||||
-rw-r--r-- | ui/webui/resources/js/util.js | 16 |
2 files changed, 19 insertions, 5 deletions
diff --git a/ui/webui/resources/js/assert.js b/ui/webui/resources/js/assert.js index 9b0be57..94f2535 100644 --- a/ui/webui/resources/js/assert.js +++ b/ui/webui/resources/js/assert.js @@ -62,7 +62,11 @@ function assertNotReached(opt_message) { * @template T */ function assertInstanceof(value, type, opt_message) { - assert(value instanceof type, - opt_message || value + ' is not a[n] ' + (type.name || typeof type)); + // We don't use assert immediately here so that we avoid constructing an error + // message if we don't have to. + if (!(value instanceof type)) { + assertNotReached(opt_message || 'Value ' + value + + ' is not a[n] ' + (type.name || typeof type)); + } return value; } diff --git a/ui/webui/resources/js/util.js b/ui/webui/resources/js/util.js index 89d963d..9f8c246 100644 --- a/ui/webui/resources/js/util.js +++ b/ui/webui/resources/js/util.js @@ -11,9 +11,19 @@ */ function $(id) { var el = document.getElementById(id); - var message = - 'Element ' + el + ' with id "' + id + '" is not an HTMLElement.'; - return el ? assertInstanceof(el, HTMLElement, message) : null; + return el ? assertInstanceof(el, HTMLElement) : null; +} + +// TODO(devlin): This should return SVGElement, but closure compiler is missing +// those externs. +/** + * Alias for document.getElementById. Found elements must be SVGElements. + * @param {string} id The ID of the element to find. + * @return {Element} The found element or null if not found. + */ +function getSVGElement(id) { + var el = document.getElementById(id); + return el ? assertInstanceof(el, Element) : null; } /** |