]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
encoding wip
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 15 Jan 2020 16:36:43 +0000 (17:36 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 15 Jan 2020 16:37:03 +0000 (17:37 +0100)
src/history/common.ts
src/router.ts
src/utils/encoding.ts

index c8a33f931f78f6a0f2e4ba7bbe2806a1575c63b7..8149060ab24f76ae3984498f25e47c27d184a117 100644 (file)
@@ -1,4 +1,5 @@
 import { ListenerRemover } from '../types'
+import { encodeQueryProperty, encodeHash, encodePath } from '../utils/encoding'
 
 export type HistoryQuery = Record<string, string | string[]>
 // TODO: is it reall worth allowing null to form queries like ?q&b&c
@@ -95,9 +96,6 @@ export interface RouterHistory {
 
 // Generic utils
 
-// needed for the global flag
-const PERCENT_RE = /%/g
-
 /**
  * Transforms an URI into a normalized history location
  * @param location URI to normalize
@@ -141,6 +139,9 @@ export function parseURL(location: string): HistoryLocationNormalized {
   }
 }
 
+// TODO: the encoding would be handled at a router level instead where encoding functions can be customized
+// that way the matcher can encode/decode params properly
+
 function safeDecodeUriComponent(value: string): string {
   try {
     value = decodeURIComponent(value)
@@ -182,8 +183,6 @@ export function parseQuery(search: string): HistoryQuery {
   const searchParams = (hasLeadingIM ? search.slice(1) : search).split('&')
   for (let i = 0; i < searchParams.length; ++i) {
     let [key, value] = searchParams[i].split('=')
-    key = safeDecodeUriComponent(key)
-    value = safeDecodeUriComponent(value)
     if (key in query) {
       // an extra variable for ts types
       let currentValue = query[key]
@@ -206,7 +205,9 @@ export function stringifyURL(location: HistoryLocation): string {
   let url = location.path
   let query = location.query ? stringifyQuery(location.query) : ''
 
-  return url + (query && '?' + query) + (location.hash || '')
+  return (
+    encodePath(url) + (query && '?' + query) + encodeHash(location.hash || '')
+  )
 }
 
 /**
@@ -215,7 +216,6 @@ export function stringifyURL(location: HistoryLocation): string {
  */
 export function stringifyQuery(query: RawHistoryQuery): string {
   let search = ''
-  // TODO: util function?
   for (const key in query) {
     if (search.length > 1) search += '&'
     const value = query[key]
@@ -224,13 +224,13 @@ export function stringifyQuery(query: RawHistoryQuery): string {
       search += key
       continue
     }
-    let encodedKey = safeEncodeUriComponent(key)
+    const encodedKey = encodeQueryProperty(key)
     let values: string[] = Array.isArray(value) ? value : [value]
-    values = values.map(safeEncodeUriComponent)
+    const encodedValues = values.map(encodeQueryProperty)
 
     search += `${encodedKey}=${values[0]}`
     for (let i = 1; i < values.length; i++) {
-      search += `&${encodedKey}=${values[i]}`
+      search += `&${encodedKey}=${encodedValues[i]}`
     }
   }
 
@@ -248,16 +248,6 @@ export function normalizeQuery(query: RawHistoryQuery): HistoryQuery {
   return normalizedQuery
 }
 
-// FIXME: not used
-/**
- * Prepare a URI string to be passed to pushState
- * @param uri
- */
-export function prepareURI(uri: string) {
-  // encode the % symbol so it also works on IE
-  return uri.replace(PERCENT_RE, '%25')
-}
-
 // use regular decodeURI
 // Use a renamed export instead of global.decodeURI
 // to support node and browser at the same time
index 4dabd2b712dfd0a8e7cca338834874e04f7e7939..3518dd050d151e5a8d92dfdba07ffda8d306dfa9 100644 (file)
@@ -49,6 +49,7 @@ export interface RouterOptions {
   history: RouterHistory
   routes: RouteRecord[]
   scrollBehavior?: ScrollBehavior
+  // TODO: allow customizing encoding functions
 }
 
 export interface Router {
index 4f18ab73bc88867b2f41965a477cfae5c9ad533b..41f69b0b7e9cca1b30ad972139b8d1a2fb378c08 100644 (file)
@@ -54,11 +54,14 @@ export function encodeQueryProperty(text: string): string {
     .replace(ENC_CARET_RE, '^')
 }
 
-export function encodeParam(text: string): string {
+export function encodePath(text: string): string {
   return commonEncode(text)
-    .replace(SLASH_RE, '%2F')
     .replace(HASH_RE, '%23')
     .replace(IM_RE, '%3F')
 }
 
+export function encodeParam(text: string): string {
+  return encodePath(text).replace(SLASH_RE, '%2F')
+}
+
 export const decode = decodeURIComponent