Skip to content

Marked removes non-breaking spaces in the original text #363

Closed

Description

@arturi

Consider, say, a text like this:
2 125 euro
I've put a non-breaking space between 2 and 125 so that it would always end up on the same line.

Marked pre-parses the text and completely removes the original non-breaking characters that I've put there:

Lexer.prototype.lex = function(src) {
  src = src
    .replace(/\r\n|\r/g, '\n')
    .replace(/\t/g, '    ')
    .replace(/\u00a0/g, ' ')
    .replace(/\u2424/g, '\n');
  return this.token(src, true);
};

This is where the devil hides: .replace(/\u00a0/g, ' ')

Here is more on why invisible non-breaking space characters are cool: http://destroytoday.com/findings/fix-widows-with-non-breaking-spaces/

Activity

  1. christopherscott commented on Mar 12, 2014

    @christopherscott

    +1, if anything this should be an option, or configurable

  2. daleconboy commented on Mar 12, 2014

    @daleconboy

    Yes! I've recently lost a few hours of my life tracking this very same thing down.

  3. chjj commented on Mar 13, 2014

    @chjj
    Member

    @daleconboy, I'm sorry to hear that, but many people lost several hours of their lives trying to figure out why their spaces weren't getting processed correctly when text was passed in from the DOM (see #52 - cc @OscarGodson), which is why this was added in the first place.

    I'll consider adding an option, but I want to keep their removal the default since more people probably get bit by this "feature" of contenteditable elements than not.

  4. daleconboy commented on Mar 14, 2014

    @daleconboy

    Hey, thanks for the response. I definitely sympathize with anyone who's been bitten by this quirk in any way, however I would argue against the default being wholesale replacement of non-breaking spaces.

    Reason being, it's not a bug with marked, but rather a browser behavior which shouldn't be the responsibility of marked to manage. Technically the responsibility should fall on the developer who's using the contenteditable elements to be aware of the quirk and to manage the white space handling, or conversion, on their end.

    The W3C working draft specifically calls this out to authors working with contenteditable elements:

    http://www.w3.org/TR/html51/editing.html#best-practices-for-in-page-editors

    Authors are encouraged to set the 'white-space' property on editing hosts and on markup that was originally created through these editing mechanisms to the value 'pre-wrap' …

    It seems that with contenteditable regions expected to behave in this way, you would want to preserve their expected behavior by default to avoid confusion. This, in turn, would also avoid the confusion where devs are expecting their explicitly set non-breaking spaces to behave as expected.

    And, since marked may also be used in a node environment where contenteditabe does not exist, this replacement behavior by default would be unexpected.

    Bottom line, I appreciate you considering it as an option. How you decide to set the default behavior is of course up to you. Any option is definitely better than no option. I'll cast my vote for the default being no replacement. :)

    Cheers!

  5. drscannell commented on Mar 14, 2014

    @drscannell

    @daleconboy's argument is pretty convincing. Are there other use cases for no-break spaces in markdown input? I would think a set of tests would help define the severity of the issue.

  6. OscarGodson commented on Mar 14, 2014

    @OscarGodson

    @daleconboy I like your point about the browser, except, in @arturi's post he specifically points out that spaces are good to fix a browser bug haha :) Also, i wouldn't agree that it's a browser issue. Markdown's "spec" doesn't say which kind of spaces are and aren't allowed so IMO Marked, and any markdown parser, should assume all spaces (nbsp, unicode, etc) should be considered what they are: spaces. Your suggestion, unless im misunderstanding it, is wanted to specifically ignore certain kinds of spaces.

  7. scy commented on Apr 8, 2014

    @scy

    I’m working on a Markdown-based presentation tool, and I’m using marked to generate HTML.

    Having control over when and where text wraps is vital in a good presentation. Currently, the only way I can do that with marked is by overriding the lexer with a custom one that does the same things as the original one, except for the NBSP replacement. This is of course far from future-proof: In case the original lexer changes, I have to adapt my code.

    Therefore I’m very much in favor of making this configurable. If you’re interested in a PR, let us know. And although I think that not replacing the NBSPs is the “right” thing to do, I can understand that you don’t want to break existing code that relies on marked fixing the browser behavior. So, I don’t care what the default for this option is, but please introduce one.

  8. Lendar commented on Jun 17, 2015

    @Lendar

    @scy suggestion is nice. Let me extend it with an example. It might be helpful for future readers...

    import marked from 'marked';
    
    // monkey patch for marked 0.3.3 to preserve non-breaking spaces
    marked.Lexer.prototype.lex = function (src) {
      src = src
        .replace(/\r\n|\r/g, '\n')
        .replace(/\t/g, '    ')
        .replace(/\u2424/g, '\n');
    
      return this.token(src, true);
    };
    

    UPDATED 2017-05-11: fixed syntax

  9. RichardForrester commented on Feb 15, 2016

    @RichardForrester

    Is anyone aware of an option or a work around for this issue?

    There should definitely be an option to allow non-breaking spaces to pass to the HTML.

  10. arturi commented on Feb 15, 2016

    @arturi
    Author

    I’ve solved it by extending lex, as shown here: #363 (comment).

  11. deanvaessen commented on Apr 29, 2017

    @deanvaessen

    Hello everyone,

    @Lendar 's suggestion is wonderful and if I edit this in the source code I can fix it this way. However putting it straight into my own code complains about 'this.token' not being a function. What is the best way to implement this?

    Cheers!

  12. davidchambers commented on Apr 29, 2017

    @davidchambers

    I imagine the problem, @deanvaessen, is the arrow function. Try replacing (src) => { ... } with function(src) { ... }. :)

  13. deanvaessen commented on May 4, 2017

    @deanvaessen

    Confirmed. Thank you @davidchambers :)

  14. Lendar commented on May 11, 2017

    @Lendar

    @davidchambers @deanvaessen surprised it's still relevant. Updated the example in the comment ⬆️

  15. yurikhan commented on May 28, 2017

    @yurikhan

    I am in the same boat as @scy — working on a Markdown-based presentation tool. I, too, want to control where lines break and where they never break. Please make an option that stops breaking non-breaking spaces.

    As for browsers and/or WYSIWYG editors inserting non-breaking spaces where not expicitly requested by the user, that’s their bugs and should be fixed there.

  16. 12 remaining items

  17. joshbruce commented on Feb 11, 2018

    @joshbruce
    Member

    @Feder1co5oave: Reopen or no?

    Again, I'm not sure if the original ticket was referring to the html encoded   or a unicode character discovery - not the same thing in my book. As a user, I would expect the   to be preserved, but not necessarily a special character injection of UTF-8 or something similar...am I wrong there. I concur that Chris's assessment is debatable.

    Leave it to you, brother.

  18. Feder1co5oave commented on Feb 11, 2018

    @Feder1co5oave
    Contributor
  19. joshbruce commented on Feb 11, 2018

    @joshbruce
    Member

    All right. Leaving closed for now. Flagging with newly minted #1048 for when we're ready to focus there. This could also explain the Chinese character problems with header ids, yeah?

  20. Feder1co5oave commented on Feb 11, 2018

    @Feder1co5oave
    Contributor
  21. added this to the milestone on Feb 23, 2018
  22. Feder1co5oave commented on Feb 23, 2018

    @Feder1co5oave
    Contributor

    Tagging as #1048

  23. joshbruce commented on Feb 24, 2018

    @joshbruce
    Member

    Tagging #1043 as well, just because of the "header ids" comment.

  24. modified the milestones: , v1.x - All the nope release on Apr 4, 2018
  25. UziTech commented on Dec 5, 2018

    @UziTech
    Member

    related pr #897

  26. added
    has PR
    on Dec 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    has PRproposal

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions