GitHub PullRequestReviewCommentEvent at 2014-03-20 16:33:27
Repository: kuma
Actor: riverspirit
Payload: {"comment":{"url":"https://api.github.com/repos/mozilla/kuma/pulls/comments/10803145","id":10803145,"diff_hunk":"@@ -0,0 +1,206 @@\n+var mdnPromoteLinks = window.mdnPromoteLinks || function (userSettings) {\n+\n+    // For the time being, we are not gonna do anything if querySelectorAll is not available in the browser\n+    if (!'querySelectorAll' in document) {\n+        return;\n+    }\n+    \n+    var dataset = {\n+        'JavaScript': 'https://developer.mozilla.org/docs/JavaScript',\n+        'JS Reference': 'https://developer.mozilla.org/docs/JavaScript',\n+        'JS Documentation': 'https://developer.mozilla.org/docs/JavaScript',\n+        'JS': 'https://developer.mozilla.org/docs/JavaScript',\n+        'HTML5': 'https://developer.mozilla.org/html5',\n+        'JS Array': 'https://developer.mozilla.org/docs/JavaScript/Reference/Global_Objects/Array',\n+        'JS Function': 'https://developer.mozilla.org/docs/JavaScript/Reference/Global_Objects/Function',\n+        'JS Number': 'https://developer.mozilla.org/docs/JavaScript/Reference/Global_Objects/Number',\n+        'JS RegExp': 'https://developer.mozilla.org/docs/JavaScript/Reference/Global_Objects/RegExp',\n+        'JS String': 'https://developer.mozilla.org/docs/JavaScript/Reference/Global_Objects/String',\n+        'JS Tutorial': 'https://developer.mozilla.org/docs/JavaScript/Guide',\n+        'Learn JavaScript': 'https://developer.mozilla.org/docs/JavaScript/Guide',\n+        'Learn JS': 'https://developer.mozilla.org/docs/JavaScript/Guide',\n+        'DOM': 'https://developer.mozilla.org/docs/DOM',\n+        'WebGL': 'https://developer.mozilla.org/docs/WebGL',\n+        'WebSockets': 'https://developer.mozilla.org/docs/WebSockets',\n+        'WebSocket': 'https://developer.mozilla.org/docs/WebSockets',\n+        'JSON': 'https://developer.mozilla.org/docs/JSON',\n+        'XUL': 'https://developer.mozilla.org/docs/XUL',\n+        'HTML': 'https://developer.mozilla.org/docs/Web/HTML',\n+        'CSS Reference': 'https://developer.mozilla.org/docs/Web/CSS/CSS_Reference',\n+        'CSS': 'https://developer.mozilla.org/docs/Web/CSS',\n+        'CSS3': 'https://developer.mozilla.org/docs/Web/CSS/CSS3',\n+        'CSS Transitions': 'https://developer.mozilla.org/docs/Web/CSS/Using_CSS_transitions',\n+        'CSS3 Transitions': 'https://developer.mozilla.org/docs/Web/CSS/Using_CSS_transitions',\n+        'CSS Gradients': 'https://developer.mozilla.org/docs/Web/CSS/Using_CSS_transitions',\n+        'CSS3 Gradients': 'https://developer.mozilla.org/docs/Web/CSS/Using_CSS_transitions',\n+        'linear-gradient': 'https://developer.mozilla.org/docs/Web/CSS/linear-gradient',\n+        'radial-gradient': 'https://developer.mozilla.org/docs/Web/CSS/linear-gradient',\n+        'repeating-linear-gradient': 'https://developer.mozilla.org/docs/Web/CSS/repeating-linear-gradient',\n+        'repeating-radial-gradient': 'https://developer.mozilla.org/docs/Web/CSS/repeating-radial-gradient',\n+        'CSS Animation': 'https://developer.mozilla.org/docs/Web/CSS/Using_CSS_animations',\n+        'CSS3 Animation': 'https://developer.mozilla.org/docs/Web/CSS/Using_CSS_animations',\n+        'CSS Transform': 'https://developer.mozilla.org/docs/Web/CSS/Using_CSS_transform',\n+        'CSS3 Transform': 'https://developer.mozilla.org/docs/Web/CSS/Using_CSS_transform',\n+        'CSS 3D Transform': 'https://developer.mozilla.org/docs/Web/CSS/Using_CSS_transform',\n+        'border-image': 'https://developer.mozilla.org/docs/Web/CSS/border-image',\n+        'border-image-source': 'https://developer.mozilla.org/docs/Web/CSS/border-image-source',\n+        'border-image-repeat': 'https://developer.mozilla.org/docs/Web/CSS/border-image-repeat',\n+        'border-image-width': 'https://developer.mozilla.org/docs/Web/CSS/border-image-width',\n+        'border-image-outset': 'https://developer.mozilla.org/docs/Web/CSS/border-image-outset',\n+        'CSS Flexbox': 'https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_flexible_boxes',\n+        'flexbox': 'https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_flexible_boxes',\n+        'flexible box': 'https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_flexible_boxes',\n+        'Media queries': 'https://developer.mozilla.org/docs/Web/CSS/Media_queries',\n+        'pseudo-class': 'https://developer.mozilla.org/docs/Web/CSS/pseudo-classes',\n+        'pseudo-classes': 'https://developer.mozilla.org/docs/Web/CSS/pseudo-classes',\n+        'pseudo-element': 'https://developer.mozilla.org/docs/Web/CSS/Pseudo-elements',\n+        'at-rule': 'https://developer.mozilla.org/docs/Web/CSS/At-rule',\n+        '@-rule': 'https://developer.mozilla.org/docs/Web/CSS/At-rule',\n+        'MDN': 'https://developer.mozilla.org/',\n+        'Mozilla Developer Network': 'https://developer.mozilla.org/',\n+        'devmo': 'https://developer.mozilla.org/',\n+        'Kuma': 'https://developer.mozilla.org/docs/Project:Getting_started_with_Kuma',\n+        'KumaScript': 'https://developer.mozilla.org/docs/Project:Introduction_to_KumaScript',\n+        'B2G': 'https://developer.mozilla.org/docs/Mozilla/Firefox_OS',\n+        'Firefox OS': 'https://developer.mozilla.org/docs/Mozilla/Firefox_OS',\n+        'Boot to Gecko': 'https://developer.mozilla.org/docs/Mozilla/Firefox_OS',\n+        'Persona': 'https://developer.mozilla.org/Persona',\n+        'BrowserID': 'https://developer.mozilla.org/Persona',\n+        'IndexedDB': 'https://developer.mozilla.org/docs/IndexedDB',\n+        'Vibration API': 'https://developer.mozilla.org/docs/WebAPI/Vibration',\n+        'Geolocation': 'https://developer.mozilla.org/docs/Using_geolocation',\n+        'SVG': 'https://developer.mozilla.org/docs/SVG',\n+        'ARIA': 'https://developer.mozilla.org/docs/Accessibility/ARIA',\n+        'WebRTC': 'https://developer.mozilla.org/docs/WebRTC',\n+        'WebAPI': 'https://developer.mozilla.org/docs/WebAPI',\n+        'Web apps': 'https://developer.mozilla.org/docs/Apps',\n+        'Mozilla Developer Program': 'https://developer.mozilla.org/docs/Mozilla/Developer_Program',\n+        'Emscripten': 'https://developer.mozilla.org/docs/Emscripten',\n+        'L20n': 'https://developer.mozilla.org/docs/L20n',\n+        'Firefox Marketplace': 'https://developer.mozilla.org/Marketplace',\n+        'Gecko': 'https://developer.mozilla.org/docs/Mozilla/Gecko',\n+        'XPCOM': 'https://developer.mozilla.org/docs/Mozilla/XPCOM',\n+        '#mdn': 'irc://irc.mozilla.org/mdn',\n+        '#mdndev': 'irc://irc.mozilla.org/mdndev',\n+        'mozilla-central': 'https://developer.mozilla.org/en-US/docs/mozilla-central',\n+        'Mozilla': 'https://www.mozilla.org/'\n+    };\n+\n+    var options = {\n+        includeElems: ['p', 'div', 'span'],\n+        trackingString: '?utm_source=js%20snippet&utm_medium=content%20link&utm_campaign=promote%20mdn',\n+        maxLinks: 3,\n+        linkClass: ''\n+    };\n+\n+    userSettings = (userSettings) ? userSettings : {};\n+    options = extend({}, options, userSettings);\n+    \n+    for (var i in options.extraLinks) {\n+        if (dataset[i] === undefined) {\n+            dataset[i] = options.extraLinks[i];\n+        }\n+    }\n+\n+    for (var i in options.extraLinks) {\n+        if (dataset[i] === undefined) {\n+            dataset[i] = options.extraLinks[i];\n+        }\n+    }\n+\n+    var replaceCount = 0;\n+    var re = new RegExp(/<a[^>]*>(.*?)<\\/a>/);\n+\n+    var elements = document.querySelectorAll(options.includeElems.join(', '));\n+    forEach(elements, function(o){\n+        var text = o.innerHTML;\n+        var placeholder;\n+        var placeholderIndex = 0;\n+        var anchors_existing = [];\n+        var anchors_new = [];\n+\n+        if (text.match(/<a[^>]*>(.*?)<\\/a>/g) && text.match(/<a[^>]*>(.*?)<\\/a>/g).length) {\n+            var anchorCount = text.match(/<a[^>]*>(.*?)<\\/a>/g).length;\n+\n+            for (var i = 0; i < anchorCount; i++) {\n+                var anchor = re.exec(text);\n+                placeholder = '{_m$d$n_repl$ace_' + placeholderIndex + '_}';\n+                anchors_existing[placeholder] = anchor[0];\n+                text = text.replace(re, placeholder);\n+                placeholderIndex++;\n+            }\n+        }\n+\n+        // text is now stripped of all hyperlinks","path":"media/redesign/js/promote-mdn.js","position":134,"original_position":134,"commit_id":"1348bbefdc74e7c567ab9d19cd75a810f55d5779","original_commit_id":"1348bbefdc74e7c567ab9d19cd75a810f55d5779","user":{"login":"riverspirit","id":712910,"avatar_url":"https://avatars.githubusercontent.com/u/712910?","gravatar_id":"8cc61d4e2dc63e75976a334348d473bc","url":"https://api.github.com/users/riverspirit","html_url":"https://github.com/riverspirit","followers_url":"https://api.github.com/users/riverspirit/followers","following_url":"https://api.github.com/users/riverspirit/following{/other_user}","gists_url":"https://api.github.com/users/riverspirit/gists{/gist_id}","starred_url":"https://api.github.com/users/riverspirit/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/riverspirit/subscriptions","organizations_url":"https://api.github.com/users/riverspirit/orgs","repos_url":"https://api.github.com/users/riverspirit/repos","events_url":"https://api.github.com/users/riverspirit/events{/privacy}","received_events_url":"https://api.github.com/users/riverspirit/received_events","type":"User","site_admin":false},"body":"Oh, nope. All the links in the page (specified via includeElems) will be kept intact in the final output. But first the existing links in the page (in the specified elements) are removed so that pattern matching works correctly.\r\n\r\n__Why I did like that is:__ Say there is an existing link in the code <code>&lt;a href=\"link here\"&gt;__javascript__ is awesome&lt;/a&gt;</code> in their code. In that case, we do not want to replace the keyword __javascript__ that is already part of a link. So such existing links in the matching elements are first pulled out and stored outside the html temporarily before running our pattern matcher.\r\n\r\nThe existing links that are pulled out are added back to the page once we finish with our keyword matching and replacing.\r\n\r\nIf there is a better way to do this without pulling out the existing links first, would love to do hear that.","created_at":"2014-03-20T16:33:27Z","updated_at":"2014-03-20T16:33:27Z","html_url":"https://github.com/mozilla/kuma/pull/2111#discussion_r10803145","pull_request_url":"https://api.github.com/repos/mozilla/kuma/pulls/2111","_links":{"self":{"href":"https://api.github.com/repos/mozilla/kuma/pulls/comments/10803145"},"html":{"href":"https://github.com/mozilla/kuma/pull/2111#discussion_r10803145"},"pull_request":{"href":"https://api.github.com/repos/mozilla/kuma/pulls/2111"}}}}