Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions go/src/closure/template/soyregexp/soyregexp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package soyregexp

import "regexp"

type Matcher interface {
MatchString(string) bool
}

type Regexp struct {
MustMatch bool
RegexpString string
re *regexp.Regexp
}

func (self *Regexp) MustCompile() {
self.re = regexp.MustCompile(self.RegexpString)
}

func (self *Regexp) Matches(s string) bool {
if self.re == nil {
self.re = regexp.MustCompile(self.RegexpString)
}
return self.re.MatchString(s) == self.MustMatch
}

type RegexpSlice struct {
regExps []*Regexp
}

func (self *RegexpSlice) MatchString(s string) bool {
for _, soyRegexp := range self.regExps {
if !soyRegexp.Matches(s) {
return false
}
}
return true
}

func MustCompile(regExps []*Regexp) *RegexpSlice {
for _, soyRegexp := range regExps {
soyRegexp.MustCompile()
}
return &RegexpSlice{regExps}
}
19 changes: 19 additions & 0 deletions go/src/closure/template/soyregexp/soyregexp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package soyregexp

import "testing"

func TestRegexp(t *testing.T) {
rs := MustCompile([]*Regexp{
&Regexp{MustMatch: false, RegexpString: "^(-*(?:(expression|(?:moz-)?binding)))"},
&Regexp{MustMatch: true, RegexpString: "!important"},
})
if rs.MatchString("-moz-binding: none !important;") {
t.Errorf("Expected soy regexp slice to return false as string starts with -moz-binding, but got true")
}
if rs.MatchString("color: red !important;") == false {
t.Errorf("Expected soy regexp slice to return true, but got false")
}
if rs.MatchString("color: red;") {
t.Errorf("Expected soy regexp slice to return false, but got true")
}
}
183 changes: 84 additions & 99 deletions go/src/closure/template/soyutil/bidi.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package soyutil;
package soyutil

import (
"strings"
"strings"
)


/**
* Strips str of any HTML mark-up and escapes. Imprecise in several ways, but
* precision is not very important, since the result is only meant to be used
Expand All @@ -16,13 +15,12 @@ import (
* @private
*/
func BidiStripHtmlIfNecessary(str string, opt_isHtml bool) string {
if opt_isHtml {
return _BIDI_HTML_SKIP_RE.ReplaceAllString(str, " ")
}
return str
if opt_isHtml {
return _BIDI_HTML_SKIP_RE.ReplaceAllString(str, " ")
}
return str
}


/**
* Estimate the overall directionality of text. If opt_isHtml, makes sure to
* ignore the LTR nature of the mark-up and escapes in text, making the logic
Expand All @@ -33,16 +31,15 @@ func BidiStripHtmlIfNecessary(str string, opt_isHtml bool) string {
* @return {number} 1 if text is LTR, -1 if it is RTL, and 0 if it is neutral.
*/
func BidiTextDir(text string, opt_isHtml bool) int {
text = BidiStripHtmlIfNecessary(text, opt_isHtml);
if len(text) == 0 {
return 0
}
if BidiDetectRtlDirectionality(text) {
return -1
}
return 1
};

text = BidiStripHtmlIfNecessary(text, opt_isHtml)
if len(text) == 0 {
return 0
}
if BidiDetectRtlDirectionality(text) {
return -1
}
return 1
}

/**
* Returns "dir=ltr" or "dir=rtl", depending on text's estimated
Expand All @@ -59,18 +56,18 @@ func BidiTextDir(text string, opt_isHtml bool) int {
* text in non-LTR context; else, the empty string.
*/
func BidiDirAttr(bidiGlobalDir int, text string, opt_isHtml bool) string {
dir := BidiTextDir(text, opt_isHtml)
switch {
case dir == bidiGlobalDir:
return ""
case dir < 0:
return "dir=rtl"
case dir > 0:
return "dir=ltr"
default:
return ""
}
return ""
dir := BidiTextDir(text, opt_isHtml)
switch {
case dir == bidiGlobalDir:
return ""
case dir < 0:
return "dir=rtl"
case dir > 0:
return "dir=ltr"
default:
return ""
}
return ""
}

