Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (62 loc) · 1.84 KB
/
Copy pathindex.js
File metadata and controls
69 lines (62 loc) · 1.84 KB
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
'use strict';
var toArray = require('to-array');
/**
* Retrieves a form control from the given root element. The ideal use-case is
* for a `<form>` or `<fieldset>`, (via their `elements` API) but arbitrary DOM
* elements work as well. (via `querySelectorAll`)
*
* @param {HTMLElement} root The root to search from. (usually a <form>)
* @param {String} name The control name to find.
* @return {HTMLElement|Array}
*/
module.exports = function (root, name) {
if (!isElement(root)) {
throw new TypeError('a root element is required');
}
if ('namedItem' in root) {
// the short-circuit here is because IE won't find things like <fieldset>
// even when they have an assigned name
return normalize(root.namedItem(name) || bruteForce(root, name));
} else if (root.elements) {
return normalize(root.elements.namedItem(name));
} else {
return normalize(bruteForce(root, name));
}
};
/**
* When searching an arbitrary element (or for browsers that don't support
* the elements list properly)
*
* @param {HTMLElement} root The element to search within.
* @param {String} name The control name to search for.
* @return {NodeList}
*/
function bruteForce(root, name) {
return root.querySelectorAll('[name=\'' + name + '\']');
}
/**
* Normalizes the value returned by the API:
* - when empty, return `null`
* - when only a single node, return that node directly
* - when a `NodeList`, return an `Array` instead
*
* @param {Mixed} ret The value to normalize.
* @return {Mixed}
*/
function normalize(ret) {
if (!ret) {
return null;
} else if (isElement(ret)) {
return ret;
} else if (ret.length === 0) {
return null;
} else if (ret.length === 1) {
return ret[0];
} else {
return toArray(ret);
}
}
function isElement(el) {
if (!el) return false;
return el.nodeType === 1;
}