Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions packages/taro-runtime/src/bom/URL.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isString, isUndefined } from '@tarojs/shared'

import env from '../env'
import { URLSearchParams } from './URLSearchParams'
import { TaroURLSearchParams, URLSearchParams } from './URLSearchParams'

class TaroURL {
static createObjectURL () {
Expand All @@ -19,6 +19,8 @@ class TaroURL {
#port = ''
#protocol = ''
#search: URLSearchParams
#searchModified = false
#searchRaw = ''

constructor (url: string, base?: string) {
if (!isString(url)) url = String(url)
Expand All @@ -31,7 +33,7 @@ class TaroURL {
this.#pathname = pathname || '/'
this.#port = port
this.#protocol = protocol
this.#search = new URLSearchParams(search)
this.#setSearch(search)
}

/* public property */
Expand Down Expand Up @@ -90,14 +92,16 @@ class TaroURL {
}

get search () {
if (!this.#searchModified) return this.#searchRaw

const val = this.#search.toString()
return (val.length === 0 || val.startsWith('?')) ? val : `?${val}`
}

set search (val: string) {
if (isString(val)) {
val = val.trim()
this.#search = new URLSearchParams(val)
this.#setSearch(val)
}
}

Expand Down Expand Up @@ -157,6 +161,19 @@ class TaroURL {
return this.toString()
}

#setSearch (val: string) {
const search = val ? (val.startsWith('?') ? val : `?${val}`) : ''

this.#searchRaw = search
this.#searchModified = false
this.#search = new URLSearchParams(search)
if (this.#search instanceof TaroURLSearchParams) {
this.#search._setOnChange(() => {
this.#searchModified = true
})
}
}

// convenient for deconstructor
_toRaw () {
return {
Expand Down
12 changes: 11 additions & 1 deletion packages/taro-runtime/src/bom/URLSearchParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ function encode (str: string) {
return encodeURIComponent(str).replace(findReg, replacer)
}

export const URLSearchParams = process.env.TARO_PLATFORM === 'web' ? env.window.URLSearchParams : class {
export class TaroURLSearchParams {
#dict = Object.create(null)
#onChange?: () => void

constructor (query) {
query ??= ''
Expand Down Expand Up @@ -81,12 +82,18 @@ export const URLSearchParams = process.env.TARO_PLATFORM === 'web' ? env.window.
}
}

_setOnChange (onChange?: () => void) {
this.#onChange = onChange
}

append (name: string, value: string) {
appendTo(this.#dict, name, value)
this.#onChange?.()
}

delete (name: string) {
delete this.#dict[name]
this.#onChange?.()
}

get (name: string) {
Expand All @@ -109,6 +116,7 @@ export const URLSearchParams = process.env.TARO_PLATFORM === 'web' ? env.window.

set (name: string, value: string) {
this.#dict[name] = ['' + value]
this.#onChange?.()
}

forEach (callback, thisArg) {
Expand Down Expand Up @@ -136,3 +144,5 @@ export const URLSearchParams = process.env.TARO_PLATFORM === 'web' ? env.window.
return query.join('&')
}
}

export const URLSearchParams = process.env.TARO_PLATFORM === 'web' ? env.window.URLSearchParams : TaroURLSearchParams
11 changes: 11 additions & 0 deletions packages/taro-runtime/tests/location.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ describe('location', () => {
expect(url.toString()).toBe('http://taro.com/?a=1&b=2&c=3&d=4')
}

{
const url = new URL('https://example.test/?name=|aaa')
expect(url.search).toBe('?name=|aaa')
expect(url.toString()).toBe('https://example.test/?name=|aaa')
expect(url.searchParams.get('name')).toBe('|aaa')
expect(url.searchParams.toString()).toBe('name=%7Caaa')

url.searchParams.append('age', '18')
expect(url.toString()).toBe('https://example.test/?name=%7Caaa&age=18')
}

// setters
{
const url = new URL('http://taro.com')
Expand Down
Loading