|
| 1 | +import { it, expect } from 'vitest'; |
| 2 | +import { parse, parseFragment, Parser, html, defaultTreeAdapter, type TreeAdapter } from 'parse5'; |
| 3 | +import { treeAdapters } from 'parse5-test-utils/utils/common.js'; |
| 4 | + |
| 5 | +it.each(Object.entries(treeAdapters) as [string, TreeAdapter][])( |
| 6 | + 'only reads integration-point attributes for MathML annotation-xml (%s)', |
| 7 | + (_name, adapter) => { |
| 8 | + let reads = 0; |
| 9 | + const treeAdapter: TreeAdapter = { |
| 10 | + ...adapter, |
| 11 | + getAttrList(element) { |
| 12 | + reads++; |
| 13 | + return adapter.getAttrList(element); |
| 14 | + }, |
| 15 | + }; |
| 16 | + parse('<svg a="1"><a></a><desc><b></b></desc></svg><math><mi><b></b></mi></math>', { treeAdapter }); |
| 17 | + expect(reads).toBe(0); |
| 18 | + |
| 19 | + const element = adapter.createElement('annotation-xml', html.NS.MATHML, []); |
| 20 | + const encoded = adapter.createElement('annotation-xml', html.NS.MATHML, [ |
| 21 | + { name: 'encoding', value: 'text/html' }, |
| 22 | + ]); |
| 23 | + const parser = new Parser({ treeAdapter }); |
| 24 | + expect(parser._isIntegrationPoint(html.TAG_ID.ANNOTATION_XML, element)).toBe(false); |
| 25 | + expect(parser._isIntegrationPoint(html.TAG_ID.ANNOTATION_XML, encoded)).toBe(true); |
| 26 | + expect(parser._isIntegrationPoint(html.TAG_ID.ANNOTATION_XML, encoded, html.NS.MATHML)).toBe(false); |
| 27 | + expect(reads).toBe(2); |
| 28 | + }, |
| 29 | +); |
| 30 | + |
| 31 | +const encodedContext = defaultTreeAdapter.createElement('annotation-xml', html.NS.MATHML, [ |
| 32 | + { name: 'encoding', value: 'text/html' }, |
| 33 | +]); |
| 34 | + |
| 35 | +it.each([ |
| 36 | + ['children of an HTML integration point', '<math><annotation-xml encoding=text/html>', '<i></i>', null], |
| 37 | + ['children of a plain annotation-xml', '<math><annotation-xml>', '<x></x>', null], |
| 38 | + ['p end tags', '<math><annotation-xml encoding=text/html>', '</p>', null], |
| 39 | + ['children of the fragment context', '', '<i></i>', encodedContext], |
| 40 | +])('reads the attributes of an annotation-xml element once: %s', (_name, start, repeated, context) => { |
| 41 | + let reads = 0; |
| 42 | + const treeAdapter: TreeAdapter = { |
| 43 | + ...defaultTreeAdapter, |
| 44 | + getAttrList(element) { |
| 45 | + reads++; |
| 46 | + return defaultTreeAdapter.getAttrList(element); |
| 47 | + }, |
| 48 | + }; |
| 49 | + const input = start + repeated.repeat(8); |
| 50 | + |
| 51 | + if (context) parseFragment(context, input, { treeAdapter }); |
| 52 | + else parse(input, { treeAdapter }); |
| 53 | + |
| 54 | + expect(reads).toBe(1); |
| 55 | +}); |
| 56 | + |
| 57 | +it.each(Object.entries(treeAdapters) as [string, TreeAdapter][])( |
| 58 | + 'keeps the decision with the element across stack edits (%s)', |
| 59 | + (_name, adapter) => { |
| 60 | + let reads = 0; |
| 61 | + const treeAdapter: TreeAdapter = { |
| 62 | + ...adapter, |
| 63 | + getAttrList(element) { |
| 64 | + reads++; |
| 65 | + return adapter.getAttrList(element); |
| 66 | + }, |
| 67 | + }; |
| 68 | + const parser = new Parser({ treeAdapter }); |
| 69 | + const stack = parser.openElements; |
| 70 | + const root = adapter.createElement('html', html.NS.HTML, []); |
| 71 | + const encoded = adapter.createElement('annotation-xml', html.NS.MATHML, [ |
| 72 | + { name: 'encoding', value: 'text/html' }, |
| 73 | + ]); |
| 74 | + const plain = adapter.createElement( |
| 75 | + 'annotation-xml', |
| 76 | + html.NS.MATHML, |
| 77 | + Array.from({ length: 64 }, (_, i) => ({ name: `a${i}`, value: 'v' })), |
| 78 | + ); |
| 79 | + stack.push(root, html.TAG_ID.HTML); |
| 80 | + stack.push(encoded, html.TAG_ID.ANNOTATION_XML); |
| 81 | + expect(parser.tokenizer.inForeignNode).toBe(false); |
| 82 | + stack.push(plain, html.TAG_ID.ANNOTATION_XML); |
| 83 | + expect(parser.tokenizer.inForeignNode).toBe(true); |
| 84 | + stack.pop(); |
| 85 | + expect(parser.tokenizer.inForeignNode).toBe(false); |
| 86 | + stack.insertAfter(root, plain, html.TAG_ID.ANNOTATION_XML); |
| 87 | + stack.remove(plain); |
| 88 | + stack.replace(encoded, plain); |
| 89 | + expect(parser._isIntegrationPoint(html.TAG_ID.ANNOTATION_XML, stack.current)).toBe(false); |
| 90 | + stack.push(encoded, html.TAG_ID.ANNOTATION_XML); |
| 91 | + stack.shortenToLength(2); |
| 92 | + expect(parser.tokenizer.inForeignNode).toBe(true); |
| 93 | + stack.push(encoded, html.TAG_ID.ANNOTATION_XML); |
| 94 | + stack.popAllUpToHtmlElement(); |
| 95 | + stack.push(plain, html.TAG_ID.ANNOTATION_XML); |
| 96 | + expect(parser.tokenizer.inForeignNode).toBe(true); |
| 97 | + // Each element is read on its first check and again on the first check after it was popped. |
| 98 | + expect(reads).toBe(5); |
| 99 | + }, |
| 100 | +); |
| 101 | + |
| 102 | +it('decides a fragment context afresh for each parser', () => { |
| 103 | + const context = defaultTreeAdapter.createElement('annotation-xml', html.NS.MATHML, []); |
| 104 | + expect(Parser.getFragmentParser(context).tokenizer.inForeignNode).toBe(true); |
| 105 | + defaultTreeAdapter.adoptAttributes(context, [{ name: 'encoding', value: 'text/html' }]); |
| 106 | + expect(Parser.getFragmentParser(context).tokenizer.inForeignNode).toBe(false); |
| 107 | +}); |
0 commit comments