Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | this.EXPORTED_SYMBOLS = ["TelURIParser"]; |
michael@0 | 8 | |
michael@0 | 9 | /** |
michael@0 | 10 | * Singleton providing functionality for parsing tel: and sms: URIs |
michael@0 | 11 | */ |
michael@0 | 12 | this.TelURIParser = { |
michael@0 | 13 | parseURI: function(scheme, uri) { |
michael@0 | 14 | // https://www.ietf.org/rfc/rfc2806.txt |
michael@0 | 15 | let subscriber = decodeURIComponent(uri.slice((scheme + ':').length)); |
michael@0 | 16 | |
michael@0 | 17 | if (!subscriber.length) { |
michael@0 | 18 | return null; |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | let number = ''; |
michael@0 | 22 | let pos = 0; |
michael@0 | 23 | let len = subscriber.length; |
michael@0 | 24 | |
michael@0 | 25 | // visual-separator |
michael@0 | 26 | let visualSeparator = [ ' ', '-', '.', '(', ')' ]; |
michael@0 | 27 | let digits = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ]; |
michael@0 | 28 | let dtmfDigits = [ '*', '#', 'A', 'B', 'C', 'D' ]; |
michael@0 | 29 | let pauseCharacter = [ 'p', 'w' ]; |
michael@0 | 30 | |
michael@0 | 31 | // global-phone-number |
michael@0 | 32 | if (subscriber[pos] == '+') { |
michael@0 | 33 | number += '+'; |
michael@0 | 34 | for (++pos; pos < len; ++pos) { |
michael@0 | 35 | if (visualSeparator.indexOf(subscriber[pos]) != -1) { |
michael@0 | 36 | number += subscriber[pos]; |
michael@0 | 37 | } else if (digits.indexOf(subscriber[pos]) != -1) { |
michael@0 | 38 | number += subscriber[pos]; |
michael@0 | 39 | } else { |
michael@0 | 40 | break; |
michael@0 | 41 | } |
michael@0 | 42 | } |
michael@0 | 43 | } |
michael@0 | 44 | // local-phone-number |
michael@0 | 45 | else { |
michael@0 | 46 | for (; pos < len; ++pos) { |
michael@0 | 47 | if (visualSeparator.indexOf(subscriber[pos]) != -1) { |
michael@0 | 48 | number += subscriber[pos]; |
michael@0 | 49 | } else if (digits.indexOf(subscriber[pos]) != -1) { |
michael@0 | 50 | number += subscriber[pos]; |
michael@0 | 51 | } else if (dtmfDigits.indexOf(subscriber[pos]) != -1) { |
michael@0 | 52 | number += subscriber[pos]; |
michael@0 | 53 | } else if (pauseCharacter.indexOf(subscriber[pos]) != -1) { |
michael@0 | 54 | number += subscriber[pos]; |
michael@0 | 55 | } else { |
michael@0 | 56 | break; |
michael@0 | 57 | } |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | // this means error |
michael@0 | 61 | if (!number.length) { |
michael@0 | 62 | return null; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | // isdn-subaddress |
michael@0 | 66 | if (subscriber.substring(pos, pos + 6) == ';isub=') { |
michael@0 | 67 | let subaddress = ''; |
michael@0 | 68 | |
michael@0 | 69 | for (pos += 6; pos < len; ++pos) { |
michael@0 | 70 | if (visualSeparator.indexOf(subscriber[pos]) != -1) { |
michael@0 | 71 | subaddress += subscriber[pos]; |
michael@0 | 72 | } else if (digits.indexOf(subscriber[pos]) != -1) { |
michael@0 | 73 | subaddress += subscriber[pos]; |
michael@0 | 74 | } else { |
michael@0 | 75 | break; |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | // FIXME: ignore subaddress - Bug 795242 |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | // post-dial |
michael@0 | 83 | if (subscriber.substring(pos, pos + 7) == ';postd=') { |
michael@0 | 84 | let subaddress = ''; |
michael@0 | 85 | |
michael@0 | 86 | for (pos += 7; pos < len; ++pos) { |
michael@0 | 87 | if (visualSeparator.indexOf(subscriber[pos]) != -1) { |
michael@0 | 88 | subaddress += subscriber[pos]; |
michael@0 | 89 | } else if (digits.indexOf(subscriber[pos]) != -1) { |
michael@0 | 90 | subaddress += subscriber[pos]; |
michael@0 | 91 | } else if (dtmfDigits.indexOf(subscriber[pos]) != -1) { |
michael@0 | 92 | subaddress += subscriber[pos]; |
michael@0 | 93 | } else if (pauseCharacter.indexOf(subscriber[pos]) != -1) { |
michael@0 | 94 | subaddress += subscriber[pos]; |
michael@0 | 95 | } else { |
michael@0 | 96 | break; |
michael@0 | 97 | } |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | // FIXME: ignore subaddress - Bug 795242 |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | // area-specific |
michael@0 | 104 | if (subscriber.substring(pos, pos + 15) == ';phone-context=') { |
michael@0 | 105 | pos += 15; |
michael@0 | 106 | |
michael@0 | 107 | // global-network-prefix | local-network-prefix | private-prefi |
michael@0 | 108 | number = subscriber.substring(pos, subscriber.length) + number; |
michael@0 | 109 | } |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | // Ignore MWI and USSD codes. See 794034. |
michael@0 | 113 | if (number.match(/[#\*]/) && !number.match(/^[#\*]\d+$/)) { |
michael@0 | 114 | return null; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | return number || null; |
michael@0 | 118 | } |
michael@0 | 119 | }; |
michael@0 | 120 |