lexer.js 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. // A CSS Lexer. This file is a bit unusual -- it is a more or less
  5. // direct translation of layout/style/nsCSSScanner.cpp and
  6. // layout/style/CSSLexer.cpp into JS. This implements the
  7. // CSSLexer.webidl interface, and the intent is to try to keep it in
  8. // sync with changes to the platform CSS lexer. Due to this goal,
  9. // this file violates some naming conventions and consequently locally
  10. // disables some eslint rules.
  11. /* eslint-disable camelcase, no-inline-comments, mozilla/no-aArgs */
  12. /* eslint-disable no-else-return */
  13. "use strict";
  14. // White space of any kind. No value fields are used. Note that
  15. // comments do *not* count as white space; comments separate tokens
  16. // but are not themselves tokens.
  17. const eCSSToken_Whitespace = "whitespace"; //
  18. // A comment.
  19. const eCSSToken_Comment = "comment"; // /*...*/
  20. // Identifier-like tokens. mIdent is the text of the identifier.
  21. // The difference between ID and Hash is: if the text after the #
  22. // would have been a valid Ident if the # hadn't been there, the
  23. // scanner produces an ID token. Otherwise it produces a Hash token.
  24. // (This distinction is required by css3-selectors.)
  25. const eCSSToken_Ident = "ident"; // word
  26. const eCSSToken_Function = "function"; // word(
  27. const eCSSToken_AtKeyword = "at"; // @word
  28. const eCSSToken_ID = "id"; // #word
  29. const eCSSToken_Hash = "hash"; // #0word
  30. // Numeric tokens. mNumber is the floating-point value of the
  31. // number, and mHasSign indicates whether there was an explicit sign
  32. // (+ or -) in front of the number. If mIntegerValid is true, the
  33. // number had the lexical form of an integer, and mInteger is its
  34. // integer value. Lexically integer values outside the range of a
  35. // 32-bit signed number are clamped to the maximum values; mNumber
  36. // will indicate a 'truer' value in that case. Percentage tokens
  37. // are always considered not to be integers, even if their numeric
  38. // value is integral (100% => mNumber = 1.0). For Dimension
  39. // tokens, mIdent holds the text of the unit.
  40. const eCSSToken_Number = "number"; // 1 -5 +2e3 3.14159 7.297352e-3
  41. const eCSSToken_Dimension = "dimension"; // 24px 8.5in
  42. const eCSSToken_Percentage = "percentage"; // 85% 1280.4%
  43. // String-like tokens. In all cases, mIdent holds the text
  44. // belonging to the string, and mSymbol holds the delimiter
  45. // character, which may be ', ", or zero (only for unquoted URLs).
  46. // Bad_String and Bad_URL tokens are emitted when the closing
  47. // delimiter or parenthesis was missing.
  48. const eCSSToken_String = "string"; // 'foo bar' "foo bar"
  49. const eCSSToken_Bad_String = "bad_string"; // 'foo bar
  50. const eCSSToken_URL = "url"; // url(foobar) url("foo bar")
  51. const eCSSToken_Bad_URL = "bad_url"; // url(foo
  52. // Any one-character symbol. mSymbol holds the character.
  53. const eCSSToken_Symbol = "symbol"; // . ; { } ! *
  54. // Match operators. These are single tokens rather than pairs of
  55. // Symbol tokens because css3-selectors forbids the presence of
  56. // comments between the two characters. No value fields are used;
  57. // the token type indicates which operator.
  58. const eCSSToken_Includes = "includes"; // ~=
  59. const eCSSToken_Dashmatch = "dashmatch"; // |=
  60. const eCSSToken_Beginsmatch = "beginsmatch"; // ^=
  61. const eCSSToken_Endsmatch = "endsmatch"; // $=
  62. const eCSSToken_Containsmatch = "containsmatch"; // *=
  63. // Unicode-range token: currently used only in @font-face.
  64. // The lexical rule for this token includes several forms that are
  65. // semantically invalid. Therefore, mIdent always holds the
  66. // complete original text of the token (so we can print it
  67. // accurately in diagnostics), and mIntegerValid is true iff the
  68. // token is semantically valid. In that case, mInteger holds the
  69. // lowest value included in the range, and mInteger2 holds the
  70. // highest value included in the range.
  71. const eCSSToken_URange = "urange"; // U+007e U+01?? U+2000-206F
  72. // HTML comment delimiters, ignored as a unit when they appear at
  73. // the top level of a style sheet, for compatibility with websites
  74. // written for compatibility with pre-CSS browsers. This token type
  75. // subsumes the css2.1 CDO and CDC tokens, which are always treated
  76. // the same by the parser. mIdent holds the text of the token, for
  77. // diagnostics.
  78. const eCSSToken_HTMLComment = "htmlcomment"; // <!-- -->
  79. const eEOFCharacters_None = 0x0000;
  80. // to handle \<EOF> inside strings
  81. const eEOFCharacters_DropBackslash = 0x0001;
  82. // to handle \<EOF> outside strings
  83. const eEOFCharacters_ReplacementChar = 0x0002;
  84. // to close comments
  85. const eEOFCharacters_Asterisk = 0x0004;
  86. const eEOFCharacters_Slash = 0x0008;
  87. // to close double-quoted strings
  88. const eEOFCharacters_DoubleQuote = 0x0010;
  89. // to close single-quoted strings
  90. const eEOFCharacters_SingleQuote = 0x0020;
  91. // to close URLs
  92. const eEOFCharacters_CloseParen = 0x0040;
  93. // Bridge the char/string divide.
  94. const APOSTROPHE = "'".charCodeAt(0);
  95. const ASTERISK = "*".charCodeAt(0);
  96. const CARRIAGE_RETURN = "\r".charCodeAt(0);
  97. const CIRCUMFLEX_ACCENT = "^".charCodeAt(0);
  98. const COMMERCIAL_AT = "@".charCodeAt(0);
  99. const DIGIT_NINE = "9".charCodeAt(0);
  100. const DIGIT_ZERO = "0".charCodeAt(0);
  101. const DOLLAR_SIGN = "$".charCodeAt(0);
  102. const EQUALS_SIGN = "=".charCodeAt(0);
  103. const EXCLAMATION_MARK = "!".charCodeAt(0);
  104. const FULL_STOP = ".".charCodeAt(0);
  105. const GREATER_THAN_SIGN = ">".charCodeAt(0);
  106. const HYPHEN_MINUS = "-".charCodeAt(0);
  107. const LATIN_CAPITAL_LETTER_E = "E".charCodeAt(0);
  108. const LATIN_CAPITAL_LETTER_U = "U".charCodeAt(0);
  109. const LATIN_SMALL_LETTER_E = "e".charCodeAt(0);
  110. const LATIN_SMALL_LETTER_U = "u".charCodeAt(0);
  111. const LEFT_PARENTHESIS = "(".charCodeAt(0);
  112. const LESS_THAN_SIGN = "<".charCodeAt(0);
  113. const LINE_FEED = "\n".charCodeAt(0);
  114. const NUMBER_SIGN = "#".charCodeAt(0);
  115. const PERCENT_SIGN = "%".charCodeAt(0);
  116. const PLUS_SIGN = "+".charCodeAt(0);
  117. const QUESTION_MARK = "?".charCodeAt(0);
  118. const QUOTATION_MARK = "\"".charCodeAt(0);
  119. const REVERSE_SOLIDUS = "\\".charCodeAt(0);
  120. const RIGHT_PARENTHESIS = ")".charCodeAt(0);
  121. const SOLIDUS = "/".charCodeAt(0);
  122. const TILDE = "~".charCodeAt(0);
  123. const VERTICAL_LINE = "|".charCodeAt(0);
  124. const UCS2_REPLACEMENT_CHAR = 0xFFFD;
  125. const kImpliedEOFCharacters = [
  126. UCS2_REPLACEMENT_CHAR,
  127. ASTERISK,
  128. SOLIDUS,
  129. QUOTATION_MARK,
  130. APOSTROPHE,
  131. RIGHT_PARENTHESIS,
  132. 0
  133. ];
  134. /**
  135. * Ensure that the character is valid. If it is valid, return it;
  136. * otherwise, return the replacement character.
  137. *
  138. * @param {Number} c the character to check
  139. * @return {Number} the character or its replacement
  140. */
  141. function ensureValidChar(c) {
  142. if (c >= 0x00110000 || (c & 0xFFF800) == 0xD800) {
  143. // Out of range or a surrogate.
  144. return UCS2_REPLACEMENT_CHAR;
  145. }
  146. return c;
  147. }
  148. /**
  149. * Turn a string into an array of character codes.
  150. *
  151. * @param {String} str the input string
  152. * @return {Array} an array of character codes, one per character in
  153. * the input string.
  154. */
  155. function stringToCodes(str) {
  156. return Array.prototype.map.call(str, (c) => c.charCodeAt(0));
  157. }
  158. const IS_HEX_DIGIT = 0x01;
  159. const IS_IDSTART = 0x02;
  160. const IS_IDCHAR = 0x04;
  161. const IS_URL_CHAR = 0x08;
  162. const IS_HSPACE = 0x10;
  163. const IS_VSPACE = 0x20;
  164. const IS_SPACE = IS_HSPACE | IS_VSPACE;
  165. const IS_STRING = 0x40;
  166. const H = IS_HSPACE;
  167. const V = IS_VSPACE;
  168. const I = IS_IDCHAR;
  169. const J = IS_IDSTART;
  170. const U = IS_URL_CHAR;
  171. const S = IS_STRING;
  172. const X = IS_HEX_DIGIT;
  173. const SH = S | H;
  174. const SU = S | U;
  175. const SUI = S | U | I;
  176. const SUIJ = S | U | I | J;
  177. const SUIX = S | U | I | X;
  178. const SUIJX = S | U | I | J | X;
  179. /* eslint-disable indent, no-multi-spaces, comma-spacing, spaced-comment */
  180. const gLexTable = [
  181. // 00 01 02 03 04 05 06 07
  182. 0, S, S, S, S, S, S, S,
  183. // 08 TAB LF 0B FF CR 0E 0F
  184. S, SH, V, S, V, V, S, S,
  185. // 10 11 12 13 14 15 16 17
  186. S, S, S, S, S, S, S, S,
  187. // 18 19 1A 1B 1C 1D 1E 1F
  188. S, S, S, S, S, S, S, S,
  189. //SPC ! " # $ % & '
  190. SH, SU, 0, SU, SU, SU, SU, 0,
  191. // ( ) * + , - . /
  192. S, S, SU, SU, SU, SUI, SU, SU,
  193. // 0 1 2 3 4 5 6 7
  194. SUIX, SUIX, SUIX, SUIX, SUIX, SUIX, SUIX, SUIX,
  195. // 8 9 : ; < = > ?
  196. SUIX, SUIX, SU, SU, SU, SU, SU, SU,
  197. // @ A B C D E F G
  198. SU,SUIJX,SUIJX,SUIJX,SUIJX,SUIJX,SUIJX, SUIJ,
  199. // H I J K L M N O
  200. SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ,
  201. // P Q R S T U V W
  202. SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ,
  203. // X Y Z [ \ ] ^ _
  204. SUIJ, SUIJ, SUIJ, SU, J, SU, SU, SUIJ,
  205. // ` a b c d e f g
  206. SU,SUIJX,SUIJX,SUIJX,SUIJX,SUIJX,SUIJX, SUIJ,
  207. // h i j k l m n o
  208. SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ,
  209. // p q r s t u v w
  210. SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ,
  211. // x y z { | } ~ 7F
  212. SUIJ, SUIJ, SUIJ, SU, SU, SU, SU, S,
  213. ];
  214. /* eslint-enable indent, no-multi-spaces, comma-spacing, spaced-comment */
  215. /**
  216. * True if 'ch' is in character class 'cls', which should be one of
  217. * the constants above or some combination of them. All characters
  218. * above U+007F are considered to be in 'cls'. EOF is never in 'cls'.
  219. */
  220. function IsOpenCharClass(ch, cls) {
  221. return ch >= 0 && (ch >= 128 || (gLexTable[ch] & cls) != 0);
  222. }
  223. /**
  224. * True if 'ch' is in character class 'cls', which should be one of
  225. * the constants above or some combination of them. No characters
  226. * above U+007F are considered to be in 'cls'. EOF is never in 'cls'.
  227. */
  228. function IsClosedCharClass(ch, cls) {
  229. return ch >= 0 && ch < 128 && (gLexTable[ch] & cls) != 0;
  230. }
  231. /**
  232. * True if 'ch' is CSS whitespace, i.e. any of the ASCII characters
  233. * TAB, LF, FF, CR, or SPC.
  234. */
  235. function IsWhitespace(ch) {
  236. return IsClosedCharClass(ch, IS_SPACE);
  237. }
  238. /**
  239. * True if 'ch' is horizontal whitespace, i.e. TAB or SPC.
  240. */
  241. function IsHorzSpace(ch) {
  242. return IsClosedCharClass(ch, IS_HSPACE);
  243. }
  244. /**
  245. * True if 'ch' is vertical whitespace, i.e. LF, FF, or CR. Vertical
  246. * whitespace requires special handling when consumed, see AdvanceLine.
  247. */
  248. function IsVertSpace(ch) {
  249. return IsClosedCharClass(ch, IS_VSPACE);
  250. }
  251. /**
  252. * True if 'ch' is a character that can appear in the middle of an identifier.
  253. * This includes U+0000 since it is handled as U+FFFD, but for purposes of
  254. * GatherText it should not be included in IsOpenCharClass.
  255. */
  256. function IsIdentChar(ch) {
  257. return IsOpenCharClass(ch, IS_IDCHAR) || ch == 0;
  258. }
  259. /**
  260. * True if 'ch' is a character that by itself begins an identifier.
  261. * This includes U+0000 since it is handled as U+FFFD, but for purposes of
  262. * GatherText it should not be included in IsOpenCharClass.
  263. * (This is a subset of IsIdentChar.)
  264. */
  265. function IsIdentStart(ch) {
  266. return IsOpenCharClass(ch, IS_IDSTART) || ch == 0;
  267. }
  268. /**
  269. * True if the two-character sequence aFirstChar+aSecondChar begins an
  270. * identifier.
  271. */
  272. function StartsIdent(aFirstChar, aSecondChar) {
  273. return IsIdentStart(aFirstChar) ||
  274. (aFirstChar == HYPHEN_MINUS && (aSecondChar == HYPHEN_MINUS ||
  275. IsIdentStart(aSecondChar)));
  276. }
  277. /**
  278. * True if 'ch' is a decimal digit.
  279. */
  280. function IsDigit(ch) {
  281. return (ch >= DIGIT_ZERO) && (ch <= DIGIT_NINE);
  282. }
  283. /**
  284. * True if 'ch' is a hexadecimal digit.
  285. */
  286. function IsHexDigit(ch) {
  287. return IsClosedCharClass(ch, IS_HEX_DIGIT);
  288. }
  289. /**
  290. * Assuming that 'ch' is a decimal digit, return its numeric value.
  291. */
  292. function DecimalDigitValue(ch) {
  293. return ch - DIGIT_ZERO;
  294. }
  295. /**
  296. * Assuming that 'ch' is a hexadecimal digit, return its numeric value.
  297. */
  298. function HexDigitValue(ch) {
  299. if (IsDigit(ch)) {
  300. return DecimalDigitValue(ch);
  301. } else {
  302. // Note: c&7 just keeps the low three bits which causes
  303. // upper and lower case alphabetics to both yield their
  304. // "relative to 10" value for computing the hex value.
  305. return (ch & 0x7) + 9;
  306. }
  307. }
  308. /**
  309. * If 'ch' can be the first character of a two-character match operator
  310. * token, return the token type code for that token, otherwise return
  311. * eCSSToken_Symbol to indicate that it can't.
  312. */
  313. function MatchOperatorType(ch) {
  314. switch (ch) {
  315. case TILDE: return eCSSToken_Includes;
  316. case VERTICAL_LINE: return eCSSToken_Dashmatch;
  317. case CIRCUMFLEX_ACCENT: return eCSSToken_Beginsmatch;
  318. case DOLLAR_SIGN: return eCSSToken_Endsmatch;
  319. case ASTERISK: return eCSSToken_Containsmatch;
  320. default: return eCSSToken_Symbol;
  321. }
  322. }
  323. function Scanner(buffer) {
  324. this.mBuffer = buffer || "";
  325. this.mOffset = 0;
  326. this.mCount = this.mBuffer.length;
  327. this.mLineNumber = 1;
  328. this.mLineOffset = 0;
  329. this.mTokenLineOffset = 0;
  330. this.mTokenOffset = 0;
  331. this.mTokenLineNumber = 1;
  332. this.mEOFCharacters = eEOFCharacters_None;
  333. }
  334. Scanner.prototype = {
  335. /**
  336. * @see CSSLexer.lineNumber
  337. */
  338. get lineNumber() {
  339. return this.mTokenLineNumber - 1;
  340. },
  341. /**
  342. * @see CSSLexer.columnNumber
  343. */
  344. get columnNumber() {
  345. return this.mTokenOffset - this.mTokenLineOffset;
  346. },
  347. /**
  348. * @see CSSLexer.performEOFFixup
  349. */
  350. performEOFFixup: function (aInputString, aPreserveBackslash) {
  351. let result = aInputString;
  352. let eofChars = this.mEOFCharacters;
  353. if (aPreserveBackslash &&
  354. (eofChars & (eEOFCharacters_DropBackslash |
  355. eEOFCharacters_ReplacementChar)) != 0) {
  356. eofChars &= ~(eEOFCharacters_DropBackslash |
  357. eEOFCharacters_ReplacementChar);
  358. result += "\\";
  359. }
  360. if ((eofChars & eEOFCharacters_DropBackslash) != 0 &&
  361. result.length > 0 && result.endsWith("\\")) {
  362. result = result.slice(0, -1);
  363. }
  364. let extra = [];
  365. this.AppendImpliedEOFCharacters(eofChars, extra);
  366. let asString = String.fromCharCode.apply(null, extra);
  367. return result + asString;
  368. },
  369. /**
  370. * @see CSSLexer.nextToken
  371. */
  372. nextToken: function () {
  373. let token = {};
  374. if (!this.Next(token)) {
  375. return null;
  376. }
  377. let resultToken = {};
  378. resultToken.tokenType = token.mType;
  379. resultToken.startOffset = this.mTokenOffset;
  380. resultToken.endOffset = this.mOffset;
  381. let constructText = () => {
  382. return String.fromCharCode.apply(null, token.mIdent);
  383. };
  384. switch (token.mType) {
  385. case eCSSToken_Whitespace:
  386. break;
  387. case eCSSToken_Ident:
  388. case eCSSToken_Function:
  389. case eCSSToken_AtKeyword:
  390. case eCSSToken_ID:
  391. case eCSSToken_Hash:
  392. resultToken.text = constructText();
  393. break;
  394. case eCSSToken_Dimension:
  395. resultToken.text = constructText();
  396. /* Fall through. */
  397. case eCSSToken_Number:
  398. case eCSSToken_Percentage:
  399. resultToken.number = token.mNumber;
  400. resultToken.hasSign = token.mHasSign;
  401. resultToken.isInteger = token.mIntegerValid;
  402. break;
  403. case eCSSToken_String:
  404. case eCSSToken_Bad_String:
  405. case eCSSToken_URL:
  406. case eCSSToken_Bad_URL:
  407. resultToken.text = constructText();
  408. /* Don't bother emitting the delimiter, as it is readily extracted
  409. from the source string when needed. */
  410. break;
  411. case eCSSToken_Symbol:
  412. resultToken.text = String.fromCharCode(token.mSymbol);
  413. break;
  414. case eCSSToken_Includes:
  415. case eCSSToken_Dashmatch:
  416. case eCSSToken_Beginsmatch:
  417. case eCSSToken_Endsmatch:
  418. case eCSSToken_Containsmatch:
  419. case eCSSToken_URange:
  420. break;
  421. case eCSSToken_Comment:
  422. case eCSSToken_HTMLComment:
  423. /* The comment text is easily extracted from the source string,
  424. and is rarely useful. */
  425. break;
  426. }
  427. return resultToken;
  428. },
  429. /**
  430. * Return the raw UTF-16 code unit at position |this.mOffset + n| within
  431. * the read buffer. If that is beyond the end of the buffer, returns
  432. * -1 to indicate end of input.
  433. */
  434. Peek: function (n = 0) {
  435. if (this.mOffset + n >= this.mCount) {
  436. return -1;
  437. }
  438. return this.mBuffer.charCodeAt(this.mOffset + n);
  439. },
  440. /**
  441. * Advance |this.mOffset| over |n| code units. Advance(0) is a no-op.
  442. * If |n| is greater than the distance to end of input, will silently
  443. * stop at the end. May not be used to advance over a line boundary;
  444. * AdvanceLine() must be used instead.
  445. */
  446. Advance: function (n = 1) {
  447. if (this.mOffset + n >= this.mCount || this.mOffset + n < this.mOffset) {
  448. this.mOffset = this.mCount;
  449. } else {
  450. this.mOffset += n;
  451. }
  452. },
  453. /**
  454. * Advance |this.mOffset| over a line boundary.
  455. */
  456. AdvanceLine: function () {
  457. // Advance over \r\n as a unit.
  458. if (this.mBuffer.charCodeAt(this.mOffset) == CARRIAGE_RETURN &&
  459. this.mOffset + 1 < this.mCount &&
  460. this.mBuffer.charCodeAt(this.mOffset + 1) == LINE_FEED) {
  461. this.mOffset += 2;
  462. } else {
  463. this.mOffset += 1;
  464. }
  465. // 0 is a magical line number meaning that we don't know (i.e., script)
  466. if (this.mLineNumber != 0) {
  467. this.mLineNumber++;
  468. }
  469. this.mLineOffset = this.mOffset;
  470. },
  471. /**
  472. * Skip over a sequence of whitespace characters (vertical or
  473. * horizontal) starting at the current read position.
  474. */
  475. SkipWhitespace: function () {
  476. for (;;) {
  477. let ch = this.Peek();
  478. if (!IsWhitespace(ch)) { // EOF counts as non-whitespace
  479. break;
  480. }
  481. if (IsVertSpace(ch)) {
  482. this.AdvanceLine();
  483. } else {
  484. this.Advance();
  485. }
  486. }
  487. },
  488. /**
  489. * Skip over one CSS comment starting at the current read position.
  490. */
  491. SkipComment: function () {
  492. this.Advance(2);
  493. for (;;) {
  494. let ch = this.Peek();
  495. if (ch < 0) {
  496. this.SetEOFCharacters(eEOFCharacters_Asterisk | eEOFCharacters_Slash);
  497. return;
  498. }
  499. if (ch == ASTERISK) {
  500. this.Advance();
  501. ch = this.Peek();
  502. if (ch < 0) {
  503. this.SetEOFCharacters(eEOFCharacters_Slash);
  504. return;
  505. }
  506. if (ch == SOLIDUS) {
  507. this.Advance();
  508. return;
  509. }
  510. } else if (IsVertSpace(ch)) {
  511. this.AdvanceLine();
  512. } else {
  513. this.Advance();
  514. }
  515. }
  516. },
  517. /**
  518. * If there is a valid escape sequence starting at the current read
  519. * position, consume it, decode it, append the result to |aOutput|,
  520. * and return true. Otherwise, consume nothing, leave |aOutput|
  521. * unmodified, and return false. If |aInString| is true, accept the
  522. * additional form of escape sequence allowed within string-like tokens.
  523. */
  524. GatherEscape: function (aOutput, aInString) {
  525. let ch = this.Peek(1);
  526. if (ch < 0) {
  527. // If we are in a string (or a url() containing a string), we want to drop
  528. // the backslash on the floor. Otherwise, we want to treat it as a U+FFFD
  529. // character.
  530. this.Advance();
  531. if (aInString) {
  532. this.SetEOFCharacters(eEOFCharacters_DropBackslash);
  533. } else {
  534. aOutput.push(UCS2_REPLACEMENT_CHAR);
  535. this.SetEOFCharacters(eEOFCharacters_ReplacementChar);
  536. }
  537. return true;
  538. }
  539. if (IsVertSpace(ch)) {
  540. if (aInString) {
  541. // In strings (and in url() containing a string), escaped
  542. // newlines are completely removed, to allow splitting over
  543. // multiple lines.
  544. this.Advance();
  545. this.AdvanceLine();
  546. return true;
  547. }
  548. // Outside of strings, backslash followed by a newline is not an escape.
  549. return false;
  550. }
  551. if (!IsHexDigit(ch)) {
  552. // "Any character (except a hexadecimal digit, linefeed, carriage
  553. // return, or form feed) can be escaped with a backslash to remove
  554. // its special meaning." -- CSS2.1 section 4.1.3
  555. this.Advance(2);
  556. if (ch == 0) {
  557. aOutput.push(UCS2_REPLACEMENT_CHAR);
  558. } else {
  559. aOutput.push(ch);
  560. }
  561. return true;
  562. }
  563. // "[at most six hexadecimal digits following a backslash] stand
  564. // for the ISO 10646 character with that number, which must not be
  565. // zero. (It is undefined in CSS 2.1 what happens if a style sheet
  566. // does contain a character with Unicode codepoint zero.)"
  567. // -- CSS2.1 section 4.1.3
  568. // At this point we know we have \ followed by at least one
  569. // hexadecimal digit, therefore the escape sequence is valid and we
  570. // can go ahead and consume the backslash.
  571. this.Advance();
  572. let val = 0;
  573. let i = 0;
  574. do {
  575. val = val * 16 + HexDigitValue(ch);
  576. i++;
  577. this.Advance();
  578. ch = this.Peek();
  579. } while (i < 6 && IsHexDigit(ch));
  580. // "Interpret the hex digits as a hexadecimal number. If this
  581. // number is zero, or is greater than the maximum allowed
  582. // codepoint, return U+FFFD REPLACEMENT CHARACTER" -- CSS Syntax
  583. // Level 3
  584. if (val == 0) {
  585. aOutput.push(UCS2_REPLACEMENT_CHAR);
  586. } else {
  587. aOutput.push(ensureValidChar(val));
  588. }
  589. // Consume exactly one whitespace character after a
  590. // hexadecimal escape sequence.
  591. if (IsVertSpace(ch)) {
  592. this.AdvanceLine();
  593. } else if (IsHorzSpace(ch)) {
  594. this.Advance();
  595. }
  596. return true;
  597. },
  598. /**
  599. * Consume a run of "text" beginning with the current read position,
  600. * consisting of characters in the class |aClass| (which must be a
  601. * suitable argument to IsOpenCharClass) plus escape sequences.
  602. * Append the text to |aText|, after decoding escape sequences.
  603. *
  604. * Returns true if at least one character was appended to |aText|,
  605. * false otherwise.
  606. */
  607. GatherText: function (aClass, aText) {
  608. let start = this.mOffset;
  609. let inString = aClass == IS_STRING;
  610. for (;;) {
  611. // Consume runs of unescaped characters in one go.
  612. let n = this.mOffset;
  613. while (n < this.mCount && IsOpenCharClass(this.mBuffer.charCodeAt(n),
  614. aClass)) {
  615. n++;
  616. }
  617. if (n > this.mOffset) {
  618. let substr = this.mBuffer.slice(this.mOffset, n);
  619. Array.prototype.push.apply(aText, stringToCodes(substr));
  620. this.mOffset = n;
  621. }
  622. if (n == this.mCount) {
  623. break;
  624. }
  625. let ch = this.Peek();
  626. if (ch == 0) {
  627. this.Advance();
  628. aText.push(UCS2_REPLACEMENT_CHAR);
  629. continue;
  630. }
  631. if (ch != REVERSE_SOLIDUS) {
  632. break;
  633. }
  634. if (!this.GatherEscape(aText, inString)) {
  635. break;
  636. }
  637. }
  638. return this.mOffset > start;
  639. },
  640. /**
  641. * Scan an Ident token. This also handles Function and URL tokens,
  642. * both of which begin indistinguishably from an identifier. It can
  643. * produce a Symbol token when an apparent identifier actually led
  644. * into an invalid escape sequence.
  645. */
  646. ScanIdent: function (aToken) {
  647. if (!this.GatherText(IS_IDCHAR, aToken.mIdent)) {
  648. aToken.mSymbol = this.Peek();
  649. this.Advance();
  650. return true;
  651. }
  652. if (this.Peek() != LEFT_PARENTHESIS) {
  653. aToken.mType = eCSSToken_Ident;
  654. return true;
  655. }
  656. this.Advance();
  657. aToken.mType = eCSSToken_Function;
  658. let asString = String.fromCharCode.apply(null, aToken.mIdent);
  659. if (asString.toLowerCase() === "url") {
  660. this.NextURL(aToken);
  661. }
  662. return true;
  663. },
  664. /**
  665. * Scan an AtKeyword token. Also handles production of Symbol when
  666. * an '@' is not followed by an identifier.
  667. */
  668. ScanAtKeyword: function (aToken) {
  669. // Fall back for when '@' isn't followed by an identifier.
  670. aToken.mSymbol = COMMERCIAL_AT;
  671. this.Advance();
  672. let ch = this.Peek();
  673. if (StartsIdent(ch, this.Peek(1))) {
  674. if (this.GatherText(IS_IDCHAR, aToken.mIdent)) {
  675. aToken.mType = eCSSToken_AtKeyword;
  676. }
  677. }
  678. return true;
  679. },
  680. /**
  681. * Scan a Hash token. Handles the distinction between eCSSToken_ID
  682. * and eCSSToken_Hash, and handles production of Symbol when a '#'
  683. * is not followed by identifier characters.
  684. */
  685. ScanHash: function (aToken) {
  686. // Fall back for when '#' isn't followed by identifier characters.
  687. aToken.mSymbol = NUMBER_SIGN;
  688. this.Advance();
  689. let ch = this.Peek();
  690. if (IsIdentChar(ch) || ch == REVERSE_SOLIDUS) {
  691. let type =
  692. StartsIdent(ch, this.Peek(1)) ? eCSSToken_ID : eCSSToken_Hash;
  693. aToken.mIdent.length = 0;
  694. if (this.GatherText(IS_IDCHAR, aToken.mIdent)) {
  695. aToken.mType = type;
  696. }
  697. }
  698. return true;
  699. },
  700. /**
  701. * Scan a Number, Percentage, or Dimension token (all of which begin
  702. * like a Number). Can produce a Symbol when a '.' is not followed by
  703. * digits, or when '+' or '-' are not followed by either a digit or a
  704. * '.' and then a digit. Can also produce a HTMLComment when it
  705. * encounters '-->'.
  706. */
  707. ScanNumber: function (aToken) {
  708. let c = this.Peek();
  709. // Sign of the mantissa (-1 or 1).
  710. let sign = c == HYPHEN_MINUS ? -1 : 1;
  711. // Absolute value of the integer part of the mantissa. This is a double so
  712. // we don't run into overflow issues for consumers that only care about our
  713. // floating-point value while still being able to express the full int32_t
  714. // range for consumers who want integers.
  715. let intPart = 0;
  716. // Fractional part of the mantissa. This is a double so that when
  717. // we convert to float at the end we'll end up rounding to nearest
  718. // float instead of truncating down (as we would if fracPart were
  719. // a float and we just effectively lost the last several digits).
  720. let fracPart = 0;
  721. // Absolute value of the power of 10 that we should multiply by
  722. // (only relevant for numbers in scientific notation). Has to be
  723. // a signed integer, because multiplication of signed by unsigned
  724. // converts the unsigned to signed, so if we plan to actually
  725. // multiply by expSign...
  726. let exponent = 0;
  727. // Sign of the exponent.
  728. let expSign = 1;
  729. aToken.mHasSign = (c == PLUS_SIGN || c == HYPHEN_MINUS);
  730. if (aToken.mHasSign) {
  731. this.Advance();
  732. c = this.Peek();
  733. }
  734. let gotDot = (c == FULL_STOP);
  735. if (!gotDot) {
  736. // Scan the integer part of the mantissa.
  737. do {
  738. intPart = 10 * intPart + DecimalDigitValue(c);
  739. this.Advance();
  740. c = this.Peek();
  741. } while (IsDigit(c));
  742. gotDot = (c == FULL_STOP) && IsDigit(this.Peek(1));
  743. }
  744. if (gotDot) {
  745. // Scan the fractional part of the mantissa.
  746. this.Advance();
  747. c = this.Peek();
  748. // Power of ten by which we need to divide our next digit
  749. let divisor = 10;
  750. do {
  751. fracPart += DecimalDigitValue(c) / divisor;
  752. divisor *= 10;
  753. this.Advance();
  754. c = this.Peek();
  755. } while (IsDigit(c));
  756. }
  757. let gotE = false;
  758. if (c == LATIN_SMALL_LETTER_E || c == LATIN_CAPITAL_LETTER_E) {
  759. let expSignChar = this.Peek(1);
  760. let nextChar = this.Peek(2);
  761. if (IsDigit(expSignChar) ||
  762. ((expSignChar == HYPHEN_MINUS || expSignChar == PLUS_SIGN) &&
  763. IsDigit(nextChar))) {
  764. gotE = true;
  765. if (expSignChar == HYPHEN_MINUS) {
  766. expSign = -1;
  767. }
  768. this.Advance(); // consumes the E
  769. if (expSignChar == HYPHEN_MINUS || expSignChar == PLUS_SIGN) {
  770. this.Advance();
  771. c = nextChar;
  772. } else {
  773. c = expSignChar;
  774. }
  775. do {
  776. exponent = 10 * exponent + DecimalDigitValue(c);
  777. this.Advance();
  778. c = this.Peek();
  779. } while (IsDigit(c));
  780. }
  781. }
  782. let type = eCSSToken_Number;
  783. // Set mIntegerValid for all cases (except %, below) because we need
  784. // it for the "2n" in :nth-child(2n).
  785. aToken.mIntegerValid = false;
  786. // Time to reassemble our number.
  787. // Do all the math in double precision so it's truncated only once.
  788. let value = sign * (intPart + fracPart);
  789. if (gotE) {
  790. // Explicitly cast expSign*exponent to double to avoid issues with
  791. // overloaded pow() on Windows.
  792. value *= Math.pow(10.0, expSign * exponent);
  793. } else if (!gotDot) {
  794. // Clamp values outside of integer range.
  795. if (sign > 0) {
  796. aToken.mInteger = Math.min(intPart, Number.MAX_SAFE_INTEGER);
  797. } else {
  798. aToken.mInteger = Math.max(-intPart, Number.MIN_SAFE_INTEGER);
  799. }
  800. aToken.mIntegerValid = true;
  801. }
  802. let ident = aToken.mIdent;
  803. // Check for Dimension and Percentage tokens.
  804. if (c >= 0) {
  805. if (StartsIdent(c, this.Peek(1))) {
  806. if (this.GatherText(IS_IDCHAR, ident)) {
  807. type = eCSSToken_Dimension;
  808. }
  809. } else if (c == PERCENT_SIGN) {
  810. this.Advance();
  811. type = eCSSToken_Percentage;
  812. value = value / 100.0;
  813. aToken.mIntegerValid = false;
  814. }
  815. }
  816. aToken.mNumber = value;
  817. aToken.mType = type;
  818. return true;
  819. },
  820. /**
  821. * Scan a string constant ('foo' or "foo"). Will always produce
  822. * either a String or a Bad_String token; the latter occurs when the
  823. * close quote is missing. Always returns true (for convenience in Next()).
  824. */
  825. ScanString: function (aToken) {
  826. let aStop = this.Peek();
  827. aToken.mType = eCSSToken_String;
  828. aToken.mSymbol = aStop; // Remember how it's quoted.
  829. this.Advance();
  830. for (;;) {
  831. this.GatherText(IS_STRING, aToken.mIdent);
  832. let ch = this.Peek();
  833. if (ch == -1) {
  834. this.AddEOFCharacters(aStop == QUOTATION_MARK ?
  835. eEOFCharacters_DoubleQuote :
  836. eEOFCharacters_SingleQuote);
  837. break; // EOF ends a string token with no error.
  838. }
  839. if (ch == aStop) {
  840. this.Advance();
  841. break;
  842. }
  843. // Both " and ' are excluded from IS_STRING.
  844. if (ch == QUOTATION_MARK || ch == APOSTROPHE) {
  845. aToken.mIdent.push(ch);
  846. this.Advance();
  847. continue;
  848. }
  849. aToken.mType = eCSSToken_Bad_String;
  850. break;
  851. }
  852. return true;
  853. },
  854. /**
  855. * Scan a unicode-range token. These match the regular expression
  856. *
  857. * u\+[0-9a-f?]{1,6}(-[0-9a-f]{1,6})?
  858. *
  859. * However, some such tokens are "invalid". There are three valid forms:
  860. *
  861. * u+[0-9a-f]{x} 1 <= x <= 6
  862. * u+[0-9a-f]{x}\?{y} 1 <= x+y <= 6
  863. * u+[0-9a-f]{x}-[0-9a-f]{y} 1 <= x <= 6, 1 <= y <= 6
  864. *
  865. * All unicode-range tokens have their text recorded in mIdent; valid ones
  866. * are also decoded into mInteger and mInteger2, and mIntegerValid is set.
  867. * Note that this does not validate the numeric range, only the syntactic
  868. * form.
  869. */
  870. ScanURange: function (aResult) {
  871. let intro1 = this.Peek();
  872. let intro2 = this.Peek(1);
  873. let ch = this.Peek(2);
  874. aResult.mIdent.push(intro1);
  875. aResult.mIdent.push(intro2);
  876. this.Advance(2);
  877. let valid = true;
  878. let haveQues = false;
  879. let low = 0;
  880. let high = 0;
  881. let i = 0;
  882. do {
  883. aResult.mIdent.push(ch);
  884. if (IsHexDigit(ch)) {
  885. if (haveQues) {
  886. valid = false; // All question marks should be at the end.
  887. }
  888. low = low * 16 + HexDigitValue(ch);
  889. high = high * 16 + HexDigitValue(ch);
  890. } else {
  891. haveQues = true;
  892. low = low * 16 + 0x0;
  893. high = high * 16 + 0xF;
  894. }
  895. i++;
  896. this.Advance();
  897. ch = this.Peek();
  898. } while (i < 6 && (IsHexDigit(ch) || ch == QUESTION_MARK));
  899. if (ch == HYPHEN_MINUS && IsHexDigit(this.Peek(1))) {
  900. if (haveQues) {
  901. valid = false;
  902. }
  903. aResult.mIdent.push(ch);
  904. this.Advance();
  905. ch = this.Peek();
  906. high = 0;
  907. i = 0;
  908. do {
  909. aResult.mIdent.push(ch);
  910. high = high * 16 + HexDigitValue(ch);
  911. i++;
  912. this.Advance();
  913. ch = this.Peek();
  914. } while (i < 6 && IsHexDigit(ch));
  915. }
  916. aResult.mInteger = low;
  917. aResult.mInteger2 = high;
  918. aResult.mIntegerValid = valid;
  919. aResult.mType = eCSSToken_URange;
  920. return true;
  921. },
  922. SetEOFCharacters: function (aEOFCharacters) {
  923. this.mEOFCharacters = aEOFCharacters;
  924. },
  925. AddEOFCharacters: function (aEOFCharacters) {
  926. this.mEOFCharacters = this.mEOFCharacters | aEOFCharacters;
  927. },
  928. AppendImpliedEOFCharacters: function (aEOFCharacters, aResult) {
  929. // First, ignore eEOFCharacters_DropBackslash.
  930. let c = aEOFCharacters >> 1;
  931. // All of the remaining EOFCharacters bits represent appended characters,
  932. // and the bits are in the order that they need appending.
  933. for (let p of kImpliedEOFCharacters) {
  934. if (c & 1) {
  935. aResult.push(p);
  936. }
  937. c >>= 1;
  938. }
  939. },
  940. /**
  941. * Consume the part of an URL token after the initial 'url('. Caller
  942. * is assumed to have consumed 'url(' already. Will always produce
  943. * either an URL or a Bad_URL token.
  944. *
  945. * Exposed for use by nsCSSParser::ParseMozDocumentRule, which applies
  946. * the special lexical rules for URL tokens in a nonstandard context.
  947. */
  948. NextURL: function (aToken) {
  949. this.SkipWhitespace();
  950. // aToken.mIdent may be "url" at this point; clear that out
  951. aToken.mIdent.length = 0;
  952. let hasString = false;
  953. let ch = this.Peek();
  954. // Do we have a string?
  955. if (ch == QUOTATION_MARK || ch == APOSTROPHE) {
  956. this.ScanString(aToken);
  957. if (aToken.mType == eCSSToken_Bad_String) {
  958. aToken.mType = eCSSToken_Bad_URL;
  959. return;
  960. }
  961. hasString = true;
  962. } else {
  963. // Otherwise, this is the start of a non-quoted url (which may be empty).
  964. aToken.mSymbol = 0;
  965. this.GatherText(IS_URL_CHAR, aToken.mIdent);
  966. }
  967. // Consume trailing whitespace and then look for a close parenthesis.
  968. this.SkipWhitespace();
  969. ch = this.Peek();
  970. // ch can be less than zero indicating EOF
  971. if (ch < 0 || ch == RIGHT_PARENTHESIS) {
  972. this.Advance();
  973. aToken.mType = eCSSToken_URL;
  974. if (ch < 0) {
  975. this.AddEOFCharacters(eEOFCharacters_CloseParen);
  976. }
  977. } else {
  978. aToken.mType = eCSSToken_Bad_URL;
  979. if (!hasString) {
  980. // Consume until before the next right parenthesis, which follows
  981. // how <bad-url-token> is consumed in CSS Syntax 3 spec.
  982. // Note that, we only do this when "url(" is not followed by a
  983. // string, because in the spec, "url(" followed by a string is
  984. // handled as a url function rather than a <url-token>, so the
  985. // rest of content before ")" should be consumed in balance,
  986. // which will be done by the parser.
  987. // The closing ")" is not consumed here. It is left to the parser
  988. // so that the parser can handle both cases.
  989. do {
  990. if (IsVertSpace(ch)) {
  991. this.AdvanceLine();
  992. } else {
  993. this.Advance();
  994. }
  995. ch = this.Peek();
  996. } while (ch >= 0 && ch != RIGHT_PARENTHESIS);
  997. }
  998. }
  999. },
  1000. /**
  1001. * Primary scanner entry point. Consume one token and fill in
  1002. * |aToken| accordingly. Will skip over any number of comments first,
  1003. * and will also skip over rather than return whitespace and comment
  1004. * tokens, depending on the value of |aSkip|.
  1005. *
  1006. * Returns true if it successfully consumed a token, false if EOF has
  1007. * been reached. Will always advance the current read position by at
  1008. * least one character unless called when already at EOF.
  1009. */
  1010. Next: function (aToken, aSkip) {
  1011. let ch;
  1012. // do this here so we don't have to do it in dozens of other places
  1013. aToken.mIdent = [];
  1014. aToken.mType = eCSSToken_Symbol;
  1015. this.mTokenOffset = this.mOffset;
  1016. this.mTokenLineOffset = this.mLineOffset;
  1017. this.mTokenLineNumber = this.mLineNumber;
  1018. ch = this.Peek();
  1019. if (IsWhitespace(ch)) {
  1020. this.SkipWhitespace();
  1021. aToken.mType = eCSSToken_Whitespace;
  1022. return true;
  1023. }
  1024. if (ch == SOLIDUS && // !IsSVGMode() &&
  1025. this.Peek(1) == ASTERISK) {
  1026. this.SkipComment();
  1027. aToken.mType = eCSSToken_Comment;
  1028. return true;
  1029. }
  1030. // EOF
  1031. if (ch < 0) {
  1032. return false;
  1033. }
  1034. // 'u' could be UNICODE-RANGE or an identifier-family token
  1035. if (ch == LATIN_SMALL_LETTER_U || ch == LATIN_CAPITAL_LETTER_U) {
  1036. let c2 = this.Peek(1);
  1037. let c3 = this.Peek(2);
  1038. if (c2 == PLUS_SIGN && (IsHexDigit(c3) || c3 == QUESTION_MARK)) {
  1039. return this.ScanURange(aToken);
  1040. }
  1041. return this.ScanIdent(aToken);
  1042. }
  1043. // identifier family
  1044. if (IsIdentStart(ch)) {
  1045. return this.ScanIdent(aToken);
  1046. }
  1047. // number family
  1048. if (IsDigit(ch)) {
  1049. return this.ScanNumber(aToken);
  1050. }
  1051. if (ch == FULL_STOP && IsDigit(this.Peek(1))) {
  1052. return this.ScanNumber(aToken);
  1053. }
  1054. if (ch == PLUS_SIGN) {
  1055. let c2 = this.Peek(1);
  1056. if (IsDigit(c2) || (c2 == FULL_STOP && IsDigit(this.Peek(2)))) {
  1057. return this.ScanNumber(aToken);
  1058. }
  1059. }
  1060. // HYPHEN_MINUS can start an identifier-family token, a number-family token,
  1061. // or an HTML-comment
  1062. if (ch == HYPHEN_MINUS) {
  1063. let c2 = this.Peek(1);
  1064. let c3 = this.Peek(2);
  1065. if (IsIdentStart(c2) || (c2 == HYPHEN_MINUS && c3 != GREATER_THAN_SIGN)) {
  1066. return this.ScanIdent(aToken);
  1067. }
  1068. if (IsDigit(c2) || (c2 == FULL_STOP && IsDigit(c3))) {
  1069. return this.ScanNumber(aToken);
  1070. }
  1071. if (c2 == HYPHEN_MINUS && c3 == GREATER_THAN_SIGN) {
  1072. this.Advance(3);
  1073. aToken.mType = eCSSToken_HTMLComment;
  1074. aToken.mIdent = stringToCodes("-->");
  1075. return true;
  1076. }
  1077. }
  1078. // the other HTML-comment token
  1079. if (ch == LESS_THAN_SIGN &&
  1080. this.Peek(1) == EXCLAMATION_MARK &&
  1081. this.Peek(2) == HYPHEN_MINUS &&
  1082. this.Peek(3) == HYPHEN_MINUS) {
  1083. this.Advance(4);
  1084. aToken.mType = eCSSToken_HTMLComment;
  1085. aToken.mIdent = stringToCodes("<!--");
  1086. return true;
  1087. }
  1088. // AT_KEYWORD
  1089. if (ch == COMMERCIAL_AT) {
  1090. return this.ScanAtKeyword(aToken);
  1091. }
  1092. // HASH
  1093. if (ch == NUMBER_SIGN) {
  1094. return this.ScanHash(aToken);
  1095. }
  1096. // STRING
  1097. if (ch == QUOTATION_MARK || ch == APOSTROPHE) {
  1098. return this.ScanString(aToken);
  1099. }
  1100. // Match operators: ~= |= ^= $= *=
  1101. let opType = MatchOperatorType(ch);
  1102. if (opType != eCSSToken_Symbol && this.Peek(1) == EQUALS_SIGN) {
  1103. aToken.mType = opType;
  1104. this.Advance(2);
  1105. return true;
  1106. }
  1107. // Otherwise, a symbol (DELIM).
  1108. aToken.mSymbol = ch;
  1109. this.Advance();
  1110. return true;
  1111. },
  1112. };
  1113. /**
  1114. * Create and return a new CSS lexer, conforming to the @see CSSLexer
  1115. * webidl interface.
  1116. *
  1117. * @param {String} input the CSS text to lex
  1118. * @return {CSSLexer} the new lexer
  1119. */
  1120. function getCSSLexer(input) {
  1121. return new Scanner(input);
  1122. }
  1123. exports.getCSSLexer = getCSSLexer;