123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- goog.provide('goog.dom.safe');
- goog.provide('goog.dom.safe.InsertAdjacentHtmlPosition');
- goog.require('goog.asserts');
- goog.require('goog.dom.asserts');
- goog.require('goog.html.SafeHtml');
- goog.require('goog.html.SafeScript');
- goog.require('goog.html.SafeStyle');
- goog.require('goog.html.SafeUrl');
- goog.require('goog.html.TrustedResourceUrl');
- goog.require('goog.string');
- goog.require('goog.string.Const');
- goog.dom.safe.InsertAdjacentHtmlPosition = {
- AFTERBEGIN: 'afterbegin',
- AFTEREND: 'afterend',
- BEFOREBEGIN: 'beforebegin',
- BEFOREEND: 'beforeend'
- };
- goog.dom.safe.insertAdjacentHtml = function(node, position, html) {
- node.insertAdjacentHTML(position, goog.html.SafeHtml.unwrap(html));
- };
- goog.dom.safe.SET_INNER_HTML_DISALLOWED_TAGS_ = {
- 'MATH': true,
- 'SCRIPT': true,
- 'STYLE': true,
- 'SVG': true,
- 'TEMPLATE': true
- };
- goog.dom.safe.setInnerHtml = function(elem, html) {
- if (goog.asserts.ENABLE_ASSERTS) {
- var tagName = elem.tagName.toUpperCase();
- if (goog.dom.safe.SET_INNER_HTML_DISALLOWED_TAGS_[tagName]) {
- throw Error(
- 'goog.dom.safe.setInnerHtml cannot be used to set content of ' +
- elem.tagName + '.');
- }
- }
- elem.innerHTML = goog.html.SafeHtml.unwrap(html);
- };
- goog.dom.safe.setOuterHtml = function(elem, html) {
- elem.outerHTML = goog.html.SafeHtml.unwrap(html);
- };
- goog.dom.safe.setStyle = function(elem, style) {
- elem.style.cssText = goog.html.SafeStyle.unwrap(style);
- };
- goog.dom.safe.documentWrite = function(doc, html) {
- doc.write(goog.html.SafeHtml.unwrap(html));
- };
- goog.dom.safe.setAnchorHref = function(anchor, url) {
- goog.dom.asserts.assertIsHTMLAnchorElement(anchor);
-
- var safeUrl;
- if (url instanceof goog.html.SafeUrl) {
- safeUrl = url;
- } else {
- safeUrl = goog.html.SafeUrl.sanitize(url);
- }
- anchor.href = goog.html.SafeUrl.unwrap(safeUrl);
- };
- goog.dom.safe.setImageSrc = function(imageElement, url) {
- goog.dom.asserts.assertIsHTMLImageElement(imageElement);
-
- var safeUrl;
- if (url instanceof goog.html.SafeUrl) {
- safeUrl = url;
- } else {
- safeUrl = goog.html.SafeUrl.sanitize(url);
- }
- imageElement.src = goog.html.SafeUrl.unwrap(safeUrl);
- };
- goog.dom.safe.setEmbedSrc = function(embed, url) {
- goog.dom.asserts.assertIsHTMLEmbedElement(embed);
- embed.src = goog.html.TrustedResourceUrl.unwrap(url);
- };
- goog.dom.safe.setFrameSrc = function(frame, url) {
- goog.dom.asserts.assertIsHTMLFrameElement(frame);
- frame.src = goog.html.TrustedResourceUrl.unwrap(url);
- };
- goog.dom.safe.setIframeSrc = function(iframe, url) {
- goog.dom.asserts.assertIsHTMLIFrameElement(iframe);
- iframe.src = goog.html.TrustedResourceUrl.unwrap(url);
- };
- goog.dom.safe.setIframeSrcdoc = function(iframe, html) {
- goog.dom.asserts.assertIsHTMLIFrameElement(iframe);
- iframe.srcdoc = goog.html.SafeHtml.unwrap(html);
- };
- goog.dom.safe.setLinkHrefAndRel = function(link, url, rel) {
- goog.dom.asserts.assertIsHTMLLinkElement(link);
- link.rel = rel;
- if (goog.string.caseInsensitiveContains(rel, 'stylesheet')) {
- goog.asserts.assert(
- url instanceof goog.html.TrustedResourceUrl,
- 'URL must be TrustedResourceUrl because "rel" contains "stylesheet"');
- link.href = goog.html.TrustedResourceUrl.unwrap(url);
- } else if (url instanceof goog.html.TrustedResourceUrl) {
- link.href = goog.html.TrustedResourceUrl.unwrap(url);
- } else if (url instanceof goog.html.SafeUrl) {
- link.href = goog.html.SafeUrl.unwrap(url);
- } else {
-
- link.href = goog.html.SafeUrl.sanitize(url).getTypedStringValue();
- }
- };
- goog.dom.safe.setObjectData = function(object, url) {
- goog.dom.asserts.assertIsHTMLObjectElement(object);
- object.data = goog.html.TrustedResourceUrl.unwrap(url);
- };
- goog.dom.safe.setScriptSrc = function(script, url) {
- goog.dom.asserts.assertIsHTMLScriptElement(script);
- script.src = goog.html.TrustedResourceUrl.unwrap(url);
- };
- goog.dom.safe.setScriptContent = function(script, content) {
- goog.dom.asserts.assertIsHTMLScriptElement(script);
- script.text = goog.html.SafeScript.unwrap(content);
- };
- goog.dom.safe.setLocationHref = function(loc, url) {
- goog.dom.asserts.assertIsLocation(loc);
-
- var safeUrl;
- if (url instanceof goog.html.SafeUrl) {
- safeUrl = url;
- } else {
- safeUrl = goog.html.SafeUrl.sanitize(url);
- }
- loc.href = goog.html.SafeUrl.unwrap(safeUrl);
- };
- goog.dom.safe.openInWindow = function(
- url, opt_openerWin, opt_name, opt_specs, opt_replace) {
-
- var safeUrl;
- if (url instanceof goog.html.SafeUrl) {
- safeUrl = url;
- } else {
- safeUrl = goog.html.SafeUrl.sanitize(url);
- }
- var win = opt_openerWin || window;
- return win.open(
- goog.html.SafeUrl.unwrap(safeUrl),
-
-
-
-
- opt_name ? goog.string.Const.unwrap(opt_name) : '', opt_specs,
- opt_replace);
- };
|