|
| 1 | +// Copyright 2024 Google Inc. Use of this source code is governed by an |
| 2 | +// MIT-style license that can be found in the LICENSE file or at |
| 3 | +// https://opensource.org/licenses/MIT. |
| 4 | + |
| 5 | +import 'package:charcode/charcode.dart'; |
| 6 | +import 'package:string_scanner/string_scanner.dart'; |
| 7 | + |
| 8 | +import 'character.dart'; |
| 9 | + |
| 10 | +extension StringExtension on String { |
| 11 | + /// Returns a minimally-escaped CSS identifiers whose contents evaluates to |
| 12 | + /// [text]. |
| 13 | + /// |
| 14 | + /// Throws a [FormatException] if [text] cannot be represented as a CSS |
| 15 | + /// identifier (such as the empty string). |
| 16 | + String toCssIdentifier() { |
| 17 | + var buffer = StringBuffer(); |
| 18 | + var scanner = SpanScanner(this); |
| 19 | + |
| 20 | + void writeEscape(int character) { |
| 21 | + buffer.writeCharCode($backslash); |
| 22 | + buffer.write(character.toRadixString(16)); |
| 23 | + if (scanner.peekChar() case int(isHex: true)) { |
| 24 | + buffer.writeCharCode($space); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + void consumeSurrogatePair(int character) { |
| 29 | + if (scanner.peekChar(1) case null || int(isLowSurrogate: false)) { |
| 30 | + scanner.error( |
| 31 | + "An individual surrogates can't be represented as a CSS " |
| 32 | + "identifier.", |
| 33 | + length: 1); |
| 34 | + } else if (character.isPrivateUseHighSurrogate) { |
| 35 | + writeEscape(combineSurrogates(scanner.readChar(), scanner.readChar())); |
| 36 | + } else { |
| 37 | + buffer.writeCharCode(scanner.readChar()); |
| 38 | + buffer.writeCharCode(scanner.readChar()); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + var doubleDash = false; |
| 43 | + if (scanner.scanChar($dash)) { |
| 44 | + if (scanner.isDone) return '\\2d'; |
| 45 | + |
| 46 | + buffer.writeCharCode($dash); |
| 47 | + |
| 48 | + if (scanner.scanChar($dash)) { |
| 49 | + buffer.writeCharCode($dash); |
| 50 | + doubleDash = true; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if (!doubleDash) { |
| 55 | + switch (scanner.peekChar()) { |
| 56 | + case null: |
| 57 | + scanner.error( |
| 58 | + "The empty string can't be represented as a CSS identifier."); |
| 59 | + |
| 60 | + case 0: |
| 61 | + scanner.error("The U+0000 can't be represented as a CSS identifier."); |
| 62 | + |
| 63 | + case int character when character.isHighSurrogate: |
| 64 | + consumeSurrogatePair(character); |
| 65 | + |
| 66 | + case int(isLowSurrogate: true): |
| 67 | + scanner.error( |
| 68 | + "An individual surrogate can't be represented as a CSS " |
| 69 | + "identifier.", |
| 70 | + length: 1); |
| 71 | + |
| 72 | + case int(isNameStart: true, isPrivateUseBMP: false): |
| 73 | + buffer.writeCharCode(scanner.readChar()); |
| 74 | + |
| 75 | + case _: |
| 76 | + writeEscape(scanner.readChar()); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + loop: |
| 81 | + while (true) { |
| 82 | + switch (scanner.peekChar()) { |
| 83 | + case null: |
| 84 | + break loop; |
| 85 | + |
| 86 | + case 0: |
| 87 | + scanner.error("The U+0000 can't be represented as a CSS identifier."); |
| 88 | + |
| 89 | + case int character when character.isHighSurrogate: |
| 90 | + consumeSurrogatePair(character); |
| 91 | + |
| 92 | + case int(isLowSurrogate: true): |
| 93 | + scanner.error( |
| 94 | + "An individual surrogate can't be represented as a CSS " |
| 95 | + "identifier.", |
| 96 | + length: 1); |
| 97 | + |
| 98 | + case int(isName: true, isPrivateUseBMP: false): |
| 99 | + buffer.writeCharCode(scanner.readChar()); |
| 100 | + |
| 101 | + case _: |
| 102 | + writeEscape(scanner.readChar()); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return buffer.toString(); |
| 107 | + } |
| 108 | +} |
0 commit comments