datalist-polyfill
Advanced tools
Comparing version 1.8.0 to 1.8.1
{ | ||
"name": "datalist-polyfill", | ||
"description": "An extremely lightweight and library-dependency-free vanilla JavaScript datalist polyfill.", | ||
"version": "1.8.0", | ||
"version": "1.8.1", | ||
"homepage": "https://github.com/mfranzke/datalist-polyfill", | ||
@@ -6,0 +6,0 @@ "authors": [{ |
@@ -131,4 +131,3 @@ /* | ||
// manipulating the option inner text, that would get displayed | ||
var label = opt.label || opt.innerText.trim(), | ||
var label = opt.label, | ||
labelValueSeperator = ' / ', | ||
@@ -138,7 +137,13 @@ labelOptionPart = label.substr(0, optionValue.length + labelValueSeperator.length), | ||
if ( label !== optionValue && labelOptionPart !== optionPart ) { | ||
// the innertext should be value / label in case of that they are different | ||
if ( label && label !== optionValue && labelOptionPart !== optionPart ) { | ||
opt.innerText = optionValue + labelValueSeperator + label; | ||
// the label should be either value / label in case of that they are different, or just the option elements value | ||
opt.label = ( label ) ? optionValue + labelValueSeperator + label : optionValue; | ||
} | ||
// remove the label attribute, as it's being displayed differently on desktop and iOS Safari for our construct | ||
opt.setAttribute( 'data-label', opt.getAttribute( 'label' ) ); | ||
opt.removeAttribute( 'label' ); | ||
} else // manipulating the option inner text, that would get displayed | ||
if ( !opt.innerText.trim() ) { | ||
opt.innerText = optionValue; | ||
} | ||
@@ -145,0 +150,0 @@ newSelectValues.appendChild( opt ); |
@@ -6,2 +6,2 @@ /* | ||
*/ | ||
!function(){"use strict";if(!("list"in document.createElement("input"))||(!document.createElement("datalist")||!window.HTMLDataListElement)){!function(e){e&&e.prototype&&void 0===e.prototype.list&&Object.defineProperty(e.prototype,"list",{get:function(){if("object"==typeof this&&this instanceof e){var t=this.getAttribute("list");return document.getElementById(t)}return null}})}(window.HTMLInputElement),function(e){e&&e.prototype&&void 0===e.prototype.options&&Object.defineProperty(e.prototype,"options",{get:function(){if("object"==typeof this&&this instanceof e)return this.getElementsByTagName("option")}})}(window.HTMLElement);var e=!0,t=13,i=27,r=38,n=40,a=["text","email","number","search","tel","url"];window.addEventListener("touchstart",function t(){e=!1,window.removeEventListener("touchstart",t)});var o=function(t){var i=t.target,r=i.tagName.toLowerCase();if(r&&"input"===r&&i.getAttribute("list")){var n=i.getAttribute("list"),a=document.getElementById(n);if(null!==a){var o=a.getElementsByTagName("select")[0];if(void 0!==o){if(27===t.keyCode)return o.setAttributeNode(document.createAttribute("hidden")),void o.setAttribute("aria-hidden","true");var d=a.getElementsByTagName("option"),u=i.value,l=document.createDocumentFragment(),s=document.createDocumentFragment(),c=!1;if("email"===i.type&&i.multiple){var p=u.split(","),m=p.length-1;u=p[m].trim()}if(""!==u){Array.prototype.slice.call(d).sort(function(e,t){return e.value.localeCompare(t.value)}).forEach(function(e){var t=e.value;if(""!==t&&-1!==t.toLowerCase().indexOf(u.toLowerCase())&&!1===e.disabled){var i=e.label||e.innerText.trim(),r=" / ",n=i.substr(0,t.length+" / ".length),a=t+" / ";i!==t&&n!==a&&(e.label=i?t+" / "+i:t),l.appendChild(e),c=!0}else s.appendChild(e)}),o.appendChild(l);var b=0,g=o.options.length-1;if(e){if(-1===o.selectedIndex)switch(t.keyCode){case 38:o.selectedIndex=g;break;case 40:o.selectedIndex=0;break}}else o.selectedIndex=0;a.appendChild(s)}c?o.removeAttribute("hidden"):o.setAttributeNode(document.createAttribute("hidden")),o.setAttribute("aria-hidden",(!c).toString()),38!==t.keyCode&&40!==t.keyCode||o.focus()}}}},d=function(t){var i=t.target,r=i.tagName.toLowerCase(),n=i.type,l=window.getComputedStyle(i);if(r&&"input"===r&&i.getAttribute("list")&&a.indexOf(n)>-1){var s=t.type,c=i.getAttribute("list"),p=document.getElementById(c);if(null!==p){var m=p.getElementsByTagName("select")[0],b="focus"===s&&""!==t.target.value||t.relatedTarget&&t.relatedTarget===m,g=p.title;if(void 0===m){var v=i.getClientRects(),f=parseFloat(l.getPropertyValue("margin-right")),y=parseFloat(l.getPropertyValue("margin-left"));if(m=document.createElement("select"),e&&m.setAttribute("multiple","true"),m.setAttributeNode(document.createAttribute("hidden")),m.style.position="absolute",m.setAttribute("aria-hidden","true"),m.setAttribute("aria-live","polite"),m.setAttribute("role","listbox"),"rtl"===l.getPropertyValue("direction")?m.style.marginRight="-"+(v[0].width+y)+"px":m.style.marginLeft="-"+(v[0].width+f)+"px",m.style.borderRadius=l.getPropertyValue("border-radius"),m.style.marginTop=v[0].height+"px",m.style.minWidth=v[0].width+"px",!e){var h=document.createElement("option");h.innerText=g,h.disabled=!0,m.appendChild(h)}p.appendChild(m),e?m.addEventListener("mouseup",u):m.addEventListener("change",u),m.addEventListener("blur",u),m.addEventListener("keyup",u),i.setAttribute("autocomplete","off"),i.setAttribute("role","textbox"),i.setAttribute("aria-haspopup","true"),i.setAttribute("aria-autocomplete","list"),i.setAttribute("aria-owns",c)}switch(b?m.removeAttribute("hidden"):m.setAttributeNode(document.createAttribute("hidden")),m.setAttribute("aria-hidden",(!b).toString()),s){case"focus":i.addEventListener("keyup",o),i.addEventListener("blur",d,!0);break;case"blur":i.removeEventListener("keyup",o),i.removeEventListener("blur",d,!0);break}}}},u=function(e){var t=e.target,i=t.tagName.toLowerCase();if(i&&("select"===i||"option"===i)){var r="select"===i?t:t.parentNode,n=r.parentNode,a=n.title,o=e.type,d="keyup"===o&&13!==e.keyCode&&27!==e.keyCode;if("change"===o||"mouseup"===o||"keyup"===o&&13===e.keyCode){var u=n.id,l=document.querySelector('input[list="'+u+'"]'),s=r.value;if(null!==l&&s.length>0&&s!==a){var c=l.value,p;"email"===l.type&&l.multiple&&(p=c.lastIndexOf(","))>-1?l.value=c.slice(0,p)+","+s:l.value=s,d=!1}}d?r.removeAttribute("hidden"):r.setAttributeNode(document.createAttribute("hidden")),r.setAttribute("aria-hidden",(!d).toString())}};document.addEventListener("focus",d,!0)}}(); | ||
!function(){"use strict";if(!("list"in document.createElement("input"))||(!document.createElement("datalist")||!window.HTMLDataListElement)){!function(e){e&&e.prototype&&void 0===e.prototype.list&&Object.defineProperty(e.prototype,"list",{get:function(){if("object"==typeof this&&this instanceof e){var t=this.getAttribute("list");return document.getElementById(t)}return null}})}(window.HTMLInputElement),function(e){e&&e.prototype&&void 0===e.prototype.options&&Object.defineProperty(e.prototype,"options",{get:function(){if("object"==typeof this&&this instanceof e)return this.getElementsByTagName("option")}})}(window.HTMLElement);var e=!0,t=13,i=27,r=38,n=40,a=["text","email","number","search","tel","url"];window.addEventListener("touchstart",function t(){e=!1,window.removeEventListener("touchstart",t)});var o=function(t){var i=t.target,r=i.tagName.toLowerCase();if(r&&"input"===r&&i.getAttribute("list")){var n=i.getAttribute("list"),a=document.getElementById(n);if(null!==a){var o=a.getElementsByTagName("select")[0];if(void 0!==o){if(27===t.keyCode)return o.setAttributeNode(document.createAttribute("hidden")),void o.setAttribute("aria-hidden","true");var d=a.getElementsByTagName("option"),u=i.value,l=document.createDocumentFragment(),s=document.createDocumentFragment(),c=!1;if("email"===i.type&&i.multiple){var p=u.split(","),m=p.length-1;u=p[m].trim()}if(""!==u){Array.prototype.slice.call(d).sort(function(e,t){return e.value.localeCompare(t.value)}).forEach(function(e){var t=e.value;if(""!==t&&-1!==t.toLowerCase().indexOf(u.toLowerCase())&&!1===e.disabled){var i=e.label,r=" / ",n=i.substr(0,t.length+" / ".length),a=t+" / ";i&&i!==t&&n!==a?(e.innerText=t+" / "+i,e.setAttribute("data-label",e.getAttribute("label")),e.removeAttribute("label")):e.innerText.trim()||(e.innerText=t),l.appendChild(e),c=!0}else s.appendChild(e)}),o.appendChild(l);var b=0,g=o.options.length-1;if(e){if(-1===o.selectedIndex)switch(t.keyCode){case 38:o.selectedIndex=g;break;case 40:o.selectedIndex=0;break}}else o.selectedIndex=0;a.appendChild(s)}c?o.removeAttribute("hidden"):o.setAttributeNode(document.createAttribute("hidden")),o.setAttribute("aria-hidden",(!c).toString()),38!==t.keyCode&&40!==t.keyCode||o.focus()}}}},d=function(t){var i=t.target,r=i.tagName.toLowerCase(),n=i.type,l=window.getComputedStyle(i);if(r&&"input"===r&&i.getAttribute("list")&&a.indexOf(n)>-1){var s=t.type,c=i.getAttribute("list"),p=document.getElementById(c);if(null!==p){var m=p.getElementsByTagName("select")[0],b="focus"===s&&""!==t.target.value||t.relatedTarget&&t.relatedTarget===m,g=p.title;if(void 0===m){var v=i.getClientRects(),f=parseFloat(l.getPropertyValue("margin-right")),y=parseFloat(l.getPropertyValue("margin-left"));if(m=document.createElement("select"),e&&m.setAttribute("multiple","true"),m.setAttributeNode(document.createAttribute("hidden")),m.style.position="absolute",m.setAttribute("aria-hidden","true"),m.setAttribute("aria-live","polite"),m.setAttribute("role","listbox"),"rtl"===l.getPropertyValue("direction")?m.style.marginRight="-"+(v[0].width+y)+"px":m.style.marginLeft="-"+(v[0].width+f)+"px",m.style.borderRadius=l.getPropertyValue("border-radius"),m.style.marginTop=v[0].height+"px",m.style.minWidth=v[0].width+"px",!e){var h=document.createElement("option");h.innerText=g,h.disabled=!0,m.appendChild(h)}p.appendChild(m),e?m.addEventListener("mouseup",u):m.addEventListener("change",u),m.addEventListener("blur",u),m.addEventListener("keyup",u),i.setAttribute("autocomplete","off"),i.setAttribute("role","textbox"),i.setAttribute("aria-haspopup","true"),i.setAttribute("aria-autocomplete","list"),i.setAttribute("aria-owns",c)}switch(b?m.removeAttribute("hidden"):m.setAttributeNode(document.createAttribute("hidden")),m.setAttribute("aria-hidden",(!b).toString()),s){case"focus":i.addEventListener("keyup",o),i.addEventListener("blur",d,!0);break;case"blur":i.removeEventListener("keyup",o),i.removeEventListener("blur",d,!0);break}}}},u=function(e){var t=e.target,i=t.tagName.toLowerCase();if(i&&("select"===i||"option"===i)){var r="select"===i?t:t.parentNode,n=r.parentNode,a=n.title,o=e.type,d="keyup"===o&&13!==e.keyCode&&27!==e.keyCode;if("change"===o||"mouseup"===o||"keyup"===o&&13===e.keyCode){var u=n.id,l=document.querySelector('input[list="'+u+'"]'),s=r.value;if(null!==l&&s.length>0&&s!==a){var c=l.value,p;"email"===l.type&&l.multiple&&(p=c.lastIndexOf(","))>-1?l.value=c.slice(0,p)+","+s:l.value=s,d=!1}}d?r.removeAttribute("hidden"):r.setAttributeNode(document.createAttribute("hidden")),r.setAttribute("aria-hidden",(!d).toString())}};document.addEventListener("focus",d,!0)}}(); |
{ | ||
"name": "datalist-polyfill", | ||
"version": "1.8.0", | ||
"version": "1.8.1", | ||
"description": "An extremely lightweight and library-dependency-free vanilla JavaScript datalist polyfill.", | ||
@@ -5,0 +5,0 @@ "main": "datalist-polyfill.js", |
@@ -8,3 +8,3 @@ # datalist-polyfill | ||
## Features | ||
* Lightweight: 4.61 kB of JavaScript - less than 1.73 kB gzipped | ||
* Lightweight: 4.7 kB of JavaScript, around 1.75 kB gzipped | ||
* Fully flexible to change the datalist entries / `<option>`s | ||
@@ -68,2 +68,5 @@ * Supporting: | ||
### Version 1.8.1 - 2017/07/18 | ||
Bugfix regarding the handling of the label values. | ||
### Version 1.8.0 - 2017/07/14 | ||
@@ -70,0 +73,0 @@ Restricted the polyfill to only work with relevant input types; we’d like to exclude the ones that even already need another polyfill to „work“ correctly or have a meaningful UI, like e.g. color or date-related ones, as those polyfills should handle the support of the datalist themselves depending on their own functionality. |
Sorry, the diff of this file is not supported yet
36576
328
140