Browse Source

Added extra checks to ensure Node.js implementation doesn't break

Jasper Dansercoer 5 years ago
parent
commit
8fa8b0391e

File diff suppressed because it is too large
+ 58 - 37
dist/inputmask.js


File diff suppressed because it is too large
+ 1 - 1
dist/inputmask.min.js


File diff suppressed because it is too large
+ 56 - 35
dist/jquery.inputmask.js


File diff suppressed because it is too large
+ 1 - 1
dist/jquery.inputmask.min.js


+ 7 - 0
lib/canUseDOM.js

@@ -0,0 +1,7 @@
+const canUseDOM = !!(
+  typeof window !== "undefined" &&
+  window.document &&
+  window.document.createElement
+);
+
+export default canUseDOM;

+ 10 - 7
lib/dependencyLibs/events.js

@@ -1,6 +1,7 @@
 import extend from "./extend";
 import window from "../global/window";
 import DependencyLib from "./inputmask.dependencyLib";
+import canUseDOM from "../canUseDOM";
 
 export {on, off, trigger, Event};
 
@@ -12,13 +13,15 @@ let Event;
 if (typeof window.CustomEvent === "function") {
 	Event = window.CustomEvent;
 } else {
-	Event = function (event, params) {
-		params = params || {bubbles: false, cancelable: false, detail: undefined};
-		var evt = document.createEvent("CustomEvent");
-		evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
-		return evt;
-	};
-	Event.prototype = window.Event.prototype;
+	if (canUseDOM) {
+		Event = function (event, params) {
+			params = params || {bubbles: false, cancelable: false, detail: undefined};
+			var evt = document.createEvent("CustomEvent");
+			evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
+			return evt;
+		};
+		Event.prototype = window.Event.prototype;
+	}
 }
 
 

+ 2 - 0
lib/environment.js

@@ -1,3 +1,5 @@
+import window from "./global/window";
+
 const ua = (window.navigator && window.navigator.userAgent) || "",
 	ie = (ua.indexOf("MSIE ") > 0) || (ua.indexOf("Trident/") > 0),
 	mobile = "ontouchstart" in window, //not entirely correct but will currently do

+ 2 - 1
lib/global/window.js

@@ -1,2 +1,3 @@
-export default typeof window !== "undefined" ? window : new (eval("require('jsdom').JSDOM"))("").window;
+import canUseDOM from "../canUseDOM";
 
+export default canUseDOM ? window : {};

+ 5 - 0
lib/inputmask.js

@@ -18,10 +18,15 @@ import {checkVal, unmaskedvalue} from "./inputHandling";
 import {EventRuler} from "./eventruler";
 import definitions from "./definitions";
 import defaults from "./defaults";
+import canUseDOM from "./canUseDOM";
 
 const document = window.document, dataKey = "_inputmask_opts";
 
 function Inputmask(alias, options, internal) {
+	if (!canUseDOM) {
+		return;
+	}
+	
 	//allow instanciating without new
 	if (!(this instanceof Inputmask)) {
 		return new Inputmask(alias, options, internal);

+ 2 - 1
lib/inputmaskElement.js

@@ -1,11 +1,12 @@
 import window from "./global/window";
 import Inputmask from "./inputmask";
+import canUseDOM from "./canUseDOM";
 
 const document = window.document;
 
 // add check if it is supported by the browser
 // integrate shadowroot into maskcope
-if (document && document.head && document.head.attachShadow && window.customElements && window.customElements.get("input-mask") === undefined) {
+if (canUseDOM && document && document.head && document.head.attachShadow && window.customElements && window.customElements.get("input-mask") === undefined) {
 	class InputmaskElement extends HTMLElement {
 		constructor() {
 			super();