| 1 |
package xpath |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"math" |
| 7 |
"strconv" |
| 8 |
"strings" |
| 9 |
"sync" |
| 10 |
"unicode" |
| 11 |
) |
| 12 |
|
| 13 |
// Defined an interface of stringBuilder that compatible with |
| 14 |
// strings.Builder(go 1.10) and bytes.Buffer(< go 1.10) |
| 15 |
type stringBuilder interface { |
| 16 |
WriteRune(r rune) (n int, err error) |
| 17 |
WriteString(s string) (int, error) |
| 18 |
Reset() |
| 19 |
Grow(n int) |
| 20 |
String() string |
| 21 |
} |
| 22 |
|
| 23 |
var builderPool = sync.Pool{New: func() interface{} { |
| 24 |
return newStringBuilder() |
| 25 |
}} |
| 26 |
|
| 27 |
// The XPath function list. |
| 28 |
|
| 29 |
func predicate(q query) func(NodeNavigator) bool { |
| 30 |
type Predicater interface { |
| 31 |
Test(NodeNavigator) bool |
| 32 |
} |
| 33 |
if p, ok := q.(Predicater); ok { |
| 34 |
return p.Test |
| 35 |
} |
| 36 |
return func(NodeNavigator) bool { return true } |
| 37 |
} |
| 38 |
|
| 39 |
// positionFunc is a XPath Node Set functions position(). |
| 40 |
func positionFunc() func(query, iterator) interface{} { |
| 41 |
return func(q query, t iterator) interface{} { |
| 42 |
var ( |
| 43 |
count = 1 |
| 44 |
node = t.Current().Copy() |
| 45 |
) |
| 46 |
test := predicate(q) |
| 47 |
for node.MoveToPrevious() { |
| 48 |
if test(node) { |
| 49 |
count++ |
| 50 |
} |
| 51 |
} |
| 52 |
return float64(count) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// lastFunc is a XPath Node Set functions last(). |
| 57 |
func lastFunc() func(query, iterator) interface{} { |
| 58 |
return func(q query, t iterator) interface{} { |
| 59 |
var ( |
| 60 |
count = 0 |
| 61 |
node = t.Current().Copy() |
| 62 |
) |
| 63 |
test := predicate(q) |
| 64 |
node.MoveToFirst() |
| 65 |
for { |
| 66 |
if test(node) { |
| 67 |
count++ |
| 68 |
} |
| 69 |
if !node.MoveToNext() { |
| 70 |
break |
| 71 |
} |
| 72 |
} |
| 73 |
return float64(count) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// countFunc is a XPath Node Set functions count(node-set). |
| 78 |
func countFunc(arg query) func(query, iterator) interface{} { |
| 79 |
return func(_ query, t iterator) interface{} { |
| 80 |
var count = 0 |
| 81 |
q := functionArgs(arg) |
| 82 |
test := predicate(q) |
| 83 |
switch typ := q.Evaluate(t).(type) { |
| 84 |
case query: |
| 85 |
for node := typ.Select(t); node != nil; node = typ.Select(t) { |
| 86 |
if test(node) { |
| 87 |
count++ |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
return float64(count) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// sumFunc is a XPath Node Set functions sum(node-set). |
| 96 |
func sumFunc(arg query) func(query, iterator) interface{} { |
| 97 |
return func(_ query, t iterator) interface{} { |
| 98 |
var sum float64 |
| 99 |
switch typ := functionArgs(arg).Evaluate(t).(type) { |
| 100 |
case query: |
| 101 |
for node := typ.Select(t); node != nil; node = typ.Select(t) { |
| 102 |
if v, err := strconv.ParseFloat(node.Value(), 64); err == nil { |
| 103 |
sum += v |
| 104 |
} |
| 105 |
} |
| 106 |
case float64: |
| 107 |
sum = typ |
| 108 |
case string: |
| 109 |
v, err := strconv.ParseFloat(typ, 64) |
| 110 |
if err != nil { |
| 111 |
panic(errors.New("sum() function argument type must be a node-set or number")) |
| 112 |
} |
| 113 |
sum = v |
| 114 |
} |
| 115 |
return sum |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func asNumber(t iterator, o interface{}) float64 { |
| 120 |
switch typ := o.(type) { |
| 121 |
case query: |
| 122 |
node := typ.Select(t) |
| 123 |
if node == nil { |
| 124 |
return math.NaN() |
| 125 |
} |
| 126 |
return stringToNumber(node.Value()) |
| 127 |
case float64: |
| 128 |
return typ |
| 129 |
case string: |
| 130 |
return stringToNumber(typ) |
| 131 |
case bool: |
| 132 |
if typ { |
| 133 |
return 1 |
| 134 |
} |
| 135 |
return 0 |
| 136 |
} |
| 137 |
return math.NaN() |
| 138 |
} |
| 139 |
|
| 140 |
// ceilingFunc is a XPath Node Set functions ceiling(node-set). |
| 141 |
func ceilingFunc(arg query) func(query, iterator) interface{} { |
| 142 |
return func(_ query, t iterator) interface{} { |
| 143 |
val := asNumber(t, functionArgs(arg).Evaluate(t)) |
| 144 |
// if math.IsNaN(val) { |
| 145 |
// panic(errors.New("ceiling() function argument type must be a valid number")) |
| 146 |
// } |
| 147 |
return math.Ceil(val) |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
// floorFunc is a XPath Node Set functions floor(node-set). |
| 152 |
func floorFunc(arg query) func(query, iterator) interface{} { |
| 153 |
return func(_ query, t iterator) interface{} { |
| 154 |
val := asNumber(t, functionArgs(arg).Evaluate(t)) |
| 155 |
return math.Floor(val) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
// round implements XPath 1.0 round() (REC §4.4): halves round toward +Infinity |
| 160 |
// and NaN/±Infinity pass through, so the result stays a number. |
| 161 |
func round(f float64) float64 { |
| 162 |
if math.IsNaN(f) || math.IsInf(f, 0) { |
| 163 |
return f |
| 164 |
} |
| 165 |
if f >= -0.5 && f < 0.5 { |
| 166 |
return f * 0 // keep the sign of zero per §4.4 |
| 167 |
} |
| 168 |
r := math.Floor(f) |
| 169 |
if f-r >= 0.5 { |
| 170 |
r++ |
| 171 |
} |
| 172 |
return r |
| 173 |
} |
| 174 |
|
| 175 |
// roundFunc is a XPath Node Set functions round(node-set). |
| 176 |
func roundFunc(arg query) func(query, iterator) interface{} { |
| 177 |
return func(_ query, t iterator) interface{} { |
| 178 |
val := asNumber(t, functionArgs(arg).Evaluate(t)) |
| 179 |
return round(val) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
// nameFunc is a XPath functions name([node-set]). |
| 184 |
func nameFunc(arg query) func(query, iterator) interface{} { |
| 185 |
return func(_ query, t iterator) interface{} { |
| 186 |
var v NodeNavigator |
| 187 |
if arg == nil { |
| 188 |
v = t.Current() |
| 189 |
} else { |
| 190 |
v = arg.Clone().Select(t) |
| 191 |
if v == nil { |
| 192 |
return "" |
| 193 |
} |
| 194 |
} |
| 195 |
ns := v.Prefix() |
| 196 |
if ns == "" { |
| 197 |
return v.LocalName() |
| 198 |
} |
| 199 |
return ns + ":" + v.LocalName() |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
// localNameFunc is a XPath functions local-name([node-set]). |
| 204 |
func localNameFunc(arg query) func(query, iterator) interface{} { |
| 205 |
return func(_ query, t iterator) interface{} { |
| 206 |
var v NodeNavigator |
| 207 |
if arg == nil { |
| 208 |
v = t.Current() |
| 209 |
} else { |
| 210 |
v = arg.Clone().Select(t) |
| 211 |
if v == nil { |
| 212 |
return "" |
| 213 |
} |
| 214 |
} |
| 215 |
return v.LocalName() |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
// namespaceFunc is a XPath functions namespace-uri([node-set]). |
| 220 |
func namespaceFunc(arg query) func(query, iterator) interface{} { |
| 221 |
return func(_ query, t iterator) interface{} { |
| 222 |
var v NodeNavigator |
| 223 |
if arg == nil { |
| 224 |
v = t.Current() |
| 225 |
} else { |
| 226 |
// Get the first node in the node-set if specified. |
| 227 |
v = arg.Clone().Select(t) |
| 228 |
if v == nil { |
| 229 |
return "" |
| 230 |
} |
| 231 |
} |
| 232 |
// fix about namespace-uri() bug: https://github.com/antchfx/xmlquery/issues/22 |
| 233 |
// TODO: In the next version, add NamespaceURL() to the NodeNavigator interface. |
| 234 |
type namespaceURL interface { |
| 235 |
NamespaceURL() string |
| 236 |
} |
| 237 |
if f, ok := v.(namespaceURL); ok { |
| 238 |
return f.NamespaceURL() |
| 239 |
} |
| 240 |
return v.Prefix() |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
func asBool(t iterator, v interface{}) bool { |
| 245 |
switch v := v.(type) { |
| 246 |
case nil: |
| 247 |
return false |
| 248 |
case *NodeIterator: |
| 249 |
return v.MoveNext() |
| 250 |
case bool: |
| 251 |
return v |
| 252 |
case float64: |
| 253 |
return v != 0 |
| 254 |
case string: |
| 255 |
return v != "" |
| 256 |
case query: |
| 257 |
return v.Select(t) != nil |
| 258 |
default: |
| 259 |
panic(fmt.Errorf("unexpected type: %T", v)) |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
func stringToNumber(s string) float64 { |
| 264 |
s = strings.TrimSpace(s) |
| 265 |
f, err := strconv.ParseFloat(s, 64) |
| 266 |
if err != nil { |
| 267 |
return math.NaN() |
| 268 |
} |
| 269 |
return f |
| 270 |
} |
| 271 |
|
| 272 |
// formatNumber converts a number to a string per the XPath 1.0 spec (REC 4.2): |
| 273 |
// no exponent for finite values, "Infinity"/"-Infinity", "NaN", and "0" for -0. |
| 274 |
func formatNumber(f float64) string { |
| 275 |
switch { |
| 276 |
case math.IsNaN(f): |
| 277 |
return "NaN" |
| 278 |
case math.IsInf(f, 1): |
| 279 |
return "Infinity" |
| 280 |
case math.IsInf(f, -1): |
| 281 |
return "-Infinity" |
| 282 |
case f == 0: |
| 283 |
return "0" |
| 284 |
default: |
| 285 |
return strconv.FormatFloat(f, 'f', -1, 64) |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
func asString(t iterator, v interface{}) string { |
| 290 |
switch v := v.(type) { |
| 291 |
case nil: |
| 292 |
return "" |
| 293 |
case bool: |
| 294 |
if v { |
| 295 |
return "true" |
| 296 |
} |
| 297 |
return "false" |
| 298 |
case float64: |
| 299 |
return formatNumber(v) |
| 300 |
case string: |
| 301 |
return v |
| 302 |
case query: |
| 303 |
node := v.Select(t) |
| 304 |
if node == nil { |
| 305 |
return "" |
| 306 |
} |
| 307 |
return node.Value() |
| 308 |
default: |
| 309 |
panic(fmt.Errorf("unexpected type: %T", v)) |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
// booleanFunc is a XPath functions boolean([node-set]). |
| 314 |
func booleanFunc(arg1 query) func(query, iterator) interface{} { |
| 315 |
return func(_ query, t iterator) interface{} { |
| 316 |
v := functionArgs(arg1).Evaluate(t) |
| 317 |
return asBool(t, v) |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
// numberFunc is a XPath functions number([node-set]). |
| 322 |
func numberFunc(arg1 query) func(query, iterator) interface{} { |
| 323 |
return func(_ query, t iterator) interface{} { |
| 324 |
v := functionArgs(arg1).Evaluate(t) |
| 325 |
return asNumber(t, v) |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
// stringFunc is a XPath functions string([node-set]). |
| 330 |
func stringFunc(arg1 query) func(query, iterator) interface{} { |
| 331 |
return func(q query, t iterator) interface{} { |
| 332 |
if arg1 == nil { |
| 333 |
return t.Current().Value() |
| 334 |
} |
| 335 |
v := functionArgs(arg1).Evaluate(t) |
| 336 |
return asString(t, v) |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
// startwithFunc is a XPath functions starts-with(string, string). |
| 341 |
func startwithFunc(arg1, arg2 query) func(query, iterator) interface{} { |
| 342 |
return func(_ query, t iterator) interface{} { |
| 343 |
var ( |
| 344 |
m, n string |
| 345 |
ok bool |
| 346 |
) |
| 347 |
switch typ := functionArgs(arg1).Evaluate(t).(type) { |
| 348 |
case string: |
| 349 |
m = typ |
| 350 |
case query: |
| 351 |
node := typ.Select(t) |
| 352 |
if node == nil { |
| 353 |
return false |
| 354 |
} |
| 355 |
m = node.Value() |
| 356 |
default: |
| 357 |
panic(errors.New("starts-with() function argument type must be string")) |
| 358 |
} |
| 359 |
n, ok = functionArgs(arg2).Evaluate(t).(string) |
| 360 |
if !ok { |
| 361 |
panic(errors.New("starts-with() function argument type must be string")) |
| 362 |
} |
| 363 |
return strings.HasPrefix(m, n) |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
// endwithFunc is a XPath functions ends-with(string, string). |
| 368 |
func endwithFunc(arg1, arg2 query) func(query, iterator) interface{} { |
| 369 |
return func(_ query, t iterator) interface{} { |
| 370 |
var ( |
| 371 |
m, n string |
| 372 |
ok bool |
| 373 |
) |
| 374 |
switch typ := functionArgs(arg1).Evaluate(t).(type) { |
| 375 |
case string: |
| 376 |
m = typ |
| 377 |
case query: |
| 378 |
node := typ.Select(t) |
| 379 |
if node == nil { |
| 380 |
return false |
| 381 |
} |
| 382 |
m = node.Value() |
| 383 |
default: |
| 384 |
panic(errors.New("ends-with() function argument type must be string")) |
| 385 |
} |
| 386 |
n, ok = functionArgs(arg2).Evaluate(t).(string) |
| 387 |
if !ok { |
| 388 |
panic(errors.New("ends-with() function argument type must be string")) |
| 389 |
} |
| 390 |
return strings.HasSuffix(m, n) |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
// containsFunc is a XPath functions contains(string or @attr, string). |
| 395 |
func containsFunc(arg1, arg2 query) func(query, iterator) interface{} { |
| 396 |
return func(_ query, t iterator) interface{} { |
| 397 |
var ( |
| 398 |
m, n string |
| 399 |
ok bool |
| 400 |
) |
| 401 |
switch typ := functionArgs(arg1).Evaluate(t).(type) { |
| 402 |
case string: |
| 403 |
m = typ |
| 404 |
case query: |
| 405 |
node := typ.Select(t) |
| 406 |
if node == nil { |
| 407 |
return false |
| 408 |
} |
| 409 |
m = node.Value() |
| 410 |
default: |
| 411 |
panic(errors.New("contains() function argument type must be string")) |
| 412 |
} |
| 413 |
|
| 414 |
n, ok = functionArgs(arg2).Evaluate(t).(string) |
| 415 |
if !ok { |
| 416 |
panic(errors.New("contains() function argument type must be string")) |
| 417 |
} |
| 418 |
|
| 419 |
return strings.Contains(m, n) |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
// matchesFunc is an XPath function that tests a given string against a regexp pattern. |
| 424 |
// Note: does not support https://www.w3.org/TR/xpath-functions-31/#func-matches 3rd optional `flags` argument; if |
| 425 |
// needed, directly put flags in the regexp pattern, such as `(?i)^pattern$` for `i` flag. |
| 426 |
func matchesFunc(arg1, arg2 query) func(query, iterator) interface{} { |
| 427 |
return func(_ query, t iterator) interface{} { |
| 428 |
var s string |
| 429 |
switch typ := functionArgs(arg1).Evaluate(t).(type) { |
| 430 |
case string: |
| 431 |
s = typ |
| 432 |
case query: |
| 433 |
node := typ.Select(t) |
| 434 |
if node == nil { |
| 435 |
return "" |
| 436 |
} |
| 437 |
s = node.Value() |
| 438 |
} |
| 439 |
var pattern string |
| 440 |
var ok bool |
| 441 |
if pattern, ok = functionArgs(arg2).Evaluate(t).(string); !ok { |
| 442 |
panic(errors.New("matches() function second argument type must be string")) |
| 443 |
} |
| 444 |
re, err := getRegexp(pattern) |
| 445 |
if err != nil { |
| 446 |
panic(fmt.Errorf("matches() function second argument is not a valid regexp pattern, err: %s", err.Error())) |
| 447 |
} |
| 448 |
return re.MatchString(s) |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
// normalizespaceFunc is XPath functions normalize-space(string?) |
| 453 |
func normalizespaceFunc(arg1 query) func(query, iterator) interface{} { |
| 454 |
return func(_ query, t iterator) interface{} { |
| 455 |
var m string |
| 456 |
switch typ := functionArgs(arg1).Evaluate(t).(type) { |
| 457 |
case string: |
| 458 |
m = typ |
| 459 |
case query: |
| 460 |
node := typ.Select(t) |
| 461 |
if node == nil { |
| 462 |
return "" |
| 463 |
} |
| 464 |
m = node.Value() |
| 465 |
} |
| 466 |
var b = builderPool.Get().(stringBuilder) |
| 467 |
b.Grow(len(m)) |
| 468 |
|
| 469 |
runeStr := []rune(strings.TrimSpace(m)) |
| 470 |
l := len(runeStr) |
| 471 |
for i := range runeStr { |
| 472 |
r := runeStr[i] |
| 473 |
isSpace := unicode.IsSpace(r) |
| 474 |
if !(isSpace && (i+1 < l && unicode.IsSpace(runeStr[i+1]))) { |
| 475 |
if isSpace { |
| 476 |
r = ' ' |
| 477 |
} |
| 478 |
b.WriteRune(r) |
| 479 |
} |
| 480 |
} |
| 481 |
result := b.String() |
| 482 |
b.Reset() |
| 483 |
builderPool.Put(b) |
| 484 |
|
| 485 |
return result |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
// substringFunc is XPath functions substring function returns a part of a given string. |
| 490 |
func substringFunc(arg1, arg2, arg3 query) func(query, iterator) interface{} { |
| 491 |
return func(_ query, t iterator) interface{} { |
| 492 |
var m string |
| 493 |
switch typ := functionArgs(arg1).Evaluate(t).(type) { |
| 494 |
case string: |
| 495 |
m = typ |
| 496 |
case query: |
| 497 |
node := typ.Select(t) |
| 498 |
if node == nil { |
| 499 |
return "" |
| 500 |
} |
| 501 |
m = node.Value() |
| 502 |
} |
| 503 |
|
| 504 |
var start, length float64 |
| 505 |
var ok bool |
| 506 |
if start, ok = functionArgs(arg2).Evaluate(t).(float64); !ok { |
| 507 |
panic(errors.New("substring() function first argument type must be number")) |
| 508 |
} |
| 509 |
// fix https://github.com/antchfx/xpath/issues/109 |
| 510 |
start = math.Round(start) |
| 511 |
if start > float64(len(m)) { |
| 512 |
return "" |
| 513 |
} |
| 514 |
if arg3 == nil { |
| 515 |
if start <= 0 { |
| 516 |
return m |
| 517 |
} |
| 518 |
return m[int(start)-1:] |
| 519 |
} |
| 520 |
|
| 521 |
if length, ok = functionArgs(arg3).Evaluate(t).(float64); !ok { |
| 522 |
panic(errors.New("substring() function second argument type must be number")) |
| 523 |
} |
| 524 |
length = math.Round(length) |
| 525 |
if length <= 0 { |
| 526 |
return "" |
| 527 |
} |
| 528 |
if length > float64(len(m)) { |
| 529 |
length = float64(len(m)) |
| 530 |
} |
| 531 |
if start < 0 { |
| 532 |
length = length - math.Abs(start) |
| 533 |
if length <= 1 { |
| 534 |
return "" |
| 535 |
} |
| 536 |
return m[:int(length-1)] |
| 537 |
} |
| 538 |
if start == 0 { |
| 539 |
return m[:int(length-1)] |
| 540 |
} |
| 541 |
return m[int(start-1):int(length+start-1)] |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
// substringIndFunc is XPath functions substring-before/substring-after function returns a part of a given string. |
| 546 |
func substringIndFunc(arg1, arg2 query, after bool) func(query, iterator) interface{} { |
| 547 |
return func(_ query, t iterator) interface{} { |
| 548 |
var str string |
| 549 |
switch v := functionArgs(arg1).Evaluate(t).(type) { |
| 550 |
case string: |
| 551 |
str = v |
| 552 |
case query: |
| 553 |
node := v.Select(t) |
| 554 |
if node == nil { |
| 555 |
return "" |
| 556 |
} |
| 557 |
str = node.Value() |
| 558 |
} |
| 559 |
var word string |
| 560 |
switch v := functionArgs(arg2).Evaluate(t).(type) { |
| 561 |
case string: |
| 562 |
word = v |
| 563 |
case query: |
| 564 |
node := v.Select(t) |
| 565 |
if node == nil { |
| 566 |
return "" |
| 567 |
} |
| 568 |
word = node.Value() |
| 569 |
} |
| 570 |
if word == "" { |
| 571 |
return "" |
| 572 |
} |
| 573 |
|
| 574 |
i := strings.Index(str, word) |
| 575 |
if i < 0 { |
| 576 |
return "" |
| 577 |
} |
| 578 |
if after { |
| 579 |
return str[i+len(word):] |
| 580 |
} |
| 581 |
return str[:i] |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
// stringLengthFunc is XPATH string-length( [string] ) function that returns a number |
| 586 |
// equal to the number of characters in a given string. |
| 587 |
func stringLengthFunc(arg1 query) func(query, iterator) interface{} { |
| 588 |
return func(_ query, t iterator) interface{} { |
| 589 |
switch v := functionArgs(arg1).Evaluate(t).(type) { |
| 590 |
case string: |
| 591 |
return float64(len(v)) |
| 592 |
case query: |
| 593 |
node := v.Select(t) |
| 594 |
if node == nil { |
| 595 |
break |
| 596 |
} |
| 597 |
return float64(len(node.Value())) |
| 598 |
} |
| 599 |
return float64(0) |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
// translateFunc is XPath functions translate() function returns a replaced string. |
| 604 |
func translateFunc(arg1, arg2, arg3 query) func(query, iterator) interface{} { |
| 605 |
return func(_ query, t iterator) interface{} { |
| 606 |
str := asString(t, functionArgs(arg1).Evaluate(t)) |
| 607 |
src := asString(t, functionArgs(arg2).Evaluate(t)) |
| 608 |
dst := asString(t, functionArgs(arg3).Evaluate(t)) |
| 609 |
|
| 610 |
replace := make([]string, 0, len(src)) |
| 611 |
for i, s := range src { |
| 612 |
d := "" |
| 613 |
if i < len(dst) { |
| 614 |
d = string(dst[i]) |
| 615 |
} |
| 616 |
replace = append(replace, string(s), d) |
| 617 |
} |
| 618 |
return strings.NewReplacer(replace...).Replace(str) |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
// replaceFunc is XPath functions replace() function returns a replaced string. |
| 623 |
func replaceFunc(arg1, arg2, arg3 query) func(query, iterator) interface{} { |
| 624 |
return func(_ query, t iterator) interface{} { |
| 625 |
str := asString(t, functionArgs(arg1).Evaluate(t)) |
| 626 |
src := asString(t, functionArgs(arg2).Evaluate(t)) |
| 627 |
dst := asString(t, functionArgs(arg3).Evaluate(t)) |
| 628 |
e, err := getRegexp(src) |
| 629 |
if err != nil { |
| 630 |
panic(fmt.Errorf("replace() function second argument is not a valid regexp pattern, err: %s", err.Error())) |
| 631 |
} |
| 632 |
|
| 633 |
// replace all $i to ${i} for golang regexp.Expand |
| 634 |
for idx := e.NumSubexp(); idx > 0; idx-- { |
| 635 |
dst = strings.ReplaceAll(dst, fmt.Sprintf("$%d", idx), fmt.Sprintf("${%d}", idx)) |
| 636 |
} |
| 637 |
|
| 638 |
return e.ReplaceAllString(str, dst) |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
// notFunc is XPATH functions not(expression) function operation. |
| 643 |
func notFunc(arg1 query) func(query, iterator) interface{} { |
| 644 |
return func(_ query, t iterator) interface{} { |
| 645 |
switch v := functionArgs(arg1).Evaluate(t).(type) { |
| 646 |
case bool: |
| 647 |
return !v |
| 648 |
case query: |
| 649 |
node := v.Select(t) |
| 650 |
return node == nil |
| 651 |
default: |
| 652 |
return false |
| 653 |
} |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
// concatFunc is the concat function concatenates two or more |
| 658 |
// strings and returns the resulting string. |
| 659 |
// concat( string1 , string2 [, stringn]* ) |
| 660 |
func concatFunc(args ...query) func(query, iterator) interface{} { |
| 661 |
return func(_ query, t iterator) interface{} { |
| 662 |
b := builderPool.Get().(stringBuilder) |
| 663 |
for _, v := range args { |
| 664 |
v = functionArgs(v) |
| 665 |
b.WriteString(asString(t, v.Evaluate(t))) |
| 666 |
} |
| 667 |
result := b.String() |
| 668 |
b.Reset() |
| 669 |
builderPool.Put(b) |
| 670 |
|
| 671 |
return result |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
// https://github.com/antchfx/xpath/issues/43 |
| 676 |
func functionArgs(q query) query { |
| 677 |
if _, ok := q.(*functionQuery); ok { |
| 678 |
return q |
| 679 |
} |
| 680 |
return q.Clone() |
| 681 |
} |
| 682 |
|
| 683 |
func reverseFunc(q query, t iterator) func() NodeNavigator { |
| 684 |
var list []NodeNavigator |
| 685 |
for { |
| 686 |
node := q.Select(t) |
| 687 |
if node == nil { |
| 688 |
break |
| 689 |
} |
| 690 |
list = append(list, node.Copy()) |
| 691 |
} |
| 692 |
i := len(list) |
| 693 |
return func() NodeNavigator { |
| 694 |
if i <= 0 { |
| 695 |
return nil |
| 696 |
} |
| 697 |
i-- |
| 698 |
node := list[i] |
| 699 |
return node |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
// string-join is a XPath Node Set functions string-join(node-set, separator). |
| 704 |
func stringJoinFunc(q, arg1 query) func(query, iterator) interface{} { |
| 705 |
return func(_ query, t iterator) interface{} { |
| 706 |
var separator string |
| 707 |
switch v := functionArgs(arg1).Evaluate(t).(type) { |
| 708 |
case string: |
| 709 |
separator = v |
| 710 |
case query: |
| 711 |
node := v.Select(t) |
| 712 |
if node != nil { |
| 713 |
separator = node.Value() |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
q = functionArgs(q) |
| 718 |
test := predicate(q) |
| 719 |
var parts []string |
| 720 |
switch v := q.Evaluate(t).(type) { |
| 721 |
case string: |
| 722 |
return v |
| 723 |
case query: |
| 724 |
for node := v.Select(t); node != nil; node = v.Select(t) { |
| 725 |
if test(node) { |
| 726 |
parts = append(parts, node.Value()) |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
return strings.Join(parts, separator) |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
// lower-case is XPATH function that converts a string to lower case. |
| 735 |
func lowerCaseFunc(arg1 query) func(query, iterator) interface{} { |
| 736 |
return func(_ query, t iterator) interface{} { |
| 737 |
v := functionArgs(arg1).Evaluate(t) |
| 738 |
return strings.ToLower(asString(t, v)) |
| 739 |
} |
| 740 |
} |