diff --git a/cocos/2d/utils/html-text-parser.ts b/cocos/2d/utils/html-text-parser.ts
index 1fd54932fc..50658368ee 100644
--- a/cocos/2d/utils/html-text-parser.ts
+++ b/cocos/2d/utils/html-text-parser.ts
@@ -172,6 +172,7 @@ export class HtmlTextParser {
header = /^(img(\s)*src(\s)*=[^>]+\/)/.exec(attribute);
let remainingArgument = '';
+ let rightQuot = -1;
if (header && header[0].length > 0) {
tagName = header[0].trim();
if (tagName.startsWith('img') && tagName[tagName.length - 1] === '/') {
@@ -184,7 +185,10 @@ export class HtmlTextParser {
tagName = attribute.substr(0, header[0].length);
// remove space and = character
remainingArgument = attribute.substring(tagName.length).trim();
- nextSpace = remainingArgument.indexOf(' ');
+
+ rightQuot = this.getRightQuotationIndex(remainingArgument);
+
+ nextSpace = remainingArgument.indexOf(' ', rightQuot + 1 >= remainingArgument.length ? -1 : rightQuot + 1);
tagValue = (nextSpace > -1) ? remainingArgument.substr(0, nextSpace) : remainingArgument;
tagName = tagName.replace(/[^a-zA-Z]/g, '').trim();
@@ -308,6 +312,21 @@ export class HtmlTextParser {
return obj;
}
+ private getRightQuotationIndex (remainingArgument: string) {
+ let leftQuot = -1;
+ let rightQuot = -1;
+ // Skip a pair of quotations for avoiding spaces in image name are detected.
+ leftQuot = remainingArgument.indexOf('\'');
+ if (leftQuot > -1) {
+ rightQuot = remainingArgument.indexOf('\'', leftQuot + 1 >= remainingArgument.length ? -1 : leftQuot + 1);
+ } else {
+ leftQuot = remainingArgument.indexOf('"');
+ rightQuot = remainingArgument.indexOf('"', leftQuot + 1 >= remainingArgument.length ? -1 : leftQuot + 1);
+ }
+
+ return rightQuot;
+ }
+
private _processEventHandler (eventString: string) {
const obj = {};
let index = 0;
diff --git a/tests/ui/richtext.test.ts b/tests/ui/richtext.test.ts
index 31f447ec01..ca74e13513 100644
--- a/tests/ui/richtext.test.ts
+++ b/tests/ui/richtext.test.ts
@@ -1,6 +1,62 @@
+import { HtmlTextParser } from "../../cocos/2d";
import { CacheMode, HorizontalTextAlignment, RichText, Sprite } from "../../cocos/2d/components";
import { Node } from "../../cocos/core/scene-graph/node";
+test('parse-richtext', () => {
+ const htmlTextParser = new HtmlTextParser();
+ let node =new Node();
+ node.addComponent(RichText);
+ let richtext = node.getComponent(RichText) as RichText;
+
+ let attribute = "
这是一段文本";
+ richtext.string = attribute;
+ let htmlAttrArray = htmlTextParser.parse(attribute);
+ expect(htmlAttrArray.length).toStrictEqual(2);
+ expect(htmlAttrArray[0].style.src).toStrictEqual('1 23');
+ expect(htmlAttrArray[0].style.imageWidth).toStrictEqual(80);
+ expect(htmlAttrArray[0].style.imageHeight).toStrictEqual(89);
+ expect(htmlAttrArray[0].style.imageAlign).toStrictEqual('top');
+ expect(htmlAttrArray[1].text).toStrictEqual('这是一段文本');
+
+ attribute = "
这是一段文本";
+ htmlAttrArray = htmlTextParser.parse(attribute);
+ expect(htmlAttrArray.length).toStrictEqual(2);
+ expect(htmlAttrArray[0].style.src).toStrictEqual('123');
+ expect(htmlAttrArray[0].style.imageWidth).toStrictEqual(80);
+ expect(htmlAttrArray[0].style.imageHeight).toStrictEqual(89);
+ expect(htmlAttrArray[0].style.imageAlign).toStrictEqual('top');
+ expect(htmlAttrArray[1].text).toStrictEqual('这是一段文本');
+
+ attribute = "
前面有三个空格,这是一段文本";
+ htmlAttrArray = htmlTextParser.parse(attribute);
+ expect(htmlAttrArray.length).toStrictEqual(2);
+ expect(htmlAttrArray[0].style.src).toStrictEqual('12 3');
+ expect(htmlAttrArray[0].style.imageWidth).toStrictEqual(80);
+ expect(htmlAttrArray[0].style.imageHeight).toStrictEqual(89);
+ expect(htmlAttrArray[0].style.imageAlign).toStrictEqual('top');
+ expect(htmlAttrArray[1].text).toStrictEqual(' 前面有三个空格,这是一段文本');
+
+ attribute = "
";
+ htmlAttrArray = htmlTextParser.parse(attribute);
+ expect(htmlAttrArray.length).toStrictEqual(1);
+ expect(htmlAttrArray[0].style.src).toStrictEqual('1 23');
+
+ // following tests are exceptional writing
+
+ attribute = "
"; // 'src' missing
+ htmlAttrArray = htmlTextParser.parse(attribute);
+ expect(htmlAttrArray.length).toStrictEqual(0);
+
+ attribute = "
"; // '/' missing
+ htmlAttrArray = htmlTextParser.parse(attribute);
+ expect(htmlAttrArray.length).toStrictEqual(0);
+
+ attribute = "
{
let node = new Node();
node.addComponent(RichText);