| 1 |
/** |
| 2 |
* @module |
| 3 |
*/ |
| 4 |
|
| 5 |
'use strict'; |
| 6 |
|
| 7 |
require('../core-upgrade.js'); |
| 8 |
|
| 9 |
var entities = require('entities'); |
| 10 |
|
| 11 |
var ContentUtils = require('../lib/utils/ContentUtils.js').ContentUtils; |
| 12 |
var DOMUtils = require('../lib/utils/DOMUtils.js').DOMUtils; |
| 13 |
var DOMDataUtils = require('../lib/utils/DOMDataUtils.js').DOMDataUtils; |
| 14 |
var Util = require('../lib/utils/Util.js').Util; |
| 15 |
var WTUtils = require('../lib/utils/WTUtils.js').WTUtils; |
| 16 |
var DOMNormalizer = require('../lib/html2wt/DOMNormalizer.js').DOMNormalizer; |
| 17 |
var MockEnv = require('./MockEnv.js').MockEnv; |
| 18 |
|
| 19 |
var TestUtils = {}; |
| 20 |
|
| 21 |
/** |
| 22 |
* Little helper function for encoding XML entities. |
| 23 |
* |
| 24 |
* @param {string} string |
| 25 |
* @return {string} |
| 26 |
*/ |
| 27 |
TestUtils.encodeXml = function(string) { |
| 28 |
return entities.encodeXML(string); |
| 29 |
}; |
| 30 |
|
| 31 |
/** |
| 32 |
* Specialized normalization of the PHP parser & Parsoid output, to ignore |
| 33 |
* a few known-ok differences in parser test runs. |
| 34 |
* |
| 35 |
* This code is also used by the Parsoid round-trip testing code. |
| 36 |
* |
| 37 |
* If parsoidOnly is true-ish, we allow more markup through (like property |
| 38 |
* and typeof attributes), for better checking of parsoid-only test cases. |
| 39 |
* |
| 40 |
* @param {string} domBody |
| 41 |
* @param {Object} options |
| 42 |
* @param {boolean} [options.parsoidOnly=false] |
| 43 |
* @param {boolean} [options.preserveIEW=false] |
| 44 |
* @param {boolean} [options.hackNormalize=false] |
| 45 |
* @return {string} |
| 46 |
*/ |
| 47 |
TestUtils.normalizeOut = function(domBody, options) { |
| 48 |
if (!options) { |
| 49 |
options = {}; |
| 50 |
} |
| 51 |
const parsoidOnly = options.parsoidOnly; |
| 52 |
const preserveIEW = options.preserveIEW; |
| 53 |
|
| 54 |
if (options.hackyNormalize) { |
| 55 |
// Mock env obj |
| 56 |
// |
| 57 |
// FIXME: This is ugly. |
| 58 |
// (a) The normalizer shouldn't need the full env. |
| 59 |
// Pass options and a logger instead? |
| 60 |
// (b) DOM diff code is using page-id for some reason. |
| 61 |
// That feels like a carryover of 2013 era code. |
| 62 |
// If possible, get rid of it and diff-mark dependency |
| 63 |
// on the env object. |
| 64 |
const env = new MockEnv({}, null); |
| 65 |
if (typeof (domBody) === 'string') { |
| 66 |
domBody = env.createDocument(domBody).body; |
| 67 |
} |
| 68 |
var mockState = { |
| 69 |
env, |
| 70 |
selserMode: false, |
| 71 |
}; |
| 72 |
DOMDataUtils.visitAndLoadDataAttribs(domBody, { markNew: true }); |
| 73 |
domBody = (new DOMNormalizer(mockState).normalize(domBody)); |
| 74 |
DOMDataUtils.visitAndStoreDataAttribs(domBody); |
| 75 |
} else if (typeof (domBody) === 'string') { |
| 76 |
domBody = DOMUtils.parseHTML(domBody).body; |
| 77 |
} |
| 78 |
|
| 79 |
var stripTypeof = parsoidOnly ? |
| 80 |
/^mw:Placeholder$/ : |
| 81 |
/^mw:(?:DisplaySpace|Placeholder|Nowiki|Transclusion|Entity)$/; |
| 82 |
domBody = this.unwrapSpansAndNormalizeIEW(domBody, stripTypeof, parsoidOnly, preserveIEW); |
| 83 |
var out = ContentUtils.toXML(domBody, { innerXML: true }); |
| 84 |
// NOTE that we use a slightly restricted regexp for "attribute" |
| 85 |
// which works for the output of DOM serialization. For example, |
| 86 |
// we know that attribute values will be surrounded with double quotes, |
| 87 |
// not unquoted or quoted with single quotes. The serialization |
| 88 |
// algorithm is given by: |
| 89 |
// http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#serializing-html-fragments |
| 90 |
if (!/[^<]*(<\w+(\s+[^\0-\cZ\s"'>\/=]+(="[^"]*")?)*\/?>[^<]*)*/.test(out)) { |
| 91 |
throw new Error("normalizeOut input is not in standard serialized form"); |
| 92 |
} |
| 93 |
|
| 94 |
// Eliminate a source of indeterminacy from leaked strip markers |
| 95 |
out = out.replace(/UNIQ-.*?-QINU/g, ''); |
| 96 |
|
| 97 |
// Normalize COINS ids -- they aren't stable |
| 98 |
out = out.replace(/\s?id=['"]coins_\d+['"]/ig, ''); |
| 99 |
|
| 100 |
// maplink extension |
| 101 |
out = out.replace(/\s?data-overlays='[^']*'/ig, ''); |
| 102 |
|
| 103 |
if (parsoidOnly) { |
| 104 |
// unnecessary attributes, we don't need to check these |
| 105 |
// style is in there because we should only check classes. |
| 106 |
out = out.replace(/ (data-parsoid|prefix|about|rev|datatype|inlist|usemap|vocab)=\\?"[^\"]*\\?"/g, ''); |
| 107 |
// single-quoted variant |
| 108 |
out = out.replace(/ (data-parsoid|prefix|about|rev|datatype|inlist|usemap|vocab)=\\?'[^\']*\\?'/g, ''); |
| 109 |
// apos variant |
| 110 |
out = out.replace(/ (data-parsoid|prefix|about|rev|datatype|inlist|usemap|vocab)='.*?'/g, ''); |
| 111 |
|
| 112 |
// strip self-closed <nowiki /> because we frequently test WTS |
| 113 |
// <nowiki> insertion by providing an html/parsoid section with the |
| 114 |
// <meta> tags stripped out, allowing the html2wt test to verify that |
| 115 |
// the <nowiki> is correctly added during WTS, while still allowing |
| 116 |
// the html2html and wt2html versions of the test to pass as a |
| 117 |
// sanity check. If <meta>s were not stripped, these tests would all |
| 118 |
// have to be modified and split up. Not worth it at this time. |
| 119 |
// (see commit 689b22431ad690302420d049b10e689de6b7d426) |
| 120 |
out = out |
| 121 |
.replace(/<span typeof="mw:Nowiki"><\/span>/g, ''); |
| 122 |
|
| 123 |
return out; |
| 124 |
} |
| 125 |
|
| 126 |
// Normalize headings by stripping out Parsoid-added ids so that we don't |
| 127 |
// have to add these ids to every parser test that uses headings. |
| 128 |
// We will test the id generation scheme separately via mocha tests. |
| 129 |
out = out.replace(/(<h[1-6].*?) id="[^"]*"([^>]*>)/g, '$1$2'); |
| 130 |
|
| 131 |
// strip meta/link elements |
| 132 |
out = out |
| 133 |
.replace(/<\/?(?:meta|link)(?: [^\0-\cZ\s"'>\/=]+(?:=(?:"[^"]*"|'[^']*'))?)*\/?>/g, ''); |
| 134 |
// Ignore troublesome attributes. |
| 135 |
// Strip JSON attributes like data-mw and data-parsoid early so that |
| 136 |
// comment stripping in normalizeNewlines does not match unbalanced |
| 137 |
// comments in wikitext source. |
| 138 |
out = out.replace(/ (data-parsoid|prefix|about|rev|datatype|inlist|usemap|vocab|data-mw|resource|rel|property|class)=\\?"[^\"]*\\?"/g, ''); |
| 139 |
// single-quoted variant |
| 140 |
out = out.replace(/ (data-parsoid|prefix|about|rev|datatype|inlist|usemap|vocab|data-mw|resource|rel|property|class)=\\?'[^\']*\\?'/g, ''); |
| 141 |
// strip typeof last |
| 142 |
out = out.replace(/ typeof="[^\"]*"/g, ''); |
| 143 |
|
| 144 |
return out |
| 145 |
// replace mwt ids |
| 146 |
.replace(/ id="mw((t\d+)|([\w-]{2,}))"/g, '') |
| 147 |
.replace(/<span[^>]+about="[^"]*"[^>]*>/g, '') |
| 148 |
.replace(/(\s)<span>\s*<\/span>\s*/g, '$1') |
| 149 |
.replace(/<span>\s*<\/span>/g, '') |
| 150 |
.replace(/(href=")(?:\.?\.\/)+/g, '$1') |
| 151 |
// replace unnecessary URL escaping |
| 152 |
.replace(/ href="[^"]*"/g, Util.decodeURI) |
| 153 |
// strip thumbnail size prefixes |
| 154 |
.replace(/(src="[^"]*?)\/thumb(\/[0-9a-f]\/[0-9a-f]{2}\/[^\/]+)\/[0-9]+px-[^"\/]+(?=")/g, '$1$2'); |
| 155 |
}; |
| 156 |
|
| 157 |
/** |
| 158 |
* Normalize newlines in IEW to spaces instead. |
| 159 |
* |
| 160 |
* @param {Node} body |
| 161 |
* The document `<body>` node to normalize. |
| 162 |
* @param {RegExp} [stripSpanTypeof] |
| 163 |
* @param {boolean} [parsoidOnly=false] |
| 164 |
* @param {boolean} [preserveIEW=false] |
| 165 |
* @return {Node} |
| 166 |
*/ |
| 167 |
TestUtils.unwrapSpansAndNormalizeIEW = function(body, stripSpanTypeof, parsoidOnly, preserveIEW) { |
| 168 |
var newlineAround = function(node) { |
| 169 |
return node && /^(BODY|CAPTION|DIV|DD|DT|LI|P|TABLE|TR|TD|TH|TBODY|DL|OL|UL|H[1-6])$/.test(node.nodeName); |
| 170 |
}; |
| 171 |
var unwrapSpan; // forward declare |
| 172 |
var cleanSpans = function(node) { |
| 173 |
var child, next; |
| 174 |
if (!stripSpanTypeof) { |
| 175 |
return; |
| 176 |
} |
| 177 |
for (child = node.firstChild; child; child = next) { |
| 178 |
next = child.nextSibling; |
| 179 |
if (child.nodeName === 'SPAN' && |
| 180 |
stripSpanTypeof.test(child.getAttribute('typeof') || '')) { |
| 181 |
unwrapSpan(node, child); |
| 182 |
} |
| 183 |
} |
| 184 |
}; |
| 185 |
unwrapSpan = function(parent, node) { |
| 186 |
// first recurse to unwrap any spans in the immediate children. |
| 187 |
cleanSpans(node); |
| 188 |
// now unwrap this span. |
| 189 |
DOMUtils.migrateChildren(node, parent, node); |
| 190 |
parent.removeChild(node); |
| 191 |
}; |
| 192 |
var visit = function(node, stripLeadingWS, stripTrailingWS, inPRE) { |
| 193 |
var child, next, prev; |
| 194 |
if (node.nodeName === 'PRE') { |
| 195 |
// Preserve newlines in <pre> tags |
| 196 |
inPRE = true; |
| 197 |
} |
| 198 |
if (!preserveIEW && DOMUtils.isText(node)) { |
| 199 |
if (!inPRE) { |
| 200 |
node.data = node.data.replace(/\s+/g, ' '); |
| 201 |
} |
| 202 |
if (stripLeadingWS) { |
| 203 |
node.data = node.data.replace(/^\s+/, ''); |
| 204 |
} |
| 205 |
if (stripTrailingWS) { |
| 206 |
node.data = node.data.replace(/\s+$/, ''); |
| 207 |
} |
| 208 |
} |
| 209 |
// unwrap certain SPAN nodes |
| 210 |
cleanSpans(node); |
| 211 |
// now remove comment nodes |
| 212 |
if (!parsoidOnly) { |
| 213 |
for (child = node.firstChild; child; child = next) { |
| 214 |
next = child.nextSibling; |
| 215 |
if (DOMUtils.isComment(child)) { |
| 216 |
node.removeChild(child); |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
// reassemble text nodes split by a comment or span, if necessary |
| 221 |
node.normalize(); |
| 222 |
// now recurse. |
| 223 |
if (node.nodeName === 'PRE') { |
| 224 |
// hack, since PHP adds a newline before </pre> |
| 225 |
stripLeadingWS = false; |
| 226 |
stripTrailingWS = true; |
| 227 |
} else if (node.nodeName === 'SPAN' && |
| 228 |
/^mw[:]/.test(node.getAttribute('typeof') || '')) { |
| 229 |
// SPAN is transparent; pass the strip parameters down to kids |
| 230 |
} else { |
| 231 |
stripLeadingWS = stripTrailingWS = newlineAround(node); |
| 232 |
} |
| 233 |
child = node.firstChild; |
| 234 |
// Skip over the empty mw:FallbackId <span> and strip leading WS |
| 235 |
// on the other side of it. |
| 236 |
if (/^H[1-6]$/.test(node.nodeName) && |
| 237 |
child && WTUtils.isFallbackIdSpan(child)) { |
| 238 |
child = child.nextSibling; |
| 239 |
} |
| 240 |
for (; child; child = next) { |
| 241 |
next = child.nextSibling; |
| 242 |
visit(child, |
| 243 |
stripLeadingWS, |
| 244 |
stripTrailingWS && !child.nextSibling, |
| 245 |
inPRE); |
| 246 |
stripLeadingWS = false; |
| 247 |
} |
| 248 |
if (inPRE || preserveIEW) { |
| 249 |
return node; |
| 250 |
} |
| 251 |
// now add newlines around appropriate nodes. |
| 252 |
for (child = node.firstChild; child; child = next) { |
| 253 |
prev = child.previousSibling; |
| 254 |
next = child.nextSibling; |
| 255 |
if (newlineAround(child)) { |
| 256 |
if (prev && DOMUtils.isText(prev)) { |
| 257 |
prev.data = prev.data.replace(/\s*$/, '\n'); |
| 258 |
} else { |
| 259 |
prev = node.ownerDocument.createTextNode('\n'); |
| 260 |
node.insertBefore(prev, child); |
| 261 |
} |
| 262 |
if (next && DOMUtils.isText(next)) { |
| 263 |
next.data = next.data.replace(/^\s*/, '\n'); |
| 264 |
} else { |
| 265 |
next = node.ownerDocument.createTextNode('\n'); |
| 266 |
node.insertBefore(next, child.nextSibling); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
return node; |
| 271 |
}; |
| 272 |
// clone body first, since we're going to destructively mutate it. |
| 273 |
return visit(body.cloneNode(true), true, true, false); |
| 274 |
}; |
| 275 |
|
| 276 |
if (typeof module === "object") { |
| 277 |
module.exports.TestUtils = TestUtils; |
| 278 |
} |