johnpatrickmorgan / wtfautolayout

The source code for Why The Failure, Auto Layout?
https://www.wtfautolayout.com
MIT License
1.08k stars 22 forks source link

Labels with contractions fail to render #8

Open mdiasdev opened 6 years ago

mdiasdev commented 6 years ago

If a UILabel has text that contains an apostrophe the parser fails with:

UNEXPECTED INPUT:
...
EXPECTED:
']'

Example:

(
    "<NSLayoutConstraint:0x600000688070 UIView:0x7fcae969d920.height == 50   (active)>",
    "<NSLayoutConstraint:0x600000688700 V:[MyApp.UIButton:0x7fcae969f500'No Thanks']-(0)-|   (active, names: '|':UIView:0x7fcae969f320 )>",
    "<NSLayoutConstraint:0x6000006887a0 V:|-(0)-[MyApp.UIButton:0x7fcae969f500'No Thanks']   (active, names: '|':UIView:0x7fcae969f320 )>",
    "<NSLayoutConstraint:0x600000688b10 V:|-(10)-[MyApp.UILabel:0x7fcae969ece0'Keep track of your friend's ...']   (active, names: '|':UIView:0x7fcae969eb00 )>",
    "<NSLayoutConstraint:0x600000688bb0 V:[MyApp.UILabel:0x7fcae969ece0'Keep track of your friend's ...']-(15)-[UIView:0x7fcae969f320]   (active)>",
    "<NSLayoutConstraint:0x600000688c00 V:[UIView:0x7fcae969f320]-(15)-|   (active, names: '|':UIView:0x7fcae969eb00 )>",
    "<NSLayoutConstraint:0x600000688e80 V:[UIStackView:0x7fcae969d720]-(0)-|   (active, names: '|':UITableViewCellContentView:0x7fcae969d530 )>",
    "<NSLayoutConstraint:0x600000688f20 V:|-(0)-[UIStackView:0x7fcae969d720]   (active, names: '|':UITableViewCellContentView:0x7fcae969d530 )>",
    "<NSLayoutConstraint:0x600000688f70 'UISV-canvas-connection' UIStackView:0x7fcae969d720.top == UIView:0x7fcae969d920.top   (active)>",
    "<NSLayoutConstraint:0x600000689060 'UISV-canvas-connection' V:[UIView:0x7fcae969eb00]-(0)-|   (active, names: '|':UIStackView:0x7fcae969d720 )>",
    "<NSLayoutConstraint:0x6000006890b0 'UISV-spacing' V:[UIView:0x7fcae969d920]-(0)-[UIView:0x7fcae969eb00]   (active)>",
    "<NSLayoutConstraint:0x60400048fd20 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fcae969d530.height == 50   (active)>"
)

if you change friend's to friends the parser is again able to handle the input

johnpatrickmorgan commented 6 years ago

Thanks for raising this issue. This relates to the identifier parser in ConstraintsParser+Instances.swift:

https://github.com/johnpatrickmorgan/wtfautolayout/blob/823cdf9bd10cd416d313b6b131e16cc0995abafa/Sources/App/Parsing/ConstraintsParser%2BInstances.swift#L36

Currently it does not allow single-quote marks, which stops it from greedily consuming the whole line as an identifier. It will be a challenge to address this issue within the current limitations of the parsing framework, especially to avoid unrelated errors being reported as bad identifiers (since the framework reports the error furthest along the input stream).

I'll mark this as an enhancement, and give it some thought. Thanks again.

mdiasdev commented 6 years ago

Thanks @johnpatrickmorgan, that sounds great! Is it possible to detect that there's an odd number of apostrophes and update the error message to reflect that as a short-term fix? Admittedly I have not looked through the source code -- so I don't know if it's possible -- just trying to offer some potential help for other developers scratching their heads as I was yesterday

johnpatrickmorgan commented 6 years ago

Thanks, yes that seems like a good short-term approach. As an aside, I think U+2019 is the preferred character for possessive apostrophes, which would avoid the issue in this case at least.