dataset.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright 2009 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Utilities for adding, removing and setting values in
  16. * an Element's dataset.
  17. * See {@link http://www.w3.org/TR/html5/Overview.html#dom-dataset}.
  18. *
  19. * @author nicksay@google.com (Alex Nicksay)
  20. */
  21. goog.provide('goog.dom.dataset');
  22. goog.require('goog.labs.userAgent.browser');
  23. goog.require('goog.string');
  24. goog.require('goog.userAgent.product');
  25. /**
  26. * Whether using the dataset property is allowed.
  27. *
  28. * In IE (up to and including IE 11), setting element.dataset in JS does not
  29. * propagate values to CSS, breaking expressions such as
  30. * `content: attr(data-content)` that would otherwise work.
  31. * See {@link https://github.com/google/closure-library/issues/396}.
  32. *
  33. * In Safari >= 9, reading from element.dataset sometimes returns
  34. * undefined, even though the corresponding data- attribute has a value.
  35. * See {@link https://bugs.webkit.org/show_bug.cgi?id=161454}.
  36. * @const
  37. * @private
  38. */
  39. goog.dom.dataset.ALLOWED_ =
  40. !goog.userAgent.product.IE && !goog.labs.userAgent.browser.isSafari();
  41. /**
  42. * The DOM attribute name prefix that must be present for it to be considered
  43. * for a dataset.
  44. * @type {string}
  45. * @const
  46. * @private
  47. */
  48. goog.dom.dataset.PREFIX_ = 'data-';
  49. /**
  50. * Sets a custom data attribute on an element. The key should be
  51. * in camelCase format (e.g "keyName" for the "data-key-name" attribute).
  52. * @param {Element} element DOM node to set the custom data attribute on.
  53. * @param {string} key Key for the custom data attribute.
  54. * @param {string} value Value for the custom data attribute.
  55. */
  56. goog.dom.dataset.set = function(element, key, value) {
  57. if (goog.dom.dataset.ALLOWED_ && element.dataset) {
  58. element.dataset[key] = value;
  59. } else {
  60. element.setAttribute(
  61. goog.dom.dataset.PREFIX_ + goog.string.toSelectorCase(key), value);
  62. }
  63. };
  64. /**
  65. * Gets a custom data attribute from an element. The key should be
  66. * in camelCase format (e.g "keyName" for the "data-key-name" attribute).
  67. * @param {Element} element DOM node to get the custom data attribute from.
  68. * @param {string} key Key for the custom data attribute.
  69. * @return {?string} The attribute value, if it exists.
  70. */
  71. goog.dom.dataset.get = function(element, key) {
  72. if (goog.dom.dataset.ALLOWED_ && element.dataset) {
  73. // Android browser (non-chrome) returns the empty string for
  74. // element.dataset['doesNotExist'].
  75. if (!(key in element.dataset)) {
  76. return null;
  77. }
  78. return element.dataset[key];
  79. } else {
  80. return element.getAttribute(
  81. goog.dom.dataset.PREFIX_ + goog.string.toSelectorCase(key));
  82. }
  83. };
  84. /**
  85. * Removes a custom data attribute from an element. The key should be
  86. * in camelCase format (e.g "keyName" for the "data-key-name" attribute).
  87. * @param {Element} element DOM node to get the custom data attribute from.
  88. * @param {string} key Key for the custom data attribute.
  89. */
  90. goog.dom.dataset.remove = function(element, key) {
  91. if (goog.dom.dataset.ALLOWED_ && element.dataset) {
  92. // In strict mode Safari will trigger an error when trying to delete a
  93. // property which does not exist.
  94. if (goog.dom.dataset.has(element, key)) {
  95. delete element.dataset[key];
  96. }
  97. } else {
  98. element.removeAttribute(
  99. goog.dom.dataset.PREFIX_ + goog.string.toSelectorCase(key));
  100. }
  101. };
  102. /**
  103. * Checks whether custom data attribute exists on an element. The key should be
  104. * in camelCase format (e.g "keyName" for the "data-key-name" attribute).
  105. *
  106. * @param {Element} element DOM node to get the custom data attribute from.
  107. * @param {string} key Key for the custom data attribute.
  108. * @return {boolean} Whether the attribute exists.
  109. */
  110. goog.dom.dataset.has = function(element, key) {
  111. if (goog.dom.dataset.ALLOWED_ && element.dataset) {
  112. return key in element.dataset;
  113. } else if (element.hasAttribute) {
  114. return element.hasAttribute(
  115. goog.dom.dataset.PREFIX_ + goog.string.toSelectorCase(key));
  116. } else {
  117. return !!(
  118. element.getAttribute(
  119. goog.dom.dataset.PREFIX_ + goog.string.toSelectorCase(key)));
  120. }
  121. };
  122. /**
  123. * Gets all custom data attributes as a string map. The attribute names will be
  124. * camel cased (e.g., data-foo-bar -> dataset['fooBar']). This operation is not
  125. * safe for attributes having camel-cased names clashing with already existing
  126. * properties (e.g., data-to-string -> dataset['toString']).
  127. * @param {!Element} element DOM node to get the data attributes from.
  128. * @return {!Object} The string map containing data attributes and their
  129. * respective values.
  130. */
  131. goog.dom.dataset.getAll = function(element) {
  132. if (goog.dom.dataset.ALLOWED_ && element.dataset) {
  133. return element.dataset;
  134. } else {
  135. var dataset = {};
  136. var attributes = element.attributes;
  137. for (var i = 0; i < attributes.length; ++i) {
  138. var attribute = attributes[i];
  139. if (goog.string.startsWith(attribute.name, goog.dom.dataset.PREFIX_)) {
  140. // We use substr(5), since it's faster than replacing 'data-' with ''.
  141. var key = goog.string.toCamelCase(attribute.name.substr(5));
  142. dataset[key] = attribute.value;
  143. }
  144. }
  145. return dataset;
  146. }
  147. };