Commit a689fe53 authored by mfluharty's avatar mfluharty

Prefer working with`.dataset`

Instead of `.getAttribute()` and `.setAttribute()`
parent 3ada7bde
...@@ -79,7 +79,7 @@ export default class BlobViewer { ...@@ -79,7 +79,7 @@ export default class BlobViewer {
switchToInitialViewer() { switchToInitialViewer() {
const initialViewer = this.$fileHolder[0].querySelector('.blob-viewer:not(.hidden)'); const initialViewer = this.$fileHolder[0].querySelector('.blob-viewer:not(.hidden)');
let initialViewerName = initialViewer.getAttribute('data-type'); let initialViewerName = initialViewer.dataset.type;
if (this.switcher && window.location.hash.indexOf('#L') === 0) { if (this.switcher && window.location.hash.indexOf('#L') === 0) {
initialViewerName = SIMPLE_VIEWER_NAME; initialViewerName = SIMPLE_VIEWER_NAME;
...@@ -105,14 +105,15 @@ export default class BlobViewer { ...@@ -105,14 +105,15 @@ export default class BlobViewer {
} }
static linkifyURLs(viewer) { static linkifyURLs(viewer) {
if (viewer.getAttribute('data-linkified')) return; if (viewer.dataset.linkified) return;
document.querySelectorAll('.js-blob-content .code .line').forEach(line => { document.querySelectorAll('.js-blob-content .code .line').forEach(line => {
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign
line.innerHTML = line.innerHTML.replace(blobLinkRegex, '<a href="$&">$&</a>'); line.innerHTML = line.innerHTML.replace(blobLinkRegex, '<a href="$&">$&</a>');
}); });
viewer.setAttribute('data-linkified', 'true'); // eslint-disable-next-line no-param-reassign
viewer.dataset.linkified = true;
} }
switchViewHandler(e) { switchViewHandler(e) {
...@@ -120,25 +121,19 @@ export default class BlobViewer { ...@@ -120,25 +121,19 @@ export default class BlobViewer {
e.preventDefault(); e.preventDefault();
this.switchToViewer(target.getAttribute('data-viewer')); this.switchToViewer(target.dataset.viewer);
} }
toggleCopyButtonState() { toggleCopyButtonState() {
if (!this.copySourceBtn) return; if (!this.copySourceBtn) return;
if (this.simpleViewer.getAttribute('data-loaded')) { if (this.simpleViewer.dataset.loaded) {
this.copySourceBtn.setAttribute('title', __('Copy file contents')); this.copySourceBtn.dataset.title = __('Copy file contents');
this.copySourceBtn.classList.remove('disabled'); this.copySourceBtn.classList.remove('disabled');
} else if (this.activeViewer === this.simpleViewer) { } else if (this.activeViewer === this.simpleViewer) {
this.copySourceBtn.setAttribute( this.copySourceBtn.dataset.title = __('Wait for the file to load to copy its contents');
'title',
__('Wait for the file to load to copy its contents'),
);
this.copySourceBtn.classList.add('disabled'); this.copySourceBtn.classList.add('disabled');
} else { } else {
this.copySourceBtn.setAttribute( this.copySourceBtn.dataset.title = __('Switch to the source to copy the file contents');
'title',
__('Switch to the source to copy the file contents'),
);
this.copySourceBtn.classList.add('disabled'); this.copySourceBtn.classList.add('disabled');
} }
...@@ -187,17 +182,17 @@ export default class BlobViewer { ...@@ -187,17 +182,17 @@ export default class BlobViewer {
static loadViewer(viewerParam) { static loadViewer(viewerParam) {
const viewer = viewerParam; const viewer = viewerParam;
const url = viewer.getAttribute('data-url'); const { url, loaded, loading } = viewer.dataset;
if (!url || viewer.getAttribute('data-loaded') || viewer.getAttribute('data-loading')) { if (!url || loaded || loading) {
return Promise.resolve(viewer); return Promise.resolve(viewer);
} }
viewer.setAttribute('data-loading', 'true'); viewer.dataset.loading = true;
return axios.get(url).then(({ data }) => { return axios.get(url).then(({ data }) => {
viewer.innerHTML = data.html; viewer.innerHTML = data.html;
viewer.setAttribute('data-loaded', 'true'); viewer.dataset.loaded = true;
return viewer; return viewer;
}); });
......
...@@ -94,14 +94,15 @@ export default class EditBlob { ...@@ -94,14 +94,15 @@ export default class EditBlob {
initBlobContentLinkClickability() { initBlobContentLinkClickability() {
this.editor.renderer.on('afterRender', () => { this.editor.renderer.on('afterRender', () => {
document.querySelectorAll('.ace_text-layer .ace_line > *').forEach(token => { document.querySelectorAll('.ace_text-layer .ace_line > *').forEach(token => {
if (token.getAttribute('data-linkified') || !token.textContent.includes('http')) return; if (token.dataset.linkified || !token.textContent.includes('http')) return;
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign
token.innerHTML = token.innerHTML.replace( token.innerHTML = token.innerHTML.replace(
blobLinkRegex, blobLinkRegex,
'<a target="_blank" href="$&">$&</a>', '<a target="_blank" href="$&">$&</a>',
); );
token.setAttribute('data-linkified', 'true'); // eslint-disable-next-line no-param-reassign
token.dataset.linkified = true;
}); });
}); });
} }
......
...@@ -76,9 +76,9 @@ describe('Blob viewer', () => { ...@@ -76,9 +76,9 @@ describe('Blob viewer', () => {
asyncClick() asyncClick()
.then(() => asyncClick()) .then(() => asyncClick())
.then(() => { .then(() => {
expect( expect(document.querySelector('.blob-viewer[data-type="simple"]').dataset.loaded).toBe(
document.querySelector('.blob-viewer[data-type="simple"]').getAttribute('data-loaded'), 'true',
).toBe('true'); );
done(); done();
}) })
...@@ -100,9 +100,7 @@ describe('Blob viewer', () => { ...@@ -100,9 +100,7 @@ describe('Blob viewer', () => {
}); });
it('has tooltip when disabled', () => { it('has tooltip when disabled', () => {
expect(copyButton.getAttribute('data-original-title')).toBe( expect(copyButton.dataset.title).toBe('Switch to the source to copy the file contents');
'Switch to the source to copy the file contents',
);
}); });
it('is blurred when clicked and disabled', () => { it('is blurred when clicked and disabled', () => {
...@@ -136,7 +134,7 @@ describe('Blob viewer', () => { ...@@ -136,7 +134,7 @@ describe('Blob viewer', () => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click(); document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
setTimeout(() => { setTimeout(() => {
expect(copyButton.getAttribute('data-original-title')).toBe('Copy file contents'); expect(copyButton.dataset.title).toBe('Copy file contents');
done(); done();
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment