Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/dm/annotations/ve.dm.MWExternalLinkAnnotation.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel MWExternalLinkAnnotation class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * DataModel MediaWiki external link annotation.
10 *
11 * Example HTML sources:
12 *
13 * <a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a>
14 * <a rel="mw:ExtLink" class="external text" href="http://example.com">Link content</a>
15 * <a rel="mw:ExtLink" class="external autonumber" href="http://example.com"></a>
16 * <a rel="mw:WikiLink/Interwiki" href="http://en.wikipedia.org/wiki/Foo">en:Foo</a>
17 *
18 * Each example is semantically slightly different, but they don't need special treatment (yet).
19 *
20 * @class
21 * @extends ve.dm.LinkAnnotation
22 * @constructor
23 * @param {Object} element
24 */
25 ve.dm.MWExternalLinkAnnotation = function VeDmMWExternalLinkAnnotation() {
26 // Parent constructor
27 ve.dm.MWExternalLinkAnnotation.super.apply( this, arguments );
28 };
29
30 /* Inheritance */
31
32 OO.inheritClass( ve.dm.MWExternalLinkAnnotation, ve.dm.LinkAnnotation );
33
34 /* Static Properties */
35
36 ve.dm.MWExternalLinkAnnotation.static.name = 'link/mwExternal';
37
38 // Allow additional 'rel' values in Parsoid output (T321437)
39 // Allow all unknown types for external paste (handled in toDataElement)
40 ve.dm.MWExternalLinkAnnotation.static.allowedRdfaTypes = null;
41
42 ve.dm.MWExternalLinkAnnotation.static.toDataElement = function ( domElements, converter ) {
43 const domElement = domElements[ 0 ],
44 type = domElement.getAttribute( 'rel' ) || domElement.getAttribute( 'typeof' ) || domElement.getAttribute( 'property' ) || '',
45 types = type.trim().split( /\s+/ );
46
47 // If the link doesn't have a known RDFa type...
48 if ( types.indexOf( 'mw:ExtLink' ) === -1 && types.indexOf( 'mw:WikiLink/Interwiki' ) === -1 ) {
49 // ...when pasting: auto-convert it to the correct type (internal/external/span)
50 if ( converter.isFromClipboard() ) {
51 if ( domElement.hasAttribute( 'href' ) ) {
52 const annotation = ve.ui.MWLinkAction.static.getLinkAnnotation( domElement.getAttribute( 'href' ), converter.getHtmlDocument() );
53 return annotation.element;
54 } else {
55 // Convert href-less links to a plain span, which will get stripped by sanitization
56 return ve.dm.SpanAnnotation.static.toDataElement.apply( ve.dm.SpanAnnotation.static, arguments );
57 }
58 }
59 // ...otherwise (in Parsoid HTML): something is terribly wrong, alienate to avoid making
60 // unnecessary DOM changes in Parsoid output and causing dirty diffs (T267282)
61 return null;
62 }
63
64 // Parent method
65 const dataElement = ve.dm.MWExternalLinkAnnotation.super.static.toDataElement.apply( this, arguments );
66
67 dataElement.attributes.rel = type;
68 return dataElement;
69 };
70
71 ve.dm.MWExternalLinkAnnotation.static.toDomElements = function ( dataElement, doc, converter ) {
72 // Parent method
73 const domElements = ve.dm.MWExternalLinkAnnotation.super.static.toDomElements.apply( this, arguments );
74
75 if ( converter.isForPreview() ) {
76 // Ensure there is an 'external' class when rendering, as this may have been created locally.
77 domElements[ 0 ].setAttribute( 'class', 'external' );
78 }
79
80 // we just created that link so the 'rel' attribute should be safe
81 domElements[ 0 ].setAttribute( 'rel', dataElement.attributes.rel || 'mw:ExtLink' );
82 return domElements;
83 };
84
85 ve.dm.MWExternalLinkAnnotation.static.describeChange = function ( key, change ) {
86 if ( key === 'href' ) {
87 return ve.dm.LinkAnnotation.static.describeChange( key, change );
88 }
89 return null;
90 };
91
92 /* Methods */
93
94 /**
95 * @return {Object}
96 */
97 ve.dm.MWExternalLinkAnnotation.prototype.getComparableObject = function () {
98 return {
99 type: this.getType(),
100 href: this.getAttribute( 'href' ),
101 rel: this.getAttribute( 'rel' ) || 'mw:ExtLink'
102 };
103 };
104
105 /**
106 * @inheritdoc
107 */
108 ve.dm.MWExternalLinkAnnotation.prototype.getComparableHtmlAttributes = function () {
109 // Assume that wikitext never adds meaningful html attributes for comparison purposes,
110 // although ideally this should be decided by Parsoid (Bug T95028).
111 return {};
112 };
113
114 /* Registration */
115
116 ve.dm.modelRegistry.register( ve.dm.MWExternalLinkAnnotation );