Path: blob/trunk/third_party/closure/goog/dom/element.js
4509 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56goog.module('goog.dom.element');7goog.module.declareLegacyNamespace();89const NodeType = goog.require('goog.dom.NodeType');10const TagName = goog.require('goog.dom.TagName');11const utils = goog.require('goog.utils');1213/** @const {string} */14const HTML_NAMESPACE = 'http://www.w3.org/1999/xhtml';1516/**17* Determines if a value is a DOM Element.18* @param {*} value19* @return {boolean}20*/21const isElement = (value) => {22return utils.isObject(value) &&23/** @type {!Node} */ (value).nodeType === NodeType.ELEMENT;24};2526/**27* Determines if a value is a DOM HTML Element.28* @param {*} value29* @return {boolean}30*/31const isHtmlElement = (value) => {32return utils.isObject(value) && isElement(value) &&33// namespaceURI of old browsers (FF < 3.6, IE < 9) will be null.34(!/** @type {!Element} */ (value).namespaceURI ||35/** @type {!Element} */ (value).namespaceURI === HTML_NAMESPACE);36};3738/**39* Determines if a value is a DOM HTML Element of a specified tag name. For40* modern browsers, tags that provide access to special DOM APIs are implemented41* as special subclasses of HTMLElement.42* @param {*} value43* @param {!TagName} tagName44* @return {boolean}45*/46const isHtmlElementOfType = (value, tagName) => {47return utils.isObject(value) && isHtmlElement(value) &&48// Some uncommon JS environments (e.g. Cobalt 9) have issues with tag49// capitalization.50(/** @type {!HTMLElement} */ (value).tagName.toUpperCase() ===51tagName.toString());52};5354/**55* Determines if a value is an <A> Element.56* @param {*} value57* @return {boolean}58*/59const isHtmlAnchorElement = (value) => {60return isHtmlElementOfType(value, TagName.A);61};6263/**64* Determines if a value is a <BUTTON> Element.65* @param {*} value66* @return {boolean}67*/68const isHtmlButtonElement = (value) => {69return isHtmlElementOfType(value, TagName.BUTTON);70};7172/**73* Determines if a value is a <LINK> Element.74* @param {*} value75* @return {boolean}76*/77const isHtmlLinkElement = (value) => {78return isHtmlElementOfType(value, TagName.LINK);79};8081/**82* Determines if a value is an <IMG> Element.83* @param {*} value84* @return {boolean}85*/86const isHtmlImageElement = (value) => {87return isHtmlElementOfType(value, TagName.IMG);88};8990/**91* Determines if a value is an <AUDIO> Element.92* @param {*} value93* @return {boolean}94*/95const isHtmlAudioElement = (value) => {96return isHtmlElementOfType(value, TagName.AUDIO);97};9899/**100* Determines if a value is a <VIDEO> Element.101* @param {*} value102* @return {boolean}103*/104const isHtmlVideoElement = (value) => {105return isHtmlElementOfType(value, TagName.VIDEO);106};107108/**109* Determines if a value is an <INPUT> Element.110* @param {*} value111* @return {boolean}112*/113const isHtmlInputElement = (value) => {114return isHtmlElementOfType(value, TagName.INPUT);115};116117/**118* Determines if a value is a <TEXTAREA> Element.119* @param {*} value120* @return {boolean}121*/122const isHtmlTextAreaElement = (value) => {123return isHtmlElementOfType(value, TagName.TEXTAREA);124};125126/**127* Determines if a value is a <CANVAS> Element.128* @param {*} value129* @return {boolean}130*/131const isHtmlCanvasElement = (value) => {132return isHtmlElementOfType(value, TagName.CANVAS);133};134135/**136* Determines if a value is an <EMBED> Element.137* @param {*} value138* @return {boolean}139*/140const isHtmlEmbedElement = (value) => {141return isHtmlElementOfType(value, TagName.EMBED);142};143144/**145* Determines if a value is a <FORM> Element.146* @param {*} value147* @return {boolean}148*/149const isHtmlFormElement = (value) => {150return isHtmlElementOfType(value, TagName.FORM);151};152153/**154* Determines if a value is a <FRAME> Element.155* @param {*} value156* @return {boolean}157*/158const isHtmlFrameElement = (value) => {159return isHtmlElementOfType(value, TagName.FRAME);160};161162/**163* Determines if a value is an <IFRAME> Element.164* @param {*} value165* @return {boolean}166*/167const isHtmlIFrameElement = (value) => {168return isHtmlElementOfType(value, TagName.IFRAME);169};170171/**172* Determines if a value is an <OBJECT> Element.173* @param {*} value174* @return {boolean}175*/176const isHtmlObjectElement = (value) => {177return isHtmlElementOfType(value, TagName.OBJECT);178};179180/**181* Determines if a value is a <SCRIPT> Element.182* @param {*} value183* @return {boolean}184*/185const isHtmlScriptElement = (value) => {186return isHtmlElementOfType(value, TagName.SCRIPT);187};188189exports = {190isElement,191isHtmlElement,192isHtmlElementOfType,193isHtmlAnchorElement,194isHtmlButtonElement,195isHtmlLinkElement,196isHtmlImageElement,197isHtmlAudioElement,198isHtmlVideoElement,199isHtmlInputElement,200isHtmlTextAreaElement,201isHtmlCanvasElement,202isHtmlEmbedElement,203isHtmlFormElement,204isHtmlFrameElement,205isHtmlIFrameElement,206isHtmlObjectElement,207isHtmlScriptElement,208};209210211