Skip to content

Commit d339f64

Browse files
ashishkum-msChromium LUCI CQ
authored andcommitted
[EditContext] Add UseCounter for updateText and updateSelection
This CL adds use counters to analyze the impact of implementing the change proposed in [1]. Following counters are added: 1. kEditContextUpdateTextRangePrecedesOrOverlapsSelection: Tracks when updateText is called and the update range precedes or overlaps the selection. 2. kEditContextUpdateTextRangePrecedesCompositionRange: Tracks when updateText is called and the update range precedes the composition range during an active composition. 3. kEditContextUpdateTextRangeOverlapsCompositionRange: Tracks when updateText is called and the update range overlaps with the composition range during an active composition. 4. kEditContextUpdateSelectionDuringActiveComposition: Tracks when updateSelection is called during an active composition. [1] w3c/edit-context#115 These counters will help us understand how frequently these behaviors occur before making changes to the spec implementation. Bug: 379170477 Change-Id: I8043fdb6161137f79d8eaa3336b90d93ca25042c Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6549358 Reviewed-by: Dan Clark <[email protected]> Commit-Queue: Ashish Kumar <[email protected]> Cr-Commit-Position: refs/heads/main@{#1462602}
1 parent 12c2d46 commit d339f64

File tree

4 files changed

+119
-1
lines changed

4 files changed

+119
-1
lines changed

