-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HTML Credits #6331
HTML Credits #6331
Changes from 4 commits
3f6e682
24a89f8
a1c93c2
62e797f
ef54816
002ab19
60f98ae
080392c
440a9e6
664df19
6e53d5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -686,6 +686,34 @@ https://github.com/google/draco | |
>License for the specific language governing permissions and limitations under | ||
>the License. | ||
|
||
### JSXSS | ||
|
||
https://github.com/leizongmin/js-xss | ||
|
||
> Copyright (c) 2012-2017 Zongmin Lei(雷宗民) <[email protected]> | ||
> http://ucdok.com | ||
> | ||
> The MIT License | ||
> | ||
> Permission is hereby granted, free of charge, to any person obtaining | ||
> a copy of this software and associated documentation files (the | ||
> "Software"), to deal in the Software without restriction, including | ||
> without limitation the rights to use, copy, modify, merge, publish, | ||
> distribute, sublicense, and/or sell copies of the Software, and to | ||
> permit persons to whom the Software is furnished to do so, subject to | ||
> the following conditions: | ||
> | ||
> The above copyright notice and this permission notice shall be | ||
> included in all copies or substantial portions of the Software. | ||
> | ||
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
> NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
> LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
> OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
> WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
Tests | ||
===== | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,39 @@ | ||
define([ | ||
'./defaultValue', | ||
'./defined', | ||
'./defineProperties', | ||
'./DeveloperError' | ||
], function( | ||
defaultValue, | ||
defined, | ||
defineProperties, | ||
DeveloperError) { | ||
'./defaultValue', | ||
'./defined', | ||
'./defineProperties', | ||
'./deprecationWarning', | ||
'./DeveloperError', | ||
'../ThirdParty/xss' | ||
], function( | ||
defaultValue, | ||
defined, | ||
defineProperties, | ||
deprecationWarning, | ||
DeveloperError, | ||
xss) { | ||
'use strict'; | ||
|
||
var nextCreditId = 0; | ||
var creditToId = {}; | ||
|
||
function createDomNode(html) { | ||
var span = document.createElement('span'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought we were going to make this a div? Also, this function no longer seems necessary since it's only used in the equally small There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In fact, getElement can also be moved into the |
||
span.innerHTML = html; | ||
|
||
return span; | ||
} | ||
|
||
function getElement(credit) { | ||
var element = document.createElement('span'); | ||
var text = credit.text; | ||
var link = credit.link; | ||
var a; | ||
if (credit._hasImage) { | ||
var content = document.createElement('img'); | ||
content.src = credit.imageUrl; | ||
if (defined(text)) { | ||
content.alt = text; | ||
content.title = text; | ||
} | ||
if (credit._hasLink) { | ||
a = document.createElement('a'); | ||
a.appendChild(content); | ||
a.href = link; | ||
a.target = '_blank'; | ||
element.appendChild(a); | ||
} else { | ||
element.appendChild(content); | ||
} | ||
element.className = 'cesium-credit-image'; | ||
} else { | ||
if (credit._hasLink) { | ||
a = document.createElement('a'); | ||
a.textContent = text; | ||
a.href = link; | ||
a.target = '_blank'; | ||
element.appendChild(a); | ||
} else { | ||
element.textContent = text; | ||
} | ||
element.className = 'cesium-credit-text'; | ||
} | ||
return element; | ||
var html = credit.html; | ||
html = xss(html); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are we using for a whitelist here? Because it looks like not even the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turns out the fact that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm just using the default whitelist https://github.com/leizongmin/js-xss/blob/master/lib/default.js I'll look into the |
||
return createDomNode(html); | ||
} | ||
|
||
/** | ||
* A credit contains data pertaining to how to display attributions/credits for certain content on the screen. | ||
* @param {Object} [options] An object with the following properties | ||
* @param {String} [options.text] The text to be displayed on the screen if no imageUrl is specified. | ||
* @param {String} [options.imageUrl] The source location for an image | ||
* @param {String} [options.link] A URL location for which the credit will be hyperlinked | ||
* @param {Boolean} [options.showOnScreen=false] If true, the credit will be visible in the main credit container. Otherwise, it will appear in a popover | ||
* @param {String} html An string representing an html code snippet (can be text only) | ||
* @param {Boolean} [showOnScreen=false] If true, the credit will be visible in the main credit container. Otherwise, it will appear in a popover | ||
* | ||
* @alias Credit | ||
* @constructor | ||
|
@@ -71,38 +48,74 @@ define([ | |
* link : 'https://cesiumjs.org/' | ||
* }); | ||
*/ | ||
function Credit(options) { | ||
options = defaultValue(options, defaultValue.EMPTY_OBJECT); | ||
function Credit(html, showOnScreen) { | ||
var id; | ||
var key; | ||
if (typeof html !== 'string') { | ||
var options = defaultValue(html, defaultValue.EMPTY_OBJECT); | ||
deprecationWarning('Credit options', 'The options parameter has been deprecated and will be removed in Cesium 1.46. Instead, pass in an HTML string (or a string of text)'); | ||
showOnScreen = defaultValue(options.showOnScreen, showOnScreen); | ||
var text = options.text; | ||
var imageUrl = options.imageUrl; | ||
var link = options.link; | ||
|
||
var text = options.text; | ||
var imageUrl = options.imageUrl; | ||
var link = options.link; | ||
var showOnScreen = defaultValue(options.showOnScreen, false); | ||
var hasLink = (defined(link)); | ||
var hasImage = (defined(imageUrl)); | ||
var hasText = (defined(text)); | ||
|
||
var hasLink = (defined(link)); | ||
var hasImage = (defined(imageUrl)); | ||
var hasText = (defined(text)); | ||
//>>includeStart('debug', pragmas.debug); | ||
if (!hasText && !hasImage && !hasLink) { | ||
throw new DeveloperError('options.text, options.imageUrl, or options.link is required.'); | ||
} | ||
//>>includeEnd('debug'); | ||
|
||
//>>includeStart('debug', pragmas.debug); | ||
if (!hasText && !hasImage && !hasLink) { | ||
throw new DeveloperError('options.text, options.imageUrl, or options.link is required.'); | ||
} | ||
//>>includeEnd('debug'); | ||
if (!hasText && !hasImage) { | ||
text = link; | ||
} | ||
|
||
if (!hasText && !hasImage) { | ||
text = link; | ||
} | ||
this._text = text; | ||
this._imageUrl = imageUrl; | ||
this._link = link; | ||
this._hasLink = hasLink; | ||
this._hasImage = hasImage; | ||
|
||
this._text = text; | ||
this._imageUrl = imageUrl; | ||
this._link = link; | ||
this._hasLink = hasLink; | ||
this._hasImage = hasImage; | ||
this._showOnScreen = showOnScreen; | ||
var element = document.createElement('span'); | ||
var a; | ||
if (hasImage) { | ||
var content = document.createElement('img'); | ||
content.src = imageUrl; | ||
if (defined(text)) { | ||
content.alt = text; | ||
content.title = text; | ||
} | ||
if (hasLink) { | ||
a = document.createElement('a'); | ||
a.appendChild(content); | ||
a.href = link; | ||
a.target = '_blank'; | ||
element.appendChild(a); | ||
} else { | ||
element.appendChild(content); | ||
} | ||
element.className = 'cesium-credit-image'; | ||
} else { | ||
if (hasLink) { | ||
a = document.createElement('a'); | ||
a.textContent = text; | ||
a.href = link; | ||
a.target = '_blank'; | ||
element.appendChild(a); | ||
} else { | ||
element.textContent = text; | ||
} | ||
element.className = 'cesium-credit-text'; | ||
} | ||
|
||
// Credits are immutable so generate an id to use to optimize equal() | ||
var id; | ||
var key = JSON.stringify([text, imageUrl, link]); | ||
html = '<span>' + element.innerHTML + '</span>'; | ||
key = JSON.stringify([text, imageUrl, link]); | ||
} else { | ||
key = html; | ||
} | ||
|
||
if (defined(creditToId[key])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we even need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
id = creditToId[key]; | ||
|
@@ -111,12 +124,27 @@ define([ | |
creditToId[key] = id; | ||
} | ||
|
||
this._id = id; | ||
showOnScreen = defaultValue(showOnScreen, false); | ||
|
||
// Credits are immutable so generate an id to use to optimize equal() | ||
this._id = id; | ||
this._html = html; | ||
this._showOnScreen = showOnScreen; | ||
this._element = undefined; | ||
} | ||
|
||
defineProperties(Credit.prototype, { | ||
/** | ||
* The credit content | ||
* @memberof Credit.prototype | ||
* @type {String} | ||
* @readonly | ||
*/ | ||
html : { | ||
get : function() { | ||
return this._html; | ||
} | ||
}, | ||
/** | ||
* The credit text | ||
* @memberof Credit.prototype | ||
|
@@ -125,6 +153,7 @@ define([ | |
*/ | ||
text : { | ||
get : function() { | ||
deprecationWarning('Credit.text', 'Credit.text is deprecated and will be removed in Cesium 1.46. Instead, use Credit.html to get the credit content.'); | ||
return this._text; | ||
} | ||
}, | ||
|
@@ -137,6 +166,7 @@ define([ | |
*/ | ||
imageUrl : { | ||
get : function() { | ||
deprecationWarning('Credit.imageUrl', 'Credit.imageUrl is deprecated and will be removed in Cesium 1.46. Instead, use Credit.html to get the credit content.'); | ||
return this._imageUrl; | ||
} | ||
}, | ||
|
@@ -149,6 +179,7 @@ define([ | |
*/ | ||
link : { | ||
get : function() { | ||
deprecationWarning('Credit.link', 'Credit.link is deprecated and will be removed in Cesium 1.46. Instead, use Credit.html to get the credit content.'); | ||
return this._link; | ||
} | ||
}, | ||
|
@@ -200,6 +231,7 @@ define([ | |
* @returns {Boolean} | ||
*/ | ||
Credit.prototype.hasImage = function() { | ||
deprecationWarning('Credit.hasImage', 'Credit.hasImage is deprecated and will be removed in Cesium 1.46.'); | ||
return this._hasImage; | ||
}; | ||
|
||
|
@@ -209,6 +241,7 @@ define([ | |
* @returns {Boolean} | ||
*/ | ||
Credit.prototype.hasLink = function() { | ||
deprecationWarning('Credit.hasLink', 'Credit.hasLink is deprecated and will be removed in Cesium 1.46.'); | ||
return this._hasLink; | ||
}; | ||
|
||
|
@@ -236,5 +269,26 @@ define([ | |
return Credit.equals(this, credit); | ||
}; | ||
|
||
/** | ||
* @private | ||
* @param attribution | ||
* @return {Credit} | ||
*/ | ||
Credit.getIonCredit = function(attribution) { | ||
var credit; | ||
var showOnScreen = defined(attribution.collapsible) && !attribution.collapsible; | ||
if (defined(attribution.html)) { | ||
credit = new Credit(attribution.html, showOnScreen); | ||
} else { | ||
credit = new Credit({ | ||
text: attribution.text, | ||
link: attribution.url, | ||
imageUrl: attribution.image | ||
}, showOnScreen); | ||
} | ||
credit._isIon = true; | ||
return credit; | ||
}; | ||
|
||
return Credit; | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should have createDomNode automatically add target='_blank' to all credit links (then we can remove them all from this PR)