|
231 | 231 | // Will be committed, sent and emptied by a call to trackTrans. |
232 | 232 | ecommerceTransaction = ecommerceTransactionTemplate(), |
233 | 233 |
|
234 | | - // Element types relevant to form tracking |
235 | | - formTrackingNodeNames = { |
236 | | - 'INPUT': true, |
237 | | - 'TEXTAREA': true, |
238 | | - 'SELECT': true |
239 | | - }, |
| 234 | + // Tag names of mutable elements inside a form |
| 235 | + innerElementTags = ['textarea', 'input', 'select'], |
240 | 236 |
|
241 | 237 | outQueueManager = new requestQueue.OutQueueManager(functionName, namespace); |
242 | 238 |
|
|
934 | 930 | * Get an identifier for a form, input, textarea, or select element |
935 | 931 | */ |
936 | 932 | function getFormElementName(elt) { |
937 | | - return elt.name || elt.id || elt.type || elt.nodeName; |
| 933 | + return elt[lodash.find(['name', 'id', 'type', 'nodeName'], function (propName) { |
| 934 | + |
| 935 | + // If elt has a child whose name is "id", that element will be returned |
| 936 | + // instead of the actual id of elt unless we ensure that a string is returned |
| 937 | + return typeof elt[propName] === 'string'; |
| 938 | + })]; |
938 | 939 | } |
939 | 940 |
|
940 | 941 | /* |
|
953 | 954 | * Returns a list of the input, textarea, and select elements inside a form along with their values |
954 | 955 | */ |
955 | 956 | function getInnerFormElements(elt) { |
956 | | - return lodash.map(lodash.filter(elt.children, function (child) { |
957 | | - |
958 | | - // Only include mutable inner elements |
959 | | - return formTrackingNodeNames[child.nodeName.toUpperCase()] && child.type !== 'submit'; |
960 | | - }), function (child) { |
961 | | - var elementJson = { |
962 | | - name: getFormElementName(child), |
963 | | - value: child.value, |
964 | | - nodeName: child.nodeName, |
965 | | - }; |
966 | | - if (child.type && child.nodeName.toUpperCase() === 'INPUT') { |
967 | | - elementJson.type = child.type; |
968 | | - } |
969 | | - return elementJson; |
| 957 | + var innerElements = []; |
| 958 | + var formElements = lodash.map(innerElementTags, function (tagname) { |
| 959 | + lodash.map(elt.getElementsByTagName(tagname), function (child) { |
| 960 | + if (child.type === 'submit') { |
| 961 | + return; |
| 962 | + } |
| 963 | + var elementJson = { |
| 964 | + name: getFormElementName(child), |
| 965 | + value: child.value, |
| 966 | + nodeName: child.nodeName, |
| 967 | + }; |
| 968 | + if (child.type && child.nodeName.toUpperCase() === 'INPUT') { |
| 969 | + elementJson.type = child.type; |
| 970 | + } |
| 971 | + |
| 972 | + if ((child.type === 'checkbox' || child.type === 'radio') && !child.checked) { |
| 973 | + elementJson.value = null; |
| 974 | + } |
| 975 | + innerElements.push(elementJson); |
| 976 | + }); |
970 | 977 | }); |
| 978 | + |
| 979 | + return innerElements; |
971 | 980 | } |
972 | 981 |
|
973 | 982 | /* |
|
977 | 986 | return function (e) { |
978 | 987 | var elt = e.target; |
979 | 988 | var type = elt.nodeName.toUpperCase() === 'INPUT' ? elt.type : null; |
980 | | - core.trackFormChange(getParentFormName(elt), getFormElementName(elt), elt.nodeName, type, lodash.map(elt.classList), elt.value, context); |
| 989 | + var value = (elt.type === 'checkbox' && !elt.checked) ? null : elt.value; |
| 990 | + core.trackFormChange(getParentFormName(elt), getFormElementName(elt), elt.nodeName, type, lodash.map(elt.classList), value, context); |
981 | 991 | }; |
982 | 992 | } |
983 | 993 |
|
|
997 | 1007 | * Add value change event listeners to all mutable inner form elements |
998 | 1008 | */ |
999 | 1009 | function addFormListeners (context) { |
1000 | | - var innerElementTags = ['textarea', 'input', 'select']; |
1001 | 1010 | var trackingMarker = trackerId + 'form'; |
1002 | 1011 |
|
1003 | 1012 | lodash.map(innerElementTags, function (tagname) { |
|
0 commit comments