How to use PercentageToken method in Playwright Internal

Best JavaScript code snippet using playwright-internal

vimtokenizer.js

Source:vimtokenizer.js Github

copy

Full Screen

...451 consume() && currtoken.append([0x2e, code]) && switchto('number-fraction');452 else453 emit() && switchto('data') && reconsume();454 } else if (code == 0x25)455 emit(new PercentageToken(currtoken)) && switchto('data');456 else if (code == 0x45 || code == 0x65) {457 if (digit(next()))458 consume() && currtoken.append([0x25, code]) && switchto('sci-notation');459 else if ((next(1) == 0x2b || next(1) == 0x2d) && digit(next(2)))460 currtoken.append([0x25, next(1), next(2)]) && consume(2) && switchto('sci-notation');461 else462 create(new DimensionToken(currtoken, code)) && switchto('dimension');463 } else if (code == 0x2d) {464 if (namestartchar(next()))465 consume() && create(new DimensionToken(currtoken, [0x2d, code])) && switchto('dimension');466 else if (next(1) == 0x5c && badescape(next(2)))467 parseerror() && emit() && switchto('data') && reconsume();468 else if (next(1) == 0x5c)469 consume() && create(new DimensionToken(currtoken, [0x2d, consumeEscape()])) && switchto('dimension');470 else471 emit() && switchto('data') && reconsume();472 } else if (namestartchar(code))473 create(new DimensionToken(currtoken, code)) && switchto('dimension');474 else if (code == 0x5c) {475 if (badescape(next))476 parseerror() && emit() && switchto('data') && reconsume();477 else478 create(new DimensionToken(currtoken, consumeEscape)) && switchto('dimension');479 } else480 emit() && switchto('data') && reconsume();481 break;482 case "number-fraction":483 currtoken.type = "number";484 if (digit(code))485 currtoken.append(code);486 else if (code == 0x25)487 emit(new PercentageToken(currtoken)) && switchto('data');488 else if (code == 0x45 || code == 0x65) {489 if (digit(next()))490 consume() && currtoken.append([0x65, code]) && switchto('sci-notation');491 else if ((next(1) == 0x2b || next(1) == 0x2d) && digit(next(2)))492 currtoken.append([0x65, next(1), next(2)]) && consume(2) && switchto('sci-notation');493 else494 create(new DimensionToken(currtoken, code)) && switchto('dimension');495 } else if (code == 0x2d) {496 if (namestartchar(next()))497 consume() && create(new DimensionToken(currtoken, [0x2d, code])) && switchto('dimension');498 else if (next(1) == 0x5c && badescape(next(2)))499 parseerror() && emit() && switchto('data') && reconsume();500 else if (next(1) == 0x5c)501 consume() && create(new DimensionToken(currtoken, [0x2d, consumeEscape()])) && switchto('dimension');502 else503 emit() && switchto('data') && reconsume();504 } else if (namestartchar(code))505 create(new DimensionToken(currtoken, code)) && switchto('dimension');506 else if (code == 0x5c) {507 if (badescape(next))508 parseerror() && emit() && switchto('data') && reconsume();509 else510 create(new DimensionToken(currtoken, consumeEscape())) && switchto('dimension');511 } else512 emit() && switchto('data') && reconsume();513 break;514 case "dimension":515 if (namechar(code))516 currtoken.append(code);517 else if (code == 0x5c) {518 if (badescape(next()))519 parseerror() && emit() && switchto('data') && reconsume();520 else521 currtoken.append(consumeEscape());522 } else523 emit() && switchto('data') && reconsume();524 break;525 case "sci-notation":526 currtoken.type = "number";527 if (digit(code))528 currtoken.append(code);529 else530 emit() && switchto('data') && reconsume();531 break;532 case "url":533 if (eof())534 parseerror() && emit(new BadURLToken) && switchto('data');535 else if (code == 0x22)536 switchto('url-double-quote');537 else if (code == 0x27)538 switchto('url-single-quote');539 else if (code == 0x29)540 emit(new URLToken) && switchto('data');541 else if (whitespace(code))542 donothing();543 else544 switchto('url-unquoted') && reconsume();545 break;546 case "url-double-quote":547 if (!( currtoken instanceof URLToken))548 create(new URLToken);549 if (eof())550 parseerror() && emit(new BadURLToken) && switchto('data');551 else if (code == 0x22)552 switchto('url-end');553 else if (newline(code))554 parseerror() && switchto('bad-url');555 else if (code == 0x5c) {556 if (newline(next()))557 consume();558 else if (badescape(next()))559 parseerror() && emit(new BadURLToken) && switchto('data') && reconsume();560 else561 currtoken.append(consumeEscape());562 } else563 currtoken.append(code);564 break;565 case "url-single-quote":566 if (!( currtoken instanceof URLToken))567 create(new URLToken);568 if (eof())569 parseerror() && emit(new BadURLToken) && switchto('data');570 else if (code == 0x27)571 switchto('url-end');572 else if (newline(code))573 parseerror() && switchto('bad-url');574 else if (code == 0x5c) {575 if (newline(next()))576 consume();577 else if (badescape(next()))578 parseerror() && emit(new BadURLToken) && switchto('data') && reconsume();579 else580 currtoken.append(consumeEscape());581 } else582 currtoken.append(code);583 break;584 case "url-end":585 if (eof())586 parseerror() && emit(new BadURLToken) && switchto('data');587 else if (whitespace(code))588 donothing();589 else if (code == 0x29)590 emit() && switchto('data');591 else592 parseerror() && switchto('bad-url') && reconsume();593 break;594 case "url-unquoted":595 if (!( currtoken instanceof URLToken))596 create(new URLToken);597 if (eof())598 parseerror() && emit(new BadURLToken) && switchto('data');599 else if (whitespace(code))600 switchto('url-end');601 else if (code == 0x29)602 emit() && switchto('data');603 else if (code == 0x22 || code == 0x27 || code == 0x28 || nonprintable(code))604 parseerror() && switchto('bad-url');605 else if (code == 0x5c) {606 if (badescape(next()))607 parseerror() && switchto('bad-url');608 else609 currtoken.append(consumeEscape());610 } else611 currtoken.append(code);612 break;613 case "bad-url":614 if (eof())615 parseerror() && emit(new BadURLToken) && switchto('data');616 else if (code == 0x29)617 emit(new BadURLToken) && switchto('data');618 else if (code == 0x5c) {619 if (badescape(next()))620 donothing();621 else622 consumeEscape();623 } else624 donothing();625 break;626 case "unicode-range":627 // We already know that the current code is a hexdigit.628 var start = [code], end = [code];629 for (var total = 1; total < 6; total++) {630 if (hexdigit(next())) {631 consume();632 start.push(code);633 end.push(code);634 } else635 break;636 }637 if (next() == 0x3f) {638 for (; total < 6; total++) {639 if (next() == 0x3f) {640 consume();641 start.push("0".charCodeAt(0));642 end.push("f".charCodeAt(0));643 } else644 break;645 }646 emit(new UnicodeRangeToken(start, end)) && switchto('data');647 } else if (next(1) == 0x2d && hexdigit(next(2))) {648 consume();649 consume();650 end = [code];651 for (var total = 1; total < 6; total++) {652 if (hexdigit(next())) {653 consume();654 end.push(code);655 } else656 break;657 }658 emit(new UnicodeRangeToken(start, end)) && switchto('data');659 } else660 emit(new UnicodeRangeToken(start)) && switchto('data');661 break;662 default:663 catchfire("Unknown state '" + state + "'");664 }665 }666 }667 function stringFromCodeArray(arr) {668 return String.fromCharCode.apply(null, arr.filter(function(e) {669 return e;670 }));671 }672 function CSSParserToken(options) {673 return this;674 }675 CSSParserToken.prototype.finish = function() {676 return this;677 }678 CSSParserToken.prototype.toString = function() {679 return this.tokenType;680 }681 CSSParserToken.prototype.toSourceString = CSSParserToken.prototype.toString;682 CSSParserToken.prototype.toJSON = function() {683 return this.toString();684 }685 function BadStringToken() {686 return this;687 }688 BadStringToken.prototype = new CSSParserToken;689 BadStringToken.prototype.tokenType = "BADSTRING";690 function BadURLToken() {691 return this;692 }693 BadURLToken.prototype = new CSSParserToken;694 BadURLToken.prototype.tokenType = "BADURL";695 function WhitespaceToken() {696 return this;697 }698 WhitespaceToken.prototype = new CSSParserToken;699 WhitespaceToken.prototype.tokenType = "WHITESPACE";700 WhitespaceToken.prototype.toString = function() {701 return "WS";702 }703 WhitespaceToken.prototype.toSourceString = function() {704 return " ";705 }706 function CDOToken() {707 return this;708 }709 CDOToken.prototype = new CSSParserToken;710 CDOToken.prototype.tokenType = "CDO";711 function CDCToken() {712 return this;713 }714 CDCToken.prototype = new CSSParserToken;715 CDCToken.prototype.tokenType = "CDC";716 function ColonToken() {717 return this;718 }719 ColonToken.prototype = new CSSParserToken;720 ColonToken.prototype.tokenType = ":";721 function SemicolonToken() {722 return this;723 }724 SemicolonToken.prototype = new CSSParserToken;725 SemicolonToken.prototype.tokenType = ";";726 function OpenCurlyToken() {727 return this;728 }729 OpenCurlyToken.prototype = new CSSParserToken;730 OpenCurlyToken.prototype.tokenType = "{";731 function CloseCurlyToken() {732 return this;733 }734 CloseCurlyToken.prototype = new CSSParserToken;735 CloseCurlyToken.prototype.tokenType = "}";736 function OpenSquareToken() {737 return this;738 }739 OpenSquareToken.prototype = new CSSParserToken;740 OpenSquareToken.prototype.tokenType = "[";741 function CloseSquareToken() {742 return this;743 }744 CloseSquareToken.prototype = new CSSParserToken;745 CloseSquareToken.prototype.tokenType = "]";746 function OpenParenToken() {747 return this;748 }749 OpenParenToken.prototype = new CSSParserToken;750 OpenParenToken.prototype.tokenType = "(";751 function CloseParenToken() {752 return this;753 }754 CloseParenToken.prototype = new CSSParserToken;755 CloseParenToken.prototype.tokenType = ")";756 function EOFToken() {757 return this;758 }759 EOFToken.prototype = new CSSParserToken;760 EOFToken.prototype.tokenType = "EOF";761 function DelimToken(code) {762 this.value = String.fromCharCode(code);763 return this;764 }765 DelimToken.prototype = new CSSParserToken;766 DelimToken.prototype.tokenType = "DELIM";767 DelimToken.prototype.toString = function() {768 return "DELIM(" + this.value + ")";769 }770 DelimToken.prototype.toSourceString = function() {771 return this.value;772 }773 function StringValuedToken() {774 return this;775 }776 StringValuedToken.prototype = new CSSParserToken;777 StringValuedToken.prototype.append = function(val) {778 if ( val instanceof Array) {779 for (var i = 0; i < val.length; i++) {780 this.value.push(val[i]);781 }782 } else {783 this.value.push(val);784 }785 return true;786 }787 StringValuedToken.prototype.finish = function() {788 this.value = this.valueAsString();789 return this;790 }791 StringValuedToken.prototype.ASCIImatch = function(str) {792 return this.valueAsString().toLowerCase() == str.toLowerCase();793 }794 StringValuedToken.prototype.valueAsString = function() {795 if ( typeof this.value == 'string')796 return this.value;797 return stringFromCodeArray(this.value);798 }799 StringValuedToken.prototype.valueAsCodes = function() {800 if ( typeof this.value == 'string') {801 var ret = [];802 for (var i = 0; i < this.value.length; i++)803 ret.push(this.value.charCodeAt(i));804 return ret;805 }806 return this.value.filter(function(e) {807 return e;808 });809 }810 function IdentifierToken(val) {811 this.value = [];812 this.append(val);813 }814 IdentifierToken.prototype = new StringValuedToken;815 IdentifierToken.prototype.tokenType = "IDENT";816 IdentifierToken.prototype.toString = function() {817 return "IDENT(" + this.value + ")";818 }819 IdentifierToken.prototype.toSourceString = function() {820 return this.value;821 }822 function FunctionToken(val) {823 // These are always constructed by passing an IdentifierToken824 this.value = val.finish().value;825 }826 FunctionToken.prototype = new StringValuedToken;827 FunctionToken.prototype.tokenType = "FUNCTION";828 FunctionToken.prototype.toString = function() {829 return "FUNCTION(" + this.value + ")";830 }831 FunctionToken.prototype.toSourceString = function() {832 return this.value;833 }834 function AtKeywordToken(val) {835 this.value = [];836 this.append(val);837 }838 AtKeywordToken.prototype = new StringValuedToken;839 AtKeywordToken.prototype.tokenType = "AT-KEYWORD";840 AtKeywordToken.prototype.toString = function() {841 return "AT(" + this.value + ")";842 }843 AtKeywordToken.prototype.toSourceString = function() {844 return "@" + this.value;845 }846 function HashToken(val) {847 this.value = [];848 this.append(val);849 }850 HashToken.prototype = new StringValuedToken;851 HashToken.prototype.tokenType = "HASH";852 HashToken.prototype.toString = function() {853 return "HASH(" + this.value + ")";854 }855 HashToken.prototype.toSourceString = function() {856 return "#" + this.value;857 }858 function StringToken(val) {859 this.value = [];860 this.append(val);861 }862 StringToken.prototype = new StringValuedToken;863 StringToken.prototype.tokenType = "STRING";864 StringToken.prototype.toString = function() {865 return "\"" + this.value + "\"";866 }867 StringToken.prototype.toSourceString = StringToken.prototype.toString;868 function URLToken(val) {869 this.value = [];870 this.append(val);871 }872 URLToken.prototype = new StringValuedToken;873 URLToken.prototype.tokenType = "URL";874 URLToken.prototype.toString = function() {875 return "URL(" + this.value + ")";876 }877 URLToken.prototype.toSourceString = function() {878 return "url('" + this.value + "')";879 }880 function NumberToken(val) {881 this.value = [];882 this.append(val);883 this.type = "integer";884 }885 NumberToken.prototype = new StringValuedToken;886 NumberToken.prototype.tokenType = "NUMBER";887 NumberToken.prototype.toString = function() {888 if (this.type == "integer")889 return "INT(" + this.value + ")";890 return "NUMBER(" + this.value + ")";891 }892 NumberToken.prototype.toSourceString = function() {893 if (this.type == "integer")894 return this.value;895 return this.value;896 }897 NumberToken.prototype.finish = function() {898 this.repr = this.valueAsString();899 this.value = this.repr * 1;900 if (Math.abs(this.value) % 1 != 0)901 this.type = "number";902 return this;903 }904 function PercentageToken(val) {905 // These are always created by passing a NumberToken as val906 val.finish();907 this.value = val.value;908 this.repr = val.repr;909 }910 PercentageToken.prototype = new CSSParserToken;911 PercentageToken.prototype.tokenType = "PERCENTAGE";912 PercentageToken.prototype.toString = function() {913 return "PERCENTAGE(" + this.value + ")";914 }915 PercentageToken.prototype.toSourceString = function() {916 return this.value + '%';917 }918 function DimensionToken(val, unit) {...

Full Screen

Full Screen

parse-css.js

Source:parse-css.js Github

copy

Full Screen

