From b076b60c7c3b20dd6c586a47a8f7b4c1742efa5b Mon Sep 17 00:00:00 2001 From: Jordan Becker Date: Sat, 13 Feb 2021 17:34:02 +0100 Subject: [PATCH] Use previousElementSibling instead of querySelector to get canvas node reference This prevents crashes when previous elements of the page share the same class as the defined canvasClass. Fixes #23 --- dist/js/index.js | 2 +- dist/js/index.min.js | 2 +- src/js/index.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dist/js/index.js b/dist/js/index.js index 4bd668b..d779c11 100644 --- a/dist/js/index.js +++ b/dist/js/index.js @@ -33,7 +33,7 @@ var setCaptcha = function setCaptcha($el, options, shouldReset) { if (!shouldReset) { $el.insertAdjacentHTML('beforebegin', "\n \n ")); - this.$captchaEl = document.querySelector(".".concat(options.canvasClass)); + this.$captchaEl = $el.previousElementSibling; this.$captchaTextContext = this.$captchaEl.getContext('2d'); this.$captchaTextContext = Object.assign(this.$captchaTextContext, options.canvasStyle); } diff --git a/dist/js/index.min.js b/dist/js/index.min.js index 3a50a54..0d0e7fd 100644 --- a/dist/js/index.min.js +++ b/dist/js/index.min.js @@ -1 +1 @@ -!function(t,e){void 0===t&&void 0!==window&&(t=window),"function"==typeof define&&define.amd?define([],function(){return t.jCaptcha=e()}):"object"==typeof module&&module.exports?module.exports=e():t.jCaptcha=e()}(this,function(){"use strict";function t(){i=Math.round(8*Math.random())+1,n=Math.round(8*Math.random())+1,c=i+n}function e(t,e,a){a||(t.insertAdjacentHTML("beforebegin",'\n \n ')),this.$captchaEl=document.querySelector(".".concat(e.canvasClass)),this.$captchaTextContext=this.$captchaEl.getContext("2d"),this.$captchaTextContext=Object.assign(this.$captchaTextContext,e.canvasStyle)),this.$captchaTextContext.clearRect(0,0,e.canvasStyle.width,e.canvasStyle.height),this.$captchaTextContext.fillText("".concat(i," + ").concat(n," ").concat(e.requiredValue),0,0)}function a(t){var e=0\n \n ')),this.$captchaEl=t.previousElementSibling,this.$captchaTextContext=this.$captchaEl.getContext("2d"),this.$captchaTextContext=Object.assign(this.$captchaTextContext,e.canvasStyle)),this.$captchaTextContext.clearRect(0,0,e.canvasStyle.width,e.canvasStyle.height),this.$captchaTextContext.fillText("".concat(c," + ").concat(n," ").concat(e.requiredValue),0,0)}function a(){var t=0 `); - this.$captchaEl = document.querySelector(`.${options.canvasClass}`); + this.$captchaEl = $el.previousElementSibling; this.$captchaTextContext = this.$captchaEl.getContext('2d'); this.$captchaTextContext = Object.assign(this.$captchaTextContext, options.canvasStyle);