code_junkie

Who has solved the long-word-breaks-my-div problem? (hint: not stackoverflow)

Nov 12th, 2011
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. averyvery­longword
  2.  
  3. averyverylongword
  4.  
  5. averyvery-
  6. longword
  7.  
  8. /([^s-]{5})([^s-]{5})/ → $1­$2
  9.  
  10. .word-break {
  11. /* The following styles prevent unbroken strings from breaking the layout */
  12. width: 300px; /* set to whatever width you need */
  13. overflow: auto;
  14. white-space: -moz-pre-wrap; /* Mozilla */
  15. white-space: -hp-pre-wrap; /* HP printers */
  16. white-space: -o-pre-wrap; /* Opera 7 */
  17. white-space: -pre-wrap; /* Opera 4-6 */
  18. white-space: pre-wrap; /* CSS 2.1 */
  19. white-space: pre-line; /* CSS 3 (and 2.1 as well, actually) */
  20. word-wrap: break-word; /* IE */
  21. -moz-binding: url('xbl.xml#wordwrap'); /* Firefox (using XBL) */
  22. }
  23.  
  24. <?xml version="1.0" encoding="utf-8"?>
  25. <bindings xmlns="http://www.mozilla.org/xbl"
  26. xmlns:html="http://www.w3.org/1999/xhtml">
  27. <!--
  28. More information on XBL:
  29. http://developer.mozilla.org/en/docs/XBL:XBL_1.0_Reference
  30.  
  31. Example of implementing the CSS 'word-wrap' feature:
  32. http://blog.stchur.com/2007/02/22/emulating-css-word-wrap-for-mozillafirefox/
  33. -->
  34. <binding id="wordwrap" applyauthorstyles="false">
  35. <implementation>
  36. <constructor>
  37. //<![CDATA[
  38. var elem = this;
  39.  
  40. doWrap();
  41. elem.addEventListener('overflow', doWrap, false);
  42.  
  43. function doWrap() {
  44. var walker = document.createTreeWalker(elem, NodeFilter.SHOW_TEXT, null, false);
  45. while (walker.nextNode()) {
  46. var node = walker.currentNode;
  47. node.nodeValue = node.nodeValue.split('').join(String.fromCharCode('8203'));
  48. }
  49. }
  50. //]]>
  51. </constructor>
  52. </implementation>
  53. </binding>
  54. </bindings>
  55.  
  56. .word_wrap
  57. {
  58. white-space: pre-wrap; /* css-3 */
  59. white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
  60. white-space: -pre-wrap; /* Opera 4-6 */
  61. white-space: -o-pre-wrap; /* Opera 7 */
  62. word-wrap: break-word; /* Internet Explorer 5.5+ */
  63. }
  64.  
  65. #consumeralerts_leftcol{
  66. float:left;
  67. width: 250px;
  68. margin-bottom:10px;
  69. word-wrap: break-word;
  70. }
  71.  
  72. word-wrap: break-word;
  73.  
  74. overflow: hidden;
  75.  
  76. <style type="text/css">
  77. .cell {
  78. float: left;
  79. width: 100px;
  80. border: 1px solid;
  81. line-height: 1em;
  82. }
  83. </style>
  84.  
  85. <div class="cell">TopLeft</div>
  86. <div class="cell">TopMiddlePlusSomeOtherTextWhichMakesItToLong</div>
  87. <div class="cell">TopRight</div>
  88. <br/>
  89. <div class="cell">BottomLeft</div>
  90. <div class="cell">BottomMiddle</div>
  91. <div class="cell">bottomRight</div>
  92.  
  93. <style type="text/css">
  94. .column {
  95. float: left;
  96. min-width: 100px;
  97. }
  98. .cell2 {
  99. border: 1px solid;
  100. line-height: 1em;
  101. }
  102. </style>
  103.  
  104. <div class="column">
  105. <div class="cell2">TopLeft</div>
  106. <div class="cell2">BottomLeft</div>
  107. </div>
  108. <div class="column">
  109. <div class="cell2">TopMiddlePlusSomeOtherTextWhichMakesItToLong</div>
  110. <div class="cell2">BottomMiddle</div>
  111. </div>
  112. <div class="column">
  113. <div class="cell2">TopRight</div>
  114. <div class="cell2">bottomRight</div>
  115. </div>
  116. <br/>
  117.  
  118. 'abcde12345678901234'.replace(/([^s-]{5})([^s-]{5})/g, '$1&shy;$2')
  119.  
  120. abcde&shy;12345678901234
  121.  
  122. .replace(/([^s-]{5})(?=[^s-])/g, '$1&shy;')
  123.  
  124. abcde&shy;12345&shy;67890&shy;1234
  125.  
  126. makeWrappable = function(str, position)
  127. {
  128. if (!str)
  129. return '';
  130. position = position || 15; // default to breaking after 15 chars
  131. // matches every requested number of chars that's not whitespace or one of the special chars defined below
  132. var longRunsRegex = cachedRegex('([^\s\./\,_@\|-]{' + position + '})(?=[^\s\./\,_@\|-])', 'g');
  133. return str
  134. .replace(longRunsRegex, '$1&#8203;') // put a zero-width space every requested number of chars that's not whitespace or a special char
  135. .replace(makeWrappable.SPECIAL_CHARS_REGEX, '$1&#8203;'); // and one after special chars we want to allow breaking after
  136. };
  137. makeWrappable.SPECIAL_CHARS_REGEX = /([./\,_@|-])/g; // period, forward slash, backslash, comma, underscore, @, |, hyphen
  138.  
  139.  
  140. cachedRegex = function(reString, reFlags)
  141. {
  142. var key = reString + (reFlags ? ':::' + reFlags : '');
  143. if (!cachedRegex.cache[key])
  144. cachedRegex.cache[key] = new RegExp(reString, reFlags);
  145. return cachedRegex.cache[key];
  146. };
  147. cachedRegex.cache = {};
  148.  
  149. makeWrappable('12345678901234567890 12345678901234567890 1234567890/1234567890')
Add Comment
Please, Sign In to add comment