How to force breaking of non breakable strings?

后端 未结 9 1047
-上瘾入骨i
-上瘾入骨i 2021-02-04 18:52

I have an HTML page that I generate from the data contained in a database. The database sometimes contains long strings that the browser can\'t break because the strings don\'t

相关标签:
9条回答
  • 2021-02-04 19:40

    Based on this article and this one as well: the "Shy Hyphen" or "Soft Hyphen" can be written in HTML as: ­ / ­ / &#xAD (173 dec = AD hex). They all convert to the U+00AD character.

    The JavaScript textContent and nodeValue of the DOM Text Nodes are not 'entity encoded' - they just contain the actual entities. In order to write these characters you must therefore encode them yourself: \xAD is a simple way to write the same character in a JavaScript string. String.fromCharCode(173) would also work.

    Based on your own VERY good answer - a jQuery Plugin version:

    $.fn.replaceInText = function(oldText, newText) {
      // contents() gets all child dom nodes -- each lets us operate on them
      this.contents().each(function() {
        if (this.nodeType == 3) { // text node found, do the replacement
            if (this.textContent) {
                this.textContent = this.textContent.replace(oldText, newText);
            } else { // support to IE
                this.nodeValue = this.nodeValue.replace(oldText, newText);
            }
        } else {
          // other types of nodes - scan them for same replace
          $(this).replaceInText(oldText, newText);
        }
      });
      return this;
    };
    
    $(function() {
        $('div').replaceInText(/\w{10}/g, "$&\xAD");
    });
    

    A side note:

    I think that the place this should happen is NOT in JavaScript - it should be in the server side code. If this is only a page used to display data- you could easily do a similar regexp replace on the text before it is sent to the browser. However the JavaScript solution offers one advantage(or disadvantage depending on how you want to look at it) - It doesn't add any extraneous characters to the data until the script executes, which means any robots crawling your HTML output for data wont see the shy hyphens. Although the HTML spec interprets it as a "hyphenation hint" and an invisible character its not guaranteed across the rest of the Unicode world: (quote from Unicode standard via the second article I linked)

    U+00AD soft hyphen indicates a hyphenation point, where a line-break is preferred when a word is to be hyphenated. Depending on the script, the visible rendering of this character when a line break occurs may differ (for example, in some scripts it is rendered as a hyphen -, while in others it may be invisible).

    Another Note: Found in this other SO Question - it seems that the "Zero Width Space" character ​ / ​ / U+200b is another option you might want to explore. It would be \x20\x0b as a javascript string.

    0 讨论(0)
  • 2021-02-04 19:43

    You can use jQuery to achieve that, but How : Let me explain a little bit. First you need to add the reference and there is a plug-in which may help you : Read More Plugin - JQuery But you need to penetrate your code during the fetch phase. At this point you can handle this problem in HttpHandler or Page_PreInit phase but w/o any server side code it must be hard or perhaps there isn't any way. I don't know but you should be able to add something in your database-fetched html page.

    0 讨论(0)
  • 2021-02-04 19:46

    As it has been pointed out numerous times, no, there is nothing you can do about it, without preprocessing the strings programmatically before displaying them.

    I know there is a strategy with inserting the soft hyphen character (­), where needed, but does not seem like a popular option.

    Check out this question: Soft hyphen in HTML ( vs. ­)

    0 讨论(0)
提交回复
热议问题