third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4888,6 +4888,10 @@ enum WebFeature {
48884888
kPopoverShown = 5579,
48894889
kInputParsedParentOptionOrOptgroup = 5580,
48904890
kNavigatorUAData_toJSON = 5581,
4891+
kEditContextUpdateTextRangePrecedesOrOverlapsSelection = 5582,
4892+
kEditContextUpdateTextRangePrecedesCompositionRange = 5583,
4893+
kEditContextUpdateTextRangeOverlapsCompositionRange = 5584,
4894+
kEditContextUpdateSelectionDuringActiveComposition = 5585,
48914895

48924896
// Add new features immediately above this line. Don't change assigned
48934897
// numbers of any item, and don't reuse removed slots. Also don't add extra

third_party/blink/renderer/core/editing/ime/edit_context.cc

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,16 @@ void EditContext::updateSelection(uint32_t start,
243243
TRACE_EVENT2("ime", "EditContext::updateSelection", "start",
244244
std::to_string(start), "end", std::to_string(end));
245245

246-
SetSelection(std::min(start, text_.length()), std::min(end, text_.length()));
246+
uint32_t bound_start = std::min(start, text_.length());
247+
uint32_t bound_end = std::min(end, text_.length());
248+
if (has_composition_ &&
249+
(bound_start != selection_start_ || bound_end != selection_end_)) {
250+
UseCounter::Count(
251+
GetExecutionContext(),
252+
WebFeature::kEditContextUpdateSelectionDuringActiveComposition);
253+
}
254+
255+
SetSelection(bound_start, bound_end);
247256
if (!has_composition_)
248257
return;
249258

@@ -309,6 +318,29 @@ void EditContext::updateText(uint32_t start,
309318
}
310319
end = std::min(end, text_.length());
311320
start = std::min(start, end);
321+
322+
if (OrderedSelectionEnd() > start) {
323+
UseCounter::Count(
324+
GetExecutionContext(),
325+
WebFeature::kEditContextUpdateTextRangePrecedesOrOverlapsSelection);
326+
}
327+
328+
if (has_composition_ && composition_range_end_ > start) {
329+
if (composition_range_start_ >= end) {
330+
// Example:
331+
// Composition range: [3, 6], Update range: [1, 2]
332+
UseCounter::Count(
333+
GetExecutionContext(),
334+
WebFeature::kEditContextUpdateTextRangePrecedesCompositionRange);
335+
} else {
336+
// Example:
337+
// Composition range: [3, 6], Update range: [4, 7]
338+
UseCounter::Count(
339+
GetExecutionContext(),
340+
WebFeature::kEditContextUpdateTextRangeOverlapsCompositionRange);
341+
}
342+
}
343+
312344
text_ = text_.Substring(0, start) + new_text + text_.Substring(end);
313345
}
314346

third_party/blink/web_tests/editing/input/edit-context.html

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,83 @@
759759

760760
test.remove();
761761
}, 'Testing UseCounter for textformatupdate event.');
762+
763+
test(function(){
764+
const kEditContextUpdateTextRangePrecedesOrOverlapsSelection = 5582;
765+
const kEditContextUpdateTextRangePrecedesCompositionRange = 5583;
766+
const kEditContextUpdateTextRangeOverlapsCompositionRange = 5584;
767+
internals.clearUseCounter(document, kEditContextUpdateTextRangePrecedesOrOverlapsSelection);
768+
internals.clearUseCounter(document, kEditContextUpdateTextRangePrecedesCompositionRange);
769+
internals.clearUseCounter(document, kEditContextUpdateTextRangeOverlapsCompositionRange);
770+
771+
const text = "ABCD";
772+
const editContext = new EditContext({text: text, selectionStart: 1, selectionEnd: 1});
773+
const test = document.createElement("div");
774+
test.textContent = text;
775+
document.body.appendChild(test);
776+
test.editContext = editContext;
777+
test.focus();
778+
779+
assert_false(internals.isUseCounted(document, kEditContextUpdateTextRangePrecedesOrOverlapsSelection));
780+
781+
// UpdateText range is outside the selection range.
782+
editContext.updateText(4, 4, "1");
783+
assert_false(internals.isUseCounted(document, kEditContextUpdateTextRangePrecedesOrOverlapsSelection));
784+
785+
// UpdateText range is less than selection range.
786+
editContext.updateText(0, 0, "2");
787+
assert_true(internals.isUseCounted(document, kEditContextUpdateTextRangePrecedesOrOverlapsSelection));
788+
789+
editContext.updateSelection(1, 1);
790+
textInputController.setComposition("foo");
791+
assert_false(internals.isUseCounted(document, kEditContextUpdateTextRangePrecedesCompositionRange));
792+
assert_false(internals.isUseCounted(document, kEditContextUpdateTextRangeOverlapsCompositionRange));
793+
794+
// UpdeText range is outside the composition range.
795+
editContext.updateText(5, 5, "3");
796+
assert_false(internals.isUseCounted(document, kEditContextUpdateTextRangePrecedesCompositionRange));
797+
assert_false(internals.isUseCounted(document, kEditContextUpdateTextRangeOverlapsCompositionRange));
798+
799+
// UpdateText range is less than composition range but does not overlap.
800+
editContext.updateText(0, 0, "4");
801+
assert_true(internals.isUseCounted(document, kEditContextUpdateTextRangePrecedesCompositionRange));
802+
assert_false(internals.isUseCounted(document, kEditContextUpdateTextRangeOverlapsCompositionRange));
803+
804+
// UpdateText range overlaps with composition range.
805+
editContext.updateText(0, 3, "5");
806+
assert_true(internals.isUseCounted(document, kEditContextUpdateTextRangeOverlapsCompositionRange));
807+
808+
test.remove();
809+
}, 'Testing UseCounter for updateText based on update range and selection/composition range.');
810+
811+
test(function(){
812+
const kEditContextUpdateSelectionDuringActiveComposition = 5585;
813+
internals.clearUseCounter(document, kEditContextUpdateSelectionDuringActiveComposition);
814+
815+
const text = "ABCD";
816+
const editContext = new EditContext({text: text, selectionStart: 0, selectionEnd: 0});
817+
const test = document.createElement("div");
818+
test.textContent = text;
819+
document.body.appendChild(test);
820+
test.editContext = editContext;
821+
test.focus();
822+
823+
// Tracks when selection is updated to new value while a composition is active.
824+
assert_false(internals.isUseCounted(document, kEditContextUpdateSelectionDuringActiveComposition));
825+
editContext.updateSelection(1, 1);
826+
assert_false(internals.isUseCounted(document, kEditContextUpdateSelectionDuringActiveComposition));
827+
828+
textInputController.setComposition("foo");
829+
assert_equals(editContext.selectionStart, 4);
830+
assert_equals(editContext.selectionEnd, 4);
831+
editContext.updateSelection(4, 4);
832+
assert_false(internals.isUseCounted(document, kEditContextUpdateSelectionDuringActiveComposition));
833+
834+
editContext.updateSelection(2, 2);
835+
assert_true(internals.isUseCounted(document, kEditContextUpdateSelectionDuringActiveComposition));
836+
837+
test.remove();
838+
}, 'Testing UseCounter for updateSelection during an active composition.');
762839
</script>
763840
</body>
764841
</html>

tools/metrics/histograms/metadata/blink/enums.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6209,6 +6209,11 @@ Called by update_use_counter_feature_enum.py.-->
62096209
<int value="5579" label="PopoverShown"/>
62106210
<int value="5580" label="InputParsedParentOptionOrOptgroup"/>
62116211
<int value="5581" label="NavigatorUAData_toJSON"/>
6212+
<int value="5582"
6213+
label="EditContextUpdateTextRangePrecedesOrOverlapsSelection"/>
6214+
<int value="5583" label="EditContextUpdateTextRangePrecedesCompositionRange"/>
6215+
<int value="5584" label="EditContextUpdateTextRangeOverlapsCompositionRange"/>
6216+
<int value="5585" label="EditContextUpdateSelectionDuringActiveComposition"/>
62126217
</enum>
62136218

62146219
<!-- LINT.ThenChange(//third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom:WebFeature) -->

0 commit comments

Comments
 (0)