1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | 1 1 6 1 6 1 1 2 1 2 1 10 1 9 1 1 1 2 1 1 1 4 1 1 1 4 4 1 2 1 2 2 2 2 1 2 2 2 1 5 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict'; var elements = require('../elements'); /** * Get the node name of an element, lowercased * @memberof elements * @param {HTMLElement} elt * @return {String} */ elements.nodeName = /** @function * @param elt */function (elt) { return elt.nodeName.toLowerCase(); }; /** * Get the document of an element * @memberof elements * @param {HTMLElement} elt * @return {Document} */ elements.document = /** @function * @param elt */function (elt) { return elt && elt.ownerDocument || document; }; /** * Set attributes * @memberof elements * @param {HTMLElement} elt * @param {String[]} attrs */ elements.setAttributes = /** @function * @param elt * @param attrs */function (elt, attrs) { for (var key in attrs) { elements.setAttribute(elt, key, attrs[key]); } }; /* overrided in compat.dom.js */ /** * Get an attribute value * @param {HTMLElement} elt * @param {String} name * @return {String|null} */ elements.getAttribute = /** @function * @param elt * @param name */function (elt, name) { return elt.getAttribute(name) || null; }; /* overrided in compat.dom.js */ /** * Set an attribute value * @param {HTMLElement} elt * @param {String} name * @param {String|null} value */ elements.setAttribute = /** @function * @param elt * @param name * @param value */function (elt, name, value) { if (value === null) { elt.removeAttribute(name); } else { elt.setAttribute(name, value); } }; /** * Remove an attribute * @param {HTMLElement} elt * @param {String} name */ elements.removeAttribute = /** @function * @param elt * @param name */function (elt, name) { elt.removeAttribute(name); }; /* overrided in compat.dom.js */ /** * Get a element's property * @param {HTMLElement} elt * @param {String} name * @return {*} */ elements.getProperty = /** @function * @param elt * @param name */function (elt, name) { return elt[name]; }; /* overrided in compat.dom.js */ /** * Set a element's property * @param {HTMLElement} elt * @param {String} name * @param {*} value */ elements.setProperty = /** @function * @param elt * @param name * @param value */function (elt, name, value) { elt[name] = value; }; /** * Set a element's class attribute * @param {HTMLElement} elt * @param {String} _class */ elements.setClass = /** @function * @param elt * @param _class */function (elt, _class) { elements.setAttribute(elt, 'class', _class); }; /** * Set a element's id attribute * @param {HTMLElement} elt * @param {String} id */ elements.setId = /** @function * @param elt * @param id */function (elt, id) { elements.setAttribute(elt, 'id', id); }; /* overrided in compat.dom.js */ /** * Add a class to an element's class attribute * @param {HTMLElement} elt * @param {String} classes list of class separated by a whitespaces */ elements.addClass = /** @function * @param elt * @param classes */function (elt, classes) { classes = classes.split(' '); elt.classList.add.apply(elt.classList, classes); }; /* overrided in compat.dom.js */ /** * Return if a class is in the class list of an element * @param {HTMLElement} elt * @param {String} _class * @return {boolean} */ elements.hasClass = /** @function * @param elt * @param _class */function (elt, _class) { return elt.classList.contains(_class); }; /* overrided in compat.dom.js */ /** * Remove a class to an element's class attribute * @param {HTMLElement} elt * @param {String} classes * @return {boolean} if at least one of the classes was in the class list */ elements.removeClass = /** @function * @param elt * @param classes */function (elt, classes) { var oldLength = elt.classList.length; classes = classes.split(' '); elt.classList.remove.apply(elt.classList, classes); return oldLength !== elt.classList.length; }; /* overrided in compat.dom.js */ /** * Toggle a class to an element's class attribute * @param {HTMLElement} elt * @param {String} _class * @return {boolean} if the class was added the class list (vs false if removed) */ elements.toggleClass = /** @function * @param elt * @param _class */function (elt, _class) { var oldLength = elt.classList.length; elt.classList.toggle(_class); return oldLength < elt.classList.length; }; /** * Get the value of an input, select or option * @param {HTMLElement} elt * @return {String|undefined|null|DOMString|boolean} */ elements.getValue = /** @function * @param elt */function (elt) { switch (elements.nodeName(elt)) { // case 'input': /* even for radio and checkbox : we want to return value */ // return elt.value; case 'select': var options = elt.options, index = elt.selectedIndex; Eif (elt.type === 'select-one') { return index >= 0 ? elements.getValue(options[index]) : null; } var values = []; for (var i = 0, length = options.length; i < length; i++) { var opt = options[i]; if (opt.selected) { values.push(elements.getValue(opt)); } } return values; case 'option': var val = elements.getAttribute(elt, 'value'); return val !== null ? val : elt.text; } return elt.value; }; /** * Set the value of an input, select or option * @param {HTMLElement} elt * @param {String|undefined|null|DOMString|boolean} value */ elements.setValue = /** @function * @param elt * @param value */function (elt, value) { switch (elements.nodeName(elt)) { case 'input': switch (elt.type) { case 'checkbox': case 'radio': elt.checked = !!value; return; default: elt.value = '' + value; } return; case 'select': var options = elt.options; if (typeof value !== 'function') { if (Array.isArray(value)) { value = ( /** @function * @param value */function (value) { return this.indexOf(value) !== -1; }).bind(value); } else { value = ( /** @function * @param value */function (value) { return this === value; }).bind(value); } } for (var i = 0, length = options.length; i < length; i++) { var opt = options[i]; opt.selected = value(elements.getValue(opt)); } break; case 'option': // TODO setValue( select parent, option value ) break; default: throw new Error(); } }; var _regexpGetAlias = /-(\w)/g; elements.styleAlias = { float: 'cssFloat' in document.documentElement.style ? 'cssFloat' : 'styleFloat' }; elements.styleHooks = {}; elements._getStyleAlias = /** @function * @param elt * @param property */function (elt, property) { if (!elements.styleAlias[property]) { elements.styleAlias[property] = property.replace(_regexpGetAlias, function (m, l) { return l.toUpperCase(); }); } return elements.styleAlias[property]; }; /* overrided in compat.dom.js */ /** * Get the style property * @param {HTMLElement} elt * @param {String} property * @return {String|undefined} */ elements.getStyle = /** @function * @param elt * @param property */function (elt, property) { var prop = elements._getStyleAlias(elt, property), handler = elements.styleHooks[prop]; if (handler && handler.get) { return handler.get(elt); } var computed = window.getComputedStyle(elt, null); return computed.getPropertyValue(prop) || computed[prop]; }; /** * Get the style property * @param {HTMLElement} elt * @param {String} property * @return {Number} */ elements.getIntStyle = /** @function * @param elt * @param property */function (elt, property) { return parseInt(elements.getStyle(elt, property) || 0, 10); }; /** * Set a style property * @param {HTMLElement} elt * @param {String} property * @param {String} value * @return {Number} */ elements.setStyle = /** @function * @param elt * @param property * @param value */function (elt, property, value) { var prop = elements._getStyleAlias(elt, property), handler = elements.styleHooks[prop]; if (handler && handler.set) { return handler.set(elt); } elt.style[prop] = value; }; /** * Get the inner content as html * @param {HTMLElement} elt * @return {String|null} */ elements.getHtml = /** @function * @param elt */function (elt) { return elements.getProperty(elt, 'innerHTML'); }; /** * Get the inner content as text * @param {HTMLElement} elt * @return {String|null} */ elements.getText = /** @function * @param elt */function (elt) { return elements.getProperty(elt, 'textContent'); }; /** * Check if the element matches the selectors * @param {HTMLElement} elt * @param {String} selectors * @return {boolean} */ elements.matches = /** @function * @param elt * @param selectors */function (elt, selectors) { return elt.matches(selectors); }; /** * Check if the input element is editable (!disabled and !readonly) * @param {HTMLElement} elt * @return {boolean} */ elements.isEditable = /** @function * @param elt */function (elt) { if (elements.nodeName(elt) === 'input') { return !elt.disabled && !elt.readonly; } }; //# sourceMappingURL=dom.js.map |