PageRenderTime 24ms CodeModel.GetById 17ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-core/src/main/scala/org/fusesource/scalate/scuery/support/Matcher.scala

http://github.com/scalate/scalate
Scala | 73 lines | 34 code | 10 blank | 29 comment | 2 complexity | aa29ec1247ba805450cbcb3779f03a52 MD5 | raw file
 1/**
 2 * Copyright (C) 2009-2011 the original author or authors.
 3 * See the notice.md file distributed with this work for additional
 4 * information regarding copyright ownership.
 5 *
 6 * Licensed under the Apache License, Version 2.0 (the "License");
 7 * you may not use this file except in compliance with the License.
 8 * You may obtain a copy of the License at
 9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package org.fusesource.scalate.scuery.support
19
20import xml.Node
21
22/**
23 * Represents a matcher on a set of nodes, typically used on attributes in CSS3 selectors
24 *
25 * @version $Revision: 1.1 $
26 */
27trait Matcher {
28  def matches(nodes: Seq[Node]): Boolean
29}
30
31abstract class TextMatcher extends Matcher {
32  def matches(nodes: Seq[Node]): Boolean = {
33    val text = nodes.mkString(" ")
34    matches(text)
35  }
36
37  def matches(text: String): Boolean
38}
39
40case class EqualsMatch(expected: String) extends TextMatcher {
41  def matches(text: String) = text == expected
42}
43
44case class PrefixMatch(prefix: String) extends TextMatcher {
45  def matches(text: String) = text.startsWith(prefix)
46}
47
48case class SuffixMatch(suffix: String) extends TextMatcher {
49  def matches(text: String) = text.endsWith(suffix)
50}
51case class SubstringMatch(substring: String) extends TextMatcher {
52  def matches(text: String) = text.contains(substring)
53}
54
55/**
56 * Matches a whole word after splitting up the value by whitespace
57 */
58case class IncludesMatch(word: String) extends TextMatcher {
59  def matches(text: String) = text.split("\\s").contains(word)
60}
61/**
62 * Matches text starting with the given value or with
63 * value immediately followed by "-" (U+002D)
64 */
65case class DashMatch(value: String) extends TextMatcher {
66  private[this] val valueWithDash = value + "-"
67
68  def matches(text: String) = text.startsWith(value) || text.startsWith(valueWithDash)
69}
70
71object MatchesAny extends TextMatcher {
72  def matches(text: String) = true
73}