|
| 1 | +/* istanbul ignore file */ |
| 2 | + |
| 3 | + |
| 4 | +// normalize-selector-rev-02.js |
| 5 | +/* |
| 6 | + author: kyle simpson (@getify) |
| 7 | + original source: https://gist.github.com/getify/9679380 |
| 8 | +
|
| 9 | + modified for tests by david kaye (@dfkaye) |
| 10 | + 21 march 2014 |
| 11 | +
|
| 12 | + rev-02 incorporate kyle's changes 3/2/42014 |
| 13 | +*/ |
| 14 | + |
| 15 | +export function normalizeSelector(sel) { |
| 16 | + // save unmatched text, if any |
| 17 | + function saveUnmatched() { |
| 18 | + if (unmatched) { |
| 19 | + // whitespace needed after combinator? |
| 20 | + if (tokens.length > 0 && /^[~+>]$/.test(tokens[tokens.length - 1])) { |
| 21 | + tokens.push(" "); |
| 22 | + } |
| 23 | + |
| 24 | + // save unmatched text |
| 25 | + tokens.push(unmatched); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + var tokens = [], |
| 30 | + match, |
| 31 | + unmatched, |
| 32 | + regex, |
| 33 | + state = [0], |
| 34 | + next_match_idx = 0, |
| 35 | + prev_match_idx, |
| 36 | + not_escaped_pattern = /(?:[^\\]|(?:^|[^\\])(?:\\\\)+)$/, |
| 37 | + whitespace_pattern = /^\s+$/, |
| 38 | + state_patterns = [ |
| 39 | + /\s+|\/\*|["'>~+\[\(]/g, // general |
| 40 | + /\s+|\/\*|["'\[\]\(\)]/g, // [..] set |
| 41 | + /\s+|\/\*|["'\[\]\(\)]/g, // (..) set |
| 42 | + null, // string literal (placeholder) |
| 43 | + /\*\//g, // comment |
| 44 | + ]; |
| 45 | + sel = sel.trim(); |
| 46 | + |
| 47 | + while (true) { |
| 48 | + unmatched = ""; |
| 49 | + |
| 50 | + regex = state_patterns[state[state.length - 1]]; |
| 51 | + |
| 52 | + regex.lastIndex = next_match_idx; |
| 53 | + match = regex.exec(sel); |
| 54 | + |
| 55 | + // matched text to process? |
| 56 | + if (match) { |
| 57 | + prev_match_idx = next_match_idx; |
| 58 | + next_match_idx = regex.lastIndex; |
| 59 | + |
| 60 | + // collect the previous string chunk not matched before this token |
| 61 | + if (prev_match_idx < next_match_idx - match[0].length) { |
| 62 | + unmatched = sel.substring( |
| 63 | + prev_match_idx, |
| 64 | + next_match_idx - match[0].length |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + // general, [ ] pair, ( ) pair? |
| 69 | + if (state[state.length - 1] < 3) { |
| 70 | + saveUnmatched(); |
| 71 | + |
| 72 | + // starting a [ ] pair? |
| 73 | + if (match[0] === "[") { |
| 74 | + state.push(1); |
| 75 | + } |
| 76 | + // starting a ( ) pair? |
| 77 | + else if (match[0] === "(") { |
| 78 | + state.push(2); |
| 79 | + } |
| 80 | + // starting a string literal? |
| 81 | + else if (/^["']$/.test(match[0])) { |
| 82 | + state.push(3); |
| 83 | + state_patterns[3] = new RegExp(match[0], "g"); |
| 84 | + } |
| 85 | + // starting a comment? |
| 86 | + else if (match[0] === "/*") { |
| 87 | + state.push(4); |
| 88 | + } |
| 89 | + // ending a [ ] or ( ) pair? |
| 90 | + else if (/^[\]\)]$/.test(match[0]) && state.length > 0) { |
| 91 | + state.pop(); |
| 92 | + } |
| 93 | + // handling whitespace or a combinator? |
| 94 | + else if (/^(?:\s+|[~+>])$/.test(match[0])) { |
| 95 | + // need to insert whitespace before? |
| 96 | + if ( |
| 97 | + tokens.length > 0 && |
| 98 | + !whitespace_pattern.test(tokens[tokens.length - 1]) && |
| 99 | + state[state.length - 1] === 0 |
| 100 | + ) { |
| 101 | + // add normalized whitespace |
| 102 | + tokens.push(" "); |
| 103 | + } |
| 104 | + |
| 105 | + // case-insensitive attribute selector CSS L4 |
| 106 | + if ( |
| 107 | + state[state.length - 1] === 1 && |
| 108 | + tokens.length === 5 && |
| 109 | + tokens[2].charAt(tokens[2].length - 1) === "=" |
| 110 | + ) { |
| 111 | + tokens[4] = " " + tokens[4]; |
| 112 | + } |
| 113 | + |
| 114 | + // whitespace token we can skip? |
| 115 | + if (whitespace_pattern.test(match[0])) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // save matched text |
| 121 | + tokens.push(match[0]); |
| 122 | + } |
| 123 | + // otherwise, string literal or comment |
| 124 | + else { |
| 125 | + // save unmatched text |
| 126 | + tokens[tokens.length - 1] += unmatched; |
| 127 | + |
| 128 | + // unescaped terminator to string literal or comment? |
| 129 | + if (not_escaped_pattern.test(tokens[tokens.length - 1])) { |
| 130 | + // comment terminator? |
| 131 | + if (state[state.length - 1] === 4) { |
| 132 | + // ok to drop comment? |
| 133 | + if ( |
| 134 | + tokens.length < 2 || |
| 135 | + whitespace_pattern.test(tokens[tokens.length - 2]) |
| 136 | + ) { |
| 137 | + tokens.pop(); |
| 138 | + } |
| 139 | + // otherwise, turn comment into whitespace |
| 140 | + else { |
| 141 | + tokens[tokens.length - 1] = " "; |
| 142 | + } |
| 143 | + |
| 144 | + // handled already |
| 145 | + match[0] = ""; |
| 146 | + } |
| 147 | + |
| 148 | + state.pop(); |
| 149 | + } |
| 150 | + |
| 151 | + // append matched text to existing token |
| 152 | + tokens[tokens.length - 1] += match[0]; |
| 153 | + } |
| 154 | + } |
| 155 | + // otherwise, end of processing (no more matches) |
| 156 | + else { |
| 157 | + unmatched = sel.substr(next_match_idx); |
| 158 | + saveUnmatched(); |
| 159 | + |
| 160 | + break; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return tokens.join("").trim(); |
| 165 | +} |
0 commit comments