forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathXMLUtils.js
More file actions
1 lines (1 loc) · 4.85 KB
/
XMLUtils.js
File metadata and controls
1 lines (1 loc) · 4.85 KB
1
define(function(require,exports,module){var TokenUtils=require("utils/TokenUtils");const TOKEN_TAG=1,TOKEN_ATTR=2,TOKEN_VALUE=3;var regexWhitespace=/^\s+$/;function _createTagInfo(token,tokenType,offset,exclusionList,tagName,attrName,shouldReplace){return{token:token||null,tokenType:tokenType||null,offset:offset||0,exclusionList:exclusionList||[],tagName:tagName||"",attrName:attrName||"",shouldReplace:shouldReplace||!1}}function _getTagAttributes(editor,constPos){var pos,ctx,ctxPrev,ctxNext,ctxTemp,tagName,exclusionList=[],shouldReplace;if(pos=$.extend({},constPos),ctx=TokenUtils.getInitialContext(editor._codeMirror,pos),null===(ctxTemp=$.extend(!0,{},ctx)).token.type&®exWhitespace.test(ctxTemp.token.string)&&TokenUtils.moveSkippingWhitespace(TokenUtils.moveNextToken,ctxTemp)){if(null===ctxTemp.token.type&&"="===ctxTemp.token.string||"string"===ctxTemp.token.type)return null;TokenUtils.moveSkippingWhitespace(TokenUtils.movePrevToken,ctxTemp)}for("attribute"===ctxTemp.token.type&&TokenUtils.moveSkippingWhitespace(TokenUtils.moveNextToken,ctxTemp)&&null===ctxTemp.token.type&&"="===ctxTemp.token.string&&(shouldReplace=!0),pos=$.extend({},constPos),ctxPrev=TokenUtils.getInitialContext(editor._codeMirror,pos);TokenUtils.movePrevToken(ctxPrev);){if(ctxPrev.token.type&&ctxPrev.token.type.indexOf("tag bracket")>=0&&("</"===ctxPrev.token.string||-1!==ctxPrev.token.string.indexOf(">")))return null;if("attribute"===ctxPrev.token.type&&exclusionList.push(ctxPrev.token.string),"tag"===ctxPrev.token.type&&(tagName=ctxPrev.token.string,TokenUtils.movePrevToken(ctxPrev))){if("tag bracket"===ctxPrev.token.type&&"<"===ctxPrev.token.string)break;return null}}for(pos=$.extend({},constPos),ctxNext=TokenUtils.getInitialContext(editor._codeMirror,pos);TokenUtils.moveNextToken(ctxNext);){if("string"===ctxNext.token.type&&'"'===ctxNext.token.string)return null;if("tag bracket"===ctxNext.token.type&&(ctxNext.token.string.indexOf(">")>=0||"<"===ctxNext.token.string))break;"attribute"===ctxNext.token.type&&-1===exclusionList.indexOf(ctxNext.token.string)&&exclusionList.push(ctxNext.token.string)}return{tagName:tagName,exclusionList:exclusionList,shouldReplace:shouldReplace}}function _getTagAttributeValue(editor,pos){var ctx,tagName,attrName,exclusionList=[],offset,textBefore,textAfter;for(ctx=TokenUtils.getInitialContext(editor._codeMirror,pos),offset=TokenUtils.offsetInToken(ctx),"string"===ctx.token.type&&/\s+/.test(ctx.token.string)&&(textBefore=ctx.token.string.substr(1,offset),textAfter=ctx.token.string.substr(offset),/^['"]$/.test(ctx.token.string.substr(-1,1))&&(textAfter=textAfter.substr(0,textAfter.length-1)),exclusionList=(exclusionList=(exclusionList=exclusionList.concat(textBefore.split(/\s+/).slice(0,-1))).concat(textAfter.split(/\s+/))).filter(function(value){if(value.length>0)return!0}));TokenUtils.movePrevToken(ctx);){if("tag bracket"===ctx.token.type){if("</"===ctx.token.string)return null;if(ctx.token.string.indexOf(">")>=0||"<"===ctx.token.string)break}if("attribute"!==ctx.token.type||attrName||(attrName=ctx.token.string),"tag"===ctx.token.type&&(tagName=ctx.token.string,TokenUtils.movePrevToken(ctx))){if("tag bracket"===ctx.token.type&&"<"===ctx.token.string)break;return null}}return{tagName:tagName,attrName:attrName,exclusionList:exclusionList}}function getTagInfo(editor,pos){var ctx,offset,tagAttrs,tagAttrValue;if(ctx=TokenUtils.getInitialContext(editor._codeMirror,pos),offset=TokenUtils.offsetInToken(ctx),ctx.token&&"tag bracket"===ctx.token.type&&"<"===ctx.token.string)return _createTagInfo(ctx.token,TOKEN_TAG);if(ctx.token&&"tag"===ctx.token.type){if(TokenUtils.movePrevToken(ctx)&&"tag bracket"===ctx.token.type&&"<"===ctx.token.string)return TokenUtils.moveNextToken(ctx),_createTagInfo(ctx.token,TOKEN_TAG,offset)}else if(ctx.token&&("attribute"===ctx.token.type||null===ctx.token.type&®exWhitespace.test(ctx.token.string))){if((tagAttrs=_getTagAttributes(editor,pos))&&tagAttrs.tagName)return _createTagInfo(ctx.token,TOKEN_ATTR,offset,tagAttrs.exclusionList,tagAttrs.tagName,null,tagAttrs.shouldReplace)}else if(ctx.token&&(null===ctx.token.type&&"="===ctx.token.string||"string"===ctx.token.type&&/^['"]$/.test(ctx.token.string.charAt(0)))){if("string"===ctx.token.type&&/^['"]$/.test(ctx.token.string.substr(-1,1))&&1!==ctx.token.string.length&&ctx.token.end===pos.ch)return _createTagInfo();if((tagAttrValue=_getTagAttributeValue(editor,pos))&&tagAttrValue.tagName&&tagAttrValue.attrName)return _createTagInfo(ctx.token,TOKEN_VALUE,offset,tagAttrValue.exclusionList,tagAttrValue.tagName,tagAttrValue.attrName)}return _createTagInfo()}function getValueQuery(tagInfo){var query;return"="===tagInfo.token.string?"":(query=tagInfo.token.string.substr(1,tagInfo.offset-1)).split(/\s+/).slice(-1)[0]}exports.getTagInfo=getTagInfo,exports.getValueQuery=getValueQuery,exports.regexWhitespace=regexWhitespace,exports.TOKEN_TAG=TOKEN_TAG,exports.TOKEN_ATTR=TOKEN_ATTR,exports.TOKEN_VALUE=TOKEN_VALUE});