cocos 之 xml注释

116 阅读1分钟
<!-- comment -->

xml的注释语法:以<!--开头,-->结尾

  • 正确的示例
<!-- 注释文本不显示 -->显示的文本
  • 错误的写法:
<!-- 注释

Cocos2dx

Cocos2dx使用的是rapidxml解析xml

如果缺失了结尾,rapidxml在底层会继续在后续文本中寻找结尾,因为找不到结尾,所以认为开头之后的所有内容都是注释,相关的代码

xml是严格禁止使用<&,因为和语法冲突,要显示对应的字符,需要转换为实体

符号实体(以&开头,;结尾)
<&lt;
>&gt;
&&amp;
'apos;
"&quot;

Cocos Creator

Creator中是可以正常显示<!--,相关代码为:

native层使用的是tinyxml2,但是和RichText无关

  • cocos2d/Components/CCRichText.js
_updateRichText () {
    if (!this.enabledInHierarchy) return;
    let newTextArray = _htmlTextParser.parse(this.string);
}
  • cocos2d/core/util/html-text-parse.js
 parse: function(htmlString) {
        this._resultObjectArray = [];
        if (!htmlString) {
            return this._resultObjectArray;
        }
        this._stack = [];

        var startIndex = 0;
        var length = htmlString.length;
        while (startIndex < length) {
            // 这里没找到闭合标签,直接认为整个就是一个文本
            var tagEndIndex = htmlString.indexOf('>', startIndex);
            var tagBeginIndex = -1;
            if (tagEndIndex >= 0) {
                tagBeginIndex = htmlString.lastIndexOf('<', tagEndIndex);
                var noTagBegin = tagBeginIndex < (startIndex - 1);

                if (noTagBegin) {
                    tagBeginIndex = htmlString.indexOf('<', tagEndIndex + 1);
                    tagEndIndex = htmlString.indexOf('>', tagBeginIndex + 1);
                }
            }

            if (tagBeginIndex < 0) {
                this._stack.pop();
                this._processResult(htmlString.substring(startIndex));
                startIndex = length;
            } else {
                var newStr = htmlString.substring(startIndex, tagBeginIndex);
                var tagStr = htmlString.substring(tagBeginIndex + 1, tagEndIndex);
                if (tagStr === "") newStr = htmlString.substring(startIndex, tagEndIndex + 1);
                this._processResult(newStr);
                if (tagEndIndex === -1) {
                    // cc.error('The HTML tag is invalid!');
                    tagEndIndex = tagBeginIndex;
                } else if (htmlString.charAt(tagBeginIndex + 1) === '\/'){
                    this._stack.pop();
                } else {
                    this._addToStack(tagStr);
                }
                startIndex = tagEndIndex + 1;
            }
        }


        return this._resultObjectArray;
    },