Skip to content

Commit d98eab2

Browse files
committed
apply the target attribute on semantic links
1 parent c1556cc commit d98eab2

File tree

1 file changed

+29
-13
lines changed
  • packages/url_launcher/url_launcher_web/lib/src

1 file changed

+29
-13
lines changed

packages/url_launcher/url_launcher_web/lib/src/link.dart

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -512,12 +512,16 @@ class LinkViewController extends PlatformViewController {
512512
}
513513
}
514514

515+
late LinkTarget _target;
516+
String get _htmlTargetAttribute => _getHtmlTargetAttribute(_target);
517+
515518
/// Set the [LinkTarget] value for this link.
516519
void setTarget(LinkTarget target) {
517-
_element.setAttribute('target', _getHtmlTarget(target));
520+
_target = target;
521+
_element.setAttribute('target', _htmlTargetAttribute);
518522
}
519523

520-
String _getHtmlTarget(LinkTarget target) {
524+
String _getHtmlTargetAttribute(LinkTarget target) {
521525
switch (target) {
522526
case LinkTarget.defaultTarget:
523527
case LinkTarget.self:
@@ -537,18 +541,30 @@ class LinkViewController extends PlatformViewController {
537541
///
538542
/// Returns null if [target] is not a semantics element for one of our Links.
539543
static int? _getViewIdFromSemanticLink(html.Element? target) {
540-
// TODO: what if `target` IS the <a> semantic element?
541-
if (target != null && _isWithinSemanticTree(target)) {
542-
final html.Element? semanticLink = _getClosestSemanticLink(target);
543-
if (semanticLink != null) {
544-
// TODO: Find out the view ID of semantic link.
545-
final String? semanticIdentifier = semanticLink.getAttribute('semantic-identifier');
546-
if (semanticIdentifier != null) {
547-
return _instancesBySemanticIdentifier[semanticIdentifier]?.viewId;
548-
}
549-
}
544+
if (target == null) {
545+
return null;
546+
}
547+
if (!_isWithinSemanticTree(target)) {
548+
return null;
549+
}
550+
551+
final html.Element? semanticLink = _getClosestSemanticLink(target);
552+
if (semanticLink == null) {
553+
return null;
550554
}
551-
return null;
555+
556+
final String? semanticIdentifier = semanticLink.getAttribute('semantic-identifier');
557+
if (semanticIdentifier == null) {
558+
return null;
559+
}
560+
561+
final LinkViewController? controller = _instancesBySemanticIdentifier[semanticIdentifier];
562+
if (controller == null) {
563+
return null;
564+
}
565+
566+
semanticLink.setAttribute('target', controller._htmlTargetAttribute);
567+
return controller.viewId;
552568
}
553569

554570
@override

0 commit comments

Comments
 (0)