PageRenderTime 13ms CodeModel.GetById 9ms RepoModel.GetById 0ms 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. */
  18. package org.fusesource.scalate.scuery.support
  19. import xml.Node
  20. /**
  21. * Represents a matcher on a set of nodes, typically used on attributes in CSS3 selectors
  22. *
  23. * @version $Revision: 1.1 $
  24. */
  25. trait Matcher {
  26. def matches(nodes: Seq[Node]): Boolean
  27. }
  28. abstract class TextMatcher extends Matcher {
  29. def matches(nodes: Seq[Node]): Boolean = {
  30. val text = nodes.mkString(" ")
  31. matches(text)
  32. }
  33. def matches(text: String): Boolean
  34. }
  35. case class EqualsMatch(expected: String) extends TextMatcher {
  36. def matches(text: String) = text == expected
  37. }
  38. case class PrefixMatch(prefix: String) extends TextMatcher {
  39. def matches(text: String) = text.startsWith(prefix)
  40. }
  41. case class SuffixMatch(suffix: String) extends TextMatcher {
  42. def matches(text: String) = text.endsWith(suffix)
  43. }
  44. case class SubstringMatch(substring: String) extends TextMatcher {
  45. def matches(text: String) = text.contains(substring)
  46. }
  47. /**
  48. * Matches a whole word after splitting up the value by whitespace
  49. */
  50. case class IncludesMatch(word: String) extends TextMatcher {
  51. def matches(text: String) = text.split("\\s").contains(word)
  52. }
  53. /**
  54. * Matches text starting with the given value or with
  55. * value immediately followed by "-" (U+002D)
  56. */
  57. case class DashMatch(value: String) extends TextMatcher {
  58. private[this] val valueWithDash = value + "-"
  59. def matches(text: String) = text.startsWith(value) || text.startsWith(valueWithDash)
  60. }
  61. object MatchesAny extends TextMatcher {
  62. def matches(text: String) = true
  63. }