...301 token.unit = consumeAName();302 token.text = token.unit;303 } else if(next() == 0x25) {304 consume();305 token = new PercentageToken();306 token.value = num.value;307 token.repr = num.repr;308 } else {309 var token = new NumberToken();310 token.value = num.value;311 token.repr = num.repr;312 token.type = num.type;313 }314 token.number = token.value;315 token.isInteger = token.type === "integer";316 // FIXME hasSign317 return token;318 };319 var consumeAnIdentlikeToken = function() {320 var str = consumeAName();321 if(str.toLowerCase() == "url" && next() == 0x28) {322 consume();323 while(whitespace(next(1)) && whitespace(next(2)))324 consume();325 if((next() == 0x22 || next() == 0x27) ||326 (whitespace(next()) && (next(2) == 0x22 || next(2) == 0x27))) {327 while(whitespace(next()))328 consume();329 consume();330 let str = consumeAStringToken();331 while(whitespace(next()))332 consume();333 // The closing paren.334 consume();335 return new URLToken(str.text);336 } else {337 return consumeAURLToken();338 }339 } else if(next() == 0x28) {340 consume();341 return new FunctionToken(str);342 } else {343 return new IdentToken(str);344 }345 };346 var consumeAStringToken = function(endingCodePoint) {347 if(endingCodePoint === undefined) endingCodePoint = code;348 var string = "";349 while(consume()) {350 if(code == endingCodePoint || eof()) {351 return new StringToken(string);352 } else if(newline(code)) {353 reconsume();354 return new BadStringToken(string);355 } else if(code == 0x5c) {356 if(eof(next())) {357 donothing();358 } else if(newline(next())) {359 consume();360 } else {361 string += stringFromCode(consumeEscape());362 }363 } else {364 string += stringFromCode(code);365 }366 }367 };368 var consumeAURLToken = function() {369 var token = new URLToken("");370 while(whitespace(next())) consume();371 if(eof(next())) return token;372 while(consume()) {373 if(code == 0x29 || eof()) {374 break;375 } else if(whitespace(code)) {376 while(whitespace(next()))377 consume();378 if(next() == 0x29 || eof(next())) {379 consume();380 break;381 } else {382 consumeTheRemnantsOfABadURL();383 return new BadURLToken();384 }385 } else if(code == 0x22 || code == 0x27 || code == 0x28 || nonprintable(code)) {386 parseerror();387 consumeTheRemnantsOfABadURL();388 return new BadURLToken();389 } else if(code == 0x5c) {390 if(startsWithAValidEscape()) {391 token.value += stringFromCode(consumeEscape());392 } else {393 parseerror();394 consumeTheRemnantsOfABadURL();395 return new BadURLToken();396 }397 } else {398 token.value += stringFromCode(code);399 }400 }401 token.text = token.value;402 return token;403 };404 var consumeEscape = function() {405 // Assume the the current character is the \406 // and the next code point is not a newline.407 consume();408 if(hexdigit(code)) {409 // Consume 1-6 hex digits410 var digits = [code];411 for(var total = 0; total < 5; total++) {412 if(hexdigit(next())) {413 consume();414 digits.push(code);415 } else {416 break;417 }418 }419 if(whitespace(next())) consume();420 var value = parseInt(digits.map(function(x){return String.fromCharCode(x);}).join(''), 16);421 if( value > maximumallowedcodepoint ) value = 0xfffd;422 return value;423 } else if(eof()) {424 return 0xfffd;425 } else {426 return code;427 }428 };429 var areAValidEscape = function(c1, c2) {430 if(c1 != 0x5c) return false;431 if(newline(c2)) return false;432 return true;433 };434 var startsWithAValidEscape = function() {435 return areAValidEscape(code, next());436 };437 var wouldStartAnIdentifier = function(c1, c2, c3) {438 if(c1 == 0x2d) {439 return namestartchar(c2) || c2 == 0x2d || areAValidEscape(c2, c3);440 } else if(namestartchar(c1)) {441 return true;442 } else if(c1 == 0x5c) {443 return areAValidEscape(c1, c2);444 } else {445 return false;446 }447 };448 var startsWithAnIdentifier = function() {449 return wouldStartAnIdentifier(code, next(1), next(2));450 };451 var wouldStartANumber = function(c1, c2, c3) {452 if(c1 == 0x2b || c1 == 0x2d) {453 if(digit(c2)) return true;454 if(c2 == 0x2e && digit(c3)) return true;455 return false;456 } else if(c1 == 0x2e) {457 if(digit(c2)) return true;458 return false;459 } else if(digit(c1)) {460 return true;461 } else {462 return false;463 }464 };465 var startsWithANumber = function() {466 return wouldStartANumber(code, next(1), next(2));467 };468 var consumeAName = function() {469 var result = "";470 while(consume()) {471 if(namechar(code)) {472 result += stringFromCode(code);473 } else if(startsWithAValidEscape()) {474 result += stringFromCode(consumeEscape());475 } else {476 reconsume();477 return result;478 }479 }480 };481 var consumeANumber = function() {482 var repr = [];483 var type = "integer";484 if(next() == 0x2b || next() == 0x2d) {485 consume();486 repr += stringFromCode(code);487 }488 while(digit(next())) {489 consume();490 repr += stringFromCode(code);491 }492 if(next(1) == 0x2e && digit(next(2))) {493 consume();494 repr += stringFromCode(code);495 consume();496 repr += stringFromCode(code);497 type = "number";498 while(digit(next())) {499 consume();500 repr += stringFromCode(code);501 }502 }503 var c1 = next(1), c2 = next(2), c3 = next(3);504 if((c1 == 0x45 || c1 == 0x65) && digit(c2)) {505 consume();506 repr += stringFromCode(code);507 consume();508 repr += stringFromCode(code);509 type = "number";510 while(digit(next())) {511 consume();512 repr += stringFromCode(code);513 }514 } else if((c1 == 0x45 || c1 == 0x65) && (c2 == 0x2b || c2 == 0x2d) && digit(c3)) {515 consume();516 repr += stringFromCode(code);517 consume();518 repr += stringFromCode(code);519 consume();520 repr += stringFromCode(code);521 type = "number";522 while(digit(next())) {523 consume();524 repr += stringFromCode(code);525 }526 }527 var value = convertAStringToANumber(repr);528 return {type:type, value:value, repr:repr};529 };530 var convertAStringToANumber = function(string) {531 // CSS's number rules are identical to JS, afaik.532 return +string;533 };534 var consumeTheRemnantsOfABadURL = function() {535 while(consume()) {536 if(code == 0x2d || eof()) {537 return;538 } else if(startsWithAValidEscape()) {539 consumeEscape();540 donothing();541 } else {542 donothing();543 }544 }545 };546 var iterationCount = 0;547 while(!eof(next())) {548 var token = consumeAToken();549 if (options.loc) {550 token.loc = {};551 token.loc.start = {line:locStart.line, column:locStart.column};552 token.loc.end = {line:line, column:column};553 }554 if (options.offsets) {555 token.startOffset = offsetStart;556 token.endOffset = i + 1;557 }558 yield token;559 iterationCount++;560 if(iterationCount > str.length*2) return "I'm infinite-looping!";561 }562}563function CSSParserToken() { throw "Abstract Base Class"; }564CSSParserToken.prototype.toJSON = function() {565 return {token: this.tokenType};566};567CSSParserToken.prototype.toString = function() { return this.tokenType; };568CSSParserToken.prototype.toSource = function() { return ''+this; };569function BadStringToken(text) {570 this.text = text;571 return this;572}573BadStringToken.prototype = Object.create(CSSParserToken.prototype);574BadStringToken.prototype.tokenType = "bad_string";575function BadURLToken() { return this; }576BadURLToken.prototype = Object.create(CSSParserToken.prototype);577BadURLToken.prototype.tokenType = "bad_url";578function WhitespaceToken() { return this; }579WhitespaceToken.prototype = Object.create(CSSParserToken.prototype);580WhitespaceToken.prototype.tokenType = "whitespace";581WhitespaceToken.prototype.toString = function() { return "WS"; };582WhitespaceToken.prototype.toSource = function() { return " "; };583function CDOToken() { return this; }584CDOToken.prototype = Object.create(CSSParserToken.prototype);585CDOToken.prototype.tokenType = "htmlcomment";586CDOToken.prototype.toSource = function() { return "<!--"; };587function CDCToken() { return this; }588CDCToken.prototype = Object.create(CSSParserToken.prototype);589CDCToken.prototype.tokenType = "htmlcomment";590CDCToken.prototype.toSource = function() { return "-->"; };591function ColonToken() { return this; }592ColonToken.prototype = Object.create(CSSParserToken.prototype);593ColonToken.prototype.tokenType = "symbol";594ColonToken.prototype.text = ":";595function SemicolonToken() { return this; }596SemicolonToken.prototype = Object.create(CSSParserToken.prototype);597SemicolonToken.prototype.tokenType = "symbol";598SemicolonToken.prototype.text = ";";599function CommaToken() { return this; }600CommaToken.prototype = Object.create(CSSParserToken.prototype);601CommaToken.prototype.tokenType = "symbol";602CommaToken.prototype.text = ",";603function GroupingToken() { throw "Abstract Base Class"; }604GroupingToken.prototype = Object.create(CSSParserToken.prototype);605function OpenCurlyToken() { this.value = "{"; this.mirror = "}"; return this; }606OpenCurlyToken.prototype = Object.create(GroupingToken.prototype);607OpenCurlyToken.prototype.tokenType = "symbol";608OpenCurlyToken.prototype.text = "{";609function CloseCurlyToken() { this.value = "}"; this.mirror = "{"; return this; }610CloseCurlyToken.prototype = Object.create(GroupingToken.prototype);611CloseCurlyToken.prototype.tokenType = "symbol";612CloseCurlyToken.prototype.text = "}";613function OpenSquareToken() { this.value = "["; this.mirror = "]"; return this; }614OpenSquareToken.prototype = Object.create(GroupingToken.prototype);615OpenSquareToken.prototype.tokenType = "symbol";616OpenSquareToken.prototype.text = "[";617function CloseSquareToken() { this.value = "]"; this.mirror = "["; return this; }618CloseSquareToken.prototype = Object.create(GroupingToken.prototype);619CloseSquareToken.prototype.tokenType = "symbol";620CloseSquareToken.prototype.text = "]";621function OpenParenToken() { this.value = "("; this.mirror = ")"; return this; }622OpenParenToken.prototype = Object.create(GroupingToken.prototype);623OpenParenToken.prototype.tokenType = "symbol";624OpenParenToken.prototype.text = "(";625function CloseParenToken() { this.value = ")"; this.mirror = "("; return this; }626CloseParenToken.prototype = Object.create(GroupingToken.prototype);627CloseParenToken.prototype.tokenType = "symbol";628CloseParenToken.prototype.text = ")";629function IncludeMatchToken() { return this; }630IncludeMatchToken.prototype = Object.create(CSSParserToken.prototype);631IncludeMatchToken.prototype.tokenType = "includes";632function DashMatchToken() { return this; }633DashMatchToken.prototype = Object.create(CSSParserToken.prototype);634DashMatchToken.prototype.tokenType = "dashmatch";635function PrefixMatchToken() { return this; }636PrefixMatchToken.prototype = Object.create(CSSParserToken.prototype);637PrefixMatchToken.prototype.tokenType = "beginsmatch";638function SuffixMatchToken() { return this; }639SuffixMatchToken.prototype = Object.create(CSSParserToken.prototype);640SuffixMatchToken.prototype.tokenType = "endsmatch";641function SubstringMatchToken() { return this; }642SubstringMatchToken.prototype = Object.create(CSSParserToken.prototype);643SubstringMatchToken.prototype.tokenType = "containsmatch";644function ColumnToken() { return this; }645ColumnToken.prototype = Object.create(CSSParserToken.prototype);646ColumnToken.prototype.tokenType = "||";647function EOFToken() { return this; }648EOFToken.prototype = Object.create(CSSParserToken.prototype);649EOFToken.prototype.tokenType = "EOF";650EOFToken.prototype.toSource = function() { return ""; };651function DelimToken(code) {652 this.value = stringFromCode(code);653 this.text = this.value;654 return this;655}656DelimToken.prototype = Object.create(CSSParserToken.prototype);657DelimToken.prototype.tokenType = "symbol";658DelimToken.prototype.toString = function() { return "DELIM("+this.value+")"; };659DelimToken.prototype.toJSON = function() {660 var json = this.constructor.prototype.constructor.prototype.toJSON.call(this);661 json.value = this.value;662 return json;663};664DelimToken.prototype.toSource = function() {665 if(this.value == "\\")666 return "\\\n";667 else668 return this.value;669};670function StringValuedToken() { throw "Abstract Base Class"; }671StringValuedToken.prototype = Object.create(CSSParserToken.prototype);672StringValuedToken.prototype.ASCIIMatch = function(str) {673 return this.value.toLowerCase() == str.toLowerCase();674};675StringValuedToken.prototype.toJSON = function() {676 var json = this.constructor.prototype.constructor.prototype.toJSON.call(this);677 json.value = this.value;678 return json;679};680function IdentToken(val) {681 this.value = val;682 this.text = val;683}684IdentToken.prototype = Object.create(StringValuedToken.prototype);685IdentToken.prototype.tokenType = "ident";686IdentToken.prototype.toString = function() { return "IDENT("+this.value+")"; };687IdentToken.prototype.toSource = function() {688 return escapeIdent(this.value);689};690function FunctionToken(val) {691 this.value = val;692 this.text = val;693 this.mirror = ")";694}695FunctionToken.prototype = Object.create(StringValuedToken.prototype);696FunctionToken.prototype.tokenType = "function";697FunctionToken.prototype.toString = function() { return "FUNCTION("+this.value+")"; };698FunctionToken.prototype.toSource = function() {699 return escapeIdent(this.value) + "(";700};701function AtKeywordToken(val) {702 this.value = val;703 this.text = val;704}705AtKeywordToken.prototype = Object.create(StringValuedToken.prototype);706AtKeywordToken.prototype.tokenType = "at";707AtKeywordToken.prototype.toString = function() { return "AT("+this.value+")"; };708AtKeywordToken.prototype.toSource = function() {709 return "@" + escapeIdent(this.value);710};711function HashToken(val) {712 this.value = val;713 this.text = val;714 this.type = "unrestricted";715}716HashToken.prototype = Object.create(StringValuedToken.prototype);717HashToken.prototype.tokenType = "hash";718HashToken.prototype.toString = function() { return "HASH("+this.value+")"; };719HashToken.prototype.toJSON = function() {720 var json = this.constructor.prototype.constructor.prototype.toJSON.call(this);721 json.value = this.value;722 json.type = this.type;723 return json;724};725HashToken.prototype.toSource = function() {726 if(this.type == "id") {727 return "#" + escapeIdent(this.value);728 } else {729 return "#" + escapeHash(this.value);730 }731};732function StringToken(val) {733 this.value = val;734 this.text = val;735}736StringToken.prototype = Object.create(StringValuedToken.prototype);737StringToken.prototype.tokenType = "string";738StringToken.prototype.toString = function() {739 return '"' + escapeString(this.value) + '"';740};741function CommentToken(val) {742 this.value = val;743}744CommentToken.prototype = Object.create(StringValuedToken.prototype);745CommentToken.prototype.tokenType = "comment";746CommentToken.prototype.toString = function() {747 return '/*' + this.value + '*/';748}749CommentToken.prototype.toSource = CommentToken.prototype.toString;750function URLToken(val) {751 this.value = val;752 this.text = val;753}754URLToken.prototype = Object.create(StringValuedToken.prototype);755URLToken.prototype.tokenType = "url";756URLToken.prototype.toString = function() { return "URL("+this.value+")"; };757URLToken.prototype.toSource = function() {758 return 'url("' + escapeString(this.value) + '")';759};760function NumberToken() {761 this.value = null;762 this.type = "integer";763 this.repr = "";764}765NumberToken.prototype = Object.create(CSSParserToken.prototype);766NumberToken.prototype.tokenType = "number";767NumberToken.prototype.toString = function() {768 if(this.type == "integer")769 return "INT("+this.value+")";770 return "NUMBER("+this.value+")";771};772NumberToken.prototype.toJSON = function() {773 var json = this.constructor.prototype.constructor.prototype.toJSON.call(this);774 json.value = this.value;775 json.type = this.type;776 json.repr = this.repr;777 return json;778};779NumberToken.prototype.toSource = function() { return this.repr; };780function PercentageToken() {781 this.value = null;782 this.repr = "";783}784PercentageToken.prototype = Object.create(CSSParserToken.prototype);785PercentageToken.prototype.tokenType = "percentage";786PercentageToken.prototype.toString = function() { return "PERCENTAGE("+this.value+")"; };787PercentageToken.prototype.toJSON = function() {788 var json = this.constructor.prototype.constructor.prototype.toJSON.call(this);789 json.value = this.value;790 json.repr = this.repr;791 return json;792};793PercentageToken.prototype.toSource = function() { return this.repr + "%"; };794function DimensionToken() {...

Full Screen

Full Screen

CSSTokenizerTests.js

Source:CSSTokenizerTests.js Github

copy

Full Screen

