如何将JS移植到AS3?

时间:2015-05-19 19:12:41

标签: javascript actionscript-3 porting

我想在AS3中使用Twitter's official getTweetLength函数。 我需要在JavaScript中更改哪些内容才能在AS3中使用它?是否有类似转换器的东西,因为两种语言看起来都很相似。

 twttr.txt.regexen.urlHasHttps = /^https:\/\//i;

    // Returns the length of Tweet text with consideration to t.co URL replacement
      // and chars outside the basic multilingual plane that use 2 UTF16 code points
      twttr.txt.getTweetLength = function(text, options) {
        if (!options) {
          options = {
              // These come from https://api.twitter.com/1/help/configuration.json
              // described by https://dev.twitter.com/docs/api/1/get/help/configuration
              short_url_length: 22,
              short_url_length_https: 23
          };
        }
        var textLength = twttr.txt.getUnicodeTextLength(text),
            urlsWithIndices = twttr.txt.extractUrlsWithIndices(text);
        twttr.txt.modifyIndicesFromUTF16ToUnicode(text, urlsWithIndices);

        for (var i = 0; i < urlsWithIndices.length; i++) {
            // Subtract the length of the original URL
          textLength += urlsWithIndices[i].indices[0] - urlsWithIndices[i].indices[1];

          // Add 23 characters for URL starting with https://
          // Otherwise add 22 characters
          if (urlsWithIndices[i].url.toLowerCase().match(twttr.txt.regexen.urlHasHttps)) {
             textLength += options.short_url_length_https;
          } else {
            textLength += options.short_url_length;
          }
        }

        return textLength;
      };


    twttr.txt.getUnicodeTextLength = function(text) {
        return text.replace(twttr.txt.regexen.non_bmp_code_pairs, ' ').length;
      };


    twttr.txt.extractUrlsWithIndices = function(text, options) {
        if (!options) {
          options = {extractUrlsWithoutProtocol: true};
        }
        if (!text || (options.extractUrlsWithoutProtocol ? !text.match(/\./) : !text.match(/:/))) {
          return [];
        }

        var urls = [];

        while (twttr.txt.regexen.extractUrl.exec(text)) {
          var before = RegExp.$2, url = RegExp.$3, protocol = RegExp.$4, domain = RegExp.$5, path = RegExp.$7;
          var endPosition = twttr.txt.regexen.extractUrl.lastIndex,
              startPosition = endPosition - url.length;

          // if protocol is missing and domain contains non-ASCII characters,
          // extract ASCII-only domains.
          if (!protocol) {
            if (!options.extractUrlsWithoutProtocol
                || before.match(twttr.txt.regexen.invalidUrlWithoutProtocolPrecedingChars)) {
              continue;
            }
            var lastUrl = null,
                asciiEndPosition = 0;
            domain.replace(twttr.txt.regexen.validAsciiDomain, function(asciiDomain) {
              var asciiStartPosition = domain.indexOf(asciiDomain, asciiEndPosition);
              asciiEndPosition = asciiStartPosition + asciiDomain.length;
              lastUrl = {
                url: asciiDomain,
                indices: [startPosition + asciiStartPosition, startPosition + asciiEndPosition]
              };
              if (path
                  || asciiDomain.match(twttr.txt.regexen.validSpecialShortDomain)
                  || !asciiDomain.match(twttr.txt.regexen.invalidShortDomain)) {
                urls.push(lastUrl);
              }
            });

            // no ASCII-only domain found. Skip the entire URL.
            if (lastUrl == null) {
              continue;
            }

            // lastUrl only contains domain. Need to add path and query if they exist.
            if (path) {
              lastUrl.url = url.replace(domain, lastUrl.url);
              lastUrl.indices[1] = endPosition;
            }
          } else {
            // In the case of t.co URLs, don't allow additional path characters.
            if (url.match(twttr.txt.regexen.validTcoUrl)) {
              url = RegExp.lastMatch;
              endPosition = startPosition + url.length;
            }
            urls.push({
              url: url,
              indices: [startPosition, endPosition]
            });
          }
        }

        return urls;
      };


    twttr.txt.modifyIndicesFromUTF16ToUnicode = function(text, entities) {
        twttr.txt.convertUnicodeIndices(text, entities, true);
      };

0 个答案:

没有答案