Skip to content

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
Mate Hegedus committed Nov 20, 2017
2 parents ee6dffa + dca21ea commit 70479bc
Show file tree
Hide file tree
Showing 50 changed files with 1,241 additions and 1,172 deletions.
32 changes: 12 additions & 20 deletions modules/DOMUtils.js
@@ -1,17 +1,9 @@
export const canUseDOM = !!(
typeof window !== 'undefined' && window.document && window.document.createElement
typeof window !== "undefined" &&
window.document &&
window.document.createElement
)

export const addEventListener = (node, event, listener) =>
node.addEventListener
? node.addEventListener(event, listener, false)
: node.attachEvent('on' + event, listener)

export const removeEventListener = (node, event, listener) =>
node.removeEventListener
? node.removeEventListener(event, listener, false)
: node.detachEvent('on' + event, listener)

export const getConfirmation = (message, callback) =>
callback(window.confirm(message)) // eslint-disable-line no-alert

Expand All @@ -25,34 +17,34 @@ export const getConfirmation = (message, callback) =>
export const supportsHistory = () => {
const ua = window.navigator.userAgent

if ((ua.indexOf('Android 2.') !== -1 || ua.indexOf('Android 4.0') !== -1) &&
ua.indexOf('Mobile Safari') !== -1 &&
ua.indexOf('Chrome') === -1 &&
ua.indexOf('Windows Phone') === -1
if (
(ua.indexOf("Android 2.") !== -1 || ua.indexOf("Android 4.0") !== -1) &&
ua.indexOf("Mobile Safari") !== -1 &&
ua.indexOf("Chrome") === -1 &&
ua.indexOf("Windows Phone") === -1
)
return false

return window.history && 'pushState' in window.history
return window.history && "pushState" in window.history
}

/**
* Returns true if browser fires popstate on hash change.
* IE10 and IE11 do not.
*/
export const supportsPopStateOnHashChange = () =>
window.navigator.userAgent.indexOf('Trident') === -1
window.navigator.userAgent.indexOf("Trident") === -1

/**
* Returns false if using go(n) with hash history causes a full page reload.
*/
export const supportsGoWithoutReloadUsingHash = () =>
window.navigator.userAgent.indexOf('Firefox') === -1
window.navigator.userAgent.indexOf("Firefox") === -1

/**
* Returns true if a given popstate event is an extraneous WebKit event.
* Accounts for the fact that Chrome on iOS fires real popstate events
* containing undefined state when pressing the back button.
*/
export const isExtraneousPopstateEvent = event =>
event.state === undefined &&
navigator.userAgent.indexOf('CriOS') === -1
event.state === undefined && navigator.userAgent.indexOf("CriOS") === -1
40 changes: 21 additions & 19 deletions modules/LocationUtils.js
@@ -1,32 +1,30 @@
import resolvePathname from 'resolve-pathname'
import valueEqual from 'value-equal'
import { parsePath } from './PathUtils'
import resolvePathname from "resolve-pathname"
import valueEqual from "value-equal"
import { parsePath } from "./PathUtils"

export const createLocation = (path, state, key, currentLocation) => {
let location
if (typeof path === 'string') {
if (typeof path === "string") {
// Two-arg form: push(path, state)
location = parsePath(path)
location.state = state
} else {
// One-arg form: push(location)
location = { ...path }

if (location.pathname === undefined)
location.pathname = ''
if (location.pathname === undefined) location.pathname = ""

if (location.search) {
if (location.search.charAt(0) !== '?')
location.search = '?' + location.search
if (location.search.charAt(0) !== "?")
location.search = "?" + location.search
} else {
location.search = ''
location.search = ""
}

if (location.hash) {
if (location.hash.charAt(0) !== '#')
location.hash = '#' + location.hash
if (location.hash.charAt(0) !== "#") location.hash = "#" + location.hash
} else {
location.hash = ''
location.hash = ""
}

if (state !== undefined && location.state === undefined)
Expand All @@ -38,28 +36,32 @@ export const createLocation = (path, state, key, currentLocation) => {
} catch (e) {
if (e instanceof URIError) {
throw new URIError(
'Pathname "' + location.pathname + '" could not be decoded. ' +
'This is likely caused by an invalid percent-encoding.'
'Pathname "' +
location.pathname +
'" could not be decoded. ' +
"This is likely caused by an invalid percent-encoding."
)
} else {
throw e
}
}

if (key)
location.key = key
if (key) location.key = key

if (currentLocation) {
// Resolve incomplete/relative pathname relative to current location.
if (!location.pathname) {
location.pathname = currentLocation.pathname
} else if (location.pathname.charAt(0) !== '/') {
location.pathname = resolvePathname(location.pathname, currentLocation.pathname)
} else if (location.pathname.charAt(0) !== "/") {
location.pathname = resolvePathname(
location.pathname,
currentLocation.pathname
)
}
} else {
// When there is no prior location and pathname is empty, set it to /
if (!location.pathname) {
location.pathname = '/'
location.pathname = "/"
}
}

Expand Down
40 changes: 19 additions & 21 deletions modules/PathUtils.js
@@ -1,53 +1,51 @@
export const addLeadingSlash = (path) =>
path.charAt(0) === '/' ? path : '/' + path
export const addLeadingSlash = path =>
path.charAt(0) === "/" ? path : "/" + path

export const stripLeadingSlash = (path) =>
path.charAt(0) === '/' ? path.substr(1) : path
export const stripLeadingSlash = path =>
path.charAt(0) === "/" ? path.substr(1) : path

export const hasBasename = (path, prefix) =>
path.toLowerCase().lastIndexOf(prefix.toLowerCase(), 0) === 0 && "/?#".indexOf(path.charAt(prefix.length)) !== -1

export const stripBasename = (path, prefix) =>
hasBasename(path, prefix) ? path.substr(prefix.length) : path

export const stripTrailingSlash = (path) =>
path.charAt(path.length - 1) === '/' ? path.slice(0, -1) : path
export const stripTrailingSlash = path =>
path.charAt(path.length - 1) === "/" ? path.slice(0, -1) : path

export const parsePath = (path) => {
let pathname = path || '/'
let search = ''
let hash = ''
export const parsePath = path => {
let pathname = path || "/"
let search = ""
let hash = ""

const hashIndex = pathname.indexOf('#')
const hashIndex = pathname.indexOf("#")
if (hashIndex !== -1) {
hash = pathname.substr(hashIndex)
pathname = pathname.substr(0, hashIndex)
}

const searchIndex = pathname.indexOf('?')
const searchIndex = pathname.indexOf("?")
if (searchIndex !== -1) {
search = pathname.substr(searchIndex)
pathname = pathname.substr(0, searchIndex)
}

return {
pathname,
search: search === '?' ? '' : search,
hash: hash === '#' ? '' : hash
search: search === "?" ? "" : search,
hash: hash === "#" ? "" : hash
}
}

export const createPath = (location) => {
export const createPath = location => {
const { pathname, search, hash } = location

let path = pathname || '/'
let path = pathname || "/"

if (search && search !== '?')
path += (search.charAt(0) === '?' ? search : `?${search}`)

if (hash && hash !== '#')
path += (hash.charAt(0) === '#' ? hash : `#${hash}`)
if (search && search !== "?")
path += search.charAt(0) === "?" ? search : `?${search}`

if (hash && hash !== "#") path += hash.charAt(0) === "#" ? hash : `#${hash}`

return path
}

0 comments on commit 70479bc

Please sign in to comment.