Skip to content

Commit d709199

Browse files
feat: Update CssBoxWidget to handle rtl marker boxes (#1270)
1 parent 496d1aa commit d709199

File tree

11 files changed

+2177
-178
lines changed

11 files changed

+2177
-178
lines changed

example/lib/main.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,11 @@ class MyHomePageState extends State<MyHomePage> {
296296
backgroundColor: const Color.fromARGB(0x50, 0xee, 0xee, 0xee),
297297
),
298298
"th": Style(
299-
padding: const EdgeInsets.all(6),
299+
padding: HtmlPaddings.all(6),
300300
backgroundColor: Colors.grey,
301301
),
302302
"td": Style(
303-
padding: const EdgeInsets.all(6),
303+
padding: HtmlPaddings.all(6),
304304
border: const Border(bottom: BorderSide(color: Colors.grey)),
305305
),
306306
'h5': Style(maxLines: 2, textOverflow: TextOverflow.ellipsis),

lib/src/builtins/styled_element_builtin.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ class StyledElementBuiltIn extends HtmlExtension {
176176
continue italics;
177177
case "div":
178178
styledElement.style = Style(
179-
margin: Margins.all(0),
180179
display: Display.block,
181180
);
182181
break;
@@ -338,14 +337,22 @@ class StyledElementBuiltIn extends HtmlExtension {
338337
styledElement.style = Style(
339338
display: Display.block,
340339
listStyleType: ListStyleType.decimal,
341-
padding: const EdgeInsets.only(left: 40),
340+
padding: HtmlPaddings.only(inlineStart: 40),
341+
margin: Margins(
342+
blockStart: Margin(1, Unit.em),
343+
blockEnd: Margin(1, Unit.em),
344+
),
342345
);
343346
break;
344347
case "ul":
345348
styledElement.style = Style(
346349
display: Display.block,
347350
listStyleType: ListStyleType.disc,
348-
padding: const EdgeInsets.only(left: 40),
351+
padding: HtmlPaddings.only(inlineStart: 40),
352+
margin: Margins(
353+
blockStart: Margin(1, Unit.em),
354+
blockEnd: Margin(1, Unit.em),
355+
),
349356
);
350357
break;
351358
case "p":

lib/src/css_box_widget.dart

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,19 @@ class CssBoxWidget extends StatelessWidget {
5656
? _generateMarkerBoxSpan(style)
5757
: null;
5858

59+
final direction = _checkTextDirection(context, textDirection);
60+
final padding = style.padding?.resolve(direction);
61+
5962
return _CSSBoxRenderer(
6063
width: style.width ?? Width.auto(),
6164
height: style.height ?? Height.auto(),
62-
paddingSize: style.padding?.collapsedSize ?? Size.zero,
65+
paddingSize: padding?.collapsedSize ?? Size.zero,
6366
borderSize: style.border?.dimensions.collapsedSize ?? Size.zero,
6467
margins: style.margin ?? Margins.zero,
6568
display: style.display ?? Display.inline,
6669
childIsReplaced: childIsReplaced,
6770
emValue: _calculateEmValue(style, context),
68-
textDirection: _checkTextDirection(context, textDirection),
71+
textDirection: direction,
6972
shrinkWrap: shrinkWrap,
7073
children: [
7174
Container(
@@ -74,7 +77,7 @@ class CssBoxWidget extends StatelessWidget {
7477
color: style.backgroundColor, //Colors the padding and content boxes
7578
),
7679
width: _shouldExpandToFillBlock() ? double.infinity : null,
77-
padding: style.padding ?? EdgeInsets.zero,
80+
padding: padding,
7881
child: top
7982
? child
8083
: MediaQuery(
@@ -224,8 +227,8 @@ class _CSSBoxRenderer extends MultiChildRenderObjectWidget {
224227
final bool shrinkWrap;
225228

226229
@override
227-
_RenderCSSBox createRenderObject(BuildContext context) {
228-
return _RenderCSSBox(
230+
RenderCSSBox createRenderObject(BuildContext context) {
231+
return RenderCSSBox(
229232
display: display,
230233
width: width..normalize(emValue),
231234
height: height..normalize(emValue),
@@ -239,7 +242,7 @@ class _CSSBoxRenderer extends MultiChildRenderObjectWidget {
239242
}
240243

241244
@override
242-
void updateRenderObject(BuildContext context, _RenderCSSBox renderObject) {
245+
void updateRenderObject(BuildContext context, RenderCSSBox renderObject) {
243246
renderObject
244247
..display = display
245248
..width = (width..normalize(emValue))
@@ -253,10 +256,21 @@ class _CSSBoxRenderer extends MultiChildRenderObjectWidget {
253256
}
254257

255258
Margins _preProcessMargins(Margins margins, bool shrinkWrap) {
256-
Margin leftMargin = margins.left ?? Margin.zero();
257-
Margin rightMargin = margins.right ?? Margin.zero();
258-
Margin topMargin = margins.top ?? Margin.zero();
259-
Margin bottomMargin = margins.bottom ?? Margin.zero();
259+
late Margin leftMargin;
260+
late Margin rightMargin;
261+
Margin topMargin = margins.top ?? margins.blockStart ?? Margin.zero();
262+
Margin bottomMargin = margins.bottom ?? margins.blockEnd ?? Margin.zero();
263+
264+
switch (textDirection) {
265+
case TextDirection.rtl:
266+
leftMargin = margins.left ?? margins.inlineEnd ?? Margin.zero();
267+
rightMargin = margins.right ?? margins.inlineStart ?? Margin.zero();
268+
break;
269+
case TextDirection.ltr:
270+
leftMargin = margins.left ?? margins.inlineStart ?? Margin.zero();
271+
rightMargin = margins.right ?? margins.inlineEnd ?? Margin.zero();
272+
break;
273+
}
260274

261275
//Preprocess margins to a pixel value
262276
leftMargin.normalize(emValue);
@@ -295,12 +309,14 @@ class _CSSBoxRenderer extends MultiChildRenderObjectWidget {
295309
}
296310
}
297311

312+
@visibleForTesting
313+
298314
/// Implements the CSS layout algorithm
299-
class _RenderCSSBox extends RenderBox
315+
class RenderCSSBox extends RenderBox
300316
with
301317
ContainerRenderObjectMixin<RenderBox, CSSBoxParentData>,
302318
RenderBoxContainerDefaultsMixin<RenderBox, CSSBoxParentData> {
303-
_RenderCSSBox({
319+
RenderCSSBox({
304320
required Display display,
305321
required Width width,
306322
required Height height,
@@ -593,7 +609,20 @@ class _RenderCSSBox extends RenderBox
593609
final offsetHeight = distance -
594610
(markerBox.getDistanceToBaseline(TextBaseline.alphabetic) ??
595611
markerBox.size.height);
596-
markerBoxParentData.offset = Offset(-markerBox.size.width, offsetHeight);
612+
switch (_textDirection) {
613+
case TextDirection.rtl:
614+
markerBoxParentData.offset = Offset(
615+
child.size.width,
616+
offsetHeight,
617+
);
618+
break;
619+
case TextDirection.ltr:
620+
markerBoxParentData.offset = Offset(
621+
-markerBox.size.width,
622+
offsetHeight,
623+
);
624+
break;
625+
}
597626
}
598627
}
599628

@@ -701,10 +730,11 @@ class _RenderCSSBox extends RenderBox
701730
}
702731

703732
return Margins(
704-
left: marginLeft,
705-
right: marginRight,
706-
top: margins.top,
707-
bottom: margins.bottom);
733+
left: marginLeft,
734+
right: marginRight,
735+
top: margins.top,
736+
bottom: margins.bottom,
737+
);
708738
}
709739

710740
@override

0 commit comments

Comments
 (0)