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;