Skip to content

Commit

Permalink
Prevents pre-existing classes to be wiped and fixes the export for no…
Browse files Browse the repository at this point in the history
…dejs
  • Loading branch information
rogeriotaques committed Feb 13, 2019
1 parent b7767d3 commit 4691113
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
"browser": true,
"node": true
},
"extends": ["prettier-airbnb"],
"extends": [
"prettier-airbnb"
],
"rules": {
"no-extra-semi": "off",
"operator-linebreak": "off",
"object-curly-spacing": "off",
"prefer-template": "off",
"space-before-function-paren": "off",
"wrap-iife": "off"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "detectr.js",
"version": "1.8.0",
"version": "1.8.1",
"description": "Empowers websites with target classes to be stylized.",
"main": "dist/detectr.js",
"scripts": {
Expand Down
32 changes: 18 additions & 14 deletions src/detectr.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* to be discontinued. (http://rafael.adm.br/css_browser_selector/).
*/

;(function detecterjs($) {
const version = '1.8.0'
const DetectrJs = (function detecterjs($) {
const version = '1.8.1'

/**
* Whenever .trim() isn't supported, makes it be.
Expand Down Expand Up @@ -48,7 +48,7 @@
*/
let detect = function detect() {
const rendered = []
const {implementation} = doc
const { implementation } = doc
const webkitVersion = /applewebkit\/(\d{1,})/.test(ua) ? RegExp.$1 : false

let sysVersion = ''
Expand Down Expand Up @@ -289,16 +289,20 @@
return rendered
}

// retrieve current classes attached
let currentClassNames = doc.documentElement.className.split(' ')

// convert 'detect' from function to array
// and avoid unnecessary processing
detect = detect()

// inject the information in the HTML tag.
// getting the classes list from the original documentElement
// helps prevent wiping classes included while detectr.js execution.
element.className = doc.documentElement.className
.split(' ')
.concat(detect)
// concat all detected classes to the existing ones and make sure they are unique
// this prevent wiping pre-existing classes attached by different processes.
currentClassNames = currentClassNames.concat(detect)
currentClassNames = currentClassNames.filter((v, i) => currentClassNames.indexOf(v) === i)

// inject the new classes set in the HTML tag.
element.className = currentClassNames
.join(' ')
.trim()

Expand All @@ -309,9 +313,6 @@
}
}

// make detectr return available on global scope of console.
const originalClassNames = element.className

// execute and exposes detectr.js to the browser
// eslint-disable-next-line
$.detectr = detectr($.navigator.userAgent)
Expand All @@ -320,7 +321,6 @@
* The listener engine for resize event ...
*/
const resizing = function resizing() {
element.className = originalClassNames
$.detectr = detectr($.navigator.userAgent) // eslint-disable-line
}

Expand All @@ -332,4 +332,8 @@
} else if ($.addEventListener) {
$.addEventListener('resize', resizing, true)
}
})(window)
})

DetectrJs(window)

export default DetectrJs

0 comments on commit 4691113

Please sign in to comment.