diff --git a/src/parse/state/tag.ts b/src/parse/state/tag.ts index f7f5a93574..d6e9706714 100644 --- a/src/parse/state/tag.ts +++ b/src/parse/state/tag.ts @@ -490,8 +490,13 @@ function read_sequence(parser: Parser, done: () => boolean): Node[] { if (current_chunk.data) chunks.push(current_chunk); chunks.forEach(chunk => { - if (chunk.type === 'Text') - chunk.data = decode_character_references(chunk.data); + if (chunk.type === 'Text') { + let decoded = decode_character_references(chunk.data); + if (chunk.data != decoded) { + chunk.raw = chunk.data; + chunk.data = decoded; + } + } }); return chunks; diff --git a/src/parse/state/text.ts b/src/parse/state/text.ts index f739c8cc9e..503d79aa08 100644 --- a/src/parse/state/text.ts +++ b/src/parse/state/text.ts @@ -14,10 +14,15 @@ export default function text(parser: Parser) { data += parser.template[parser.index++]; } - parser.current().children.push({ + let node = { start, end: parser.index, type: 'Text', data: decode_character_references(data), - }); + }; + + if (node.data != data) + node.raw = data; + + parser.current().children.push(node); } diff --git a/test/parser/samples/attribute-escaped/output.json b/test/parser/samples/attribute-escaped/output.json index 6a4143e674..79b135eb86 100644 --- a/test/parser/samples/attribute-escaped/output.json +++ b/test/parser/samples/attribute-escaped/output.json @@ -20,7 +20,8 @@ "start": 15, "end": 33, "type": "Text", - "data": "\"quoted\"" + "data": "\"quoted\"", + "raw": ""quoted"" } ] } diff --git a/test/parser/samples/convert-entities-in-element/output.json b/test/parser/samples/convert-entities-in-element/output.json index fb0f5b288e..92b5aafb00 100644 --- a/test/parser/samples/convert-entities-in-element/output.json +++ b/test/parser/samples/convert-entities-in-element/output.json @@ -15,7 +15,8 @@ "start": 3, "end": 20, "type": "Text", - "data": "Hello & World" + "data": "Hello & World", + "raw": "Hello & World" } ] } diff --git a/test/parser/samples/convert-entities/output.json b/test/parser/samples/convert-entities/output.json index ca1c1356f8..967895383d 100644 --- a/test/parser/samples/convert-entities/output.json +++ b/test/parser/samples/convert-entities/output.json @@ -8,7 +8,8 @@ "start": 0, "end": 17, "type": "Text", - "data": "Hello & World" + "data": "Hello & World", + "raw": "Hello & World" } ] }, diff --git a/test/parser/samples/nbsp/output.json b/test/parser/samples/nbsp/output.json index 4fa318ce48..c47257c873 100644 --- a/test/parser/samples/nbsp/output.json +++ b/test/parser/samples/nbsp/output.json @@ -15,7 +15,8 @@ "start": 6, "end": 12, "type": "Text", - "data": " " + "data": " ", + "raw": " " } ] }