/**
Expand All @@ -89,11 +86,10 @@ func BidiDirAttr(bidiGlobalDir int, text string, opt_isHtml bool) string {
* bidiGlobalDir.
*/
func BidiMarkAfter(bidiGlobalDir int, text string, opt_isHtml bool) string {
dir := BidiTextDir(text, opt_isHtml)
return BidiMarkAfterKnownDir(bidiGlobalDir, dir, text, opt_isHtml)
dir := BidiTextDir(text, opt_isHtml)
return BidiMarkAfterKnownDir(bidiGlobalDir, dir, text, opt_isHtml)
}


/**
* Returns a Unicode BiDi mark matching bidiGlobalDir (LRM or RLM) if the
* directionality or the exit directionality of text are opposite to
Expand All @@ -111,18 +107,17 @@ func BidiMarkAfter(bidiGlobalDir int, text string, opt_isHtml bool) string {
* bidiGlobalDir.
*/
func BidiMarkAfterKnownDir(bidiGlobalDir int, dir int, text string, opt_isHtml bool) string {
switch {
case bidiGlobalDir > 0 && (dir < 0 || BidiIsRtlExitText(text, opt_isHtml)):
return "\u200E" // LRM
case bidiGlobalDir < 0 && (dir > 0 || BidiIsLtrExitText(text, opt_isHtml)):
return "\u200F" // RLM
default:
return ""
}
return ""
switch {
case bidiGlobalDir > 0 && (dir < 0 || BidiIsRtlExitText(text, opt_isHtml)):
return "\u200E" // LRM
case bidiGlobalDir < 0 && (dir > 0 || BidiIsLtrExitText(text, opt_isHtml)):
return "\u200F" // RLM
default:
return ""
}
return ""
}


/**
* Returns str wrapped in a <span dir=ltr|rtl> according to its directionality -
* but only if that is neither neutral nor the same as the global context.
Expand All @@ -137,21 +132,20 @@ func BidiMarkAfterKnownDir(bidiGlobalDir int, dir int, text string, opt_isHtml b
* @return {string} The wrapped string.
*/
func BidiSpanWrap(bidiGlobalDir int, str string, isHtml bool) string {
var output string
textDir := BidiTextDir(str, isHtml)
reset := BidiMarkAfterKnownDir(bidiGlobalDir, textDir, str, isHtml)
switch {
case textDir > 0 && bidiGlobalDir <= 0:
output = "<span dir=\"ltr\">" + str + "</span>"
case textDir < 0 && bidiGlobalDir >= 0:
output = "<span dir=\"rtl\">" + str + "</span>"
default:
output = str
}
return output + reset
var output string
textDir := BidiTextDir(str, isHtml)
reset := BidiMarkAfterKnownDir(bidiGlobalDir, textDir, str, isHtml)
switch {
case textDir > 0 && bidiGlobalDir <= 0:
output = "<span dir=\"ltr\">" + str + "</span>"
case textDir < 0 && bidiGlobalDir >= 0:
output = "<span dir=\"rtl\">" + str + "</span>"
default:
output = str
}
return output + reset
}


/**
* Returns str wrapped in Unicode BiDi formatting characters according to its
* directionality, i.e. either LRE or RLE at the beginning and PDF at the end -
Expand All @@ -167,21 +161,20 @@ func BidiSpanWrap(bidiGlobalDir int, str string, isHtml bool) string {
* @return {string} The wrapped string.
*/
func BidiUnicodeWrap(bidiGlobalDir int, str string, isHtml bool) string {
var output string
textDir := BidiTextDir(str, isHtml)
reset := BidiMarkAfterKnownDir(bidiGlobalDir, textDir, str, isHtml)
switch {
case textDir > 0 && bidiGlobalDir <= 0:
output = "\u202A" + str + "\u202C"
case textDir < 0 && bidiGlobalDir >= 0:
output = "\u202B" + str + "\u202C"
default:
output = str
}
return output + reset
var output string
textDir := BidiTextDir(str, isHtml)
reset := BidiMarkAfterKnownDir(bidiGlobalDir, textDir, str, isHtml)
switch {
case textDir > 0 && bidiGlobalDir <= 0:
output = "\u202A" + str + "\u202C"
case textDir < 0 && bidiGlobalDir >= 0:
output = "\u202B" + str + "\u202C"
default:
output = str
}
return output + reset
}


