"use strict"; /** * Created by viktor.shevchenko on 7/22/2014. */ (function () { 'use strict'; angular.module('knowledgeArticleModule') .directive('processKaContent', ['$location', '$anchorScroll', '$compile', function ($location, $anchorScroll, $compile) { return { restrict: 'A', link: function (scope, iElement, iAttr) { var snippet = iAttr.processKaContent, content = $('
').append($.parseHTML(snippet)), links = content.find('a'), anchorRegEx = /^#[^-[\]{}()<>*+?.,\\\/^$|#]+$/; //Below value received from ccs param: knowledgeAnchorParser //Backslash not supported so shouldn't be removed from regex at all. //It is recommended not to modify the ccs regex from security perspective. //However configurability given for corner cases. if (iAttr.anchorParser && iAttr.anchorParser !== 'skip') { try { anchorRegEx = "^#[^" + iAttr.anchorParser + "]+$"; anchorRegEx = new RegExp(anchorRegEx); } catch (e) { console.error("Error in Regex sent through ccs"); anchorRegEx = /^#[^-[\]{}()<>*+?.,\\\/^$|#]+$/; } } else if (iAttr.anchorParser === 'skip') { // DRSMX-63584:[Ford] Using Old regEX as a fallback when knowledgeAnchorParser value is set to skip anchorRegEx = /^#[\w\d]/; } scope.scrollToAnchor = function (event, anchor) { $location.hash(anchor); $anchorScroll(); event.preventDefault(); event.stopPropagation(); }; if (links.length) { $.each(links, function (i, elm) { var link = $(elm), href = link.attr('href'); if (anchorRegEx.test(href) && href.indexOf("#/knowledge/latest/") === -1) { //Escape \ and " from the regEx href = href.replace(/\\/g, "\\\\").replace(/\"/g, "\\\""); link.attr('ng-click', 'scrollToAnchor($event, "' + href.replace(/^#/, '') + '")'); $compile(link)(scope); } }); } iElement.append(content); } }; }]); }());