| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
QUnit.module( 've.dm.MWInternalLinkAnnotation', ve.test.utils.newMwEnvironment() ); |
| 8 |
|
| 9 |
QUnit.test( 'toDataElement', ( assert ) => { |
| 10 |
|
| 11 |
const getCases = () => { |
| 12 |
const |
| 13 |
externalLink = ( href ) => { |
| 14 |
const link = document.createElement( 'a' ); |
| 15 |
link.setAttribute( 'href', href ); |
| 16 |
return link; |
| 17 |
}, |
| 18 |
internalLink = ( pageTitle, params ) => { |
| 19 |
const link = document.createElement( 'a' ); |
| 20 |
link.setAttribute( 'href', mw.Title.newFromText( pageTitle ).getUrl( params ) ); |
| 21 |
return link; |
| 22 |
}, |
| 23 |
parsoidLink = ( href ) => { |
| 24 |
const link = document.createElement( 'a' ); |
| 25 |
if ( mw.config.get( 'wgArticlePath' ).includes( '?' ) ) { |
| 26 |
href = href.replace( './', './index.php?title=' ); |
| 27 |
} |
| 28 |
link.setAttribute( 'href', href ); |
| 29 |
return link; |
| 30 |
}; |
| 31 |
return [ |
| 32 |
{ |
| 33 |
msg: 'Not an internal link', |
| 34 |
element: externalLink( 'http://example.com/' ), |
| 35 |
expected: { |
| 36 |
type: 'link/mwExternal', |
| 37 |
attributes: { |
| 38 |
href: 'http://example.com/' |
| 39 |
} |
| 40 |
} |
| 41 |
}, |
| 42 |
{ |
| 43 |
msg: 'Simple', |
| 44 |
element: internalLink( 'Foo' ), |
| 45 |
expected: { |
| 46 |
type: 'link/mwInternal', |
| 47 |
attributes: { |
| 48 |
lookupTitle: 'Foo', |
| 49 |
normalizedTitle: 'Foo', |
| 50 |
title: 'Foo' |
| 51 |
} |
| 52 |
} |
| 53 |
}, |
| 54 |
{ |
| 55 |
msg: 'Relative path', |
| 56 |
element: parsoidLink( './Foo' ), |
| 57 |
expected: { |
| 58 |
type: 'link/mwInternal', |
| 59 |
attributes: { |
| 60 |
lookupTitle: 'Foo', |
| 61 |
normalizedTitle: 'Foo', |
| 62 |
title: 'Foo' |
| 63 |
} |
| 64 |
} |
| 65 |
}, |
| 66 |
{ |
| 67 |
msg: 'History link', |
| 68 |
element: internalLink( 'Foo', { action: 'history' } ), |
| 69 |
expected: { |
| 70 |
type: 'link/mwExternal', |
| 71 |
attributes: { |
| 72 |
href: mw.Title.newFromText( 'Foo' ).getUrl( { action: 'history' } ) |
| 73 |
} |
| 74 |
} |
| 75 |
}, |
| 76 |
{ |
| 77 |
msg: 'Diff link', |
| 78 |
element: internalLink( 'Foo', { diff: '3', oldid: '2' } ), |
| 79 |
expected: { |
| 80 |
type: 'link/mwExternal', |
| 81 |
attributes: { |
| 82 |
href: mw.Title.newFromText( 'Foo' ).getUrl( { diff: '3', oldid: '2' } ) |
| 83 |
} |
| 84 |
} |
| 85 |
}, |
| 86 |
{ |
| 87 |
msg: 'Red link', |
| 88 |
element: internalLink( 'Foo', { action: 'edit', redlink: '1' } ), |
| 89 |
expected: { |
| 90 |
type: 'link/mwInternal', |
| 91 |
attributes: { |
| 92 |
lookupTitle: 'Foo', |
| 93 |
normalizedTitle: 'Foo', |
| 94 |
title: 'Foo' |
| 95 |
} |
| 96 |
} |
| 97 |
}, |
| 98 |
{ |
| 99 |
|
| 100 |
msg: 'Percent encoded characters', |
| 101 |
element: internalLink( 'Foo?' ), |
| 102 |
expected: { |
| 103 |
type: 'link/mwInternal', |
| 104 |
attributes: { |
| 105 |
lookupTitle: 'Foo?', |
| 106 |
normalizedTitle: 'Foo?', |
| 107 |
title: 'Foo?' |
| 108 |
} |
| 109 |
} |
| 110 |
}, |
| 111 |
{ |
| 112 |
|
| 113 |
msg: 'Fragments', |
| 114 |
element: internalLink( 'Foo#bar' ), |
| 115 |
expected: { |
| 116 |
type: 'link/mwInternal', |
| 117 |
attributes: { |
| 118 |
lookupTitle: 'Foo', |
| 119 |
normalizedTitle: 'Foo#bar', |
| 120 |
title: 'Foo#bar' |
| 121 |
} |
| 122 |
} |
| 123 |
}, |
| 124 |
{ |
| 125 |
|
| 126 |
msg: 'Question marks in fragments', |
| 127 |
element: internalLink( 'Foo#bar?' ), |
| 128 |
expected: { |
| 129 |
type: 'link/mwInternal', |
| 130 |
attributes: { |
| 131 |
lookupTitle: 'Foo', |
| 132 |
normalizedTitle: 'Foo#' + mw.util.escapeIdForLink( 'bar?' ), |
| 133 |
title: 'Foo#' + mw.util.escapeIdForLink( 'bar?' ) |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
]; |
| 138 |
}; |
| 139 |
|
| 140 |
const converter = new ve.dm.Converter( ve.dm.modelRegistry, ve.dm.nodeFactory, ve.dm.annotationFactory, ve.dm.metaItemFactory ); |
| 141 |
|
| 142 |
const articlePaths = [ |
| 143 |
{ |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
msg: 'query string URL', |
| 148 |
config: { |
| 149 |
wgServer: 'http://example.org', |
| 150 |
wgScript: '/w/index.php', |
| 151 |
wgArticlePath: '/w/index.php?title=$1' |
| 152 |
}, |
| 153 |
|
| 154 |
base: 'http://example.org/w/' |
| 155 |
}, |
| 156 |
{ |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
msg: 'short URL using pathinfo', |
| 161 |
config: { |
| 162 |
wgServer: 'http://example.org', |
| 163 |
wgScript: '/w/index.php', |
| 164 |
wgArticlePath: '/w/index.php/$1' |
| 165 |
}, |
| 166 |
|
| 167 |
base: 'http://example.org/w/index.php/' |
| 168 |
}, |
| 169 |
{ |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
msg: 'proper short URL', |
| 174 |
config: { |
| 175 |
wgServer: 'http://example.org', |
| 176 |
wgScript: '/w/index.php', |
| 177 |
wgArticlePath: '/wiki/$1' |
| 178 |
}, |
| 179 |
|
| 180 |
base: 'http://example.org/wiki/' |
| 181 |
} |
| 182 |
]; |
| 183 |
|
| 184 |
for ( const pathData of articlePaths ) { |
| 185 |
|
| 186 |
mw.config.set( pathData.config ); |
| 187 |
|
| 188 |
const doc = ve.dm.mwExample.createExampleDocumentFromData( [], null, pathData.base ); |
| 189 |
|
| 190 |
|
| 191 |
converter.doc = doc.getHtmlDocument(); |
| 192 |
converter.targetDoc = doc.getHtmlDocument(); |
| 193 |
converter.store = doc.getStore(); |
| 194 |
converter.internalList = doc.getInternalList(); |
| 195 |
converter.contextStack = []; |
| 196 |
converter.fromClipboard = true; |
| 197 |
|
| 198 |
|
| 199 |
for ( const caseItem of getCases() ) { |
| 200 |
assert.deepEqual( |
| 201 |
ve.dm.MWInternalLinkAnnotation.static.toDataElement( [ caseItem.element ], converter ), |
| 202 |
caseItem.expected, |
| 203 |
caseItem.msg + ': ' + pathData.msg |
| 204 |
); |
| 205 |
} |
| 206 |
} |
| 207 |
} ); |
| 208 |
|
| 209 |
QUnit.test.each( 'getFragment', { |
| 210 |
'No fragment returns null': { |
| 211 |
original: 'Foo', |
| 212 |
expected: null |
| 213 |
}, |
| 214 |
'Invalid title returns null': { |
| 215 |
original: 'A%20B', |
| 216 |
expected: null |
| 217 |
}, |
| 218 |
'Blank fragment returns empty string': { |
| 219 |
original: 'Foo#', |
| 220 |
expected: '' |
| 221 |
}, |
| 222 |
'Extant fragment returns same string': { |
| 223 |
original: 'Foo#bar', |
| 224 |
expected: 'bar' |
| 225 |
}, |
| 226 |
'Hash-bang works returns full string': { |
| 227 |
original: 'Foo#!bar', |
| 228 |
expected: '!bar' |
| 229 |
}, |
| 230 |
'Double-hash returns everything after the first hash': { |
| 231 |
original: 'Foo##bar', |
| 232 |
expected: '#bar' |
| 233 |
}, |
| 234 |
'Multi-fragment returns everything after the first hash': { |
| 235 |
original: 'Foo#bar#baz#bat', |
| 236 |
expected: 'bar#baz#bat' |
| 237 |
} |
| 238 |
}, ( assert, { original, expected } ) => { |
| 239 |
assert.strictEqual( ve.dm.MWInternalLinkAnnotation.static.getFragment( original ), expected ); |
| 240 |
} ); |