function coalesceEMTags(elHTML) {
elHTML = elHTML.replace(/<em>([^<>]*)<\/em>, <em>([^<>]*)<\/em>/g, "<em>$1, $2</em>");
elHTML = elHTML.replace(/<em>([^<>]*)<\/em>: <em>([^<>]*)<\/em>/g, "<em>$1, $2</em>");
return elHTML;
elHTML = elHTML.replace(/<em>([^<>]*)<\/em>, <em>([^<>]*)<\/em>,[\s\n.]*<em>([^<>]*)<\/em>/g, "<em>$1, $2, $3</em>")
}
function elementIsP(el) {
return (el.tagName.toLowerCase() === 'p');
}
function elementIsEM(el) {
return (el.tagName.toLowerCase() === 'em');
}
function breakOnEM(brElement) {
let defparent = document.getElementsByClassName('rudef')[0];
// coalesce adjacent <em> tags so we don't add breaks
// unnecessarily
defparent.innerHTML = coalesceEMTags(defparent.innerHTML);
var descendants = defparent.querySelectorAll("*");
let ignoreEM = false;
for (var i = 0; i < descendants.length; i++) {
let thisDescendant = descendants[i];
if (elementIsP(thisDescendant) ) {
let elHTML = thisDescendant.innerHTML;
// if the first descendant after <p> is <em>, ignore
// because we don't want to add a line break
// on the first one
ignoreEM = elHTML.startsWith('<em>');
}
if ( elementIsEM(thisDescendant) ) {
if (!ignoreEM) {
// check if the content is just an abbreviation
// there may be others, so we need to watch
if (thisDescendant.textContent === 'гл.') {
continue;
}
const elem = document.createElement(brElement);
thisDescendant.parentNode.insertBefore(elem, thisDescendant);
} else {
ignoreEM = false;
}
}
}
}
breakOnEM('p');
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.