Skip to content
This repository was archived by the owner on Sep 2, 2020. It is now read-only.

Commit 106d474

Browse files
authored
Merge pull request #125 from wikimedia/update/redlinks-method-signature
Breaking: remove 'fragment' parameter from redlinks transform.
2 parents f7e6f57 + 4a8f1ca commit 106d474

File tree

2 files changed

+11
-57
lines changed

2 files changed

+11
-57
lines changed

src/transform/RedLinks.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@ const configureRedLinkTemplate = (span, anchor) => {
1212
}
1313

1414
/**
15-
* Finds red links in a document or document fragment.
16-
* @param {!(Document|DocumentFragment)} content Document or fragment in which to seek red links.
15+
* Finds red links in a document.
16+
* @param {!Document} content Document in which to seek red links.
1717
* @return {!Array.<HTMLAnchorElement>} Array of zero or more red link anchors.
1818
*/
19-
const redLinkAnchorsInContent = content => Polyfill.querySelectorAll(content, 'a.new')
19+
const redLinkAnchorsInDocument = content => Polyfill.querySelectorAll(content, 'a.new')
2020

2121
/**
2222
* Makes span to be used as cloning template for red link anchor replacements.
23-
* @param {!Document} document Document to use to create span element. Reminder: this can't be a
24-
* document fragment because fragments don't implement 'createElement'.
23+
* @param {!Document} document Document to use to create span element.
2524
* @return {!HTMLSpanElement} Span element suitable for use as template for red link anchor
2625
* replacements.
2726
*/
@@ -36,17 +35,13 @@ const newRedLinkTemplate = document => document.createElement('span')
3635
const replaceAnchorWithSpan = (anchor, span) => anchor.parentNode.replaceChild(span, anchor)
3736

3837
/**
39-
* Hides red link anchors in either a document or a document fragment so they are unclickable and
40-
* unfocusable.
38+
* Hides red link anchors in a document so they are unclickable and unfocusable.
4139
* @param {!Document} document Document in which to hide red links.
42-
* @param {?DocumentFragment} fragment If specified, red links are hidden in the fragment and the
43-
* document is used only for span cloning.
4440
* @return {void}
4541
*/
46-
const hideRedLinks = (document, fragment) => {
42+
const hideRedLinks = document => {
4743
const spanTemplate = newRedLinkTemplate(document)
48-
const content = fragment !== undefined ? fragment : document
49-
redLinkAnchorsInContent(content)
44+
redLinkAnchorsInDocument(document)
5045
.forEach(redLink => {
5146
const span = spanTemplate.cloneNode(false)
5247
configureRedLinkTemplate(span, redLink)
@@ -58,7 +53,7 @@ export default {
5853
hideRedLinks,
5954
test: {
6055
configureRedLinkTemplate,
61-
redLinkAnchorsInContent,
56+
redLinkAnchorsInDocument,
6257
newRedLinkTemplate,
6358
replaceAnchorWithSpan
6459
}

test/transform/RedLinks.test.js

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import assert from 'assert'
22
import domino from 'domino'
3-
import dominoDocumentFragment from '../utilities/DominoDocumentFragment'
43
import pagelib from '../../build/wikimedia-page-library-transform'
54

6-
const documentFragmentFromHTMLString = dominoDocumentFragment.documentFragmentFromHTMLString
75
const configureRedLinkTemplate = pagelib.RedLinks.test.configureRedLinkTemplate
8-
const redLinkAnchorsInContent = pagelib.RedLinks.test.redLinkAnchorsInContent
6+
const redLinkAnchorsInDocument = pagelib.RedLinks.test.redLinkAnchorsInDocument
97
const newRedLinkTemplate = pagelib.RedLinks.test.newRedLinkTemplate
108
const replaceAnchorWithSpan = pagelib.RedLinks.test.replaceAnchorWithSpan
119
const hideRedLinks = pagelib.RedLinks.hideRedLinks
@@ -29,18 +27,10 @@ describe('RedLinks', () => {
2927
})
3028
})
3129

32-
describe('redLinkAnchorsInContent()', () => {
30+
describe('redLinkAnchorsInDocument()', () => {
3331
it('should find one red link in a document', () => {
3432
const doc = domino.createDocument('<a id="link1">1</a><a id="link2" class="new">2</a>')
35-
const redLinkAnchors = redLinkAnchorsInContent(doc)
36-
assert.ok(redLinkAnchors.length === 1)
37-
assert.ok(redLinkAnchors[0].id === 'link2')
38-
})
39-
40-
it('should find one red link in a document fragment', () => {
41-
const frag =
42-
documentFragmentFromHTMLString('<a id="link1">1</a><a id="link2" class="new">2</a>')
43-
const redLinkAnchors = redLinkAnchorsInContent(frag)
33+
const redLinkAnchors = redLinkAnchorsInDocument(doc)
4434
assert.ok(redLinkAnchors.length === 1)
4535
assert.ok(redLinkAnchors[0].id === 'link2')
4636
})
@@ -72,25 +62,6 @@ describe('RedLinks', () => {
7262
// Ensure the swap happened - element '#two' should now be a SPAN
7363
assert.ok(elementTwoTagName(doc) === 'SPAN')
7464
})
75-
76-
it('should replace a document fragment anchor with a span', () => {
77-
// We'll swap the 'A#two' with 'SPAN#two'
78-
const frag =
79-
documentFragmentFromHTMLString('<a id="one">1</a><a id="two" class="new">2</a>')
80-
const elementTwoTagName = frag => frag.querySelector('#two').tagName // eslint-disable-line require-jsdoc, max-len
81-
// #two should initially be an 'A'
82-
assert.ok(elementTwoTagName(frag) === 'A')
83-
84-
// Here's the replacement 'SPAN#two'
85-
const span = domino.createDocument().createElement('SPAN')
86-
span.id = 'two'
87-
88-
// Swap!
89-
replaceAnchorWithSpan(frag.querySelector('#two'), span)
90-
91-
// Ensure the swap happened - element '#two' should now be a SPAN
92-
assert.ok(elementTwoTagName(frag) === 'SPAN')
93-
})
9465
})
9566

9667
describe('hideRedLinks()', () => {
@@ -103,17 +74,5 @@ describe('RedLinks', () => {
10374
assert.ok(item2.tagName === 'SPAN')
10475
assert.ok(item2.innerHTML === '<b>2</b>')
10576
})
106-
107-
it('should hide the expected red links in a document fragment', () => {
108-
const doc = domino.createDocument()
109-
const frag =
110-
documentFragmentFromHTMLString('<a id="item1">1</a><a id="item2" class="new"><b>2</b></a>')
111-
hideRedLinks(doc, frag)
112-
const item1 = frag.querySelector('#item1')
113-
const item2 = frag.querySelector('.new')
114-
assert.ok(item1.tagName === 'A')
115-
assert.ok(item2.tagName === 'SPAN')
116-
assert.ok(item2.innerHTML === '<b>2</b>')
117-
})
11877
})
11978
})

0 commit comments

Comments
 (0)