( this: IcuCreateOpCodes | void, opcodes?: IcuCreateOpCodes, )
| 134 | * @param opcodes `I18nCreateOpCodes` if invoked as a function. |
| 135 | */ |
| 136 | export function icuCreateOpCodesToString( |
| 137 | this: IcuCreateOpCodes | void, |
| 138 | opcodes?: IcuCreateOpCodes, |
| 139 | ): string[] { |
| 140 | const parser = new OpCodeParser(opcodes || (Array.isArray(this) ? this : [])); |
| 141 | let lines: string[] = []; |
| 142 | |
| 143 | function consumeOpCode(opCode: number): string { |
| 144 | const parent = getParentFromIcuCreateOpCode(opCode); |
| 145 | const ref = getRefFromIcuCreateOpCode(opCode); |
| 146 | switch (getInstructionFromIcuCreateOpCode(opCode)) { |
| 147 | case IcuCreateOpCode.AppendChild: |
| 148 | return `(lView[${parent}] as Element).appendChild(lView[${lastRef}])`; |
| 149 | case IcuCreateOpCode.Attr: |
| 150 | return `(lView[${ref}] as Element).setAttribute("${parser.consumeString()}", "${parser.consumeString()}")`; |
| 151 | } |
| 152 | throw new Error('Unexpected OpCode: ' + getInstructionFromIcuCreateOpCode(opCode)); |
| 153 | } |
| 154 | |
| 155 | let lastRef = -1; |
| 156 | while (parser.hasMore()) { |
| 157 | let value = parser.consumeNumberStringOrMarker(); |
| 158 | if (value === ICU_MARKER) { |
| 159 | const text = parser.consumeString(); |
| 160 | lastRef = parser.consumeNumber(); |
| 161 | lines.push(`lView[${lastRef}] = document.createComment("${text}")`); |
| 162 | } else if (value === ELEMENT_MARKER) { |
| 163 | const text = parser.consumeString(); |
| 164 | lastRef = parser.consumeNumber(); |
| 165 | lines.push(`lView[${lastRef}] = document.createElement("${text}")`); |
| 166 | } else if (typeof value === 'string') { |
| 167 | lastRef = parser.consumeNumber(); |
| 168 | lines.push(`lView[${lastRef}] = document.createTextNode("${value}")`); |
| 169 | } else if (typeof value === 'number') { |
| 170 | const line = consumeOpCode(value); |
| 171 | line && lines.push(line); |
| 172 | } else { |
| 173 | throw new Error('Unexpected value'); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return lines; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Converts `I18nRemoveOpCodes` array into a human readable format. |
no test coverage detected
searching dependent graphs…