1// Copyright 2020 Breakside Inc.2//3// Licensed under the Breakside Public License, Version 1.0 (the "License");4// you may not use this file except in compliance with the License.5// If a copy of the License was not distributed with this file, you may6// obtain a copy at7//8// http://breakside.io/licenses/LICENSE-1.0.txt9//10// Unless required by applicable law or agreed to in writing, software11// distributed under the License is distributed on an "AS IS" BASIS,12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13// See the License for the specific language governing permissions and14// limitations under the License.15// #import CSSOM16// #import TestKit17"use strict";18JSClass("CSSTokenizerTests", TKTestSuite, {19 testIdentifier: function(){20 var css = "test";21 var tokenizer = CSSTokenizer.init();22 var tokens = tokenizer.tokenize(css);23 TKAssertEquals(tokens.length, 1);24 TKAssertInstance(tokens[0], CSSTokenizer.IdentifierToken);25 TKAssertEquals(tokens[0].name, "test");26 css = "test Test _test -test --test";27 tokenizer = CSSTokenizer.init();28 tokens = tokenizer.tokenize(css);29 TKAssertEquals(tokens.length, 9);30 TKAssertInstance(tokens[0], CSSTokenizer.IdentifierToken);31 TKAssertInstance(tokens[1], CSSTokenizer.WhitespaceToken);32 TKAssertInstance(tokens[2], CSSTokenizer.IdentifierToken);33 TKAssertInstance(tokens[3], CSSTokenizer.WhitespaceToken);34 TKAssertInstance(tokens[4], CSSTokenizer.IdentifierToken);35 TKAssertInstance(tokens[5], CSSTokenizer.WhitespaceToken);36 TKAssertInstance(tokens[6], CSSTokenizer.IdentifierToken);37 TKAssertInstance(tokens[7], CSSTokenizer.WhitespaceToken);38 TKAssertInstance(tokens[8], CSSTokenizer.IdentifierToken);39 TKAssertEquals(tokens[0].name, "test");40 TKAssertEquals(tokens[2].name, "Test");41 TKAssertEquals(tokens[4].name, "_test");42 TKAssertEquals(tokens[6].name, "-test");43 TKAssertEquals(tokens[8].name, "--test");44 TKAssertEquals(tokens[0].toString(), "test");45 TKAssertEquals(tokens[2].toString(), "Test");46 TKAssertEquals(tokens[8].toString(), "--test");47 },48 testWhitespace: function(){49 var css = "\n\n\ttest \t \r\nTest\r_test \f \f\t \n";50 var tokenizer = CSSTokenizer.init();51 var tokens = tokenizer.tokenize(css);52 TKAssertEquals(tokens.length, 7);53 TKAssertInstance(tokens[0], CSSTokenizer.WhitespaceToken);54 TKAssertInstance(tokens[1], CSSTokenizer.IdentifierToken);55 TKAssertInstance(tokens[2], CSSTokenizer.WhitespaceToken);56 TKAssertInstance(tokens[3], CSSTokenizer.IdentifierToken);57 TKAssertInstance(tokens[4], CSSTokenizer.WhitespaceToken);58 TKAssertInstance(tokens[5], CSSTokenizer.IdentifierToken);59 TKAssertInstance(tokens[6], CSSTokenizer.WhitespaceToken);60 TKAssertEquals(tokens[1].name, "test");61 TKAssertEquals(tokens[3].name, "Test");62 TKAssertEquals(tokens[5].name, "_test");63 TKAssertEquals(tokens[0].whitespace, "\n\n\t");64 TKAssertEquals(tokens[2].whitespace, " \t \r\n");65 TKAssertEquals(tokens[4].whitespace, "\r");66 TKAssertEquals(tokens[6].whitespace, " \f \f\t \n");67 TKAssertEquals(tokens[0].toString(), "\n\n\t");68 TKAssertEquals(tokens[2].toString(), " \t \r\n");69 TKAssertEquals(tokens[6].toString(), " \f \f\t \n");70 },71 testComment: function(){72 var css = "/*\n this is a comment\n two lines\n*/\n/**/Test/*comment*/_test/* comment */";73 var tokenizer = CSSTokenizer.init();74 var tokens = tokenizer.tokenize(css);75 TKAssertEquals(tokens.length, 7);76 TKAssertInstance(tokens[0], CSSTokenizer.CommentToken);77 TKAssertInstance(tokens[1], CSSTokenizer.WhitespaceToken);78 TKAssertInstance(tokens[2], CSSTokenizer.CommentToken);79 TKAssertInstance(tokens[3], CSSTokenizer.IdentifierToken);80 TKAssertInstance(tokens[4], CSSTokenizer.CommentToken);81 TKAssertInstance(tokens[5], CSSTokenizer.IdentifierToken);82 TKAssertInstance(tokens[6], CSSTokenizer.CommentToken);83 TKAssertEquals(tokens[3].name, "Test");84 TKAssertEquals(tokens[5].name, "_test");85 TKAssertEquals(tokens[1].whitespace, "\n");86 TKAssertEquals(tokens[0].text, "\n this is a comment\n two lines\n");87 TKAssertEquals(tokens[2].text, "");88 TKAssertEquals(tokens[4].text, "comment");89 TKAssertEquals(tokens[6].text, " comment ");90 TKAssertEquals(tokens[0].toString(), "/*\n this is a comment\n two lines\n*/");91 TKAssertEquals(tokens[2].toString(), "/**/");92 TKAssertEquals(tokens[4].toString(), "/*comment*/");93 TKAssertEquals(tokens[6].toString(), "/* comment */");94 },95 testString: function(){96 var css = "'test' 'tes\\'t' \"Test\" \"Tes\\\"t\" 'te\\\nst'";97 var tokenizer = CSSTokenizer.init();98 var tokens = tokenizer.tokenize(css);99 TKAssertEquals(tokens.length, 9);100 TKAssertInstance(tokens[0], CSSTokenizer.StringToken);101 TKAssertInstance(tokens[1], CSSTokenizer.WhitespaceToken);102 TKAssertInstance(tokens[2], CSSTokenizer.StringToken);103 TKAssertInstance(tokens[3], CSSTokenizer.WhitespaceToken);104 TKAssertInstance(tokens[4], CSSTokenizer.StringToken);105 TKAssertInstance(tokens[5], CSSTokenizer.WhitespaceToken);106 TKAssertInstance(tokens[6], CSSTokenizer.StringToken);107 TKAssertInstance(tokens[7], CSSTokenizer.WhitespaceToken);108 TKAssertInstance(tokens[8], CSSTokenizer.StringToken);109 TKAssertEquals(tokens[0].quote, "'");110 TKAssertEquals(tokens[0].value, "test");111 TKAssertEquals(tokens[2].quote, "'");112 TKAssertEquals(tokens[2].value, "tes't");113 TKAssertEquals(tokens[4].quote, '"');114 TKAssertEquals(tokens[4].value, 'Test');115 TKAssertEquals(tokens[6].quote, '"');116 TKAssertEquals(tokens[6].value, 'Tes"t');117 TKAssertEquals(tokens[8].quote, "'");118 TKAssertEquals(tokens[8].value, "test");119 TKAssertEquals(tokens[0].toString(), "'test'");120 TKAssertEquals(tokens[2].toString(), "'tes\\'t'");121 TKAssertEquals(tokens[4].toString(), '"Test"');122 TKAssertEquals(tokens[6].toString(), '"Tes\\"t"');123 TKAssertEquals(tokens[0].toString(), "'test'");124 },125 testNumbers: function(){126 var css = "1,12,123,1.23,.123,1e23";127 var tokenizer = CSSTokenizer.init();128 var tokens = tokenizer.tokenize(css);129 TKAssertEquals(tokens.length, 11);130 TKAssertInstance(tokens[0], CSSTokenizer.NumberToken);131 TKAssertInstance(tokens[1], CSSTokenizer.CommaToken);132 TKAssertInstance(tokens[2], CSSTokenizer.NumberToken);133 TKAssertInstance(tokens[3], CSSTokenizer.CommaToken);134 TKAssertInstance(tokens[4], CSSTokenizer.NumberToken);135 TKAssertInstance(tokens[5], CSSTokenizer.CommaToken);136 TKAssertInstance(tokens[6], CSSTokenizer.NumberToken);137 TKAssertInstance(tokens[7], CSSTokenizer.CommaToken);138 TKAssertInstance(tokens[8], CSSTokenizer.NumberToken);139 TKAssertInstance(tokens[9], CSSTokenizer.CommaToken);140 TKAssertInstance(tokens[10], CSSTokenizer.NumberToken);141 TKAssertExactEquals(tokens[0].value, 1);142 TKAssertExactEquals(tokens[2].value, 12);143 TKAssertExactEquals(tokens[4].value, 123);144 TKAssertFloatEquals(tokens[6].value, 1.23);145 TKAssertFloatEquals(tokens[8].value, 0.123);146 TKAssertFloatEquals(tokens[10].value, 1e23);147 TKAssertEquals(tokens[0].toString(), "1");148 TKAssertEquals(tokens[2].toString(), "12");149 TKAssertEquals(tokens[4].toString(), "123");150 TKAssertEquals(tokens[6].toString(), "1.23");151 TKAssertEquals(tokens[8].toString(), "0.123"); // rewritten with leading 0152 TKAssertEquals(tokens[10].toString(), "1e+23"); // rewritten with + exponent153 css = "+1,+12,+123,+1.23,+.123,+1e23";154 tokenizer = CSSTokenizer.init();155 tokens = tokenizer.tokenize(css);156 TKAssertEquals(tokens.length, 11);157 TKAssertInstance(tokens[0], CSSTokenizer.NumberToken);158 TKAssertInstance(tokens[1], CSSTokenizer.CommaToken);159 TKAssertInstance(tokens[2], CSSTokenizer.NumberToken);160 TKAssertInstance(tokens[3], CSSTokenizer.CommaToken);161 TKAssertInstance(tokens[4], CSSTokenizer.NumberToken);162 TKAssertInstance(tokens[5], CSSTokenizer.CommaToken);163 TKAssertInstance(tokens[6], CSSTokenizer.NumberToken);164 TKAssertInstance(tokens[7], CSSTokenizer.CommaToken);165 TKAssertInstance(tokens[8], CSSTokenizer.NumberToken);166 TKAssertInstance(tokens[9], CSSTokenizer.CommaToken);167 TKAssertInstance(tokens[10], CSSTokenizer.NumberToken);168 TKAssertExactEquals(tokens[0].value, 1);169 TKAssertExactEquals(tokens[2].value, 12);170 TKAssertExactEquals(tokens[4].value, 123);171 TKAssertFloatEquals(tokens[6].value, 1.23);172 TKAssertFloatEquals(tokens[8].value, 0.123);173 TKAssertFloatEquals(tokens[10].value, 1e23);174 // all rewritten without leading plus175 TKAssertEquals(tokens[0].toString(), "1");176 TKAssertEquals(tokens[2].toString(), "12");177 TKAssertEquals(tokens[4].toString(), "123");178 TKAssertEquals(tokens[6].toString(), "1.23");179 TKAssertEquals(tokens[8].toString(), "0.123"); // rewritten with leading 0180 TKAssertEquals(tokens[10].toString(), "1e+23"); // rewritten with + exponent181 css = "-1,-12,-123,-1.23,-.123,-1e23";182 tokenizer = CSSTokenizer.init();183 tokens = tokenizer.tokenize(css);184 TKAssertEquals(tokens.length, 11);185 TKAssertInstance(tokens[0], CSSTokenizer.NumberToken);186 TKAssertInstance(tokens[1], CSSTokenizer.CommaToken);187 TKAssertInstance(tokens[2], CSSTokenizer.NumberToken);188 TKAssertInstance(tokens[3], CSSTokenizer.CommaToken);189 TKAssertInstance(tokens[4], CSSTokenizer.NumberToken);190 TKAssertInstance(tokens[5], CSSTokenizer.CommaToken);191 TKAssertInstance(tokens[6], CSSTokenizer.NumberToken);192 TKAssertInstance(tokens[7], CSSTokenizer.CommaToken);193 TKAssertInstance(tokens[8], CSSTokenizer.NumberToken);194 TKAssertInstance(tokens[9], CSSTokenizer.CommaToken);195 TKAssertInstance(tokens[10], CSSTokenizer.NumberToken);196 TKAssertExactEquals(tokens[0].value, -1);197 TKAssertExactEquals(tokens[2].value, -12);198 TKAssertExactEquals(tokens[4].value, -123);199 TKAssertFloatEquals(tokens[6].value, -1.23);200 TKAssertFloatEquals(tokens[8].value, -0.123);201 TKAssertFloatEquals(tokens[10].value, -1e23);202 TKAssertEquals(tokens[0].toString(), "-1");203 TKAssertEquals(tokens[2].toString(), "-12");204 TKAssertEquals(tokens[4].toString(), "-123");205 TKAssertEquals(tokens[6].toString(), "-1.23");206 TKAssertEquals(tokens[8].toString(), "-0.123"); // rewritten with leading 0207 TKAssertEquals(tokens[10].toString(), "-1e+23"); // rewritten with + exponent208 },209 testDimensions: function(){210 var css = "1px,12em,123abc,1.23test,.123_abc,1e23ABC";211 var tokenizer = CSSTokenizer.init();212 var tokens = tokenizer.tokenize(css);213 TKAssertEquals(tokens.length, 11);214 TKAssertInstance(tokens[0], CSSTokenizer.DimensionToken);215 TKAssertInstance(tokens[1], CSSTokenizer.CommaToken);216 TKAssertInstance(tokens[2], CSSTokenizer.DimensionToken);217 TKAssertInstance(tokens[3], CSSTokenizer.CommaToken);218 TKAssertInstance(tokens[4], CSSTokenizer.DimensionToken);219 TKAssertInstance(tokens[5], CSSTokenizer.CommaToken);220 TKAssertInstance(tokens[6], CSSTokenizer.DimensionToken);221 TKAssertInstance(tokens[7], CSSTokenizer.CommaToken);222 TKAssertInstance(tokens[8], CSSTokenizer.DimensionToken);223 TKAssertInstance(tokens[9], CSSTokenizer.CommaToken);224 TKAssertInstance(tokens[10], CSSTokenizer.DimensionToken);225 TKAssertExactEquals(tokens[0].value, 1);226 TKAssertExactEquals(tokens[2].value, 12);227 TKAssertExactEquals(tokens[4].value, 123);228 TKAssertFloatEquals(tokens[6].value, 1.23);229 TKAssertFloatEquals(tokens[8].value, 0.123);230 TKAssertFloatEquals(tokens[10].value, 1e23);231 TKAssertEquals(tokens[0].units, "px");232 TKAssertEquals(tokens[2].units, "em");233 TKAssertEquals(tokens[4].units, "abc");234 TKAssertEquals(tokens[6].units, "test");235 TKAssertEquals(tokens[8].units, "_abc");236 TKAssertEquals(tokens[10].units, "ABC");237 TKAssertEquals(tokens[0].toString(), "1px");238 TKAssertEquals(tokens[2].toString(), "12em");239 TKAssertEquals(tokens[4].toString(), "123abc");240 TKAssertEquals(tokens[6].toString(), "1.23test");241 TKAssertEquals(tokens[8].toString(), "0.123_abc"); // rewritten with leading 0242 TKAssertEquals(tokens[10].toString(), "1e+23ABC"); // rewritten with + exponent243 css = "+1px,+12em,+123abc,+1.23test,+.123_abc,+1e23ABC";244 tokenizer = CSSTokenizer.init();245 tokens = tokenizer.tokenize(css);246 TKAssertEquals(tokens.length, 11);247 TKAssertInstance(tokens[0], CSSTokenizer.DimensionToken);248 TKAssertInstance(tokens[1], CSSTokenizer.CommaToken);249 TKAssertInstance(tokens[2], CSSTokenizer.DimensionToken);250 TKAssertInstance(tokens[3], CSSTokenizer.CommaToken);251 TKAssertInstance(tokens[4], CSSTokenizer.DimensionToken);252 TKAssertInstance(tokens[5], CSSTokenizer.CommaToken);253 TKAssertInstance(tokens[6], CSSTokenizer.DimensionToken);254 TKAssertInstance(tokens[7], CSSTokenizer.CommaToken);255 TKAssertInstance(tokens[8], CSSTokenizer.DimensionToken);256 TKAssertInstance(tokens[9], CSSTokenizer.CommaToken);257 TKAssertInstance(tokens[10], CSSTokenizer.DimensionToken);258 TKAssertExactEquals(tokens[0].value, 1);259 TKAssertExactEquals(tokens[2].value, 12);260 TKAssertExactEquals(tokens[4].value, 123);261 TKAssertFloatEquals(tokens[6].value, 1.23);262 TKAssertFloatEquals(tokens[8].value, 0.123);263 TKAssertFloatEquals(tokens[10].value, 1e23);264 TKAssertEquals(tokens[0].units, "px");265 TKAssertEquals(tokens[2].units, "em");266 TKAssertEquals(tokens[4].units, "abc");267 TKAssertEquals(tokens[6].units, "test");268 TKAssertEquals(tokens[8].units, "_abc");269 TKAssertEquals(tokens[10].units, "ABC");270 // all rewritten without leading plus271 TKAssertEquals(tokens[0].toString(), "1px");272 TKAssertEquals(tokens[2].toString(), "12em");273 TKAssertEquals(tokens[4].toString(), "123abc");274 TKAssertEquals(tokens[6].toString(), "1.23test");275 TKAssertEquals(tokens[8].toString(), "0.123_abc"); // rewritten with leading 0276 TKAssertEquals(tokens[10].toString(), "1e+23ABC"); // rewritten with + exponent277 css = "-1px,-12em,-123abc,-1.23test,-.123_abc,-1e23ABC";278 tokenizer = CSSTokenizer.init();279 tokens = tokenizer.tokenize(css);280 TKAssertEquals(tokens.length, 11);281 TKAssertInstance(tokens[0], CSSTokenizer.DimensionToken);282 TKAssertInstance(tokens[1], CSSTokenizer.CommaToken);283 TKAssertInstance(tokens[2], CSSTokenizer.DimensionToken);284 TKAssertInstance(tokens[3], CSSTokenizer.CommaToken);285 TKAssertInstance(tokens[4], CSSTokenizer.DimensionToken);286 TKAssertInstance(tokens[5], CSSTokenizer.CommaToken);287 TKAssertInstance(tokens[6], CSSTokenizer.DimensionToken);288 TKAssertInstance(tokens[7], CSSTokenizer.CommaToken);289 TKAssertInstance(tokens[8], CSSTokenizer.DimensionToken);290 TKAssertInstance(tokens[9], CSSTokenizer.CommaToken);291 TKAssertInstance(tokens[10], CSSTokenizer.DimensionToken);292 TKAssertExactEquals(tokens[0].value, -1);293 TKAssertExactEquals(tokens[2].value, -12);294 TKAssertExactEquals(tokens[4].value, -123);295 TKAssertFloatEquals(tokens[6].value, -1.23);296 TKAssertFloatEquals(tokens[8].value, -0.123);297 TKAssertFloatEquals(tokens[10].value, -1e23);298 TKAssertEquals(tokens[0].units, "px");299 TKAssertEquals(tokens[2].units, "em");300 TKAssertEquals(tokens[4].units, "abc");301 TKAssertEquals(tokens[6].units, "test");302 TKAssertEquals(tokens[8].units, "_abc");303 TKAssertEquals(tokens[10].units, "ABC");304 TKAssertEquals(tokens[0].toString(), "-1px");305 TKAssertEquals(tokens[2].toString(), "-12em");306 TKAssertEquals(tokens[4].toString(), "-123abc");307 TKAssertEquals(tokens[6].toString(), "-1.23test");308 TKAssertEquals(tokens[8].toString(), "-0.123_abc"); // rewritten with leading 0309 TKAssertEquals(tokens[10].toString(), "-1e+23ABC"); // rewritten with + exponent310 },311 testPercentages: function(){312 var css = "1%,12%,123%,1.23%,.123%,1e23%";313 var tokenizer = CSSTokenizer.init();314 var tokens = tokenizer.tokenize(css);315 TKAssertEquals(tokens.length, 11);316 TKAssertInstance(tokens[0], CSSTokenizer.PercentageToken);317 TKAssertInstance(tokens[1], CSSTokenizer.CommaToken);318 TKAssertInstance(tokens[2], CSSTokenizer.PercentageToken);319 TKAssertInstance(tokens[3], CSSTokenizer.CommaToken);320 TKAssertInstance(tokens[4], CSSTokenizer.PercentageToken);321 TKAssertInstance(tokens[5], CSSTokenizer.CommaToken);322 TKAssertInstance(tokens[6], CSSTokenizer.PercentageToken);323 TKAssertInstance(tokens[7], CSSTokenizer.CommaToken);324 TKAssertInstance(tokens[8], CSSTokenizer.PercentageToken);325 TKAssertInstance(tokens[9], CSSTokenizer.CommaToken);326 TKAssertInstance(tokens[10], CSSTokenizer.PercentageToken);327 TKAssertExactEquals(tokens[0].value, 1);328 TKAssertExactEquals(tokens[2].value, 12);329 TKAssertExactEquals(tokens[4].value, 123);330 TKAssertFloatEquals(tokens[6].value, 1.23);331 TKAssertFloatEquals(tokens[8].value, 0.123);332 TKAssertFloatEquals(tokens[10].value, 1e23);333 TKAssertEquals(tokens[0].toString(), "1%");334 TKAssertEquals(tokens[2].toString(), "12%");335 TKAssertEquals(tokens[4].toString(), "123%");336 TKAssertEquals(tokens[6].toString(), "1.23%");337 TKAssertEquals(tokens[8].toString(), "0.123%"); // rewritten with leading 0338 TKAssertEquals(tokens[10].toString(), "1e+23%"); // rewritten with + exponent339 css = "+1%,+12%,+123%,+1.23%,+.123%,+1e23%";340 tokenizer = CSSTokenizer.init();341 tokens = tokenizer.tokenize(css);342 TKAssertEquals(tokens.length, 11);343 TKAssertInstance(tokens[0], CSSTokenizer.PercentageToken);344 TKAssertInstance(tokens[1], CSSTokenizer.CommaToken);345 TKAssertInstance(tokens[2], CSSTokenizer.PercentageToken);346 TKAssertInstance(tokens[3], CSSTokenizer.CommaToken);347 TKAssertInstance(tokens[4], CSSTokenizer.PercentageToken);348 TKAssertInstance(tokens[5], CSSTokenizer.CommaToken);349 TKAssertInstance(tokens[6], CSSTokenizer.PercentageToken);350 TKAssertInstance(tokens[7], CSSTokenizer.CommaToken);351 TKAssertInstance(tokens[8], CSSTokenizer.PercentageToken);352 TKAssertInstance(tokens[9], CSSTokenizer.CommaToken);353 TKAssertInstance(tokens[10], CSSTokenizer.PercentageToken);354 TKAssertExactEquals(tokens[0].value, 1);355 TKAssertExactEquals(tokens[2].value, 12);356 TKAssertExactEquals(tokens[4].value, 123);357 TKAssertFloatEquals(tokens[6].value, 1.23);358 TKAssertFloatEquals(tokens[8].value, 0.123);359 TKAssertFloatEquals(tokens[10].value, 1e23);360 // all rewritten without leading plus361 TKAssertEquals(tokens[0].toString(), "1%");362 TKAssertEquals(tokens[2].toString(), "12%");363 TKAssertEquals(tokens[4].toString(), "123%");364 TKAssertEquals(tokens[6].toString(), "1.23%");365 TKAssertEquals(tokens[8].toString(), "0.123%"); // rewritten with leading 0366 TKAssertEquals(tokens[10].toString(), "1e+23%"); // rewritten with + exponent367 css = "-1%,-12%,-123%,-1.23%,-.123%,-1e23%";368 tokenizer = CSSTokenizer.init();369 tokens = tokenizer.tokenize(css);370 TKAssertEquals(tokens.length, 11);371 TKAssertInstance(tokens[0], CSSTokenizer.PercentageToken);372 TKAssertInstance(tokens[1], CSSTokenizer.CommaToken);373 TKAssertInstance(tokens[2], CSSTokenizer.PercentageToken);374 TKAssertInstance(tokens[3], CSSTokenizer.CommaToken);375 TKAssertInstance(tokens[4], CSSTokenizer.PercentageToken);376 TKAssertInstance(tokens[5], CSSTokenizer.CommaToken);377 TKAssertInstance(tokens[6], CSSTokenizer.PercentageToken);378 TKAssertInstance(tokens[7], CSSTokenizer.CommaToken);379 TKAssertInstance(tokens[8], CSSTokenizer.PercentageToken);380 TKAssertInstance(tokens[9], CSSTokenizer.CommaToken);381 TKAssertInstance(tokens[10], CSSTokenizer.PercentageToken);382 TKAssertExactEquals(tokens[0].value, -1);383 TKAssertExactEquals(tokens[2].value, -12);384 TKAssertExactEquals(tokens[4].value, -123);385 TKAssertFloatEquals(tokens[6].value, -1.23);386 TKAssertFloatEquals(tokens[8].value, -0.123);387 TKAssertFloatEquals(tokens[10].value, -1e23);388 TKAssertEquals(tokens[0].toString(), "-1%");389 TKAssertEquals(tokens[2].toString(), "-12%");390 TKAssertEquals(tokens[4].toString(), "-123%");391 TKAssertEquals(tokens[6].toString(), "-1.23%");392 TKAssertEquals(tokens[8].toString(), "-0.123%"); // rewritten with leading 0393 TKAssertEquals(tokens[10].toString(), "-1e+23%"); // rewritten with + exponent394 },395 testHash: function(){396 var css = "#";397 var tokenizer = CSSTokenizer.init();398 var tokens = tokenizer.tokenize(css);399 TKAssertEquals(tokens.length, 1);400 TKAssertInstance(tokens[0], CSSTokenizer.DelimToken);401 TKAssertEquals(tokens[0].char, "#");402 TKAssertEquals(tokens[0].toString(), "#");403 css = "#test";404 tokenizer = CSSTokenizer.init();405 tokens = tokenizer.tokenize(css);406 TKAssertEquals(tokens.length, 1);407 TKAssertInstance(tokens[0], CSSTokenizer.HashToken);408 TKAssertEquals(tokens[0].name, "test");409 TKAssertEquals(tokens[0].type, "id");410 TKAssertEquals(tokens[0].toString(), "#test");411 css = "#9test";412 tokenizer = CSSTokenizer.init();413 tokens = tokenizer.tokenize(css);414 TKAssertEquals(tokens.length, 1);415 TKAssertInstance(tokens[0], CSSTokenizer.HashToken);416 TKAssertEquals(tokens[0].name, "9test");417 TKAssertNull(tokens[0].type);418 TKAssertEquals(tokens[0].toString(), "#9test");419 },420 testAt: function(){421 var css = "@";422 var tokenizer = CSSTokenizer.init();423 var tokens = tokenizer.tokenize(css);424 TKAssertEquals(tokens.length, 1);425 TKAssertInstance(tokens[0], CSSTokenizer.DelimToken);426 TKAssertEquals(tokens[0].char, "@");427 TKAssertEquals(tokens[0].toString(), "@");428 css = "@test";429 tokenizer = CSSTokenizer.init();430 tokens = tokenizer.tokenize(css);431 TKAssertEquals(tokens.length, 1);432 TKAssertInstance(tokens[0], CSSTokenizer.AtKeywordToken);433 TKAssertEquals(tokens[0].name, "test");434 TKAssertEquals(tokens[0].toString(), "@test");435 css = "@9test";436 tokenizer = CSSTokenizer.init();437 tokens = tokenizer.tokenize(css);438 TKAssertEquals(tokens.length, 2);439 TKAssertInstance(tokens[0], CSSTokenizer.DelimToken);440 TKAssertEquals(tokens[0].char, "@");441 TKAssertInstance(tokens[1], CSSTokenizer.DimensionToken);442 TKAssertEquals(tokens[1].value, 9);443 TKAssertEquals(tokens[1].units, "test");444 TKAssertEquals(tokens[0].toString(), "@");445 TKAssertEquals(tokens[1].toString(), "9test");446 },447 testDelimiters: function(){448 var css = "()[]{},:;<><!---->.";449 var tokenizer = CSSTokenizer.init();450 var tokens = tokenizer.tokenize(css);451 TKAssertEquals(tokens.length, 14);452 TKAssertInstance(tokens[0], CSSTokenizer.OpenParenToken);453 TKAssertInstance(tokens[1], CSSTokenizer.CloseParenToken);454 TKAssertInstance(tokens[2], CSSTokenizer.OpenSquareToken);455 TKAssertInstance(tokens[3], CSSTokenizer.CloseSquareToken);456 TKAssertInstance(tokens[4], CSSTokenizer.OpenCurlyToken);457 TKAssertInstance(tokens[5], CSSTokenizer.CloseCurlyToken);458 TKAssertInstance(tokens[6], CSSTokenizer.CommaToken);459 TKAssertInstance(tokens[7], CSSTokenizer.ColonToken);460 TKAssertInstance(tokens[8], CSSTokenizer.SemicolonToken);461 TKAssertInstance(tokens[9], CSSTokenizer.DelimToken);462 TKAssertEquals(tokens[9].char, "<");463 TKAssertInstance(tokens[10], CSSTokenizer.DelimToken);464 TKAssertEquals(tokens[10].char, ">");465 TKAssertInstance(tokens[11], CSSTokenizer.CDOToken);466 TKAssertInstance(tokens[12], CSSTokenizer.CDCToken);467 TKAssertInstance(tokens[13], CSSTokenizer.DelimToken);468 TKAssertEquals(tokens[13].char, ".");469 TKAssertEquals(tokens[0].toString(), "(");470 TKAssertEquals(tokens[1].toString(), ")");471 TKAssertEquals(tokens[2].toString(), "[");472 TKAssertEquals(tokens[3].toString(), "]");473 TKAssertEquals(tokens[4].toString(), "{");474 TKAssertEquals(tokens[5].toString(), "}");475 TKAssertEquals(tokens[6].toString(), ",");476 TKAssertEquals(tokens[7].toString(), ":");477 TKAssertEquals(tokens[8].toString(), ";");478 TKAssertEquals(tokens[9].toString(), "<");479 TKAssertEquals(tokens[10].toString(), ">");480 TKAssertEquals(tokens[11].toString(), "<!--");481 TKAssertEquals(tokens[12].toString(), "-->");482 TKAssertEquals(tokens[13].toString(), ".");483 },484 testFunctions: function(){485 var css = "test()";486 var tokenizer = CSSTokenizer.init();487 var tokens = tokenizer.tokenize(css);488 TKAssertEquals(tokens.length, 2);489 TKAssertInstance(tokens[0], CSSTokenizer.FunctionToken);490 TKAssertEquals(tokens[0].name, "test");491 TKAssertInstance(tokens[1], CSSTokenizer.CloseParenToken);492 TKAssertEquals(tokens[0].toString(), "test(");493 TKAssertEquals(tokens[1].toString(), ")");494 css = "--test('one',2, 3.4)";495 tokenizer = CSSTokenizer.init();496 tokens = tokenizer.tokenize(css);497 TKAssertEquals(tokens.length, 8);498 TKAssertInstance(tokens[0], CSSTokenizer.FunctionToken);499 TKAssertInstance(tokens[1], CSSTokenizer.StringToken);500 TKAssertInstance(tokens[2], CSSTokenizer.CommaToken);501 TKAssertInstance(tokens[3], CSSTokenizer.NumberToken);502 TKAssertInstance(tokens[4], CSSTokenizer.CommaToken);503 TKAssertInstance(tokens[5], CSSTokenizer.WhitespaceToken);504 TKAssertInstance(tokens[6], CSSTokenizer.NumberToken);505 TKAssertInstance(tokens[7], CSSTokenizer.CloseParenToken);506 TKAssertEquals(tokens[0].name, "--test");507 TKAssertEquals(tokens[1].value, "one");508 TKAssertEquals(tokens[3].value, 2);509 TKAssertFloatEquals(tokens[6].value, 3.4);510 TKAssertEquals(tokens[0].toString(), "--test(");511 TKAssertEquals(tokens[1].toString(), "'one'");512 TKAssertEquals(tokens[2].toString(), ",");513 TKAssertEquals(tokens[3].toString(), "2");514 TKAssertEquals(tokens[4].toString(), ",");515 TKAssertEquals(tokens[5].toString(), " ");516 TKAssertEquals(tokens[6].toString(), "3.4");517 TKAssertEquals(tokens[7].toString(), ")");518 },519 testURL: function(){520 var css = "url(one)";521 var tokenizer = CSSTokenizer.init();522 var tokens = tokenizer.tokenize(css);523 TKAssertEquals(tokens.length, 1);524 TKAssertInstance(tokens[0], CSSTokenizer.URLToken);525 TKAssertEquals(tokens[0].url, "one");526 TKAssertEquals(tokens[0].toString(), "url(one)");527 css = "url( one/there#hash )";528 tokenizer = CSSTokenizer.init();529 tokens = tokenizer.tokenize(css);530 TKAssertEquals(tokens.length, 1);531 TKAssertInstance(tokens[0], CSSTokenizer.URLToken);532 TKAssertEquals(tokens[0].url, "one/there#hash");533 TKAssertEquals(tokens[0].toString(), "url(one/there#hash)"); // rewritten without whitespace534 css = "url('one/there#hash')";535 tokenizer = CSSTokenizer.init();536 tokens = tokenizer.tokenize(css);537 TKAssertEquals(tokens.length, 3);538 TKAssertInstance(tokens[0], CSSTokenizer.FunctionToken);539 TKAssertEquals(tokens[0].name, "url");540 TKAssertInstance(tokens[1], CSSTokenizer.StringToken);541 TKAssertEquals(tokens[1].value, "one/there#hash");542 TKAssertInstance(tokens[2], CSSTokenizer.CloseParenToken);543 TKAssertEquals(tokens[0].toString(), "url(");544 TKAssertEquals(tokens[1].toString(), "'one/there#hash'");545 TKAssertEquals(tokens[2].toString(), ")");546 css = "url( \"one/there#hash\" )";547 tokenizer = CSSTokenizer.init();548 tokens = tokenizer.tokenize(css);549 TKAssertEquals(tokens.length, 4);550 TKAssertInstance(tokens[0], CSSTokenizer.FunctionToken);551 TKAssertEquals(tokens[0].name, "url");552 TKAssertInstance(tokens[1], CSSTokenizer.StringToken);553 TKAssertEquals(tokens[1].value, "one/there#hash");554 TKAssertInstance(tokens[2], CSSTokenizer.WhitespaceToken);555 TKAssertEquals(tokens[2].whitespace, " ");556 TKAssertInstance(tokens[3], CSSTokenizer.CloseParenToken);557 TKAssertEquals(tokens[0].toString(), "url(");558 TKAssertEquals(tokens[1].toString(), "\"one/there#hash\"");559 TKAssertEquals(tokens[2].toString(), " ");560 TKAssertEquals(tokens[3].toString(), ")");561 },...

