function URIAddEncodedOctetToBuffer(octet, result, index) { result[index++] = 37; // Char code of '%'. result[index++] = hexCharCodeArray[octet >> 4]; result[index++] = hexCharCodeArray[octet & 0x0F]; return index; }function URIEncodeOctets(octets, result, index) { if (hexCharCodeArray === 0) { // 0-F hexCharCodeArray = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70]; } index = URIAddEncodedOctetToBuffer(octets[0], result, index); if (octets[1]) index = URIAddEncodedOctetToBuffer(octets[1], result, index); if (octets[2]) index = URIAddEncodedOctetToBuffer(octets[2], result, index); if (octets[3]) index = URIAddEncodedOctetToBuffer(octets[3], result, index); return index; }// 对unicode中除了代理对外的字符编码function URIEncodeSingle(cc, result, index) { var x = (cc >> 12) & 0xF; var y = (cc >> 6) & 63; var z = cc & 63; // unicode最多值是三个字节 var octets = new InternalArray(3); // ascii码 if (cc <= 0x007F) { octets[0] = cc; } else if (cc <= 0x07FF) { octets[0] = y + 192; octets[1] = z + 128; } else { octets[0] = x + 224; octets[1] = y + 128; octets[2] = z + 128; } return URIEncodeOctets(octets, result, index); }function URIEncodePair(cc1 , cc2, result, index) { var u = ((cc1 >> 6) & 0xF) + 1; var w = (cc1 >> 2) & 0xF; var x = cc1 & 3; var y = (cc2 >> 6) & 0xF; var z = cc2 & 63; var octets = new InternalArray(4); octets[0] = (u >> 2) + 240; octets[1] = (((u & 3) << 4) | w) + 128; octets[2] = ((x << 4) | y) + 128; octets[3] = z + 128; return URIEncodeOctets(octets, result, index); }// ECMA-262, section 15.1.3function Encode(uri, unescape) { uri = TO_STRING(uri); var uriLength = uri.length; var array = new InternalArray(uriLength); var index = 0; for (var k = 0; k < uriLength; k++) { var cc1 = %_StringCharCodeAt(uri, k); if (unescape(cc1)) { array[index++] = cc1; } else { if (cc1 >= 0xDC00 && cc1 <= 0xDFFF) throw MakeURIError(); // 非高代理项 if (cc1 < 0xD800 || cc1 > 0xDBFF) { index = URIEncodeSingle(cc1, array, index); } else { // 高代理项 k++; if (k == uriLength) throw MakeURIError(); var cc2 = %_StringCharCodeAt(uri, k); // 不是合法的低代理项 if (cc2 < 0xDC00 || cc2 > 0xDFFF) throw MakeURIError(); index = URIEncodePair(cc1, cc2, array, index); } } } var result = %NewString(array.length, NEW_ONE_BYTE_STRING); for (var i = 0; i < array.length; i++) { %_OneByteSeqStringSetChar(i, array[i], result); } return result; }// ECMA-262 - 15.1.3.4function URIEncodeComponent(component) { var unescapePredicate = function(cc) { if (isAlphaNumeric(cc)) return true; // ! if (cc == 33) return true; // '()* if (39 <= cc && cc <= 42) return true; // -. if (45 <= cc && cc <= 46) return true; // _ if (cc == 95) return true; // ~ if (cc == 126) return true; return false; }; return Encode(component, unescapePredicate); }
URIEncodeComponent實作過程為,直接呼叫Encode函數進行編碼,首先利用unescapePredicate 函數跳過一些特殊的字符,這些字符不需要進行編碼,具體看代碼。基本工程為,逐個字元遍歷,如果目前字元為低代理項,則報錯(cc1 >= 0xDC00 && cc1 <= 0xDFFF);因為低代理項必須跟在高代理項後面。接著判斷如果當前位元組不是高代理項(cc1 < 0xD800 || cc1 > 0xDBFF),則直接呼叫URIEncodeSingle函數進行編碼,如果當前字元編碼為高代理項,則取下一位,如果下一位超過了字串長度,則報錯,如果下一位不是低代理項則報錯,因為高代理項後面必須跟著低代理項,如果下一位是低代理項,則調用URIEncodePair函數對代理對進行編碼。 Encode函數主要是判斷,字符的是不是不需要編碼的字符,是不是代理對,是不是一般的字符三種情形。
URIEncodeSingle函數基本流程是對一個字元進行某種運算,最後呼叫URIEncodeOctets函數依位元組編碼。 unicode碼最大是三個字節,所以URIEncodeSingle函數裡定義了數組長度為3。 URIEncodePair函數基本流程為,對高低代理項進行某種運算,高低代理項共是四個位元組,然後呼叫URIEncodeOctets函數對四個位元組進行編碼。
以上是如何利用js編碼函數encodeURIComponent進行原始碼解析的詳細內容。更多資訊請關注PHP中文網其他相關文章!