123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- var charCodeDef = require('./char-code-definitions');
- var isDigit = charCodeDef.isDigit;
- var isHexDigit = charCodeDef.isHexDigit;
- var isUppercaseLetter = charCodeDef.isUppercaseLetter;
- var isName = charCodeDef.isName;
- var isWhiteSpace = charCodeDef.isWhiteSpace;
- var isValidEscape = charCodeDef.isValidEscape;
- function getCharCode(source, offset) {
- return offset < source.length ? source.charCodeAt(offset) : 0;
- }
- function getNewlineLength(source, offset, code) {
- if (code === 13 && getCharCode(source, offset + 1) === 10 ) {
- return 2;
- }
- return 1;
- }
- function cmpChar(testStr, offset, referenceCode) {
- var code = testStr.charCodeAt(offset);
-
- if (isUppercaseLetter(code)) {
- code = code | 32;
- }
- return code === referenceCode;
- }
- function cmpStr(testStr, start, end, referenceStr) {
- if (end - start !== referenceStr.length) {
- return false;
- }
- if (start < 0 || end > testStr.length) {
- return false;
- }
- for (var i = start; i < end; i++) {
- var testCode = testStr.charCodeAt(i);
- var referenceCode = referenceStr.charCodeAt(i - start);
-
- if (isUppercaseLetter(testCode)) {
- testCode = testCode | 32;
- }
- if (testCode !== referenceCode) {
- return false;
- }
- }
- return true;
- }
- function findWhiteSpaceStart(source, offset) {
- for (; offset >= 0; offset--) {
- if (!isWhiteSpace(source.charCodeAt(offset))) {
- break;
- }
- }
- return offset + 1;
- }
- function findWhiteSpaceEnd(source, offset) {
- for (; offset < source.length; offset++) {
- if (!isWhiteSpace(source.charCodeAt(offset))) {
- break;
- }
- }
- return offset;
- }
- function findDecimalNumberEnd(source, offset) {
- for (; offset < source.length; offset++) {
- if (!isDigit(source.charCodeAt(offset))) {
- break;
- }
- }
- return offset;
- }
- function consumeEscaped(source, offset) {
-
-
- offset += 2;
-
- if (isHexDigit(getCharCode(source, offset - 1))) {
-
-
- for (var maxOffset = Math.min(source.length, offset + 5); offset < maxOffset; offset++) {
- if (!isHexDigit(getCharCode(source, offset))) {
- break;
- }
- }
-
- var code = getCharCode(source, offset);
- if (isWhiteSpace(code)) {
- offset += getNewlineLength(source, offset, code);
- }
- }
- return offset;
- }
- function consumeName(source, offset) {
-
-
- for (; offset < source.length; offset++) {
- var code = source.charCodeAt(offset);
-
- if (isName(code)) {
-
- continue;
- }
-
- if (isValidEscape(code, getCharCode(source, offset + 1))) {
-
- offset = consumeEscaped(source, offset) - 1;
- continue;
- }
-
-
- break;
- }
- return offset;
- }
- function consumeNumber(source, offset) {
- var code = source.charCodeAt(offset);
-
-
- if (code === 0x002B || code === 0x002D) {
- code = source.charCodeAt(offset += 1);
- }
-
- if (isDigit(code)) {
- offset = findDecimalNumberEnd(source, offset + 1);
- code = source.charCodeAt(offset);
- }
-
- if (code === 0x002E && isDigit(source.charCodeAt(offset + 1))) {
-
-
- code = source.charCodeAt(offset += 2);
-
-
-
- offset = findDecimalNumberEnd(source, offset);
- }
-
-
- if (cmpChar(source, offset, 101 )) {
- var sign = 0;
- code = source.charCodeAt(offset + 1);
-
- if (code === 0x002D || code === 0x002B) {
- sign = 1;
- code = source.charCodeAt(offset + 2);
- }
-
- if (isDigit(code)) {
-
-
-
-
-
- offset = findDecimalNumberEnd(source, offset + 1 + sign + 1);
- }
- }
- return offset;
- }
- function consumeBadUrlRemnants(source, offset) {
-
- for (; offset < source.length; offset++) {
- var code = source.charCodeAt(offset);
-
-
- if (code === 0x0029) {
-
- offset++;
- break;
- }
- if (isValidEscape(code, getCharCode(source, offset + 1))) {
-
-
-
-
- offset = consumeEscaped(source, offset);
- }
- }
- return offset;
- }
- module.exports = {
- consumeEscaped: consumeEscaped,
- consumeName: consumeName,
- consumeNumber: consumeNumber,
- consumeBadUrlRemnants: consumeBadUrlRemnants,
- cmpChar: cmpChar,
- cmpStr: cmpStr,
- getNewlineLength: getNewlineLength,
- findWhiteSpaceStart: findWhiteSpaceStart,
- findWhiteSpaceEnd: findWhiteSpaceEnd
- };
|