Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
wizard04wsu committed Mar 12, 2022
2 parents 001f633 + 8e60673 commit 6570cff
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This script does not support:

This is a JavaScript module that exports: [`URI`](#uri) (default), [`SegmentedString`](#segmentedstring), [`isDNSDomain`](#isdnsdomain), [`parseMailbox`](#parsemailbox)

[Try it on JSFiddle](https://jsfiddle.net/wizard04/896dmhga/)
[Try it on JSFiddle](https://jsfiddle.net/wizard04/evxog8r2/1/)

---

Expand Down
6 changes: 6 additions & 0 deletions demo/demo.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<input type="text" id="uri" placeholder="Enter a URI here" style="width:100%; box-sizing:border-box;"><br>
<button id="parse_scheme" style="clear:left;">Parse according to scheme</button>
<button id="parse_generic">Parse as a generic URI</button><br>
<br>
<input type="text" id="email" placeholder="Enter an email address here" style="width:100%; box-sizing:border-box;"><br>
<button id="parse_mailbox">Parse email address</button>
46 changes: 46 additions & 0 deletions demo/demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import URI, {isDNSDomain, parseMailbox} from "https://cdn.jsdelivr.net/gh/wizard04wsu/URI_Parsing@jsfiddle-demo/src/uri_parsing.mjs"

document.getElementById("parse_scheme").addEventListener("click", ()=>{
let val = document.getElementById("uri").value;
console.group("scheme");
try{
let obj = URI(val);
console.log("--> "+obj.toString());
console.log(obj);
}catch(e){
if(e instanceof URIError) console.error(e.message);
else throw e;
}finally{
console.groupEnd();
}
}, false);

document.getElementById("parse_generic").addEventListener("click", ()=>{
let val = document.getElementById("uri").value;
console.group("generic");
try{
let obj = URI.parse(val)
console.log("--> "+obj.toString());
console.log(obj);
}catch(e){
if(e instanceof URIError) console.error(e.message);
else throw e;
}finally{
console.groupEnd();
}
}, false);

document.getElementById("parse_mailbox").addEventListener("click", ()=>{
let val = document.getElementById("email").value;
console.group("email");
try{
let obj = parseMailbox(val);
console.log("--> "+obj.toString());
console.log(obj);
}catch(e){
if(e instanceof URIError) console.log(e.message);
else throw e;
}finally{
console.groupEnd();
}
}, false);
37 changes: 20 additions & 17 deletions src/uri_parsing.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -273,24 +273,27 @@ URI.schemeParser = {

p.to = p.path ? splitEmailAddresses(p.path) : [];

let headers = p.query.pairs || [];
let headers = p.query.pairs ? p.query.pairs.map(p=>{return {
name: decodeURIComponent(p.key),
value: p.value
}}) : [];
for(let i=0; i<headers.length; i++){
if(headers[i].value === "") continue;

headers[i].name = decodeURIComponent(headers[i].name);
if(headers[i].name === "to")
p.to = p.to.concat(splitEmailAddresses(headers[i].value));
else if(headers[i].name === "cc")
p.cc = p.cc.concat(splitEmailAddresses(headers[i].value));
else if(headers[i].name === "bcc")
p.bcc = p.bcc.concat(splitEmailAddresses(headers[i].value));
else if(headers[i].name === "subject")
p.subject += decodeURIComponent(headers[i].value);
else if(headers[i].name === "body")
p.body += decodeURIComponent(headers[i].value);
else{
headers[i].value = decodeURIComponent(headers[i].value);
p.headers.push(headers[i]);
switch(headers[i].name){
case "to":
p.to = p.to.concat(splitEmailAddresses(headers[i].value));
break; case "cc":
p.cc = p.cc.concat(splitEmailAddresses(headers[i].value));
break; case "bcc":
p.bcc = p.bcc.concat(splitEmailAddresses(headers[i].value));
break; case "subject":
p.subject += decodeURIComponent(headers[i].value);
break; case "body":
p.body += decodeURIComponent(headers[i].value);
break; default:
headers[i].value = decodeURIComponent(headers[i].value);
p.headers.push(headers[i]);
}
}

Expand Down Expand Up @@ -321,7 +324,7 @@ URI.schemeParser = {
query += "body=" + encodePart(p.body);
}
if(p.headers.length){
for(i=0; i<p.headers.length; i++){
for(let i=0; i<p.headers.length; i++){
if(query) query += "&";
query += encodePart(p.headers[i].name) + "=" + encodePart(p.headers[i].value);
}
Expand Down Expand Up @@ -1023,7 +1026,7 @@ function normalizeQueryOrFragment(queryOrFragment){
* @see [RFC 3986, section 3.4](https://tools.ietf.org/html/rfc3986#section-3.4)
*/
function parseQuery(query, pairSeparator = "&", keyValueSeparator = "="){

const parsed = new SegmentedString(function (){
let result = ""
for(const pair of this.pairs){
Expand Down

0 comments on commit 6570cff

Please sign in to comment.