Skip to content

Commit e00f5d1

Browse files
authored
Merge pull request #50 from getlang-dev/infer-xpath
infer xpath
2 parents 0048a12 + 929228a commit e00f5d1

File tree

5 files changed

+12
-12
lines changed

5 files changed

+12
-12
lines changed

packages/lib/src/values/html.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ const selectCss = (el: AnyHtmlNode, selector: string, expand: boolean) => {
5858
}
5959

6060
export const select = (el: AnyHtmlNode, selector: string, expand: boolean) => {
61-
return selector.startsWith('xpath:')
62-
? selectXpath(el, selector.slice(6), expand)
61+
return /^(\/|\.\/)/.test(selector)
62+
? selectXpath(el, selector, expand)
6363
: selectCss(el, selector, expand)
6464
}
6565

test/calls.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ describe('calls', () => {
260260
Data: `
261261
inputs { text }
262262
263-
extract xpath://div[p[contains(text(), '$text')]]
264-
-> xpath:@data-json
263+
extract //div[p[contains(text(), '$text')]]
264+
-> ./@data-json
265265
-> @json
266266
`,
267267
}
@@ -291,13 +291,13 @@ describe('calls', () => {
291291
GET http://stub
292292
293293
extract @Data({text: |'first'|})
294-
-> xpath:@data-json
294+
-> ./@data-json
295295
-> @json
296296
`,
297297
Data: `
298298
inputs { text }
299299
300-
extract xpath://div[p[contains(text(), '$text')]]
300+
extract //div[p[contains(text(), '$text')]]
301301
`,
302302
}
303303

test/request.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ describe('request', () => {
396396
extract {
397397
link1: link -> @link
398398
link2: anchor -> @html -> a -> @link
399-
link3: attr -> @html -> i -> xpath:@data-url -> @link
399+
link3: attr -> @html -> i -> ./@data-url -> @link
400400
link4: img -> @html -> img -> @link
401401
}
402402
`,

test/slice.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ describe('slice', () => {
8585
it('converts context to value prior to run', async () => {
8686
const result = await execute(`
8787
set html = |'<ul><li>one</li><li>two</li></ul>'|
88-
extract $html -> @html -> xpath://li -> |$|
88+
extract $html -> @html -> //li -> |$|
8989
`)
9090
expect(result).toEqual('one')
9191
})

test/values.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,18 +196,18 @@ describe('values', () => {
196196
test('xpath selector', async () => {
197197
const result = await execute(`
198198
set html = |"<html><h1>unweb</h1><p class='intro'>welcome</p><html>"|
199-
extract $html -> @html -> xpath://p/@class
199+
extract $html -> @html -> //p/@class
200200
`)
201201
expect(result).toEqual('intro')
202202
})
203203

204204
test.if(SELSYN)('xpath parsing error', async () => {
205205
const result = execute(`
206206
set html = |'<div>test</div>'|
207-
extract $html -> @html -> xpath:p/*&@#^
207+
extract $html -> @html -> ./p/*&@#^
208208
`)
209209
return expect(result).rejects.toThrow(
210-
new SelectorSyntaxError('XPath', 'p/*&@#^'),
210+
new SelectorSyntaxError('XPath', './p/*&@#^'),
211211
)
212212
})
213213

@@ -354,7 +354,7 @@ describe('values', () => {
354354
-> @json -> docHtml
355355
-> @html -> pre
356356
-> @js -> Literal
357-
-> @html -> xpath://p/@cooked
357+
-> @html -> //p/@cooked
358358
-> @cookies -> patientZero
359359
`
360360

0 commit comments

Comments
 (0)