From 34cb8ea0b9625c3d5ab8a91dbdc3f84740c1f4d1 Mon Sep 17 00:00:00 2001 From: Chuan Zh Date: Wed, 30 Aug 2017 18:46:57 +0800 Subject: [PATCH 1/2] solve issue https://github.com/medialize/URI.js/issues/338 --- src/URI.fragmentQuery.js | 23 ++++++++++++++++++++--- src/URI.js | 15 +++++++++++++++ test/test_fragmentQuery.js | 32 ++++++++++++++++++++------------ 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/src/URI.fragmentQuery.js b/src/URI.fragmentQuery.js index 4a73441d..1b8391c8 100644 --- a/src/URI.fragmentQuery.js +++ b/src/URI.fragmentQuery.js @@ -19,6 +19,10 @@ // uri.toString() === "http://example.org/#?bar=foo&name=value"; // uri.removeFragment("name"); // uri.toString() === "http://example.org/#?bar=foo"; +// uri.setFragment("name", "value1"); +// uri.toString() === "http://example.org/#?bar=foo&name=value1"; +// uri.setFragment("name", "value2"); +// uri.toString() === "http://example.org/#?bar=foo&name=value2"; (function (root, factory) { 'use strict'; @@ -57,12 +61,12 @@ p.fragment = function(v, build) { var prefix = this._parts.fragmentPrefix; var fragment = this._parts.fragment || ''; - + if (v === true) { if (fragment.substring(0, prefix.length) !== prefix) { return {}; } - + return URI.parseQuery(fragment.substring(prefix.length)); } else if (v !== undefined && typeof v !== 'string') { this._parts.fragment = prefix + URI.buildQuery(v); @@ -96,9 +100,22 @@ this.build(!build); return this; }; + p.setFragment = function(name, value, build) { + var prefix = this._parts.fragmentPrefix; + var data = URI.parseQuery((this._parts.fragment || '').substring(prefix.length)); + URI.setQuery(data, name, value); + this._parts.fragment = prefix + URI.buildQuery(data); + if (typeof name !== 'string') { + build = value; + } + + this.build(!build); + return this; + }; p.addHash = p.addFragment; p.removeHash = p.removeFragment; + p.setHash = p.setFragment; // extending existing object rather than defining something new return URI; -})); \ No newline at end of file +})); diff --git a/src/URI.js b/src/URI.js index 47e6d9a7..0d659d61 100644 --- a/src/URI.js +++ b/src/URI.js @@ -780,6 +780,21 @@ throw new TypeError('URI.addQuery() accepts an object, string as the name parameter'); } }; + + URI.setQuery = function(data, name, value) { + if (typeof name === 'object') { + for (var key in name) { + if (hasOwn.call(name, key)) { + URI.setQuery(data, key, name[key]); + } + } + } else if (typeof name === 'string') { + data[name] = value === undefined ? null : value; + } else { + throw new TypeError('URI.setQuery() accepts an object, string as the name parameter'); + } + }; + URI.removeQuery = function(data, name, value) { var i, length, key; diff --git a/test/test_fragmentQuery.js b/test/test_fragmentQuery.js index a98406bf..7c4d5a4b 100644 --- a/test/test_fragmentQuery.js +++ b/test/test_fragmentQuery.js @@ -5,15 +5,15 @@ module('URI.fragmentQuery'); test('storing query-data in fragment', function() { var u = URI('http://example.org'); - + deepEqual(u.fragment(true), {}, 'empty map for missing fragment'); u = URI('http://example.org/#'); deepEqual(u.fragment(true), {}, 'empty map for empty fragment'); - + u = URI('http://example.org/#?hello=world'); deepEqual(u.fragment(true), {hello: 'world'}, 'reading data object'); - + u.fragment({bar: 'foo'}); deepEqual(u.fragment(true), {bar: 'foo'}, 'setting data object'); equal(u.toString(), 'http://example.org/#?bar=foo', 'setting data object serialized'); @@ -21,39 +21,47 @@ u.addFragment('name', 'value'); deepEqual(u.fragment(true), {bar: 'foo', name: 'value'}, 'adding value'); equal(u.toString(), 'http://example.org/#?bar=foo&name=value', 'adding value serialized'); - + u.removeFragment('bar'); deepEqual(u.fragment(true), {name: 'value'}, 'removing value bar'); equal(u.toString(), 'http://example.org/#?name=value', 'removing value bar serialized'); - + u.removeFragment('name'); deepEqual(u.fragment(true), {}, 'removing value name'); equal(u.toString(), 'http://example.org/#?', 'removing value name serialized'); + + u.setFragment('name', 'value1'); + deepEqual(u.fragment(true), {name: 'value1'}, 'setting name to value1'); + equal(u.toString(), 'http://example.org/#?name=value1', 'setting name to value1 serialized'); + + u.setFragment('name', 'value2'); + deepEqual(u.fragment(true), {name: 'value2'}, 'setting name to value2'); + equal(u.toString(), 'http://example.org/#?name=value2', 'setting name to value2 serialized'); }); test('fragmentPrefix', function() { var u; - + URI.fragmentPrefix = '!'; u = URI('http://example.org'); equal(u._parts.fragmentPrefix, '!', 'init using global property'); - + u.fragment('#?hello=world'); equal(u.fragment(), '?hello=world', 'unparsed ?'); deepEqual(u.fragment(true), {}, 'parsing ? prefix'); - + u.fragment('#!hello=world'); equal(u.fragment(), '!hello=world', 'unparsed !'); deepEqual(u.fragment(true), {hello: 'world'}, 'parsing ! prefix'); - + u.fragmentPrefix('§'); equal(u.fragment(), '!hello=world', 'unparsed §'); deepEqual(u.fragment(true), {}, 'parsing § prefix'); - + u.fragment('#§hello=world'); equal(u.fragment(), '§hello=world', 'unparsed §'); deepEqual(u.fragment(true), {hello: 'world'}, 'parsing § prefix'); - + URI.fragmentPrefix = '?'; }); -})(); \ No newline at end of file +})(); From 79fecfc7b640359afa9e148cb4d75dc7bfa8b3b6 Mon Sep 17 00:00:00 2001 From: Chuan Zh Date: Wed, 6 Sep 2017 15:42:24 +0800 Subject: [PATCH 2/2] Add tests for static method URI.setQuery --- test/test.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/test.js b/test/test.js index d6efe220..385cf4ff 100644 --- a/test/test.js +++ b/test/test.js @@ -1792,6 +1792,44 @@ result = URI.joinPaths('a', '', '', 'b', '', '').toString(); equal(result, 'a/b/', 'trailing empty segment'); }); + test('setQuery', function () { + var o = {foo: 'bar'}; + + URI.setQuery(o, 'foo', 'bam'); + deepEqual(o, {foo: 'bam'}, 'set name, value'); + + URI.setQuery(o, 'array', ['one', 'two']); + deepEqual(o, {foo: 'bam', array: ['one', 'two']}, 'set name, array'); + + URI.setQuery(o, 'foo', 'qux'); + deepEqual(o, {foo: 'qux', array: ['one', 'two']}, 'override name, value'); + + o = {foo: 'bar'}; + URI.setQuery(o, {baz: 'qux'}); + deepEqual(o, {foo: 'bar', baz: 'qux'}, 'set {name: value}'); + + URI.setQuery(o, {bar: ['1', '2']}); + deepEqual(o, {foo: 'bar', bar: ['1', '2'], baz: 'qux'}, 'set {name: array}'); + + URI.setQuery(o, {foo: 'qux'}); + deepEqual(o, {foo: 'qux', bar: ['1', '2'], baz: 'qux'}, 'override {name: value}'); + + o = {foo: 'bar'}; + URI.setQuery(o, {bam: null, baz: ''}); + deepEqual(o, {foo: 'bar', bam: null, baz: ''}, 'set {name: null}'); + + o = {foo: 'bar'}; + URI.setQuery(o, 'empty'); + deepEqual(o, {foo: 'bar', empty: null}, 'set undefined'); + + o = {foo: 'bar'}; + URI.setQuery(o, 'empty', ''); + deepEqual(o, {foo: 'bar', empty: ''}, 'set empty string'); + + o = {}; + URI.setQuery(o, 'some value', 'must be encoded because of = and ? and #'); + deepEqual(o, {'some value': 'must be encoded because of = and ? and #'}, 'encoding'); + }); module('comparing URLs'); test('equals', function() {