Full Screen

Full Screen

cssTokenizer.js

Source:cssTokenizer.js Github

copy

Full Screen

...298 token.unit = consumeAName();299 return token;300 } else if(next() == 0x25) {301 consume();302 var token = new PercentageToken();303 token.value = num.value;304 token.repr = num.repr;305 return token;306 } else {307 var token = new NumberToken();308 token.value = num.value;309 token.repr = num.repr;310 token.type = num.type;311 return token;312 }313 };314 var consumeAnIdentlikeToken = function() {315 var str = consumeAName();316 if(str.toLowerCase() == "url" && next() == 0x28) {317 consume();318 while(whitespace(next(1)) && whitespace(next(2))) consume();319 if(next() == 0x22 || next() == 0x27) {320 return new FunctionToken(str);321 } else if(whitespace(next()) && (next(2) == 0x22 || next(2) == 0x27)) {322 return new FunctionToken(str);323 } else {324 return consumeAURLToken();325 }326 } else if(next() == 0x28) {327 consume();328 return new FunctionToken(str);329 } else {330 return new IdentToken(str);331 }332 };333 var consumeAStringToken = function(endingCodePoint) {334 if(endingCodePoint === undefined) endingCodePoint = code;335 var string = "";336 while(consume()) {337 if(code == endingCodePoint || eof()) {338 return new StringToken(string);339 } else if(newline(code)) {340 parseerror();341 reconsume();342 return new BadStringToken();343 } else if(code == 0x5c) {344 if(eof(next())) {345 donothing();346 } else if(newline(next())) {347 consume();348 } else {349 string += stringFromCode(consumeEscape())350 }351 } else {352 string += stringFromCode(code);353 }354 }355 };356 var consumeAURLToken = function() {357 var token = new URLToken("");358 while(whitespace(next())) consume();359 if(eof(next())) return token;360 while(consume()) {361 if(code == 0x29 || eof()) {362 return token;363 } else if(whitespace(code)) {364 while(whitespace(next())) consume();365 if(next() == 0x29 || eof(next())) {366 consume();367 return token;368 } else {369 consumeTheRemnantsOfABadURL();370 return new BadURLToken();371 }372 } else if(code == 0x22 || code == 0x27 || code == 0x28 || nonprintable(code)) {373 parseerror();374 consumeTheRemnantsOfABadURL();375 return new BadURLToken();376 } else if(code == 0x5c) {377 if(startsWithAValidEscape()) {378 token.value += stringFromCode(consumeEscape());379 } else {380 parseerror();381 consumeTheRemnantsOfABadURL();382 return new BadURLToken();383 }384 } else {385 token.value += stringFromCode(code);386 }387 }388 };389 var consumeEscape = function() {390 // Assume the the current character is the \391 // and the next code point is not a newline.392 consume();393 if(hexdigit(code)) {394 // Consume 1-6 hex digits395 var digits = [code];396 for(var total = 0; total < 5; total++) {397 if(hexdigit(next())) {398 consume();399 digits.push(code);400 } else {401 break;402 }403 }404 if(whitespace(next())) consume();405 var value = parseInt(digits.map(function(x){return String.fromCharCode(x);}).join(''), 16);406 if( value > maximumallowedcodepoint ) value = 0xfffd;407 return value;408 } else if(eof()) {409 return 0xfffd;410 } else {411 return code;412 }413 };414 var areAValidEscape = function(c1, c2) {415 if(c1 != 0x5c) return false;416 if(newline(c2)) return false;417 return true;418 };419 var startsWithAValidEscape = function() {420 return areAValidEscape(code, next());421 };422 var wouldStartAnIdentifier = function(c1, c2, c3) {423 if(c1 == 0x2d) {424 return namestartchar(c2) || c2 == 0x2d || areAValidEscape(c2, c3);425 } else if(namestartchar(c1)) {426 return true;427 } else if(c1 == 0x5c) {428 return areAValidEscape(c1, c2);429 } else {430 return false;431 }432 };433 var startsWithAnIdentifier = function() {434 return wouldStartAnIdentifier(code, next(1), next(2));435 };436 var wouldStartANumber = function(c1, c2, c3) {437 if(c1 == 0x2b || c1 == 0x2d) {438 if(digit(c2)) return true;439 if(c2 == 0x2e && digit(c3)) return true;440 return false;441 } else if(c1 == 0x2e) {442 if(digit(c2)) return true;443 return false;444 } else if(digit(c1)) {445 return true;446 } else {447 return false;448 }449 };450 var startsWithANumber = function() {451 return wouldStartANumber(code, next(1), next(2));452 };453 var consumeAName = function() {454 var result = "";455 while(consume()) {456 if(namechar(code)) {457 result += stringFromCode(code);458 } else if(startsWithAValidEscape()) {459 result += stringFromCode(consumeEscape());460 } else {461 reconsume();462 return result;463 }464 }465 };466 var consumeANumber = function() {467 var repr = [];468 var type = "integer";469 if(next() == 0x2b || next() == 0x2d) {470 consume();471 repr += stringFromCode(code);472 }473 while(digit(next())) {474 consume();475 repr += stringFromCode(code);476 }477 if(next(1) == 0x2e && digit(next(2))) {478 consume();479 repr += stringFromCode(code);480 consume();481 repr += stringFromCode(code);482 type = "number";483 while(digit(next())) {484 consume();485 repr += stringFromCode(code);486 }487 }488 var c1 = next(1), c2 = next(2), c3 = next(3);489 if((c1 == 0x45 || c1 == 0x65) && digit(c2)) {490 consume();491 repr += stringFromCode(code);492 consume();493 repr += stringFromCode(code);494 type = "number";495 while(digit(next())) {496 consume();497 repr += stringFromCode(code);498 }499 } else if((c1 == 0x45 || c1 == 0x65) && (c2 == 0x2b || c2 == 0x2d) && digit(c3)) {500 consume();501 repr += stringFromCode(code);502 consume();503 repr += stringFromCode(code);504 consume();505 repr += stringFromCode(code);506 type = "number";507 while(digit(next())) {508 consume();509 repr += stringFromCode(code);510 }511 }512 var value = convertAStringToANumber(repr);513 return {type:type, value:value, repr:repr};514 };515 var convertAStringToANumber = function(string) {516 // CSS's number rules are identical to JS, afaik.517 return +string;518 };519 var consumeTheRemnantsOfABadURL = function() {520 while(consume()) {521 if(code == 0x29 || eof()) {522 return;523 } else if(startsWithAValidEscape()) {524 consumeEscape();525 donothing();526 } else {527 donothing();528 }529 }530 };531 var iterationCount = 0;532 while(!eof(next())) {533 tokens.push(consumeAToken());534 iterationCount++;535 if(iterationCount > str.length*2) return "I'm infinite-looping!";536 }537 return tokens;538}539function CSSParserToken() { throw "Abstract Base Class"; }540CSSParserToken.prototype.toJSON = function() {541 return {token: this.tokenType};542}543CSSParserToken.prototype.toString = function() { return this.tokenType; }544CSSParserToken.prototype.toSource = function() { return ''+this; }545function BadStringToken() { return this; }546BadStringToken.prototype = Object.create(CSSParserToken.prototype);547BadStringToken.prototype.tokenType = "BADSTRING";548function BadURLToken() { return this; }549BadURLToken.prototype = Object.create(CSSParserToken.prototype);550BadURLToken.prototype.tokenType = "BADURL";551function WhitespaceToken() { return this; }552WhitespaceToken.prototype = Object.create(CSSParserToken.prototype);553WhitespaceToken.prototype.tokenType = "WHITESPACE";554WhitespaceToken.prototype.toString = function() { return "WS"; }555WhitespaceToken.prototype.toSource = function() { return " "; }556function CDOToken() { return this; }557CDOToken.prototype = Object.create(CSSParserToken.prototype);558CDOToken.prototype.tokenType = "CDO";559CDOToken.prototype.toSource = function() { return "<!--"; }560function CDCToken() { return this; }561CDCToken.prototype = Object.create(CSSParserToken.prototype);562CDCToken.prototype.tokenType = "CDC";563CDCToken.prototype.toSource = function() { return "-->"; }564function ColonToken() { return this; }565ColonToken.prototype = Object.create(CSSParserToken.prototype);566ColonToken.prototype.tokenType = ":";567function SemicolonToken() { return this; }568SemicolonToken.prototype = Object.create(CSSParserToken.prototype);569SemicolonToken.prototype.tokenType = ";";570function CommaToken() { return this; }571CommaToken.prototype = Object.create(CSSParserToken.prototype);572CommaToken.prototype.tokenType = ",";573function GroupingToken() { throw "Abstract Base Class"; }574GroupingToken.prototype = Object.create(CSSParserToken.prototype);575function OpenCurlyToken() { this.value = "{"; this.mirror = "}"; return this; }576OpenCurlyToken.prototype = Object.create(GroupingToken.prototype);577OpenCurlyToken.prototype.tokenType = "{";578function CloseCurlyToken() { this.value = "}"; this.mirror = "{"; return this; }579CloseCurlyToken.prototype = Object.create(GroupingToken.prototype);580CloseCurlyToken.prototype.tokenType = "}";581function OpenSquareToken() { this.value = "["; this.mirror = "]"; return this; }582OpenSquareToken.prototype = Object.create(GroupingToken.prototype);583OpenSquareToken.prototype.tokenType = "[";584function CloseSquareToken() { this.value = "]"; this.mirror = "["; return this; }585CloseSquareToken.prototype = Object.create(GroupingToken.prototype);586CloseSquareToken.prototype.tokenType = "]";587function OpenParenToken() { this.value = "("; this.mirror = ")"; return this; }588OpenParenToken.prototype = Object.create(GroupingToken.prototype);589OpenParenToken.prototype.tokenType = "(";590function CloseParenToken() { this.value = ")"; this.mirror = "("; return this; }591CloseParenToken.prototype = Object.create(GroupingToken.prototype);592CloseParenToken.prototype.tokenType = ")";593function IncludeMatchToken() { return this; }594IncludeMatchToken.prototype = Object.create(CSSParserToken.prototype);595IncludeMatchToken.prototype.tokenType = "~=";596function DashMatchToken() { return this; }597DashMatchToken.prototype = Object.create(CSSParserToken.prototype);598DashMatchToken.prototype.tokenType = "|=";599function PrefixMatchToken() { return this; }600PrefixMatchToken.prototype = Object.create(CSSParserToken.prototype);601PrefixMatchToken.prototype.tokenType = "^=";602function SuffixMatchToken() { return this; }603SuffixMatchToken.prototype = Object.create(CSSParserToken.prototype);604SuffixMatchToken.prototype.tokenType = "$=";605function SubstringMatchToken() { return this; }606SubstringMatchToken.prototype = Object.create(CSSParserToken.prototype);607SubstringMatchToken.prototype.tokenType = "*=";608function ColumnToken() { return this; }609ColumnToken.prototype = Object.create(CSSParserToken.prototype);610ColumnToken.prototype.tokenType = "||";611function EOFToken() { return this; }612EOFToken.prototype = Object.create(CSSParserToken.prototype);613EOFToken.prototype.tokenType = "EOF";614EOFToken.prototype.toSource = function() { return ""; }615function DelimToken(code) {616 this.value = stringFromCode(code);617 return this;618}619DelimToken.prototype = Object.create(CSSParserToken.prototype);620DelimToken.prototype.tokenType = "DELIM";621DelimToken.prototype.toString = function() { return "DELIM("+this.value+")"; }622DelimToken.prototype.toJSON = function() {623 var json = this.constructor.prototype.constructor.prototype.toJSON.call(this);624 json.value = this.value;625 return json;626}627DelimToken.prototype.toSource = function() {628 if(this.value == "\\")629 return "\\\n";630 else631 return this.value;632}633function StringValuedToken() { throw "Abstract Base Class"; }634StringValuedToken.prototype = Object.create(CSSParserToken.prototype);635StringValuedToken.prototype.ASCIIMatch = function(str) {636 return this.value.toLowerCase() == str.toLowerCase();637}638StringValuedToken.prototype.toJSON = function() {639 var json = this.constructor.prototype.constructor.prototype.toJSON.call(this);640 json.value = this.value;641 return json;642}643function IdentToken(val) {644 this.value = val;645}646IdentToken.prototype = Object.create(StringValuedToken.prototype);647IdentToken.prototype.tokenType = "IDENT";648IdentToken.prototype.toString = function() { return "IDENT("+this.value+")"; }649IdentToken.prototype.toSource = function() {650 return escapeIdent(this.value);651}652function FunctionToken(val) {653 this.value = val;654 this.mirror = ")";655}656FunctionToken.prototype = Object.create(StringValuedToken.prototype);657FunctionToken.prototype.tokenType = "FUNCTION";658FunctionToken.prototype.toString = function() { return "FUNCTION("+this.value+")"; }659FunctionToken.prototype.toSource = function() {660 return escapeIdent(this.value) + "(";661}662function AtKeywordToken(val) {663 this.value = val;664}665AtKeywordToken.prototype = Object.create(StringValuedToken.prototype);666AtKeywordToken.prototype.tokenType = "AT-KEYWORD";667AtKeywordToken.prototype.toString = function() { return "AT("+this.value+")"; }668AtKeywordToken.prototype.toSource = function() {669 return "@" + escapeIdent(this.value);670}671function HashToken(val) {672 this.value = val;673 this.type = "unrestricted";674}675HashToken.prototype = Object.create(StringValuedToken.prototype);676HashToken.prototype.tokenType = "HASH";677HashToken.prototype.toString = function() { return "HASH("+this.value+")"; }678HashToken.prototype.toJSON = function() {679 var json = this.constructor.prototype.constructor.prototype.toJSON.call(this);680 json.value = this.value;681 json.type = this.type;682 return json;683}684HashToken.prototype.toSource = function() {685 if(this.type == "id") {686 return "#" + escapeIdent(this.value);687 } else {688 return "#" + escapeHash(this.value);689 }690}691function StringToken(val) {692 this.value = val;693}694StringToken.prototype = Object.create(StringValuedToken.prototype);695StringToken.prototype.tokenType = "STRING";696StringToken.prototype.toString = function() {697 return '"' + escapeString(this.value) + '"';698}699function URLToken(val) {700 this.value = val;701}702URLToken.prototype = Object.create(StringValuedToken.prototype);703URLToken.prototype.tokenType = "URL";704URLToken.prototype.toString = function() { return "URL("+this.value+")"; }705URLToken.prototype.toSource = function() {706 return 'url("' + escapeString(this.value) + '")';707}708function NumberToken() {709 this.value = null;710 this.type = "integer";711 this.repr = "";712}713NumberToken.prototype = Object.create(CSSParserToken.prototype);714NumberToken.prototype.tokenType = "NUMBER";715NumberToken.prototype.toString = function() {716 if(this.type == "integer")717 return "INT("+this.value+")";718 return "NUMBER("+this.value+")";719}720NumberToken.prototype.toJSON = function() {721 var json = this.constructor.prototype.constructor.prototype.toJSON.call(this);722 json.value = this.value;723 json.type = this.type;724 json.repr = this.repr;725 return json;726}727NumberToken.prototype.toSource = function() { return this.repr; };728function PercentageToken() {729 this.value = null;730 this.repr = "";731}732PercentageToken.prototype = Object.create(CSSParserToken.prototype);733PercentageToken.prototype.tokenType = "PERCENTAGE";734PercentageToken.prototype.toString = function() { return "PERCENTAGE("+this.value+")"; }735PercentageToken.prototype.toJSON = function() {736 var json = this.constructor.prototype.constructor.prototype.toJSON.call(this);737 json.value = this.value;738 json.repr = this.repr;739 return json;740}741PercentageToken.prototype.toSource = function() { return this.repr + "%"; }742function DimensionToken() {...

Full Screen

Full Screen

tokenizer.js

Source:tokenizer.js Github

copy

Full Screen

...300 else if(code == 0x2e) {301 if(digit(next())) consume() && currtoken.append([0x2e,code]) && switchto('number-fraction');302 else emit() && switchto('data') && reconsume();303 }304 else if(code == 0x25) emit(new PercentageToken(currtoken)) && switchto('data');305 else if(code == 0x45 || code == 0x65) {306 if(digit(next())) consume() && currtoken.append([0x25,code]) && switchto('sci-notation');307 else if((next(1) == 0x2b || next(1) == 0x2d) && digit(next(2))) currtoken.append([0x25,next(1),next(2)]) && consume(2) && switchto('sci-notation');308 else create(new DimensionToken(currtoken,code)) && switchto('dimension');309 }310 else if(code == 0x2d) {311 if(namestartchar(next())) consume() && create(new DimensionToken(currtoken,[0x2d,code])) && switchto('dimension');312 else if(next(1) == 0x5c && badescape(next(2))) parseerror() && emit() && switchto('data') && reconsume();313 else if(next(1) == 0x5c) consume() && create(new DimensionToken(currtoken, [0x2d,consumeEscape()])) && switchto('dimension');314 else emit() && switchto('data') && reconsume();315 }316 else if(namestartchar(code)) create(new DimensionToken(currtoken, code)) && switchto('dimension');317 else if(code == 0x5c) {318 if(badescape(next)) parseerror() && emit() && switchto('data') && reconsume();319 else create(new DimensionToken(currtoken,consumeEscape)) && switchto('dimension');320 }321 else emit() && switchto('data') && reconsume();322 break;323 case "number-fraction":324 currtoken.type = "number";325 if(digit(code)) currtoken.append(code);326 else if(code == 0x25) emit(new PercentageToken(currtoken)) && switchto('data');327 else if(code == 0x45 || code == 0x65) {328 if(digit(next())) consume() && currtoken.append([0x65,code]) && switchto('sci-notation');329 else if((next(1) == 0x2b || next(1) == 0x2d) && digit(next(2))) currtoken.append([0x65,next(1),next(2)]) && consume(2) && switchto('sci-notation');330 else create(new DimensionToken(currtoken,code)) && switchto('dimension');331 }332 else if(code == 0x2d) {333 if(namestartchar(next())) consume() && create(new DimensionToken(currtoken,[0x2d,code])) && switchto('dimension');334 else if(next(1) == 0x5c && badescape(next(2))) parseerror() && emit() && switchto('data') && reconsume();335 else if(next(1) == 0x5c) consume() && create(new DimensionToken(currtoken, [0x2d,consumeEscape()])) && switchto('dimension');336 else emit() && switchto('data') && reconsume();337 }338 else if(namestartchar(code)) create(new DimensionToken(currtoken, code)) && switchto('dimension');339 else if(code == 0x5c) {340 if(badescape(next)) parseerror() && emit() && switchto('data') && reconsume();341 else create(new DimensionToken(currtoken,consumeEscape())) && switchto('dimension');342 }343 else emit() && switchto('data') && reconsume();344 break;345 case "dimension":346 if(namechar(code)) currtoken.append(code);347 else if(code == 0x5c) {348 if(badescape(next())) parseerror() && emit() && switchto('data') && reconsume();349 else currtoken.append(consumeEscape());350 }351 else emit() && switchto('data') && reconsume();352 break;353 case "sci-notation":354 currtoken.type = "number";355 if(digit(code)) currtoken.append(code);356 else emit() && switchto('data') && reconsume();357 break;358 case "url":359 if(eof()) parseerror() && emit(new BadURLToken) && switchto('data');360 else if(code == 0x22) switchto('url-double-quote');361 else if(code == 0x27) switchto('url-single-quote');362 else if(code == 0x29) emit(new URLToken) && switchto('data');363 else if(whitespace(code)) donothing();364 else switchto('url-unquoted') && reconsume();365 break;366 case "url-double-quote":367 if(! (currtoken instanceof URLToken)) create(new URLToken);368 if(eof()) parseerror() && emit(new BadURLToken) && switchto('data');369 else if(code == 0x22) switchto('url-end');370 else if(newline(code)) parseerror() && switchto('bad-url');371 else if(code == 0x5c) {372 if(newline(next())) consume();373 else if(badescape(next())) parseerror() && emit(new BadURLToken) && switchto('data') && reconsume();374 else currtoken.append(consumeEscape());375 }376 else currtoken.append(code);377 break;378 case "url-single-quote":379 if(! (currtoken instanceof URLToken)) create(new URLToken);380 if(eof()) parseerror() && emit(new BadURLToken) && switchto('data');381 else if(code == 0x27) switchto('url-end');382 else if(newline(code)) parseerror() && switchto('bad-url');383 else if(code == 0x5c) {384 if(newline(next())) consume();385 else if(badescape(next())) parseerror() && emit(new BadURLToken) && switchto('data') && reconsume();386 else currtoken.append(consumeEscape());387 }388 else currtoken.append(code);389 break;390 case "url-end":391 if(eof()) parseerror() && emit(new BadURLToken) && switchto('data');392 else if(whitespace(code)) donothing();393 else if(code == 0x29) emit() && switchto('data');394 else parseerror() && switchto('bad-url') && reconsume();395 break;396 case "url-unquoted":397 if(! (currtoken instanceof URLToken)) create(new URLToken);398 if(eof()) parseerror() && emit(new BadURLToken) && switchto('data');399 else if(whitespace(code)) switchto('url-end');400 else if(code == 0x29) emit() && switchto('data');401 else if(code == 0x22 || code == 0x27 || code == 0x28 || nonprintable(code)) parseerror() && switchto('bad-url');402 else if(code == 0x5c) {403 if(badescape(next())) parseerror() && switchto('bad-url');404 else currtoken.append(consumeEscape());405 }406 else currtoken.append(code);407 break;408 case "bad-url":409 if(eof()) parseerror() && emit(new BadURLToken) && switchto('data');410 else if(code == 0x29) emit(new BadURLToken) && switchto('data');411 else if(code == 0x5c) {412 if(badescape(next())) donothing();413 else consumeEscape();414 }415 else donothing();416 break;417 case "unicode-range":418 // We already know that the current code is a hexdigit.419 var start = [code], end = [code];420 for(var total = 1; total < 6; total++) {421 if(hexdigit(next())) {422 consume();423 start.push(code);424 end.push(code);425 }426 else break;427 }428 if(next() == 0x3f) {429 for(;total < 6; total++) {430 if(next() == 0x3f) {431 consume();432 start.push("0".charCodeAt(0));433 end.push("f".charCodeAt(0));434 }435 else break;436 }437 emit(new UnicodeRangeToken(start,end)) && switchto('data');438 }439 else if(next(1) == 0x2d && hexdigit(next(2))) {440 consume();441 consume();442 end = [code];443 for(var total = 1; total < 6; total++) {444 if(hexdigit(next())) {445 consume();446 end.push(code);447 }448 else break;449 }450 emit(new UnicodeRangeToken(start,end)) && switchto('data');451 }452 else emit(new UnicodeRangeToken(start)) && switchto('data');453 break;454 default:455 catchfire("Unknown state '" + state + "'");456 }457 }458}459function stringFromCodeArray(arr) {460 return String.fromCharCode.apply(null,arr.filter(function(e){return e;}));461}462function CSSParserToken(options) { return this; }463CSSParserToken.prototype.finish = function() { return this; }464CSSParserToken.prototype.toString = function() { return this.tokenType; }465CSSParserToken.prototype.toSourceString = CSSParserToken.prototype.toString;466CSSParserToken.prototype.toJSON = function() { return this.toString(); }467function BadStringToken() { return this; }468BadStringToken.prototype = new CSSParserToken;469BadStringToken.prototype.tokenType = "BADSTRING";470function BadURLToken() { return this; }471BadURLToken.prototype = new CSSParserToken;472BadURLToken.prototype.tokenType = "BADURL";473function WhitespaceToken() { return this; }474WhitespaceToken.prototype = new CSSParserToken;475WhitespaceToken.prototype.tokenType = "WHITESPACE";476WhitespaceToken.prototype.toString = function() { return "WS"; }477WhitespaceToken.prototype.toSourceString = function() { return " "; }478function CDOToken() { return this; }479CDOToken.prototype = new CSSParserToken;480CDOToken.prototype.tokenType = "CDO";481function CDCToken() { return this; }482CDCToken.prototype = new CSSParserToken;483CDCToken.prototype.tokenType = "CDC";484function ColonToken() { return this; }485ColonToken.prototype = new CSSParserToken;486ColonToken.prototype.tokenType = ":";487function SemicolonToken() { return this; }488SemicolonToken.prototype = new CSSParserToken;489SemicolonToken.prototype.tokenType = ";";490function OpenCurlyToken() { return this; }491OpenCurlyToken.prototype = new CSSParserToken;492OpenCurlyToken.prototype.tokenType = "{";493function CloseCurlyToken() { return this; }494CloseCurlyToken.prototype = new CSSParserToken;495CloseCurlyToken.prototype.tokenType = "}";496function OpenSquareToken() { return this; }497OpenSquareToken.prototype = new CSSParserToken;498OpenSquareToken.prototype.tokenType = "[";499function CloseSquareToken() { return this; }500CloseSquareToken.prototype = new CSSParserToken;501CloseSquareToken.prototype.tokenType = "]";502function OpenParenToken() { return this; }503OpenParenToken.prototype = new CSSParserToken;504OpenParenToken.prototype.tokenType = "(";505function CloseParenToken() { return this; }506CloseParenToken.prototype = new CSSParserToken;507CloseParenToken.prototype.tokenType = ")";508function EOFToken() { return this; }509EOFToken.prototype = new CSSParserToken;510EOFToken.prototype.tokenType = "EOF";511function DelimToken(code) {512 this.value = String.fromCharCode(code);513 return this;514}515DelimToken.prototype = new CSSParserToken;516DelimToken.prototype.tokenType = "DELIM";517DelimToken.prototype.toString = function() { return "DELIM("+this.value+")"; }518DelimToken.prototype.toSourceString = function() { return this.value; }519function StringValuedToken() { return this; }520StringValuedToken.prototype = new CSSParserToken;521StringValuedToken.prototype.append = function(val) {522 if(val instanceof Array) {523 for(var i = 0; i < val.length; i++) {524 this.value.push(val[i]);525 }526 } else {527 this.value.push(val);528 }529 return true;530}531StringValuedToken.prototype.finish = function() {532 this.value = this.valueAsString();533 return this;534}535StringValuedToken.prototype.ASCIImatch = function(str) {536 return this.valueAsString().toLowerCase() == str.toLowerCase();537}538StringValuedToken.prototype.valueAsString = function() {539 if(typeof this.value == 'string') return this.value;540 return stringFromCodeArray(this.value);541}542StringValuedToken.prototype.valueAsCodes = function() {543 if(typeof this.value == 'string') {544 var ret = [];545 for(var i = 0; i < this.value.length; i++)546 ret.push(this.value.charCodeAt(i));547 return ret;548 }549 return this.value.filter(function(e){return e;});550}551function IdentifierToken(val) {552 this.value = [];553 this.append(val);554}555IdentifierToken.prototype = new StringValuedToken;556IdentifierToken.prototype.tokenType = "IDENT";557IdentifierToken.prototype.toString = function() { return "IDENT("+this.value+")"; }558IdentifierToken.prototype.toSourceString = function() { return this.value; }559function FunctionToken(val) {560 // These are always constructed by passing an IdentifierToken561 this.value = val.finish().value;562}563FunctionToken.prototype = new StringValuedToken;564FunctionToken.prototype.tokenType = "FUNCTION";565FunctionToken.prototype.toString = function() { return "FUNCTION("+this.value+")"; }566FunctionToken.prototype.toSourceString = function() { return this.value; }567function AtKeywordToken(val) {568 this.value = [];569 this.append(val);570}571AtKeywordToken.prototype = new StringValuedToken;572AtKeywordToken.prototype.tokenType = "AT-KEYWORD";573AtKeywordToken.prototype.toString = function() { return "AT("+this.value+")"; }574AtKeywordToken.prototype.toSourceString = function() { return "@"+this.value; }575function HashToken(val) {576 this.value = [];577 this.append(val);578}579HashToken.prototype = new StringValuedToken;580HashToken.prototype.tokenType = "HASH";581HashToken.prototype.toString = function() { return "HASH("+this.value+")"; }582HashToken.prototype.toSourceString = function() { return "#"+this.value; }583function StringToken(val) {584 this.value = [];585 this.append(val);586}587StringToken.prototype = new StringValuedToken;588StringToken.prototype.tokenType = "STRING";589StringToken.prototype.toString = function() { return "\""+this.value+"\""; }590StringToken.prototype.toSourceString = StringToken.prototype.toString;591function URLToken(val) {592 this.value = [];593 this.append(val);594}595URLToken.prototype = new StringValuedToken;596URLToken.prototype.tokenType = "URL";597URLToken.prototype.toString = function() { return "URL("+this.value+")"; }598URLToken.prototype.toSourceString = function() { return "url('"+this.value+"')"; }599function NumberToken(val) {600 this.value = [];601 this.append(val);602 this.type = "integer";603}604NumberToken.prototype = new StringValuedToken;605NumberToken.prototype.tokenType = "NUMBER";606NumberToken.prototype.toString = function() {607 if(this.type == "integer")608 return "INT("+this.value+")";609 return "NUMBER("+this.value+")";610}611NumberToken.prototype.toSourceString = function() {612 if(this.type == "integer")613 return this.value;614 return this.value;615}616NumberToken.prototype.finish = function() {617 this.repr = this.valueAsString();618 this.value = this.repr * 1;619 if(Math.abs(this.value) % 1 != 0) this.type = "number";620 return this;621}622function PercentageToken(val) {623 // These are always created by passing a NumberToken as val624 val.finish();625 this.value = val.value;626 this.repr = val.repr;627}628PercentageToken.prototype = new CSSParserToken;629PercentageToken.prototype.tokenType = "PERCENTAGE";630PercentageToken.prototype.toString = function() { return "PERCENTAGE("+this.value+")"; }631PercentageToken.prototype.toSourceString = function() { return this.value+'%'; }632function DimensionToken(val,unit) {633 // These are always created by passing a NumberToken as the val634 val.finish();635 this.num = val.value;636 this.unit = [];...

Full Screen

Full Screen

css-tokenizer.js

Source:css-tokenizer.js Github

copy

Full Screen

...294 else if(code == 0x2e) {295 if(digit(next())) consume() && currtoken.append([0x2e,code]) && switchto('number-fraction');296 else emit() && switchto('data') && reconsume();297 }298 else if(code == 0x25) emit(new PercentageToken(currtoken)) && switchto('data') && reconsume();299 else if(code == 0x45 || code == 0x65) {300 if(!options.scientificNotation) create(new DimensionToken(currtoken,code)) && switchto('dimension');301 else if(digit(next())) consume() && currtoken.append([0x25,code]) && switchto('sci-notation');302 else if((next(1) == 0x2b || next(1) == 0x2d) && digit(next(2))) currtoken.append([0x25,next(1),next(2)]) && consume(2) && switchto('sci-notation');303 else create(new DimensionToken(currtoken,code)) && switchto('dimension');304 }305 else if(code == 0x2d) {306 if(namestartchar(next())) consume() && create(new DimensionToken(currtoken,[0x2d,code])) && switchto('dimension');307 else if(next(1) == 0x5c && badescape(next(2))) parseerror() && emit() && switchto('data') && reconsume();308 else if(next(1) == 0x5c) consume() && create(new DimensionToken(currtoken, [0x2d,consumeEscape()])) && switchto('dimension');309 else emit() && switchto('data') && reconsume();310 }311 else if(namestartchar(code)) create(new DimensionToken(currtoken, code)) && switchto('dimension');312 else if(code == 0x5c) {313 if(badescape(next)) emit() && switchto('data') && reconsume();314 else create(new DimensionToken(currtoken,consumeEscape)) && switchto('dimension');315 }316 else emit() && switchto('data') && reconsume();317 break;318 case "number-fraction":319 currtoken.type = "number";320 if(digit(code)) currtoken.append(code);321 else if(code == 0x2e) emit() && switchto('data') && reconsume();322 else if(code == 0x25) emit(new PercentageToken(currtoken)) && switchto('data') && reconsume();323 else if(code == 0x45 || code == 0x65) {324 if(!options.scientificNotation) create(new DimensionToken(currtoken,code)) && switchto('dimension');325 else if(digit(next())) consume() && currtoken.append([0x25,code]) && switchto('sci-notation');326 else if((next(1) == 0x2b || next(1) == 0x2d) && digit(next(2))) currtoken.append([0x25,next(1),next(2)]) && consume(2) && switchto('sci-notation');327 else create(new DimensionToken(currtoken,code)) && switchto('dimension');328 }329 else if(code == 0x2d) {330 if(namestartchar(next())) consume() && create(new DimensionToken(currtoken,[0x2d,code])) && switchto('dimension');331 else if(next(1) == 0x5c && badescape(next(2))) parseerror() && emit() && switchto('data') && reconsume();332 else if(next(1) == 0x5c) consume() && create(new DimensionToken(currtoken, [0x2d,consumeEscape()])) && switchto('dimension');333 else emit() && switchto('data') && reconsume();334 }335 else if(namestartchar(code)) create(new DimensionToken(currtoken, code)) && switchto('dimension');336 else if(code == 0x5c) {337 if(badescape(next)) emit() && switchto('data') && reconsume();338 else create(new DimensionToken(currtoken,consumeEscape)) && switchto('dimension');339 }340 else emit() && switchto('data') && reconsume();341 break;342 case "dimension":343 if(namechar(code)) currtoken.append(code);344 else if(code == 0x5c) {345 if(badescape(next())) parseerror() && emit() && switchto('data') && reconsume();346 else currtoken.append(consumeEscape());347 }348 else emit() && switchto('data') && reconsume();349 break;350 case "sci-notation":351 if(digit(code)) currtoken.append(code);352 else emit() && switchto('data') && reconsume();353 break;354 case "url":355 if(code == 0x22) switchto('url-double-quote');356 else if(code == 0x27) switchto('url-single-quote');357 else if(code == 0x29) emit(new URLToken) && switchto('data');358 else if(whitespace(code)) donothing();359 else switchto('url-unquoted') && reconsume();360 break;361 case "url-double-quote":362 if(currtoken == undefined) create(new URLToken);363 if(code == 0x22) switchto('url-end');364 else if(newline(code)) parseerror() && switchto('bad-url');365 else if(code == 0x5c) {366 if(newline(next())) consume();367 else if(badescape(next())) parseerror() && emit(new BadURLToken) && switchto('data') && reconsume();368 else currtoken.append(consumeEscape());369 }370 else currtoken.append(code);371 break;372 case "url-single-quote":373 if(currtoken == undefined) create(new URLToken);374 if(code == 0x27) switchto('url-end');375 else if(newline(code)) parseerror() && switchto('bad-url');376 else if(code == 0x5c) {377 if(newline(next())) consume();378 else if(badescape(next())) parseerror() && emit(new BadURLToken) && switchto('data') && reconsume();379 else currtoken.append(consumeEscape());380 }381 else currtoken.append(code);382 break;383 case "url-end":384 if(whitespace(code)) donothing();385 else if(code == 0x29) emit() && switchto('data');386 else parseerror() && switchto('bad-url') && reconsume();387 break;388 case "url-unquoted":389 if(currtoken == undefined) create(new URLToken);390 if(whitespace(code)) switchto('url-end');391 else if(code == 0x29) emit() && switchto('data');392 else if(code == 0x22 || code == 0x27 || code == 0x28 || nonprintable(code)) parseerror() && switchto('bad-url');393 else if(code == 0x5c) {394 if(badescape(next())) parseerror() && switchto('bad-url');395 else currtoken.append(consumeEscape());396 }397 else currtoken.append(code);398 break;399 case "bad-url":400 if(code == 0x29) emit(new BadURLToken) && switchto('data');401 else if(code == 0x5c) {402 if(badescape(next())) donothing();403 else consumeEscape()404 }405 else donothing();406 break;407 case "unicode-range":408 // We already know that the current code is a hexdigit.409 var start = [code], end = [code];410 for(var total = 1; total < 6; total++) {411 if(hexdigit(next())) {412 consume();413 start.push(code);414 end.push(code);415 }416 else break;417 }418 if(next() == 0x3f) {419 for(;total < 6; total++) {420 if(next() == 0x3f) {421 consume();422 start.push("0".charCodeAt(0));423 end.push("f".charCodeAt(0));424 }425 else break;426 }427 emit(new UnicodeRangeToken(start,end)) && switchto('data');428 }429 else if(next(1) == 0x2d && hexdigit(next(2))) {430 consume();431 consume();432 end = [code];433 for(var total = 1; total < 6; total++) {434 if(hexdigit(next())) {435 consume();436 end.push(code);437 }438 else break;439 }440 emit(new UnicodeRangeToken(start,end)) && switchto('data');441 }442 else emit(new UnicodeRangeToken(start)) && switchto('data');443 break;444 default:445 console.log("Unknown state '" + state + "'");446 }447 }448}449function stringFromCodeArray(arr) {450 return String.fromCharCode.apply(null,arr.filter(function(e){return e;}));451}452function CSSParserToken(options) { return this; }453CSSParserToken.prototype.finish = function() { return this; }454CSSParserToken.prototype.toString = function() { return this.tokenType; }455CSSParserToken.prototype.toJSON = function() { return this.toString(); }456function BadStringToken() { return this; }457BadStringToken.prototype = new CSSParserToken;458BadStringToken.prototype.tokenType = "BADSTRING";459function BadURLToken() { return this; }460BadURLToken.prototype = new CSSParserToken;461BadURLToken.prototype.tokenType = "BADURL";462function WhitespaceToken() { return this; }463WhitespaceToken.prototype = new CSSParserToken;464WhitespaceToken.prototype.tokenType = "WHITESPACE";465WhitespaceToken.prototype.toString = function() { return "WS"; }466function CDOToken() { return this; }467CDOToken.prototype = new CSSParserToken;468CDOToken.prototype.tokenType = "CDO";469function CDCToken() { return this; }470CDCToken.prototype = new CSSParserToken;471CDCToken.prototype.tokenType = "CDC";472function ColonToken() { return this; }473ColonToken.prototype = new CSSParserToken;474ColonToken.prototype.tokenType = ":";475function SemicolonToken() { return this; }476SemicolonToken.prototype = new CSSParserToken;477SemicolonToken.prototype.tokenType = ";";478function OpenCurlyToken() { return this; }479OpenCurlyToken.prototype = new CSSParserToken;480OpenCurlyToken.prototype.tokenType = "{";481function CloseCurlyToken() { return this; }482CloseCurlyToken.prototype = new CSSParserToken;483CloseCurlyToken.prototype.tokenType = "}";484function OpenSquareToken() { return this; }485OpenSquareToken.prototype = new CSSParserToken;486OpenSquareToken.prototype.tokenType = "[";487function CloseSquareToken() { return this; }488CloseSquareToken.prototype = new CSSParserToken;489CloseSquareToken.prototype.tokenType = "]";490function OpenParenToken() { return this; }491OpenParenToken.prototype = new CSSParserToken;492OpenParenToken.prototype.tokenType = "(";493function CloseParenToken() { return this; }494CloseParenToken.prototype = new CSSParserToken;495CloseParenToken.prototype.tokenType = ")";496function EOFToken() { return this; }497EOFToken.prototype = new CSSParserToken;498EOFToken.prototype.tokenType = "EOF";499function DelimToken(code) {500 this.value = String.fromCharCode(code);501 return this;502}503DelimToken.prototype = new CSSParserToken;504DelimToken.prototype.tokenType = "DELIM";505DelimToken.prototype.toString = function() { return "DELIM("+this.value+")"; }506function StringValuedToken() { return this; }507StringValuedToken.prototype = new CSSParserToken;508StringValuedToken.prototype.append = function(val) {509 if(val instanceof Array) {510 for(var i = 0; i < val.length; i++) {511 this.value.push(val[i]);512 }513 } else {514 this.value.push(val);515 }516 return true;517}518StringValuedToken.prototype.finish = function() {519 this.value = stringFromCodeArray(this.value);520 return this;521}522function IdentifierToken(val) {523 this.value = [];524 this.append(val);525}526IdentifierToken.prototype = new StringValuedToken;527IdentifierToken.prototype.tokenType = "IDENT";528IdentifierToken.prototype.toString = function() { return "IDENT("+this.value+")"; }529function FunctionToken(val) {530 // These are always constructed by passing an IdentifierToken531 this.value = val.finish().value;532}533FunctionToken.prototype = new CSSParserToken;534FunctionToken.prototype.tokenType = "FUNCTION";535FunctionToken.prototype.toString = function() { return "FUNCTION("+this.value+")"; }536function AtKeywordToken(val) {537 this.value = [];538 this.append(val);539}540AtKeywordToken.prototype = new StringValuedToken;541AtKeywordToken.prototype.tokenType = "AT-KEYWORD";542AtKeywordToken.prototype.toString = function() { return "AT("+this.value+")"; }543function HashToken(val) {544 this.value = [];545 this.append(val);546}547HashToken.prototype = new StringValuedToken;548HashToken.prototype.tokenType = "HASH";549HashToken.prototype.toString = function() { return "HASH("+this.value+")"; }550function StringToken(val) {551 this.value = [];552 this.append(val);553}554StringToken.prototype = new StringValuedToken;555StringToken.prototype.tokenType = "STRING";556StringToken.prototype.toString = function() { return "\""+this.value+"\""; }557function URLToken(val) {558 this.value = [];559 this.append(val);560}561URLToken.prototype = new StringValuedToken;562URLToken.prototype.tokenType = "URL";563URLToken.prototype.toString = function() { return "URL("+this.value+")"; }564function NumberToken(val) {565 this.value = [];566 this.append(val);567 this.type = "integer";568}569NumberToken.prototype = new StringValuedToken;570NumberToken.prototype.tokenType = "NUMBER";571NumberToken.prototype.toString = function() {572 if(this.type == "integer")573 return "INT("+this.value+")";574 return "NUMBER("+this.value+")";575}576NumberToken.prototype.finish = function() {577 this.repr = stringFromCodeArray(this.value);578 this.value = this.repr * 1;579 if(Math.abs(this.value) % 1 != 0) this.type = "number";580 return this;581}582function PercentageToken(val) {583 // These are always created by passing a NumberToken as val584 val.finish();585 this.value = val.value;586 this.repr = val.repr;587}588PercentageToken.prototype = new CSSParserToken;589PercentageToken.prototype.tokenType = "PERCENTAGE";590PercentageToken.prototype.toString = function() { return "PERCENTAGE("+this.value+")"; }591function DimensionToken(val,unit) {592 // These are always created by passing a NumberToken as the val593 val.finish();594 this.num = val.value;595 this.unit = [];596 this.repr = val.repr;...

Full Screen

Full Screen

rdHighchartsFormatters.js

Source:rdHighchartsFormatters.js Github

copy

Full Screen

1//"use strict";2if (window.LogiXML === undefined) {3 window.LogiXML = {};4}5LogiXML.HighchartsFormatters = {6 setFormatters: function (chartOptions) {7 var i, length, axis, series;8 //date in categories should be date object, not timestamp9 if (chartOptions.xAxis) {10 LogiXML.HighchartsFormatters.checkForDateInAxises(chartOptions.xAxis);11 for (var j = 0; j < chartOptions.xAxis.length; j++) {12 var xAxis = chartOptions.xAxis[j];13 if (xAxis.title && xAxis.title.format)14 xAxis.title.text = LogiXML.Formatter.format(xAxis.title.text, xAxis.title.format);15 }16 }17 if (chartOptions.yAxis) {18 LogiXML.HighchartsFormatters.checkForDateInAxises(chartOptions.yAxis);19 for (var j = 0; j < chartOptions.yAxis.length; j++) {20 var yAxis = chartOptions.yAxis[j];21 if (yAxis.title && yAxis.title.format)22 yAxis.title.text = LogiXML.Formatter.format(yAxis.title.text, yAxis.title.format);23 this.checkForOnOffInAxis(yAxis);24 }25 }26 if (chartOptions.xAxis) {27 i = 0; length = chartOptions.xAxis.length;28 for (; i < length; i++) {29 axis = chartOptions.xAxis[i];30 if (axis.labels && axis.labels.formatter == 'labelFormatter') {31 axis.labels.formatter = LogiXML.HighchartsFormatters.labelFormatter;32 }33 this.checkForOnOffInAxis(axis);34 }35 }36 if (chartOptions.yAxis) {37 i = 0; length = chartOptions.yAxis.length;38 for (; i < length; i++) {39 axis = chartOptions.yAxis[i];40 if (axis.labels && axis.labels.formatter == 'labelFormatter') {41 axis.labels.formatter = LogiXML.HighchartsFormatters.labelFormatter;42 }43 if (axis.stackLabels && axis.stackLabels.formatter == 'stackLabelFormatter') {44 var format = axis.stackLabels.format;45 axis.stackLabels.formatter = LogiXML.HighchartsFormatters.stackLabelFormatter;46 axis.stackLabels._format = axis.stackLabels.format;47 axis.stackLabels.format = null;48 }49 }50 }51 if (chartOptions.legend) {52 if (chartOptions.legend && chartOptions.legend.labelFormatter == 'legendLabelFormatter') {53 chartOptions.legend.labelFormatter = LogiXML.HighchartsFormatters.legendLabelFormatter;54 chartOptions.legend._format = chartOptions.legend.labelFormat;55 chartOptions.legend.labelFormat = null;56 }57 }58 var useFormat = function (data) {59 if (data.dataLabels && data.dataLabels.formatter) {60 switch (data.dataLabels.formatter) {61 case "dataLabelFormatter":62 data.dataLabels.formatter = LogiXML.HighchartsFormatters.dataLabelFormatter;63 data.dataLabels._format = series.dataLabels.format;64 data.dataLabels.format = null;65 break;66 case "sideLabelFormatter":67 data.dataLabels.formatter = LogiXML.HighchartsFormatters.sideLabelFormatter;68 data.dataLabels._format = series.dataLabels.format;69 data.dataLabels._showPointName = series.dataLabels.showPointName;70 data.dataLabels.format = null;71 //data.dataLabels.useHTML = true;72 break;73 }74 }75 };76 if (chartOptions.series) {77 i = 0; length = chartOptions.series.length;78 for (; i < length; i++) {79 series = chartOptions.series[i];80 if (!series.data)81 continue;82 for (var f = 0; f < series.data.length; f++) {83 var dataPoint = series.data[f];84 if (dataPoint != null && dataPoint != undefined) {85 useFormat(dataPoint);86 }87 }88 useFormat(series);89 }90 }91 },92 checkForOnOffInAxis: function (axis) {93 if (axis.labels && axis.labels.format && (axis.labels.format == "Yes/No" || axis.labels.format == "On/Off" || axis.labels.format == "True/False")) {94 axis.hasBinaryValues = true;95 }96 },97 checkForDateInAxises: function (axises) {98 if (!axises || !LogiXML.HighchartsFormatters.isArray(axises)) {99 return;100 }101 var i = 0, length = axises.length;102 for (; i < length; i++) {103 if (axises[i].labels && axises[i].type == 'datetime' && axises[i].categories) {104 LogiXML.HighchartsFormatters.convertTimeStampToDate(axises[i].categories);105 }106 }107 },108 convertTimeStampToDate: function (dataArray) {109 if (!dataArray && dataArray.length == 0) {110 return;111 }112 var length = dataArray.length;113 for (var i = 0; i < length; i++) {114 dataArray[i] = new Date(dataArray[i]);115 }116 },117 isArray: function (obj) {118 return Object.prototype.toString.call(obj) === '[object Array]';119 },120 isNumber: function (n) {121 return typeof n === 'number';122 },123 splat: function (obj) {124 return LogiXML.HighchartsFormatters.isArray(obj) ? obj : [obj];125 },126 pick: function () {127 var args = arguments,128 i,129 arg,130 length = args.length;131 for (i = 0; i < length; i++) {132 arg = args[i];133 if (typeof arg !== 'undefined' && arg !== null) {134 return arg;135 }136 }137 },138 inArray: function (item, arr) {139 var len,140 i = 0;141 if (arr) {142 len = arr.length;143 for (; i < len; i++) {144 if (arr[i] === item) {145 return i;146 }147 }148 }149 // TODO?: return arr.indexOf(item)150 return -1;151 },152 tooltipFormatter: function (tooltip) {153 var quicktip = this.point.series.quicktip,154 i, length, html, tooltipData;155 if (this.point.isGroup)156 quicktip = this.point.style.quicktip;157 //remove quicktip for calculated columns158 if (this.point.isIntermediateSum || this.point.isSum) {159 this.point.qt = [];160 for (var i = 0; i < quicktip.rows.length; i++) {161 this.point.qt.push(LogiXML.HighchartsFormatters.format(this.point.y, quicktip.rows[i].format));162 }163 }164 if (!quicktip) {165 if (this.point.series.chart.autoQuicktip === false) {166 return false;167 }168 var items = this.points || LogiXML.HighchartsFormatters.splat(this),169 series = items[0].series,170 s;171 if (!this.point.series.chart.tooltip.style) {172 this.point.series.chart.tooltip.style = {};173 }174 // build the header175 //s = [series.tooltipHeaderFormatter(items[0])];176 s = [LogiXML.HighchartsFormatters.tooltipAutoHeaderFormatter(items[0])];177 // build the values178 i = 0; length = items.length;179 for (; i < length; i++) {180 series = items[i].series;181 if (series.tooltipFormatter) {182 s.push(series.tooltipFormatter(items[i]));183 } else {184 //s.push(items[i].point.tooltipFormatter(series.tooltipOptions.pointFormat));185 s.push(LogiXML.HighchartsFormatters.tooltipAutoPointFormatter(this.point));186 }187 };188 // footer189 s.push(tooltip.options.footerFormat || '');190 html = '<div class="rdquicktip-content"><div class="body">';191 html += s.join('').replace(/style\=\"[^\"]*\"/g, '').replace(/<span >Series [0-9]+<\/span>(<br\/>|:)/g, '').replace(/<br\/><span .+ Series [0-9:]+(<br\/> |: )/g, '<br/>').replace(/<span .+ Series [0-9]+<\/span><br\/>/g, '');192 html += '</div></div>';193 return html;194 }195 if (!this.point.series.chart.tooltip.style) {196 this.point.series.chart.tooltip.style = {};197 }198 this.point.series.chart.tooltip.style.padding = 0;199 html = '<div class="rdquicktip-content">';200 if (quicktip.title) {201 html += '<div class="header">' + quicktip.title + '</div>';202 }203 html += '<div class="body">';204 if (quicktip.descr) {205 html += '<p>' + quicktip.descr + '</p>';206 }207 //REPDEV-23818208 var defaultFormat = 'Percent';209 var percentageToken = '@Chart.rdStackedChartPercentage~';210 if (this.point.percentage != undefined && html.indexOf(percentageToken) > -1) {211 var formatedValue = LogiXML.Formatter.format(this.point.percentage / 100, defaultFormat);212 html = html.replace(new RegExp(percentageToken, 'g'), formatedValue);213 }214 if (quicktip.rows && quicktip.rows.length > 0) {215 html += '<table class="rdquicktip-table">';216 i = 0;217 length = quicktip.rows.length;218 for (; i < length; i++) {219 var fragment = '<tr><td>' + quicktip.rows[i].caption + '</td><td>' + LogiXML.decodeHtml(quicktip.rows[i].value || "", quicktip.rows[i].format == 'HTML') + '</td></tr>';220 //REPDEV-23818221 if (this.point.percentage != undefined && fragment.indexOf(percentageToken) > -1) {222 var formatedValue = LogiXML.Formatter.format(this.point.percentage / 100, quicktip.rows[i].format || defaultFormat);223 fragment = fragment.replace(new RegExp(percentageToken, 'g'), formatedValue);224 }225 html += fragment;226 }227 html += '</table>';228 }229 if (this.point.qt && this.point.qt.length > 0) {230 i = 0;231 length = this.point.qt.length;232 for (; i < length; i++) {233 var qtPointValue = this.point.qt[i];234 235 if (qtPointValue == "LogiHideEle") {236 return '';237 }238 //REPDEV-20468 fix issue with no returned data for tooltip239 if (!qtPointValue) {240 qtPointValue = '';241 }242 if (qtPointValue.startsWith('$0')) { // 23595 - we need to escape $0 sign243 qtPointValue = '$' + qtPointValue;244 }245 html = html.replace(new RegExp('\\{' + i + '\\}', 'g'), LogiXML.decodeHtml(qtPointValue, quicktip.rows && quicktip.rows[i] && quicktip.rows[i].format && quicktip.rows[i].format == 'HTML'));246 }247 }248 html += '</div></div></div>';249 return html;250 },251 tooltipAutoHeaderFormatter: function (point) {252 var series = point.series,253 tooltipOptions = series.tooltipOptions,254 dateTimeLabelFormats = tooltipOptions.dateTimeLabelFormats,255 xDateFormat = tooltipOptions.xDateFormat || dateTimeLabelFormats.year, // #2546256 xAxis = series.xAxis,257 isDateTime = xAxis && xAxis.options.type === 'datetime',258 isCategories = xAxis && xAxis.options.type === 'category',259 headerFormat = tooltipOptions.headerFormat,260 closestPointRange = xAxis && xAxis.closestPointRange,261 n;262 // Guess the best date format based on the closest point distance (#568)263 if (isDateTime && !xDateFormat) {264 if (closestPointRange) {265 for (n in timeUnits) {266 if (timeUnits[n] >= closestPointRange) {267 xDateFormat = dateTimeLabelFormats[n];268 break;269 }270 }271 } else {272 xDateFormat = dateTimeLabelFormats.day;273 }274 }275 if (xAxis && xAxis.options && xAxis.options.labels && xAxis.options.labels.format) {276 if (isCategories && point.point) {277 headerFormat = headerFormat.replace('{point.key}', LogiXML.Formatter.format(point.point.name, xAxis.options.labels.format));278 } else {279 headerFormat = headerFormat.replace('{point.key}', LogiXML.Formatter.format(isDateTime ? new Date(point.key) : point.key, xAxis.options.labels.format));280 }281 } else if (isDateTime && xDateFormat && LogiXML.HighchartsFormatters.isNumber(point.key)) { // Insert the header date format if any282 if (xDateFormat == "%Y" && point.key) {283 headerFormat = headerFormat.replace('{point.key}', LogiXML.Formatter.formatDate(point.key, 'd'));284 } else {285 headerFormat = headerFormat.replace('{point.key}', '{point.key:' + xDateFormat + '}');286 }287 } else if (!series.xAxis) {288 headerFormat = headerFormat.replace('{point.key:.2f}', point.key);289 tooltipOptions.pointFormat = tooltipOptions.pointFormat.replace('{series.name}:', "").replace('●', '');290 }291 else if (!series.xAxis) {292 headerFormat = headerFormat.replace('{point.key:.2f}', point.key);293 tooltipOptions.pointFormat = tooltipOptions.pointFormat.replace('{series.name}:', "");294 } else if (isCategories && point.point) {295 headerFormat = headerFormat.replace('{point.key}', point.point.name);296 }297 return Highcharts.format(headerFormat, {298 point: point,299 series: series300 });301 },302 tooltipAutoPointFormatter: function (point) {303 var series = point.series,304 tooltipOptions = series.tooltipOptions,305 pointFormat = series.tooltipOptions.pointFormat,306 seriesTooltipOptions = series.tooltipOptions,307 valueDecimals = LogiXML.HighchartsFormatters.pick(seriesTooltipOptions.valueDecimals, ''),308 xAxisIsDateTime = series.xAxis && series.xAxis.options.type === 'datetime',309 yAxisIsDateTime = series.yAxis && series.yAxis.options.type === 'datetime',310 valuePrefix = seriesTooltipOptions.valuePrefix || '',311 valueSuffix = seriesTooltipOptions.valueSuffix || '',312 pointArrayMap, i = 0, length, key,313 format, axisType, val,314 removeXKey = false;315 if (series.xAxis && series.xAxis.options && series.xAxis.options.labels && series.xAxis.options.labels.format) {316 pointFormat = pointFormat.replace('{point.x}', LogiXML.Formatter.format(xAxisIsDateTime ? new Date(point.x) : point.x, series.xAxis.options.labels.format));317 }318 if (series.yAxis && series.yAxis.options && series.yAxis.options.labels && series.yAxis.options.labels.format) {319 pointFormat = pointFormat.replace('{point.y}', LogiXML.Formatter.format(yAxisIsDateTime ? new Date(point.y) : point.y, series.yAxis.options.labels.format));320 }321 else if (series && series.options && series.options.dataLabels && series.options.dataLabels._format && series.options.type !== "pie")322 //pie chart is showing datalabels from xAxis data, so we don't need to format point.y according to DataLabels format.323 {324 pointFormat = pointFormat.replace('{point.y}', LogiXML.Formatter.format(yAxisIsDateTime ? new Date(point.y) : point.y, series.options.dataLabels._format));325 }326 // Loop over the point array map and replace unformatted values with sprintf formatting markup327 pointArrayMap = series.pointArrayMap || ['x', 'y'];328 if (LogiXML.HighchartsFormatters.inArray('x', pointArrayMap) == -1) {329 pointArrayMap.push('x');330 removeXKey = true;331 }332 length = pointArrayMap.length;333 for (; i < length; i++) {334 key = pointArrayMap[i];335 if (key == 'x') {336 axisType = series.xAxis && series.xAxis.options.type || 'category';337 } else {338 axisType = series.yAxis && series.yAxis.options.type || 'linear';339 }340 format = '';341 switch (axisType) {342 case 'category':343 //nothing to do344 break;345 case 'datetime':346 if (key == 'x') {347 format = tooltipOptions.xDateFormat;348 } else {349 format = tooltipOptions.yDateFormat;350 }351 if (!format) {352 val = point[key];353 if (val) {354 pointFormat = pointFormat.replace('{point.' + key + '}', LogiXML.Formatter.formatDate(val, 'd'));355 }356 continue;357 }358 break;359 default: //linear or log360 format = ':,.' + valueDecimals + 'f';361 break;362 }363 key = '{point.' + key;364 if (valuePrefix || valueSuffix) {365 pointFormat = pointFormat.replace(key + '}', valuePrefix + key + '}' + valueSuffix);366 }367 if (format && format !== '') {368 pointFormat = pointFormat.replace(key + '}', key + format + '}');369 }370 }371 if (removeXKey)372 pointArrayMap.removeAt(pointArrayMap.indexOf('x'));373 return Highcharts.format(pointFormat, {374 point: point,375 series: point.series376 });377 },378 dataLabelFormatter: function () {379 var format = this.series.options.dataLabels._format,380 value = this.y;381 if (this.series.options.stacking && value === 0) {382 return "";383 }384 //TODO: may be percent, total, etc. Digg it later385 if (format == 'HTML' && this.key) {386 this.key = LogiXML.decodeHtml(this.key, true)387 }388 return LogiXML.Formatter.format(value, format);389 },390 sideLabelFormatter: function () {391 var format = this.series.options.dataLabels._format,392 value = this.point && this.point.name ? this.point.name : "",393 dataValue = this.y,394 showPointName = this.series.options.dataLabels.showPointName,395 showDataValue = this.series.options.dataLabels.showDataValue,396 ret;397 //TODO: may be percent, total, etc. Digg it later398 if (format != null && format != "") {399 if (format == "Percent" || format == "p" || format == "%" || format.indexOf("%", format.length - 1) !== -1) {400 dataValue = this.percentage / 100;401 }402 dataValue = LogiXML.Formatter.format(dataValue, format);403 }404 if (showPointName && showDataValue) {405 ret = "<div>" + value + "<br />" + dataValue + "</div>";406 } else if (showPointName) {407 ret = LogiXML.Formatter.format(value, format);408 } else {409 ret = dataValue;410 }411 return ret;412 },413 legendLabelFormatter: function () {414 var format = this.chart ? this.chart.options.legend._format == 'HTML' ? "" : this.chart.options.legend._format : this.series.chart.options.legend._format;415 return LogiXML.Formatter.format(this.name, format);416 },417 stackLabelFormatter: function () {418 var format = this.options._format;419 return LogiXML.Formatter.format(this.total, format);420 },421 labelFormatter: function () {422 var format = this.axis.options.labels.format;423 if (this.dateTimeLabelFormat || this.value instanceof Date) {424 return LogiXML.Formatter.formatDate(this.value, format);425 } else {426 return LogiXML.Formatter.format(this.value, format);427 }428 // return LogiXML.Formatter.formatNumber(this.value, format, lang, global);429 //} else if (typeof this.value === 'string') {430 // return LogiXML.Formatter.formatString(this.value, format, lang, global);431 //}432 //return this.value;433 },434 format: function (value, format) {435 if (this.value instanceof Date) {436 return LogiXML.Formatter.formatDate(value, format);437 } else {438 return LogiXML.Formatter.format(value, format);439 }440 // return LogiXML.Formatter.formatNumber(this.value, format, lang, global);441 //} else if (typeof this.value === 'string') {442 // return LogiXML.Formatter.formatString(this.value, format, lang, global);443 //}444 //return this.value;445 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const { chromium } = require('playwright');2const { PercentageToken } = require('playwright/lib/internal/locators/locators');3(async () => {4 const browser = await chromium.launch();5 const page = await browser.newPage();6 const handle = await page.$(new PercentageToken('100', '100'));7 const text = await handle.innerText();8 console.log(text);9 await browser.close();10})();

Full Screen

Using AI Code Generation

copy

Full Screen

1const { PercentageToken } = require('playwright/lib/internal/locators/locators');2const { Locator } = require('playwright/lib/locator');3const { ElementHandle } = require('playwright/lib/elementHandler');4class PercentageLocator extends Locator {5 constructor(parent, selector, percentage) {6 super(parent, selector);7 this.percentage = percentage;8 }9 async _evaluate(handle, pageFunction, options, ...args) {10 const percentage = this.percentage;11 const result = await handle.evaluateHandle(12 (node, { pageFunction, percentage, args }) => {13 const element = node;14 const rect = element.getBoundingClientRect();15 const x = rect.left + rect.width * percentage;16 const y = rect.top + rect.height * percentage;17 const elementAtPoint = document.elementFromPoint(x, y);18 if (!elementAtPoint) {19 return null;20 }21 if (elementAtPoint === element) {22 return element;23 }24 if (!element.contains(elementAtPoint)) {25 return null;26 }27 return elementAtPoint;28 },29 { pageFunction, percentage, args }30 );31 const element = ElementHandle.from(result);32 return element._adoptTo(pageFunction, options);33 }34}35Locator.register('percentage', (parent, selector, percentage) => {36 return new PercentageLocator(parent, selector, percentage);37});38const { test, expect } = require('@playwright/test');39test('test', async ({ page }) => {40 const element = await page.locator('percentage=50').first();41 expect(await element.innerText()).toBe('Docs');42});

Full Screen

Using AI Code Generation

copy

Full Screen

1const { PercentageToken } = require('playwright/lib/utils/progress');2const progress = new PercentageToken();3progress.increment(50);4const { PercentageToken } = require('playwright/lib/utils/progress');5const progress = new PercentageToken();6progress.increment(50);7const { PercentageToken } = require('playwright/lib/utils/progress');8const progress = new PercentageToken();9progress.increment(50);10const { PercentageToken } = require('playwright/lib/utils/progress');11const progress = new PercentageToken();12progress.increment(50);13const { PercentageToken } = require('playwright/lib/utils/progress');14const progress = new PercentageToken();15progress.increment(50);16const { PercentageToken } = require('playwright/lib/utils/progress');17const progress = new PercentageToken();18progress.increment(50);19const { PercentageToken } = require('playwright/lib/utils/progress');20const progress = new PercentageToken();21progress.increment(50);22const { PercentageToken } = require('playwright/lib/utils/progress');23const progress = new PercentageToken();24progress.increment(50);25const { PercentageToken } = require('playwright/lib/utils/progress');

Full Screen

Using AI Code Generation

copy

Full Screen

1const { PercentageToken } = require('playwright/lib/types/types');2const percentageToken = new PercentageToken('50%');3console.log(percentageToken.evaluate(100));4const { PercentageToken } = require('playwright');5const percentageToken = new PercentageToken('50%');6console.log(percentageToken.evaluate(100));7const { PercentageToken } = require('playwright');8const percentageToken = new PercentageToken('50%');9console.log(percentageToken.evaluate(100));10const { PercentageToken } = require('playwright');11const percentageToken = new PercentageToken('50%');12console.log(percentageToken.evaluate(100));13const { PercentageToken } = require('playwright');14const percentageToken = new PercentageToken('50%');15console.log(percentageToken.evaluate(100));16const { PercentageToken } = require('playwright');17const percentageToken = new PercentageToken('50%');18console.log(percentageToken.evaluate(100));19const { PercentageToken } = require('playwright');20const percentageToken = new PercentageToken('50%');21console.log(percentageToken.evaluate(100));22const { PercentageToken } = require('playwright');23const percentageToken = new PercentageToken('50%');24console.log(percentageToken.evaluate(100));25const { PercentageToken } = require('playwright');26const percentageToken = new PercentageToken('50%');27console.log(percentageToken.evaluate(100));28const { PercentageToken } = require('playwright');29const percentageToken = new PercentageToken('50%');30console.log(percentageToken.evaluate(100));

Full Screen

Using AI Code Generation

copy

Full Screen

1const { percentageToken } = require('playwright/lib/utils/progress');2const { percentageToken } = require('playwright/lib/utils/progress');3const { percentageToken } = require('playwright/lib/utils/progress');4const { percentageToken } = require('playwright/lib/utils/progress');5const { percentageToken } = require('playwright/lib/utils/progress');6const { percentageToken } = require('playwright/lib/utils/progress');7const { percentageToken } = require('playwright/lib/utils/progress');8const { percentageToken } = require('playwright/lib/utils/progress');9const { percentageToken } = require('playwright/lib/utils/progress');10const { percentageToken } = require('playwright/lib/utils/progress');11const { percentageToken } = require('playwright/lib/utils/progress');12const {

Full Screen

Using AI Code Generation

copy

Full Screen

1const percentageToken = require('playwright/lib/utils/percentage-token');2const percentage = percentageToken(0.5);3console.log(percentage);4const percentage = percentageToken(0.5);5console.log(percentage);6const percentage = percentageToken(0.5);7console.log(percentage);8const percentage = percentageToken(0.5);9console.log(percentage);10const percentage = percentageToken(0.5);11console.log(percentage);12const percentage = percentageToken(0.5);13console.log(percentage);14const percentage = percentageToken(0.5);15console.log(percentage);16const percentage = percentageToken(0.5);17console.log(percentage);18const percentage = percentageToken(0.5);19console.log(percentage);20const percentage = percentageToken(0.5);21console.log(percentage);22const percentage = percentageToken(0.5);23console.log(percentage);24const percentage = percentageToken(0.5);25console.log(percentage);26const percentage = percentageToken(0.5);27console.log(percentage);28const percentage = percentageToken(0.5);29console.log(percentage);30const percentage = percentageToken(0.5);31console.log(percentage);32const percentage = percentageToken(0.5);33console.log(percentage);34const percentage = percentageToken(0.5);35console.log(percentage);36const percentage = percentageToken(0.5);37console.log(percentage);38const percentage = percentageToken(0.5);39console.log(percentage);40const percentage = percentageToken(0.5);41console.log(percentage);42const percentage = percentageToken(0.5);43console.log(percentage);

Full Screen

Playwright tutorial

LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.

Chapters:

  1. What is Playwright : Playwright is comparatively new but has gained good popularity. Get to know some history of the Playwright with some interesting facts connected with it.
  2. How To Install Playwright : Learn in detail about what basic configuration and dependencies are required for installing Playwright and run a test. Get a step-by-step direction for installing the Playwright automation framework.
  3. Playwright Futuristic Features: Launched in 2020, Playwright gained huge popularity quickly because of some obliging features such as Playwright Test Generator and Inspector, Playwright Reporter, Playwright auto-waiting mechanism and etc. Read up on those features to master Playwright testing.
  4. What is Component Testing: Component testing in Playwright is a unique feature that allows a tester to test a single component of a web application without integrating them with other elements. Learn how to perform Component testing on the Playwright automation framework.
  5. Inputs And Buttons In Playwright: Every website has Input boxes and buttons; learn about testing inputs and buttons with different scenarios and examples.
  6. Functions and Selectors in Playwright: Learn how to launch the Chromium browser with Playwright. Also, gain a better understanding of some important functions like “BrowserContext,” which allows you to run multiple browser sessions, and “newPage” which interacts with a page.
  7. Handling Alerts and Dropdowns in Playwright : Playwright interact with different types of alerts and pop-ups, such as simple, confirmation, and prompt, and different types of dropdowns, such as single selector and multi-selector get your hands-on with handling alerts and dropdown in Playright testing.
  8. Playwright vs Puppeteer: Get to know about the difference between two testing frameworks and how they are different than one another, which browsers they support, and what features they provide.
  9. Run Playwright Tests on LambdaTest: Playwright testing with LambdaTest leverages test performance to the utmost. You can run multiple Playwright tests in Parallel with the LammbdaTest test cloud. Get a step-by-step guide to run your Playwright test on the LambdaTest platform.
  10. Playwright Python Tutorial: Playwright automation framework support all major languages such as Python, JavaScript, TypeScript, .NET and etc. However, there are various advantages to Python end-to-end testing with Playwright because of its versatile utility. Get the hang of Playwright python testing with this chapter.
  11. Playwright End To End Testing Tutorial: Get your hands on with Playwright end-to-end testing and learn to use some exciting features such as TraceViewer, Debugging, Networking, Component testing, Visual testing, and many more.
  12. Playwright Video Tutorial: Watch the video tutorials on Playwright testing from experts and get a consecutive in-depth explanation of Playwright automation testing.

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful