Path: blob/trunk/third_party/closure/goog/asserts/dom.js
3992 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56goog.module('goog.asserts.dom');7goog.module.declareLegacyNamespace();89const TagName = goog.require('goog.dom.TagName');10const asserts = goog.require('goog.asserts');11const element = goog.require('goog.dom.element');12const utils = goog.require('goog.utils');1314/**15* Checks if the value is a DOM Element if goog.asserts.ENABLE_ASSERTS is true.16* @param {*} value The value to check.17* @return {!Element} The value, likely to be a DOM Element when asserts are18* enabled.19* @throws {!asserts.AssertionError} When the value is not an Element.20* @closurePrimitive {asserts.matchesReturn}21*/22const assertIsElement = (value) => {23if (asserts.ENABLE_ASSERTS && !element.isElement(value)) {24asserts.fail(25`Argument is not an Element; got: ${debugStringForType(value)}`);26}27return /** @type {!Element} */ (value);28};2930/**31* Checks if the value is a DOM HTMLElement if goog.asserts.ENABLE_ASSERTS is32* true.33* @param {*} value The value to check.34* @return {!HTMLElement} The value, likely to be a DOM HTMLElement when asserts35* are enabled.36* @throws {!asserts.AssertionError} When the value is not an HTMLElement.37* @closurePrimitive {asserts.matchesReturn}38*/39const assertIsHtmlElement = (value) => {40if (asserts.ENABLE_ASSERTS && !element.isHtmlElement(value)) {41asserts.fail(42`Argument is not an HTML Element; got: ${debugStringForType(value)}`);43}44return /** @type {!HTMLElement} */ (value);45};4647/**48* Checks if the value is a DOM HTMLElement of the specified tag name / subclass49* if goog.asserts.ENABLE_ASSERTS is true.50* @param {*} value The value to check.51* @param {!TagName<T>} tagName The element tagName to verify the value against.52* @return {T} The value, likely to be a DOM HTMLElement when asserts are53* enabled. The exact return type will match the parameterized type54* of the tagName as specified in goog.dom.TagName.55* @throws {!asserts.AssertionError} When the value is not an HTMLElement with56* the appropriate tagName.57* @template T58* @closurePrimitive {asserts.matchesReturn}59*/60const assertIsHtmlElementOfType = (value, tagName) => {61if (asserts.ENABLE_ASSERTS && !element.isHtmlElementOfType(value, tagName)) {62asserts.fail(63`Argument is not an HTML Element with tag name ` +64`${tagName.toString()}; got: ${debugStringForType(value)}`);65}66return /** @type {T} */ (value);67};6869/**70* Checks if the value is an HTMLAnchorElement if goog.asserts.ENABLE_ASSERTS is71* true.72* @param {*} value73* @return {!HTMLAnchorElement}74* @throws {!asserts.AssertionError}75* @closurePrimitive {asserts.matchesReturn}76*/77const assertIsHtmlAnchorElement = (value) => {78return assertIsHtmlElementOfType(value, TagName.A);79};8081/**82* Checks if the value is an HTMLButtonElement if goog.asserts.ENABLE_ASSERTS is83* true.84* @param {*} value85* @return {!HTMLButtonElement}86* @throws {!asserts.AssertionError}87* @closurePrimitive {asserts.matchesReturn}88*/89const assertIsHtmlButtonElement = (value) => {90return assertIsHtmlElementOfType(value, TagName.BUTTON);91};9293/**94* Checks if the value is an HTMLLinkElement if goog.asserts.ENABLE_ASSERTS is95* true.96* @param {*} value97* @return {!HTMLLinkElement}98* @throws {!asserts.AssertionError}99* @closurePrimitive {asserts.matchesReturn}100*/101const assertIsHtmlLinkElement = (value) => {102return assertIsHtmlElementOfType(value, TagName.LINK);103};104105/**106* Checks if the value is an HTMLImageElement if goog.asserts.ENABLE_ASSERTS is107* true.108* @param {*} value109* @return {!HTMLImageElement}110* @throws {!asserts.AssertionError}111* @closurePrimitive {asserts.matchesReturn}112*/113const assertIsHtmlImageElement = (value) => {114return assertIsHtmlElementOfType(value, TagName.IMG);115};116117/**118* Checks if the value is an HTMLAudioElement if goog.asserts.ENABLE_ASSERTS is119* true.120* @param {*} value121* @return {!HTMLAudioElement}122* @throws {!asserts.AssertionError}123* @closurePrimitive {asserts.matchesReturn}124*/125const assertIsHtmlAudioElement = (value) => {126return assertIsHtmlElementOfType(value, TagName.AUDIO);127};128129/**130* Checks if the value is an HTMLVideoElement if goog.asserts.ENABLE_ASSERTS is131* true.132* @param {*} value133* @return {!HTMLVideoElement}134* @throws {!asserts.AssertionError}135* @closurePrimitive {asserts.matchesReturn}136*/137const assertIsHtmlVideoElement = (value) => {138return assertIsHtmlElementOfType(value, TagName.VIDEO);139};140141/**142* Checks if the value is an HTMLInputElement if goog.asserts.ENABLE_ASSERTS is143* true.144* @param {*} value145* @return {!HTMLInputElement}146* @throws {!asserts.AssertionError}147* @closurePrimitive {asserts.matchesReturn}148*/149const assertIsHtmlInputElement = (value) => {150return assertIsHtmlElementOfType(value, TagName.INPUT);151};152153/**154* Checks if the value is an HTMLTextAreaElement if goog.asserts.ENABLE_ASSERTS155* is true.156* @param {*} value157* @return {!HTMLTextAreaElement}158* @throws {!asserts.AssertionError}159* @closurePrimitive {asserts.matchesReturn}160*/161const assertIsHtmlTextAreaElement = (value) => {162return assertIsHtmlElementOfType(value, TagName.TEXTAREA);163};164165/**166* Checks if the value is an HTMLCanvasElement if goog.asserts.ENABLE_ASSERTS is167* true.168* @param {*} value169* @return {!HTMLCanvasElement}170* @throws {!asserts.AssertionError}171* @closurePrimitive {asserts.matchesReturn}172*/173const assertIsHtmlCanvasElement = (value) => {174return assertIsHtmlElementOfType(value, TagName.CANVAS);175};176177/**178* Checks if the value is an HTMLEmbedElement if goog.asserts.ENABLE_ASSERTS is179* true.180* @param {*} value181* @return {!HTMLEmbedElement}182* @throws {!asserts.AssertionError}183* @closurePrimitive {asserts.matchesReturn}184*/185const assertIsHtmlEmbedElement = (value) => {186return assertIsHtmlElementOfType(value, TagName.EMBED);187};188189/**190* Checks if the value is an HTMLFormElement if goog.asserts.ENABLE_ASSERTS is191* true.192* @param {*} value193* @return {!HTMLFormElement}194* @throws {!asserts.AssertionError}195* @closurePrimitive {asserts.matchesReturn}196*/197const assertIsHtmlFormElement = (value) => {198return assertIsHtmlElementOfType(value, TagName.FORM);199};200201/**202* Checks if the value is an HTMLFrameElement if goog.asserts.ENABLE_ASSERTS is203* true.204* @param {*} value205* @return {!HTMLFrameElement}206* @throws {!asserts.AssertionError}207* @closurePrimitive {asserts.matchesReturn}208*/209const assertIsHtmlFrameElement = (value) => {210return assertIsHtmlElementOfType(value, TagName.FRAME);211};212213/**214* Checks if the value is an HTMLIFrameElement if goog.asserts.ENABLE_ASSERTS is215* true.216* @param {*} value217* @return {!HTMLIFrameElement}218* @throws {!asserts.AssertionError}219* @closurePrimitive {asserts.matchesReturn}220*/221const assertIsHtmlIFrameElement = (value) => {222return assertIsHtmlElementOfType(value, TagName.IFRAME);223};224225/**226* Checks if the value is an HTMLObjectElement if goog.asserts.ENABLE_ASSERTS is227* true.228* @param {*} value229* @return {!HTMLObjectElement}230* @throws {!asserts.AssertionError}231* @closurePrimitive {asserts.matchesReturn}232*/233const assertIsHtmlObjectElement = (value) => {234return assertIsHtmlElementOfType(value, TagName.OBJECT);235};236237/**238* Checks if the value is an HTMLScriptElement if goog.asserts.ENABLE_ASSERTS is239* true.240* @param {*} value241* @return {!HTMLScriptElement}242* @throws {!asserts.AssertionError}243* @closurePrimitive {asserts.matchesReturn}244*/245const assertIsHtmlScriptElement = (value) => {246return assertIsHtmlElementOfType(value, TagName.SCRIPT);247};248249/**250* Returns a string representation of a value's type.251* @param {*} value An object, or primitive.252* @return {string} The best display name for the value.253*/254const debugStringForType = (value) => {255if (utils.isObject(value)) {256try {257return /** @type {string|undefined} */ (value.constructor.displayName) ||258value.constructor.name ||259Object.prototype.toString.call(value);260} catch (e) {261return '<object could not be stringified>';262}263} else {264return value === undefined ? 'undefined' :265value === null ? 'null' : typeof value;266}267};268269exports = {270assertIsElement,271assertIsHtmlElement,272assertIsHtmlElementOfType,273assertIsHtmlAnchorElement,274assertIsHtmlButtonElement,275assertIsHtmlLinkElement,276assertIsHtmlImageElement,277assertIsHtmlAudioElement,278assertIsHtmlVideoElement,279assertIsHtmlInputElement,280assertIsHtmlTextAreaElement,281assertIsHtmlCanvasElement,282assertIsHtmlEmbedElement,283assertIsHtmlFormElement,284assertIsHtmlFrameElement,285assertIsHtmlIFrameElement,286assertIsHtmlObjectElement,287assertIsHtmlScriptElement,288};289290291