Skip a pair of quotations for avoiding spaces in image name are detected (#10954)

* Skip a pair of quotations for avoiding spaces in image name are detected.

(cherry picked from commit b0d0d5a6ad148238b618ab37e692b09e1523d3b1)

* add unit test

* optimize unit test case

* test functional interface "Parse" instead of  a part of code

* supplements some unit test cases

* optimize unit test

Co-authored-by: zhangmingzhen <mingzhen.zhang@cocos.com>
This commit is contained in:
Zhang mingzhen
2022-05-14 18:37:21 +08:00
committed by GitHub
parent a2afcd2cdb
commit 33c00ef16a
2 changed files with 76 additions and 1 deletions
+20 -1
View File
@@ -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;
+56
View File
@@ -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 = "<img src='1 23' width=80 height=89 align=top />这是一段文本";
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 = "<img src='123' width=80 height=89 align=top />这是一段文本";
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 = "<img src=\"12 3\" width = 80 height= 89 align =top /> 前面有三个空格,这是一段文本";
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 = "<img src='1 23'/>";
htmlAttrArray = htmlTextParser.parse(attribute);
expect(htmlAttrArray.length).toStrictEqual(1);
expect(htmlAttrArray[0].style.src).toStrictEqual('1 23');
// following tests are exceptional writing
attribute = "<img ='1 23'/>"; // 'src' missing
htmlAttrArray = htmlTextParser.parse(attribute);
expect(htmlAttrArray.length).toStrictEqual(0);
attribute = "<img src='123'>"; // '/' missing
htmlAttrArray = htmlTextParser.parse(attribute);
expect(htmlAttrArray.length).toStrictEqual(0);
attribute = "<img src='1 23\" />"; // quotations don't match
htmlAttrArray = htmlTextParser.parse(attribute);
expect(htmlAttrArray.length).toStrictEqual(1);
expect(htmlAttrArray[0].style.src).toStrictEqual('');
});
test('label.string.setter', () => {
let node = new Node();
node.addComponent(RichText);