Preventing line breaks in <code> elements

(alexwlchan.net)

14 points | by surprisetalk a day ago ago

21 comments

  • danlitt a day ago ago

    You can't parse [X]HTML with regex. Because HTML can't be parsed by regex. Regex is not a tool that can be used to correctly parse HTML.

    • layer8 a day ago ago

      Some commenters are missing that this is a reference to https://stackoverflow.com/a/1732454.

      • chrisandchris a day ago ago

        > Locked. There are disputes about this answer’s content being resolved at this time [sic]

        And also

        > Nov, 2020

        And then, StackOverflow asks itself why it looses users.

        • layer8 a day ago ago

          The answer is actually from 2009. As long as HTML/XML doesn’t suddenly become a regular language, I think it’s pretty timeless.

        • magicalist a day ago ago

          It's a 17 year old joke, maybe one of the most famous on stackoverflow for the aging engineers out there. No one wants new "funny" edits on it.

      • pwdisswordfishq a day ago ago

        Good ol' appeal to emotion devoid of technical argument.

    • rokkamokka a day ago ago

      You can parse a subset of it though, like if you're in control of the html yourself and avoid certain structures

    • zarzavat a day ago ago

      Parsing HTML with a regex is never a good option, but it's sometimes the only option.

      • pkal a day ago ago

        In the example from the article it certainly is an option. In Python you could either use a "soup" library or you could play around with a tool like https://www.w3.org/Tools/HTML-XML-utils/man1/hxpipe.html.

        The more fundamental question for me is why the author didn't decide to make make code blocks non-breaking by default, or just add the class annotations when he writes the HTML?

    • tyho a day ago ago

      You can absolutely parse HTML with regex, so long as the document is finite in length. Every finite language is regular, hence can be parsed with regex's.

      • magicalist a day ago ago

        Zalgo and situational subset parsing aside:

        > You can absolutely parse HTML with regex, so long as the document is finite in length

        This isn't sufficient, unless I'm misinterpreting what you're saying. It's not enough to have documents of finite length (all documents are finite in length), you need documents with a max length, so you have a finite number of possible documents to parse.

      • gpvos a day ago ago

        Do a web search for the parent of your comment, read the Stackoverflow answer. It's a classic. Learn about Zalgo and Tony the pony, he comes.

      • vrighter 15 hours ago ago

        and you write your regex specifically for that document length, handling all possible nesting combinations. combinatoric explosion

      • undefined a day ago ago
        [deleted]
    • matheusmoreira a day ago ago

      Tony the pony, he comes.

    • timedude a day ago ago

      What do you mean you can't. I do it all the time

    • prmoustache a day ago ago

      I do not count substitution as "parsing"

  • pwdisswordfishq a day ago ago

    Why even bother with the CSS class? Just apply text-wrap: nowrap; to all <code> elements and dispense with the fragile regex parsing.

  • dajtxx 18 hours ago ago

    The 5 cm example had a line break between the 5 and the cm when I read the page in my phone. A joke?

  • pimlottc a day ago ago

    Unfortunately this makes it much harder to read on narrow-width screens (e.g. mobile), where the use must scroll horizontally