Break Lines

A jQuery plugin for inserting HTML where there are automatic line breaks.

by CarambaMoreno

HTML

<div id="break-lines">
    <p>
        Lorem ipsum dolor sit amet, <b>consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</b> Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
    </p>
    <p>
        Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum.
    </p>
    <p>     
        Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.
    </p>
</div>

CSS

#break-lines {
    width: 400px;
}

JavaScript

/*jslint sub: true, maxlen: 80, indent: 4 */

// ==ClosureCompiler==
// @output_file_name jquery.breakLines.min.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// @code_url http://goo.gl/eL6PR
// ==/ClosureCompiler==

(function ($) {
    "use strict";

    // Written for: http://stackoverflow.com/questions/4671713/#7431801
    // by Nathan MacInnes, [email protected]

    // use square bracket notation for Closure Compiler
    $.fn['breakLines'] = function (options) {
        var defaults = {
            // HTML to insert before each new line
            'lineBreakHtml' : '<br />',
            // Set this to true to have the HTML inserted at the start of a
            // <p> or other block tag
            'atStartOfBlocks' : false,
            // false: <LINEBREAK><span>text</span>;
            // true: <span><LINEBREAK>text</span>
            'insideStartOfTags' : false,
            // If set, the element's size will be set to this before being
            // wrapped, then reset to its original value aftwerwards
            'widthToWrapTo' : false
        };
        options = $.extend(defaults, options);
        return this.each(function () {
            var textNodes, // all textNodes (as opposed to elements)
                copy, // jQuery object for copy of the current element
                el = $(this), // just so we know what we're working with
                recurseThroughNodes, // function to do the spitting/moving
                insertedBreaks, // jQuery collection of inserted line breaks
                styleAttr; // Backup of the element's style attribute

            // Backup the style attribute because we'll be changing it
            styleAttr = $(this).attr('style');

            // Make sure the height will actually change as content goes in
            el.css('height', 'auto');

            // If the user wants to wrap to a different width than the one
            // set by CSS
            if (options.widthToWrapTo !== false) {
              ...