/**
* Check the directionality of the a piece of text based on the first character
* with strong directionality.
Expand All @@ -190,10 +183,9 @@ func BidiUnicodeWrap(bidiGlobalDir int, str string, isHtml bool) string {
* @private
*/
func BidiIsRtlText(str string) bool {
return _BIDI_RTL_DIR_CHECK_RE.MatchString(str)
return _BIDI_RTL_DIR_CHECK_RE.MatchString(str)
}


/**
* Check the directionality of the a piece of text based on the first character
* with strong directionality.
Expand All @@ -202,35 +194,33 @@ func BidiIsRtlText(str string) bool {
* @private
*/
func BidiIsNeutralText(str string) bool {
return _BIDI_NEUTRAL_DIR_CHECK_RE.MatchString(str)
return _BIDI_NEUTRAL_DIR_CHECK_RE.MatchString(str)
}


/**
* Returns the RTL ratio based on word count.
* @param {string} str the string that need to be checked.
* @return {number} the ratio of RTL words among all words with directionality.
* @private
*/
func BidiRtlWordRatio(str string) float64 {
rtlCount := 0
totalCount := 0
tokens := strings.SplitN(str, " ", -1)
for _, token := range tokens {
if BidiIsRtlText(token) {
rtlCount++
totalCount++
} else if BidiIsNeutralText(token) {
totalCount++
}
}
if totalCount == 0 {
return 0
}
return float64(rtlCount) / float64(totalCount)
rtlCount := 0
totalCount := 0
tokens := strings.SplitN(str, " ", -1)
for _, token := range tokens {
if BidiIsRtlText(token) {
rtlCount++
totalCount++
} else if BidiIsNeutralText(token) {
totalCount++
}
}
if totalCount == 0 {
return 0
}
return float64(rtlCount) / float64(totalCount)
}


/**
* Check the directionality of a piece of text, return true if the piece of
* text should be laid out in RTL direction.
Expand All @@ -239,10 +229,9 @@ func BidiRtlWordRatio(str string) float64 {
* @private
*/
func BidiDetectRtlDirectionality(str string) bool {
return BidiRtlWordRatio(str) > _BIDI_RTL_DETECTION_THRESHOLD
return BidiRtlWordRatio(str) > _BIDI_RTL_DETECTION_THRESHOLD
}


/**
* Check if the exit directionality a piece of text is LTR, i.e. if the last
* strongly-directional character in the string is LTR.
Expand All @@ -253,11 +242,10 @@ func BidiDetectRtlDirectionality(str string) bool {
* @private
*/
func BidiIsLtrExitText(str string, opt_isHtml bool) bool {
testString := BidiStripHtmlIfNecessary(str, opt_isHtml)
return _BIDI_LTR_EXIT_DIR_CHECK_RE.MatchString(testString)
testString := BidiStripHtmlIfNecessary(str, opt_isHtml)
return _BIDI_LTR_EXIT_DIR_CHECK_RE.MatchString(testString)
}


/**
* Check if the exit directionality a piece of text is RTL, i.e. if the last
* strongly-directional character in the string is RTL.
Expand All @@ -268,9 +256,6 @@ func BidiIsLtrExitText(str string, opt_isHtml bool) bool {
* @private
*/
func BidiIsRtlExitText(str string, opt_isHtml bool) bool {
testString := BidiStripHtmlIfNecessary(str, opt_isHtml)
return _BIDI_RTL_EXIT_DIR_CHECK_RE.MatchString(testString)
testString := BidiStripHtmlIfNecessary(str, opt_isHtml)
return _BIDI_RTL_EXIT_DIR_CHECK_RE.MatchString(testString)
}



Loading