(data: string, rootElement: Element)
| 103 | * @return {Element} root element |
| 104 | */ |
| 105 | export function parse(data: string, rootElement: Element) { |
| 106 | const ownerDocument = rootElement.ownerDocument; |
| 107 | const root = ownerDocument.createElementNS(rootElement.namespaceURI, rootElement.localName); |
| 108 | |
| 109 | let currentParent = root as Node; |
| 110 | let currentNamespace = root.namespaceURI; |
| 111 | const stack = [root as Node]; |
| 112 | let lastTextPos = 0; |
| 113 | let match: RegExpExecArray | null; |
| 114 | data = '<q>' + data + '</q>'; |
| 115 | const tagsClosed = [] as string[]; |
| 116 | |
| 117 | if (currentNamespace !== SVG_NAMESPACE && currentNamespace !== HTML_NAMESPACE) { |
| 118 | throw new Error('Namespace not supported: ' + currentNamespace); |
| 119 | } |
| 120 | |
| 121 | while ((match = kMarkupPattern.exec(data))) { |
| 122 | const commentContents = match[1]; // <!--contents--> |
| 123 | const beginningSlash = match[2]; // ... </ ... |
| 124 | const tagName = match[3]; |
| 125 | const matchAttributes = match[4]; |
| 126 | const endSlash = match[5]; // ... /> ... |
| 127 | |
| 128 | if (lastTextPos < match.index) { |
| 129 | // if has content |
| 130 | const text = data.slice(lastTextPos, match.index); |
| 131 | currentParent.appendChild(ownerDocument.createTextNode(decodeEntities(text))); |
| 132 | } |
| 133 | lastTextPos = kMarkupPattern.lastIndex; |
| 134 | if (commentContents !== undefined) { |
| 135 | // this is a comment |
| 136 | currentParent.appendChild(ownerDocument.createComment(commentContents)); |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | const normalizedTagName = toUpper(tagName); |
| 141 | |
| 142 | if (normalizedTagName === 'SVG') { |
| 143 | currentNamespace = beginningSlash ? HTML_NAMESPACE : SVG_NAMESPACE; |
| 144 | } |
| 145 | |
| 146 | if (!beginningSlash) { |
| 147 | // not </ tags |
| 148 | if (!endSlash && kElementsClosedByOpening[currentParent.tagName]) { |
| 149 | if (kElementsClosedByOpening[currentParent.tagName][normalizedTagName]) { |
| 150 | stack.pop(); |
| 151 | currentParent = arr_back(stack); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | const childToAppend = ownerDocument.createElementNS(currentNamespace, currentNamespace === HTML_NAMESPACE ? toLower(tagName) : tagName); |
| 156 | |
| 157 | for (let attMatch; (attMatch = kAttributePattern.exec(matchAttributes)); ) { |
| 158 | const attrName = attMatch[2]; |
| 159 | const attrValue = attMatch[4] || attMatch[5] || attMatch[6]; |
| 160 | childToAppend.setAttribute(attrName, attrValue); |
| 161 | } |
| 162 |
no test coverage detected
searching dependent graphs…