1const HTML = require('../../packages/parse5/lib/common/html'); 2 3function createFragmentContext(tagName, treeAdapter) { 4 if (!tagName) { 5 return null; 6 } 7 8 let namespace = HTML.NAMESPACES.HTML; 9 const parts = tagName.split(' '); 10 11 if (parts.length > 1) { 12 tagName = parts[1]; 13 14 if (parts[0] === 'svg') { 15 namespace = HTML.NAMESPACES.SVG; 16 } else if (parts[0] === 'math') { 17 namespace = HTML.NAMESPACES.MATHML; 18 } 19 } 20 21 return treeAdapter.createElement(tagName, namespace, []); 22} 23 24module.exports = function parseDatFile(testSet, treeAdapter) { 25 const testDescrs = []; 26 let curDirective = ''; 27 let curDescr = null; 28 29 testSet.split(/\r?\n/).forEach((line, idx) => { 30 if (line === '#data') { 31 curDescr = { '#line': idx + 1 }; 32 testDescrs.push(curDescr); 33 } 34 35 if (line[0] === '#') { 36 curDirective = line; 37 curDescr[curDirective] = []; 38 } else { 39 curDescr[curDirective].push(line); 40 } 41 }); 42 43 return testDescrs.map(descr => { 44 const fragmentContextTagName = descr['#document-fragment'] && descr['#document-fragment'][0]; 45 46 return { 47 input: descr['#data'].join('\n'), 48 expected: descr['#document'].join('\n'), 49 expectedErrors: descr['#new-errors'] || [], 50 disableEntitiesDecoding: !!descr['#disable-html-entities-decoding'], 51 lineNum: descr['#line'], 52 scriptingEnabled: !descr['#script-off'], 53 fragmentContext: createFragmentContext(fragmentContextTagName, treeAdapter) 54 }; 55 }); 56}; 57