we have a StyleGroup rendering some very basic html text. We have defined styles for most common tags, including a custom one for a. We're also using href informations through the .link attributed string key to trigger events on a TTTAttributedLabel.
Analysis:
We expected to find the .link attribute populated with the original href, but this doesn't work out of the box:
If we DO NOT include a custom Style for the "a" tag, the link attribute is set to proper href
if we *DO** include the custom style inside the group, the link is not set.
The root cause seems to be in the StandardXMLAttributesResolver implementation:
if a is present in style's attributes, the applyDynamicAttributes(...) method is called. Inside the standard implementation, there's nothing about href default handling: it's taken for granted that it will be the Style to take care of it, but the style doesn't have access to the dynamic value of the href
if a is missing, the styleForUnknownXMLTag() is called, properly handling the link.
Workaround:
We were able to create a custom resolver by wrapping the standard one like this.
open class MyXMLDynamicAttributesResolver: XMLDynamicAttributesResolver {
public func styleForUnknownXMLTag(_ tag: String, to attributedString: inout AttributedString, attributes: [String : String]?, fromStyle: StyleXML) {
wrapper.styleForUnknownXMLTag(tag, to: &attributedString, attributes: attributes, fromStyle: fromStyle)
}
let wrapper: XMLDynamicAttributesResolver
init(_ wrapper: XMLDynamicAttributesResolver) {
self.wrapper = wrapper
}
public func applyDynamicAttributes(to attributedString: inout AttributedString, xmlStyle: XMLDynamicStyle, fromStyle: StyleXML) {
if xmlStyle.tag == "a" {
self.styleForUnknownXMLTag("a", to: &attributedString, attributes: xmlStyle.xmlAttributes, fromStyle: fromStyle)
}
wrapper.applyDynamicAttributes(to: &attributedString, xmlStyle: xmlStyle, fromStyle: fromStyle)
}
}
1) inside the StandardXMLAttributesResolver, fallback to the default implementation for a tags
2) make StandardXMLAttributesResolver methods (and initializer!) open and not internal/public (that's why we had to wrap the resolver inside another class instead of subclassing it)
I can quickly put together a pull request if needed but I'd like to know which is the intended behavior
Scenario:
we have a
StyleGroup
rendering some very basic html text. We have defined styles for most common tags, including a custom one fora
. We're also usinghref
informations through the.link
attributed string key to trigger events on aTTTAttributedLabel
.Analysis:
We expected to find the
.link
attribute populated with the original href, but this doesn't work out of the box:The root cause seems to be in the
StandardXMLAttributesResolver
implementation:a
is present in style's attributes, theapplyDynamicAttributes(...)
method is called. Inside the standard implementation, there's nothing about href default handling: it's taken for granted that it will be the Style to take care of it, but the style doesn't have access to the dynamic value of the hrefa
is missing, thestyleForUnknownXMLTag()
is called, properly handling the link.Workaround:
We were able to create a custom resolver by wrapping the standard one like this.
and by setting it like this in our group:
Proposed solutions:
1) inside the
StandardXMLAttributesResolver
, fallback to the default implementation fora
tags 2) makeStandardXMLAttributesResolver
methods (and initializer!)open
and not internal/public (that's why we had to wrap the resolver inside another class instead of subclassing it)I can quickly put together a pull request if needed but I'd like to know which is the intended behavior
Ciao :)