Finds a position to break a string that is not part of a I have a 'read more' angular directive copied from:
https://github.com/doukasd/AngularJS-Components/blob/master/dd-text-collapse/dd-text-collapse.js

Some of my text can already have &lt;br&gt; tags in it (I ONLY allow just &lt;br&gt;) so I just needed to handle that...below is only the extra code I wrote for this purpose.

Does it seems long for something seemingly trivial?

    //---All this to not break upon &lt;br&gt; --
    if (text.includes('&lt;br&gt;')) {
        var charAtMaxLength = text.charAt(maxLength).toLowerCase();
        //console.log(charAtMaxLength);
        switch (charAtMaxLength) {
            case '&lt;':
                if (text.charAt(maxLength + 3).toLowerCase() === '&gt;') {
                    //console.log('Decrease maxLength by 1');
                    maxLength -= 1;
                }
                break;
            case 'b':
                if (text.charAt(maxLength - 1).toLowerCase() === '&lt;') {
                    //console.log('Decrease maxLength by 2');
                    maxLength -= 2;
                }
                break;
            case 'r':
                if (text.charAt(maxLength - 2).toLowerCase() === '&lt;') {
                    //console.log('Decrease maxLength by 3');
                    maxLength -= 3;
                }
                break;
            case '&gt;':
                if (text.charAt(maxLength - 3).toLowerCase() === '&lt;') {
                    //console.log('Increase maxLength by 1');
                    maxLength += 1;
                }
                break;
            default:
                //Do nothing.
                break;
        }
        //console.log(charAtMaxLength);
    }
    //--------------------------------------
You want to check if the letter that you have matched is part of a &lt;br&gt;.
But you're doing a strange check. That has bugs.

Lets say we test:


a&lt;bc&gt;d&lt;br&gt;



we say that maxLength is 1.
You'll not break the &lt;bc&gt;.
For a dry test of this:

text.includes('&lt;br&gt;') # true
text.charAt(maxLength).toLowerCase() # '&lt;'
switch ('&lt;')
case '&lt;':
text.charAt(maxLength + 3).toLowerCase() === '&gt;' # true
maxLength -= 1 # maxLength = 0


Instead you can use, indexOf, slice and length to check if the text is not allowed.
This allows for you to easily change what you want to remove, and to expand on it.
Whilst being much smaller than what you have.


Get the, lowercase, charAt.
Find the indexof it in the remove string.
Set the start as maxLength minus the index.
Check if the index is in the remove string, and check if the slice of text from start to start plus the remove strings length is the remove string.
If it is change the maxLength to start - 1.


Or in JavaScript:

var text = 'a&lt;br&gt;d&lt;br&gt;';
var remove = '&lt;br&gt;';
var maxLength = 4;

var char = text.charAt(maxLength).toLowerCase();
var index = remove.indexOf(char);
var start = maxLength - index;
if (index != -1 &amp;&amp; text.slice(start, start + remove.length).toLowerCase() == remove) {
    maxLength = start - 1;
}