summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/editing/ime/ime_text_span.cc
blob: 0926bb93a7e58f5e3a246baf140e797ffda4e219 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/editing/ime/ime_text_span.h"

#include <algorithm>
#include "third_party/blink/public/web/web_ime_text_span.h"

namespace blink {

ImeTextSpan::ImeTextSpan(Type type,
                         unsigned start_offset,
                         unsigned end_offset,
                         const Color& underline_color,
                         ws::mojom::ImeTextSpanThickness thickness,
                         const Color& background_color,
                         const Color& suggestion_highlight_color,
                         const Vector<String>& suggestions)
    : type_(type),
      underline_color_(underline_color),
      thickness_(thickness),
      background_color_(background_color),
      suggestion_highlight_color_(suggestion_highlight_color),
      suggestions_(suggestions) {
  // Sanitize offsets by ensuring a valid range corresponding to the last
  // possible position.
  // TODO(wkorman): Consider replacing with DCHECK_LT(startOffset, endOffset).
  start_offset_ =
      std::min(start_offset, std::numeric_limits<unsigned>::max() - 1u);
  end_offset_ = std::max(start_offset_ + 1u, end_offset);
}

namespace {

Vector<String> ConvertStdVectorOfStdStringsToVectorOfStrings(
    const std::vector<std::string>& input) {
  Vector<String> output;
  for (const std::string& val : input) {
    output.push_back(String::FromUTF8(val.c_str()));
  }
  return output;
}

ImeTextSpan::Type ConvertWebTypeToType(WebImeTextSpan::Type type) {
  switch (type) {
    case WebImeTextSpan::Type::kComposition:
      return ImeTextSpan::Type::kComposition;
    case WebImeTextSpan::Type::kSuggestion:
      return ImeTextSpan::Type::kSuggestion;
    case WebImeTextSpan::Type::kMisspellingSuggestion:
      return ImeTextSpan::Type::kMisspellingSuggestion;
  }

  NOTREACHED();
  return ImeTextSpan::Type::kComposition;
}

}  // namespace

ImeTextSpan::ImeTextSpan(const WebImeTextSpan& ime_text_span)
    : ImeTextSpan(ConvertWebTypeToType(ime_text_span.type),
                  ime_text_span.start_offset,
                  ime_text_span.end_offset,
                  Color(ime_text_span.underline_color),
                  ime_text_span.thickness,
                  Color(ime_text_span.background_color),
                  Color(ime_text_span.suggestion_highlight_color),
                  ConvertStdVectorOfStdStringsToVectorOfStrings(
                      ime_text_span.suggestions)) {}
}  // namespace blink