\s*<\/span>/g, '')
.replace(/(href=")(?:\.?\.\/)+/g, '$1')
// replace unnecessary URL escaping
.replace(/ href="[^"]*"/g, Util.decodeURI)
// strip thumbnail size prefixes
.replace(/(src="[^"]*?)\/thumb(\/[0-9a-f]\/[0-9a-f]{2}\/[^\/]+)\/[0-9]+px-[^"\/]+(?=")/g, '$1$2');
};
/**
* Normalize newlines in IEW to spaces instead.
*
* @param {Node} body
* The document `` node to normalize.
* @param {RegExp} [stripSpanTypeof]
* @param {boolean} [parsoidOnly=false]
* @param {boolean} [preserveIEW=false]
* @return {Node}
*/
TestUtils.unwrapSpansAndNormalizeIEW = function(body, stripSpanTypeof, parsoidOnly, preserveIEW) {
var newlineAround = function(node) {
return node && /^(BODY|CAPTION|DIV|DD|DT|LI|P|TABLE|TR|TD|TH|TBODY|DL|OL|UL|H[1-6])$/.test(node.nodeName);
};
var unwrapSpan; // forward declare
var cleanSpans = function(node) {
var child, next;
if (!stripSpanTypeof) {
return;
}
for (child = node.firstChild; child; child = next) {
next = child.nextSibling;
if (child.nodeName === 'SPAN' &&
stripSpanTypeof.test(child.getAttribute('typeof') || '')) {
unwrapSpan(node, child);
}
}
};
unwrapSpan = function(parent, node) {
// first recurse to unwrap any spans in the immediate children.
cleanSpans(node);
// now unwrap this span.
DOMUtils.migrateChildren(node, parent, node);
parent.removeChild(node);
};
var visit = function(node, stripLeadingWS, stripTrailingWS, inPRE) {
var child, next, prev;
if (node.nodeName === 'PRE') {
// Preserve newlines in tags
inPRE = true;
}
if (!preserveIEW && DOMUtils.isText(node)) {
if (!inPRE) {
node.data = node.data.replace(/\s+/g, ' ');
}
if (stripLeadingWS) {
node.data = node.data.replace(/^\s+/, '');
}
if (stripTrailingWS) {
node.data = node.data.replace(/\s+$/, '');
}
}
// unwrap certain SPAN nodes
cleanSpans(node);
// now remove comment nodes
if (!parsoidOnly) {
for (child = node.firstChild; child; child = next) {
next = child.nextSibling;
if (DOMUtils.isComment(child)) {
node.removeChild(child);
}
}
}
// reassemble text nodes split by a comment or span, if necessary
node.normalize();
// now recurse.
if (node.nodeName === 'PRE') {
// hack, since PHP adds a newline before
stripLeadingWS = false;
stripTrailingWS = true;
} else if (node.nodeName === 'SPAN' &&
/^mw[:]/.test(node.getAttribute('typeof') || '')) {
// SPAN is transparent; pass the strip parameters down to kids
} else {
stripLeadingWS = stripTrailingWS = newlineAround(node);
}
child = node.firstChild;
// Skip over the empty mw:FallbackId and strip leading WS
// on the other side of it.
if (/^H[1-6]$/.test(node.nodeName) &&
child && WTUtils.isFallbackIdSpan(child)) {
child = child.nextSibling;
}
for (; child; child = next) {
next = child.nextSibling;
visit(child,
stripLeadingWS,
stripTrailingWS && !child.nextSibling,
inPRE);
stripLeadingWS = false;
}
if (inPRE || preserveIEW) {
return node;
}
// now add newlines around appropriate nodes.
for (child = node.firstChild; child; child = next) {
prev = child.previousSibling;
next = child.nextSibling;
if (newlineAround(child)) {
if (prev && DOMUtils.isText(prev)) {
prev.data = prev.data.replace(/\s*$/, '\n');
} else {
prev = node.ownerDocument.createTextNode('\n');
node.insertBefore(prev, child);
}
if (next && DOMUtils.isText(next)) {
next.data = next.data.replace(/^\s*/, '\n');
} else {
next = node.ownerDocument.createTextNode('\n');
node.insertBefore(next, child.nextSibling);
}
}
}
return node;
};
// clone body first, since we're going to destructively mutate it.
return visit(body.cloneNode(true), true, true, false);
};
if (typeof module === "object") {
module.exports.TestUtils = TestUtils;
}