Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add highlights for unique names on hover. #18

Merged
merged 1 commit into from
Mar 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ function printHead(ast) {
'<meta charset="utf-8">' +
'<title>' + (ast.title ? ast.title.value : 'Spec') + '</title>' +
'<style>' + readStatic('spec.css') + '</style>' +
'<style>' + readStatic('prism.css') + '</style>'
'<style>' + readStatic('prism.css') + '</style>' +
'<script>(function (){\n' + readStatic('highlightName.js') + '})()</script>'
);
}

Expand Down Expand Up @@ -489,7 +490,7 @@ function printAll(list, options) {
case 'Call':
return (
'<span class="spec-call">' +
link(node, anchorize(node.name) + '()', options) +
link(node, anchorize(node.name) + '()', options, true) +
'(' + join(node.args, ', ') + ')' +
'</span>'
);
Expand All @@ -501,7 +502,7 @@ function printAll(list, options) {
return '<span class="spec-string">' + node.value + '</span>';

case 'Variable':
return '<var>' + node.name + '</var>';
return '<var data-name="' + anchorize(node.name) + '">' + node.name + '</var>';

case 'Semantic':
var defType = node.defType === 1 ? '' : ' d' + node.defType;
Expand Down Expand Up @@ -566,7 +567,7 @@ function printAll(list, options) {
(node.isList ? ' list' : '') +
(node.isOptional ? ' optional' : '') +
'">' +
link(node, anchorize(node.name), options) +
link(node, anchorize(node.name), options, true) +
(node.params ? '<span class="spec-params">' + join(node.params) + '</span>' : '') +
'</span>'
);
Expand Down Expand Up @@ -697,14 +698,22 @@ function maybe(value) {
return value ? value : '';
}

function link(node, id, options) {
function link(node, id, options, doHighlight) {
var href = options.biblio[id];
var content = escape(node.name);
if (!href) {
if (doHighlight) {
return (
'<span data-name="' + anchorize(node.name) + '">' +
content +
'</span>'
);
}
return content;
}
return (
'<a href="' + href + '"' +
(doHighlight ? ' data-name="' + anchorize(node.name) + '"' : '') +
(href[0] !== '#' ? ' target="_blank"' : '') + '>' +
content +
'</a>'
Expand Down
26 changes: 26 additions & 0 deletions static/highlightName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var styleSheet = document.getElementsByTagName('style')[0].sheet;
var ruleIndex;

function removeHighlight() {
if (ruleIndex) {
styleSheet.deleteRule(ruleIndex);
ruleIndex = void 0;
}
}

function highlightKeyword(name) {
removeHighlight();
ruleIndex = styleSheet.insertRule(
'*[data-name="' + name + '"] { background: #FBF8D0; }',
styleSheet.cssRules.length
);
}

document.documentElement.addEventListener('mouseover', function (event) {
var nameAttribute = event.target.attributes['data-name'];
if (nameAttribute) {
highlightKeyword(nameAttribute.value);
}
});

document.documentElement.addEventListener('mouseout', removeHighlight);
7 changes: 7 additions & 0 deletions static/spec.css
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,13 @@ var {
font-style: italic;
}

*[data-name] {
transition: 0.15s background ease-out;
border-radius: 2px;
padding: 0 3px;
margin: 0 -3px;
}


/* Grammar semantics, algorithms and calls */

Expand Down