1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "mozEnglishWordUtils.h"
7 #include "nsComponentManagerUtils.h"
8 #include "nsReadableUtils.h"
9 #include "nsUnicharUtils.h"
10 #include "nsUnicodeProperties.h"
11 #include "nsCRT.h"
12 #include "mozilla/Likely.h"
13 #include "nsMemory.h"
14 
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(mozEnglishWordUtils,AddRef)15 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(mozEnglishWordUtils, AddRef)
16 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(mozEnglishWordUtils, Release)
17 
18 NS_IMPL_CYCLE_COLLECTION(mozEnglishWordUtils, mURLDetector)
19 
20 mozEnglishWordUtils::mozEnglishWordUtils() {
21   mURLDetector = do_CreateInstance(MOZ_TXTTOHTMLCONV_CONTRACTID);
22 }
23 
~mozEnglishWordUtils()24 mozEnglishWordUtils::~mozEnglishWordUtils() {}
25 
26 // This needs vast improvement
27 
28 // static
ucIsAlpha(char16_t aChar)29 bool mozEnglishWordUtils::ucIsAlpha(char16_t aChar) {
30   // XXX we have to fix callers to handle the full Unicode range
31   return nsUGenCategory::kLetter == mozilla::unicode::GetGenCategory(aChar);
32 }
33 
FindNextWord(const nsAString & aWord,uint32_t offset,int32_t * begin,int32_t * end)34 bool mozEnglishWordUtils::FindNextWord(const nsAString& aWord, uint32_t offset,
35                                        int32_t* begin, int32_t* end) {
36   if (offset >= aWord.Length()) {
37     *begin = -1;
38     *end = -1;
39     return false;
40   }
41 
42   const char16_t* word = aWord.BeginReading();
43   uint32_t length = aWord.Length();
44   const char16_t* p = word + offset;
45   const char16_t* endbuf = word + length;
46   const char16_t* startWord = p;
47 
48   // XXX These loops should be modified to handle non-BMP characters.
49   // if previous character is a word character, need to advance out of the
50   // word
51   if (offset > 0 && ucIsAlpha(*(p - 1))) {
52     while (p < endbuf && ucIsAlpha(*p)) {
53       p++;
54     }
55   }
56   while ((p < endbuf) && (!ucIsAlpha(*p))) {
57     p++;
58   }
59   startWord = p;
60   while ((p < endbuf) && ((ucIsAlpha(*p)) || (*p == '\''))) {
61     p++;
62   }
63 
64   // we could be trying to break down a url, we don't want to break a url into
65   // parts, instead we want to find out if it really is a url and if so, skip
66   // it, advancing startWord to a point after the url.
67 
68   // before we spend more time looking to see if the word is a url, look for a
69   // url identifer and make sure that identifer isn't the last character in
70   // the word fragment.
71   if ((p < endbuf - 1) && (*p == ':' || *p == '@' || *p == '.')) {
72     // ok, we have a possible url...do more research to find out if we really
73     // have one and determine the length of the url so we can skip over it.
74 
75     if (mURLDetector) {
76       int32_t startPos = -1;
77       int32_t endPos = -1;
78 
79       mURLDetector->FindURLInPlaintext(startWord, endbuf - startWord,
80                                        p - startWord, &startPos, &endPos);
81 
82       // ok, if we got a url, adjust the array bounds, skip the current url
83       // text and find the next word again
84       if (startPos != -1 && endPos != -1) {
85         startWord = p + endPos + 1;  // skip over the url
86 
87         // now recursively call FindNextWord to search for the next word now
88         // that we have skipped the url
89         return FindNextWord(aWord, startWord - word, begin, end);
90       }
91     }
92   }
93 
94   while ((p > startWord) && (*(p - 1) == '\'')) {  // trim trailing apostrophes
95     p--;
96   }
97 
98   if (startWord == endbuf) {
99     *begin = -1;
100     *end = -1;
101     return false;
102   }
103   *begin = startWord - word;
104   *end = p - word;
105   return true;
106 }
107