2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "UrlMatchesStyler.h"
19 namespace tizen_browser {
22 UrlMatchesStyler::UrlMatchesStyler() :
23 TAG_WHOLE_URL("<align=left><color=" + FONT_COLOR_NORMAL + "><font_size="+FONT_SIZE+">"),
24 TAG_WHOLE_URL_CLOSE("</color></font></align>"),
25 TAG_HIGHLIGHT("<hilight>"),
26 TAG_HIGHLIGHT_CLOSE(closeTag(TAG_HIGHLIGHT)),
27 TAG_COLOR("<color=" + FONT_COLOR_HIGHLIGHT + ">"),
28 TAG_COLOR_CLOSE(closeTag(TAG_COLOR)),
29 TAG_COMPLETE(TAG_HIGHLIGHT + TAG_COLOR),
30 TAG_COMPLETE_CLOSE(TAG_HIGHLIGHT_CLOSE + TAG_COLOR_CLOSE),
31 TAGS_COMPLETE_LEN(TAG_COMPLETE.length() + TAG_COMPLETE_CLOSE.length()) {
34 UrlMatchesStyler::~UrlMatchesStyler() {
37 string UrlMatchesStyler::closeTag(const string& tag) const {
38 string closedTag(tag);
39 return string(closedTag.insert(1, "/"));
42 string UrlMatchesStyler::getUrlHighlightedMatches(const string& styledUrl,
43 const string& highlightingKeywords) const {
44 vector < string > keywords;
45 splitKeywordsString(highlightingKeywords, keywords);
47 int_pairs rangesHighlight;
48 for (auto key : keywords) {
49 fillOccuranceRanges(styledUrl, key, rangesHighlight);
52 int_pairs mergedRangesHighlight;
53 mergeRanges(rangesHighlight, mergedRangesHighlight);
54 return getTaggedString(styledUrl, mergedRangesHighlight);
57 void UrlMatchesStyler::splitKeywordsString(const string& keywordsString,
58 vector<string>& resultKeywords) const {
59 boost::algorithm::split(resultKeywords, keywordsString,
60 boost::is_any_of("\t "), boost::token_compress_on);
61 // remove empty elements
62 for (auto it = resultKeywords.begin(); it != resultKeywords.end();) {
64 it = resultKeywords.erase(it);
71 void UrlMatchesStyler::fillOccuranceRanges(const string& _checkedString,
72 const string& _searchedMatch, int_pairs& resultRanges) const {
73 if (_checkedString.empty() || _searchedMatch.empty())
76 string checkedString(_checkedString);
77 string searchedMatch(_searchedMatch);
78 boost::algorithm::to_lower(checkedString);
79 boost::algorithm::to_lower(searchedMatch);
81 int len = searchedMatch.length();
82 vector<int> positions;
83 getMatchesPositions(checkedString, searchedMatch, positions);
84 for (auto pos : positions) {
85 resultRanges.push_back( { pos, pos + len - 1 });
89 void UrlMatchesStyler::getMatchesPositions(const string& checkedString,
90 const string& searchedMatch, vector<int>& resultPositions) const {
91 boost::regex match_regex(searchedMatch);
92 for (auto it = boost::sregex_iterator(checkedString.begin(),
93 checkedString.end(), match_regex); it != boost::sregex_iterator();
95 resultPositions.push_back(it->position());
99 void UrlMatchesStyler::mergeRanges(int_pairs& ranges, int_pairs& result) const {
100 if (ranges.size() == 0)
102 sort(ranges.begin(), ranges.end());
104 auto current = *(ranges.begin());
105 for (auto it = ranges.begin() + 1; it != ranges.end(); ++it) {
106 if (current.second >= it->first) {
107 current.second = max(current.second, it->second);
109 result.push_back(current);
113 result.push_back(current);
116 string UrlMatchesStyler::getTaggedString(const string& strToHighlight,
117 const int_pairs& ranges) const {
118 string strResult(strToHighlight);
119 int insertOffset = 0;
120 for (auto pair : ranges) {
121 strResult.insert(pair.second + insertOffset + 1, TAG_COMPLETE_CLOSE);
122 strResult.insert(pair.first + insertOffset, TAG_COMPLETE);
123 insertOffset += TAGS_COMPLETE_LEN;
125 strResult.insert(strResult.length(), TAG_WHOLE_URL_CLOSE);
126 strResult.insert(0, TAG_WHOLE_URL);
130 } /* namespace base_ui */
131 } /* namespace tizen_browser */