Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ce/ve.ce.View.js

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable View class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Generic base class for CE views.
9 *
10 * @abstract
11 * @extends OO.ui.Element
12 * @mixes OO.EventEmitter
13 *
14 * @constructor
15 * @param {ve.dm.Model} model Model to observe
16 * @param {Object} [config] Configuration options
17 */
18 ve.ce.View = function VeCeView( model, config ) {
19 // Setting this property before calling the parent constructor allows overridden #getTagName
20 // methods in view classes to have access to the model when they are called for the first time
21 // inside of OO.ui.Element
22 this.model = model;
23
24 // Parent constructor
25 ve.ce.View.super.call( this, config );
26
27 // Mixin constructors
28 OO.EventEmitter.call( this );
29
30 // Properties
31 this.live = false;
32
33 // Events
34 this.connect( this, {
35 setup: 'onSetup',
36 teardown: 'onTeardown'
37 } );
38
39 // Initialize
40 this.initialize();
41 };
42
43 /* Inheritance */
44
45 OO.inheritClass( ve.ce.View, OO.ui.Element );
46
47 OO.mixinClass( ve.ce.View, OO.EventEmitter );
48
49 /* Events */
50
51 /**
52 * @event ve.ce.View#setup
53 */
54
55 /**
56 * @event ve.ce.View#teardown
57 */
58
59 /* Static members */
60
61 /**
62 * Allowed attributes for DOM elements, in the same format as ve.dm.Model#preserveHtmlAttributes
63 *
64 * This list includes attributes that are generally safe to include in HTML loaded from a
65 * foreign source and displaying it inside the browser. It doesn't include any event attributes,
66 * for instance, which would allow arbitrary JavaScript execution. This alone is not enough to
67 * make HTML safe to display, but it helps.
68 *
69 * TODO: Rather than use a single global list, set these on a per-view basis to something that makes
70 * sense for that view in particular.
71 *
72 * @static
73 * @property {boolean|Function}
74 * @param {string} attribute
75 * @return {boolean} Attibute is allowed
76 * @inheritable
77 */
78 ve.ce.View.static.renderHtmlAttributes = function ( attribute ) {
79 const attributes = [
80 'abbr', 'about', 'align', 'alt', 'axis', 'bgcolor', 'border', 'cellpadding', 'cellspacing',
81 'char', 'charoff', 'cite', 'class', 'clear', 'color', 'colspan', 'datatype', 'datetime',
82 'dir', 'face', 'frame', 'headers', 'height', 'href', 'id', 'itemid', 'itemprop', 'itemref',
83 'itemscope', 'itemtype', 'lang', 'noshade', 'nowrap', 'property', 'rbspan', 'rel',
84 'resource', 'rev', 'rowspan', 'rules', 'scope', 'size', 'span', 'src', 'start', 'style',
85 'summary', 'title', 'type', 'typeof', 'valign', 'value', 'width'
86 ];
87 return attributes.indexOf( attribute ) !== -1;
88 };
89
90 /* Methods */
91
92 /**
93 * Get an HTML document from the model, to use for URL resolution.
94 *
95 * The default implementation returns null; subclasses should override this if they can provide
96 * a resolution document.
97 *
98 * @see #getResolvedAttribute
99 * @return {HTMLDocument|null} HTML document to use for resolution, or null if not available
100 */
101 ve.ce.View.prototype.getModelHtmlDocument = function () {
102 return null;
103 };
104
105 /**
106 * Initialize this.$element. This is called by the constructor and should be called every time
107 * this.$element is replaced.
108 */
109 ve.ce.View.prototype.initialize = function () {
110 if ( this.model.element && this.model.element.originalDomElementsHash !== undefined ) {
111 // Render attributes from original DOM elements
112 ve.dm.Converter.static.renderHtmlAttributeList(
113 this.model.getOriginalDomElements( this.model.getStore() ),
114 this.$element,
115 this.constructor.static.renderHtmlAttributes,
116 // Computed
117 true,
118 // Deep
119 !( this.model instanceof ve.dm.Node ) ||
120 !this.model.canHaveChildren() ||
121 this.model.handlesOwnChildren()
122 );
123 }
124 };
125
126 /**
127 * Handle setup event.
128 */
129 ve.ce.View.prototype.onSetup = function () {
130 this.$element.data( 'view', this );
131 };
132
133 /**
134 * Handle teardown event.
135 */
136 ve.ce.View.prototype.onTeardown = function () {
137 this.$element.removeData( 'view' );
138 };
139
140 /**
141 * Get the model the view observes.
142 *
143 * @return {ve.dm.Model} Model the view observes
144 */
145 ve.ce.View.prototype.getModel = function () {
146 return this.model;
147 };
148
149 /**
150 * Check if the view is attached to the live DOM.
151 *
152 * @return {boolean} View is attached to the live DOM
153 */
154 ve.ce.View.prototype.isLive = function () {
155 return this.live;
156 };
157
158 /**
159 * Set live state.
160 *
161 * @param {boolean} live The view has been attached to the live DOM (use false on detach)
162 * @fires ve.ce.View#setup
163 * @fires ve.ce.View#teardown
164 */
165 ve.ce.View.prototype.setLive = function ( live ) {
166 this.live = live;
167 if ( this.live ) {
168 this.emit( 'setup' );
169 } else {
170 this.emit( 'teardown' );
171 }
172 };
173
174 /**
175 * Get a resolved URL from a model attribute.
176 *
177 * @param {string} key Attribute name whose value is a URL
178 * @return {string} URL resolved according to the document's base
179 */
180 ve.ce.View.prototype.getResolvedAttribute = function ( key ) {
181 const plainValue = this.model.getAttribute( key ),
182 doc = this.getModelHtmlDocument();
183 return doc && typeof plainValue === 'string' ? ve.resolveUrl( plainValue, doc ) : plainValue;
184 };
185
186 /**
187 * Release all memory.
188 */
189 ve.ce.View.prototype.destroy = function () {
190 this.disconnect( this );
191 this.model.disconnect( this );
192 this.model = null;
193 };