b2g/components/TelURIParser.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/b2g/components/TelURIParser.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,120 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +this.EXPORTED_SYMBOLS = ["TelURIParser"];
    1.11 +
    1.12 +/**
    1.13 + * Singleton providing functionality for parsing tel: and sms: URIs
    1.14 + */
    1.15 +this.TelURIParser = {
    1.16 +  parseURI: function(scheme, uri) {
    1.17 +    // https://www.ietf.org/rfc/rfc2806.txt
    1.18 +    let subscriber = decodeURIComponent(uri.slice((scheme + ':').length));
    1.19 +
    1.20 +    if (!subscriber.length) {
    1.21 +      return null;
    1.22 +    }
    1.23 +
    1.24 +    let number = '';
    1.25 +    let pos = 0;
    1.26 +    let len = subscriber.length;
    1.27 +
    1.28 +    // visual-separator
    1.29 +    let visualSeparator = [ ' ', '-', '.', '(', ')' ];
    1.30 +    let digits = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ];
    1.31 +    let dtmfDigits = [ '*', '#', 'A', 'B', 'C', 'D' ];
    1.32 +    let pauseCharacter = [ 'p', 'w' ];
    1.33 +
    1.34 +    // global-phone-number
    1.35 +    if (subscriber[pos] == '+') {
    1.36 +      number += '+';
    1.37 +      for (++pos; pos < len; ++pos) {
    1.38 +        if (visualSeparator.indexOf(subscriber[pos]) != -1) {
    1.39 +          number += subscriber[pos];
    1.40 +        } else if (digits.indexOf(subscriber[pos]) != -1) {
    1.41 +          number += subscriber[pos];
    1.42 +        } else {
    1.43 +          break;
    1.44 +        }
    1.45 +      }
    1.46 +    }
    1.47 +    // local-phone-number
    1.48 +    else {
    1.49 +      for (; pos < len; ++pos) {
    1.50 +        if (visualSeparator.indexOf(subscriber[pos]) != -1) {
    1.51 +          number += subscriber[pos];
    1.52 +        } else if (digits.indexOf(subscriber[pos]) != -1) {
    1.53 +          number += subscriber[pos];
    1.54 +        } else if (dtmfDigits.indexOf(subscriber[pos]) != -1) {
    1.55 +          number += subscriber[pos];
    1.56 +        } else if (pauseCharacter.indexOf(subscriber[pos]) != -1) {
    1.57 +          number += subscriber[pos];
    1.58 +        } else {
    1.59 +          break;
    1.60 +        }
    1.61 +      }
    1.62 +
    1.63 +      // this means error
    1.64 +      if (!number.length) {
    1.65 +        return null;
    1.66 +      }
    1.67 +
    1.68 +      // isdn-subaddress
    1.69 +      if (subscriber.substring(pos, pos + 6) == ';isub=') {
    1.70 +        let subaddress = '';
    1.71 +
    1.72 +        for (pos += 6; pos < len; ++pos) {
    1.73 +          if (visualSeparator.indexOf(subscriber[pos]) != -1) {
    1.74 +            subaddress += subscriber[pos];
    1.75 +          } else if (digits.indexOf(subscriber[pos]) != -1) {
    1.76 +            subaddress += subscriber[pos];
    1.77 +          } else {
    1.78 +            break;
    1.79 +          }
    1.80 +        }
    1.81 +
    1.82 +        // FIXME: ignore subaddress - Bug 795242
    1.83 +      }
    1.84 +
    1.85 +      // post-dial
    1.86 +      if (subscriber.substring(pos, pos + 7) == ';postd=') {
    1.87 +        let subaddress = '';
    1.88 +
    1.89 +        for (pos += 7; pos < len; ++pos) {
    1.90 +          if (visualSeparator.indexOf(subscriber[pos]) != -1) {
    1.91 +            subaddress += subscriber[pos];
    1.92 +          } else if (digits.indexOf(subscriber[pos]) != -1) {
    1.93 +            subaddress += subscriber[pos];
    1.94 +          } else if (dtmfDigits.indexOf(subscriber[pos]) != -1) {
    1.95 +            subaddress += subscriber[pos];
    1.96 +          } else if (pauseCharacter.indexOf(subscriber[pos]) != -1) {
    1.97 +            subaddress += subscriber[pos];
    1.98 +          } else {
    1.99 +            break;
   1.100 +          }
   1.101 +        }
   1.102 +
   1.103 +        // FIXME: ignore subaddress - Bug 795242
   1.104 +      }
   1.105 +
   1.106 +      // area-specific
   1.107 +      if (subscriber.substring(pos, pos + 15) == ';phone-context=') {
   1.108 +        pos += 15;
   1.109 +
   1.110 +        // global-network-prefix | local-network-prefix | private-prefi
   1.111 +        number = subscriber.substring(pos, subscriber.length) + number;
   1.112 +      }
   1.113 +    }
   1.114 +
   1.115 +    // Ignore MWI and USSD codes. See 794034.
   1.116 +    if (number.match(/[#\*]/) && !number.match(/^[#\*]\d+$/)) {
   1.117 +      return null;
   1.118 +    }
   1.119 +
   1.120 +    return number || null;
   1.121 +  }
   1.122 +};
   1.123 +

mercurial