|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +module.exports = match; |
| 4 | + |
| 5 | +var camelcase = require('camelcase'); |
| 6 | +var zwitch = require('zwitch'); |
| 7 | +var has = require('hast-util-has-property'); |
| 8 | +var information = require('property-information'); |
| 9 | +var spaceSeparated = require('space-separated-tokens'); |
| 10 | +var commaSeparated = require('comma-separated-tokens'); |
| 11 | + |
| 12 | +var handle = zwitch('operator'); |
| 13 | +var handlers = handle.handlers; |
| 14 | + |
| 15 | +match.support = ['~', '|', '^', '$', '*']; |
| 16 | + |
| 17 | +handle.unknown = unknownOperator; |
| 18 | +handle.invalid = exists; |
| 19 | +handlers['='] = exact; |
| 20 | +handlers['~='] = spaceSeparatedList; |
| 21 | +handlers['|='] = exactOrPrefix; |
| 22 | +handlers['^='] = begins; |
| 23 | +handlers['$='] = ends; |
| 24 | +handlers['*='] = contains; |
| 25 | + |
| 26 | +function match(query, node) { |
| 27 | + var attrs = query.attrs; |
| 28 | + var length = attrs.length; |
| 29 | + var index = -1; |
| 30 | + var info; |
| 31 | + var attr; |
| 32 | + |
| 33 | + while (++index < length) { |
| 34 | + attr = attrs[index]; |
| 35 | + info = information(attr.name) || {}; |
| 36 | + attr.propertyName = info.propertyName || camelcase(attr.name); |
| 37 | + |
| 38 | + if (!handle(attr, node, info)) { |
| 39 | + return false; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return true; |
| 44 | +} |
| 45 | + |
| 46 | +/* [attr] */ |
| 47 | +function exists(query, node) { |
| 48 | + return has(node, query.propertyName); |
| 49 | +} |
| 50 | + |
| 51 | +/* [attr=value] */ |
| 52 | +function exact(query, node, info) { |
| 53 | + if (!has(node, query.propertyName)) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + return normalizeValue(node.properties[query.propertyName], info) === query.value; |
| 58 | +} |
| 59 | + |
| 60 | +/* [attr~=value] */ |
| 61 | +function spaceSeparatedList(query, node, info) { |
| 62 | + var val; |
| 63 | + |
| 64 | + if (!has(node, query.propertyName)) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + val = node.properties[query.propertyName]; |
| 69 | + |
| 70 | + /* If this is a comma-separated list, and the query is contained in it, |
| 71 | + * return true. */ |
| 72 | + if ( |
| 73 | + typeof val === 'object' && |
| 74 | + !info.commaSeparated && |
| 75 | + val.indexOf(query.value) !== -1 |
| 76 | + ) { |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + /* For all other values (including comma-separated lists), |
| 81 | + * return whether this is an exact match. */ |
| 82 | + return normalizeValue(val, info) === query.value; |
| 83 | +} |
| 84 | + |
| 85 | +/* [attr|=value] */ |
| 86 | +function exactOrPrefix(query, node, info) { |
| 87 | + var value; |
| 88 | + |
| 89 | + if (!has(node, query.propertyName)) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + value = normalizeValue(node.properties[query.propertyName], info); |
| 94 | + |
| 95 | + return Boolean( |
| 96 | + value === query.value || |
| 97 | + ( |
| 98 | + value.slice(0, query.value.length) === query.value && |
| 99 | + value.charAt(query.value.length) === '-' |
| 100 | + ) |
| 101 | + ); |
| 102 | +} |
| 103 | + |
| 104 | +/* [attr^=value] */ |
| 105 | +function begins(query, node, info) { |
| 106 | + if (!has(node, query.propertyName)) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + return normalizeValue(node.properties[query.propertyName], info) |
| 111 | + .slice(0, query.value.length) === query.value; |
| 112 | +} |
| 113 | + |
| 114 | +/* [attr$=value] */ |
| 115 | +function ends(query, node, info) { |
| 116 | + if (!has(node, query.propertyName)) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + return normalizeValue(node.properties[query.propertyName], info) |
| 121 | + .slice(-query.value.length) === query.value; |
| 122 | +} |
| 123 | + |
| 124 | +/* [attr*=value] */ |
| 125 | +function contains(query, node, info) { |
| 126 | + if (!has(node, query.propertyName)) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + return normalizeValue(node.properties[query.propertyName], info) |
| 131 | + .indexOf(query.value) !== -1; |
| 132 | +} |
| 133 | + |
| 134 | +/* istanbul ignore next - Shouldn’t be invoked, Parser throws an error instead. */ |
| 135 | +function unknownOperator(query) { |
| 136 | + throw new Error('Unknown operator `' + query.operator + '`'); |
| 137 | +} |
| 138 | + |
| 139 | +/* Stringify a HAST value back to its HTML form. */ |
| 140 | +function normalizeValue(value, info) { |
| 141 | + if (info.commaSeparated) { |
| 142 | + value = commaSeparated.stringify(value); |
| 143 | + } else if (info.spaceSeparated) { |
| 144 | + value = spaceSeparated.stringify(value); |
| 145 | + } else if (info.boolean) { |
| 146 | + /* `false` is ignored by `has()`. */ |
| 147 | + value = info.name; |
| 148 | + } else if (info.overloadedBoolean) { |
| 149 | + if (value === true) { |
| 150 | + value = info.name; |
| 151 | + } |
| 152 | + } else if (info.positiveNumeric || info.numeric) { |
| 153 | + value = String(value); |
| 154 | + } |
| 155 | + |
| 156 | + return value; |
| 157 | +} |
0 commit comments