Skip to content
Merged
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
4 changes: 2 additions & 2 deletions packages/lib/src/values/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ const selectCss = (el: AnyHtmlNode, selector: string, expand: boolean) => {
}

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

Expand Down
8 changes: 4 additions & 4 deletions test/calls.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ describe('calls', () => {
Data: `
inputs { text }

extract xpath://div[p[contains(text(), '$text')]]
-> xpath:@data-json
extract //div[p[contains(text(), '$text')]]
-> ./@data-json
-> @json
`,
}
Expand Down Expand Up @@ -291,13 +291,13 @@ describe('calls', () => {
GET http://stub

extract @Data({text: |'first'|})
-> xpath:@data-json
-> ./@data-json
-> @json
`,
Data: `
inputs { text }

extract xpath://div[p[contains(text(), '$text')]]
extract //div[p[contains(text(), '$text')]]
`,
}

Expand Down
2 changes: 1 addition & 1 deletion test/request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ describe('request', () => {
extract {
link1: link -> @link
link2: anchor -> @html -> a -> @link
link3: attr -> @html -> i -> xpath:@data-url -> @link
link3: attr -> @html -> i -> ./@data-url -> @link
link4: img -> @html -> img -> @link
}
`,
Expand Down
2 changes: 1 addition & 1 deletion test/slice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('slice', () => {
it('converts context to value prior to run', async () => {
const result = await execute(`
set html = |'<ul><li>one</li><li>two</li></ul>'|
extract $html -> @html -> xpath://li -> |$|
extract $html -> @html -> //li -> |$|
`)
expect(result).toEqual('one')
})
Expand Down
8 changes: 4 additions & 4 deletions test/values.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,18 @@ describe('values', () => {
test('xpath selector', async () => {
const result = await execute(`
set html = |"<html><h1>unweb</h1><p class='intro'>welcome</p><html>"|
extract $html -> @html -> xpath://p/@class
extract $html -> @html -> //p/@class
`)
expect(result).toEqual('intro')
})

test.if(SELSYN)('xpath parsing error', async () => {
const result = execute(`
set html = |'<div>test</div>'|
extract $html -> @html -> xpath:p/*&@#^
extract $html -> @html -> ./p/*&@#^
`)
return expect(result).rejects.toThrow(
new SelectorSyntaxError('XPath', 'p/*&@#^'),
new SelectorSyntaxError('XPath', './p/*&@#^'),
)
})

Expand Down Expand Up @@ -354,7 +354,7 @@ describe('values', () => {
-> @json -> docHtml
-> @html -> pre
-> @js -> Literal
-> @html -> xpath://p/@cooked
-> @html -> //p/@cooked
-> @cookies -> patientZero
`

Expand Down