This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[web] Use v8BreakIterator where possible #37317
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,25 @@ | |
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import '../dom.dart'; | ||
import 'fragmenter.dart'; | ||
import 'line_break_properties.dart'; | ||
import 'unicode_range.dart'; | ||
|
||
const Set<int> _kNewlines = <int>{ | ||
0x000A, // LF | ||
0x000B, // BK | ||
0x000C, // BK | ||
0x000D, // CR | ||
0x0085, // NL | ||
0x2028, // BK | ||
0x2029, // BK | ||
}; | ||
const Set<int> _kSpaces = <int>{ | ||
0x0020, // SP | ||
0x200B, // ZW | ||
}; | ||
|
||
/// Various types of line breaks as defined by the Unicode spec. | ||
enum LineBreakType { | ||
/// Indicates that a line break is possible but not mandatory. | ||
|
@@ -25,15 +40,107 @@ enum LineBreakType { | |
} | ||
|
||
/// Splits [text] into fragments based on line breaks. | ||
class LineBreakFragmenter extends TextFragmenter { | ||
const LineBreakFragmenter(super.text); | ||
abstract class LineBreakFragmenter extends TextFragmenter { | ||
factory LineBreakFragmenter(String text) { | ||
if (domWindow.Intl.v8BreakIterator != null) { | ||
return V8LineBreakFragmenter(text); | ||
} | ||
return FWLineBreakFragmenter(text); | ||
} | ||
|
||
@override | ||
List<LineBreakFragment> fragment(); | ||
} | ||
|
||
/// Flutter web's custom implementation of [LineBreakFragmenter]. | ||
class FWLineBreakFragmenter extends TextFragmenter implements LineBreakFragmenter { | ||
FWLineBreakFragmenter(super.text); | ||
|
||
@override | ||
List<LineBreakFragment> fragment() { | ||
return _computeLineBreakFragments(text); | ||
} | ||
} | ||
|
||
/// An implementation of [LineBreakFragmenter] that uses V8's | ||
/// `v8BreakIterator` API to find line breaks in the given [text]. | ||
class V8LineBreakFragmenter extends TextFragmenter implements LineBreakFragmenter { | ||
V8LineBreakFragmenter(super.text) | ||
: assert(domWindow.Intl.v8BreakIterator != null); | ||
|
||
@override | ||
List<LineBreakFragment> fragment() { | ||
final List<LineBreakFragment> breaks = <LineBreakFragment>[]; | ||
int fragmentStart = 0; | ||
|
||
final DomV8BreakIterator iterator = createV8BreakIterator(); | ||
|
||
iterator.adoptText(text); | ||
iterator.first(); | ||
while (iterator.next() != -1) { | ||
final LineBreakType type = _getBreakType(iterator); | ||
|
||
final int fragmentEnd = iterator.current(); | ||
int trailingNewlines = 0; | ||
int trailingSpaces = 0; | ||
|
||
// Calculate trailing newlines and spaces. | ||
for (int i = fragmentStart; i < fragmentEnd; i++) { | ||
final int codeUnit = text.codeUnitAt(i); | ||
if (_kNewlines.contains(codeUnit)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may be a premature optimization, but is there a way to avoid list iteration in most cases by checking that the code unit is not even in the new line or space character code range? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which iteration do you want to avoid? I'm assuming There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't realize they were constants, so assumed |
||
trailingNewlines++; | ||
trailingSpaces++; | ||
} else if (_kSpaces.contains(codeUnit)) { | ||
trailingSpaces++; | ||
} else { | ||
// Always break after a sequence of spaces. | ||
if (trailingSpaces > 0) { | ||
breaks.add(LineBreakFragment( | ||
fragmentStart, | ||
i, | ||
LineBreakType.opportunity, | ||
trailingNewlines: trailingNewlines, | ||
trailingSpaces: trailingSpaces, | ||
)); | ||
fragmentStart = i; | ||
trailingNewlines = 0; | ||
trailingSpaces = 0; | ||
} | ||
} | ||
} | ||
|
||
breaks.add(LineBreakFragment( | ||
fragmentStart, | ||
fragmentEnd, | ||
type, | ||
trailingNewlines: trailingNewlines, | ||
trailingSpaces: trailingSpaces, | ||
)); | ||
fragmentStart = fragmentEnd; | ||
} | ||
|
||
if (breaks.isEmpty || breaks.last.type == LineBreakType.mandatory) { | ||
breaks.add(LineBreakFragment(text.length, text.length, LineBreakType.endOfText, trailingNewlines: 0, trailingSpaces: 0)); | ||
} | ||
|
||
return breaks; | ||
} | ||
|
||
/// Gets break type from v8BreakIterator. | ||
LineBreakType _getBreakType(DomV8BreakIterator iterator) { | ||
final int fragmentEnd = iterator.current(); | ||
|
||
// I don't know why v8BreakIterator uses the type "none" to mean "soft break". | ||
if (iterator.breakType() != 'none') { | ||
return LineBreakType.mandatory; | ||
} | ||
if (fragmentEnd == text.length) { | ||
return LineBreakType.endOfText; | ||
} | ||
return LineBreakType.opportunity; | ||
} | ||
} | ||
|
||
class LineBreakFragment extends TextFragment { | ||
const LineBreakFragment(super.start, super.end, this.type, { | ||
required this.trailingNewlines, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would there be any value in running tests in the non-v8BreakIterator mode in Chrome? If so, we may need a way to override this in tests.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are running all line breaker tests with both our fragmenter and v8BreakIterator. That should be good enough IMHO.
Other text tests are testing other aspects of text layout, not line breaks specifically. So I don't think there's value in running non-v8BreakIterator mode in Chrome. We could always change this in the future of course.