Skip to content

Commit

Permalink
Fix and simplify MDN annotations
Browse files Browse the repository at this point in the history
Always search for the nearest <body> child and insert before it.

We may fix this in a better way going forward.
  • Loading branch information
domenic authored Oct 23, 2018
1 parent 0aecca4 commit e4fd2f0
Showing 1 changed file with 20 additions and 92 deletions.
112 changes: 20 additions & 92 deletions src/wattsi.pas
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ procedure AddMDNBox(const MDNBox: TElement;
var
MDNData, MDNSupport: TJSONObject;
MDNButton, MDNDiv, MDNDetails, SupportTable: TElement;
MDNSlug, MDNSubpath, MDNTitle, MDNSummary, BrowserID: UTF8String;
MDNSlug, MDNSubpath, MDNSummary, BrowserID: UTF8String;
i, j: Integer;
const
kMDNURLBase = 'https://developer.mozilla.org/en-US/docs/Web/';
Expand Down Expand Up @@ -260,7 +260,6 @@ procedure AddMDNBox(const MDNBox: TElement;
// See https://goo.gl/uejWa4 for documentation on the structure.
MDNSlug := MDNData['slug'];
MDNSummary := MDNData['summary'];
MDNTitle := MDNData['title'];
MDNSubpath := Copy(MDNSlug, Pos('/', MDNSlug) + 1);
MDNDiv := E(eDiv);
MDNDiv.AppendChild(E(eB, [T('MDN')]));
Expand Down Expand Up @@ -781,63 +780,38 @@ TCrossReferences = record
var
Candidate: TNode;
ID: UTF8String;
TargetAncestor, MDNBefore, MDNBox: TElement;
TargetAncestorType: TCanonicalString;
TargetAncestor, MDNBox: TElement;
begin
if (HasAncestor(Element, nsHTML, eP)) then
// Annotations for elements with p ancestors are handled later.
exit;
if (not(Element.HasAttribute('id'))) then
exit;
ID := Element.GetAttribute('id').AsString;
if (not(MDNJSONData[ID] is TJSONArray)) then
// No MDN article has a link to this ID.
exit;
MDNBox := E(eAside, ['class', 'mdn']);
MDNBefore := E(eAside, ['class', 'mdn before']);

MDNBox := E(eAside, ['class', 'mdn before']);

// Find the furthest ancestor that is a direct child of <body>
Candidate := Element;
if (HasAncestor(Element, nsHTML, ePre)
or HasAncestor(Element, nsHTML, eTable)
or HasAncestor(Element, nsHTML, eDL)) then
while not TElement(Candidate.ParentNode).IsIdentity(nsHTML, eBody) do
begin
if (HasAncestor(Element, nsHTML, ePre)) then
TargetAncestorType := ePre
else if (HasAncestor(Element, nsHTML, eTable)) then
TargetAncestorType := eTable
else if (HasAncestor(Element, nsHTML, eDL)) then
TargetAncestorType := eDL;
Candidate := Element;
repeat
Candidate := Candidate.ParentNode;
if (not (Candidate is TElement)) then
exit;
until (TElement(Candidate).IsIdentity(nsHTML, TargetAncestorType));
TargetAncestor := TElement(Candidate);
if ((TargetAncestor.PreviousSibling is TElement)
and (TElement(TargetAncestor.PreviousSibling)
.GetAttribute('class').AsString = 'mdn before')) then
// If there's already an MDN box at the point where we want this,
// then just re-use it (instead of creating another one).
MDNBefore := TElement(TargetAncestor.PreviousSibling)
else
TElement(TargetAncestor.ParentNode)
.InsertBefore(MDNBefore, TargetAncestor);
MDNBox := MDNBefore;
Candidate := Candidate.ParentNode;
end;
TargetAncestor := TElement(Candidate);

if ((TargetAncestor.PreviousSibling is TElement)
and (TElement(TargetAncestor.PreviousSibling)
.GetAttribute('class').AsString = 'mdn before')) then
begin
// If there's already an MDN box at the point where we want this,
// then just re-use it (instead of creating another one).
MDNBox := TElement(TargetAncestor.PreviousSibling)
end
else
begin
// Otherwise insert the annotation after the element being annotated.
// Headings are the only existing cases where we reach here.
if ((Element.NextSibling is TElement)
and ((Element.NextSibling as TElement)
.GetAttribute('class').AsString = 'mdn')) then
// If there's already an MDN box at the point where we want this,
// then just re-use it (instead of creating another one).
MDNBox := Element.NextSibling as TElement
else
(Element.ParentNode as TElement)
.InsertBefore(MDNBox, Element.NextSibling);
TElement(TargetAncestor.ParentNode).InsertBefore(MDNBox, TargetAncestor);
end;

AddMDNBox(MDNBox, ID, Document);
end;

Expand Down Expand Up @@ -2111,45 +2085,6 @@ procedure Save(const Document: TDocument; const FileName: AnsiString; const InSp
Write(F, WPTOutput.Text);
end;

procedure InsertMDNAnnotationForElementWithPAncestor(const Element: TElement);
var
Candidate: TNode;
ID: UTF8String;
PElement, MDNBox: TElement;
begin
// In order to minimize the cases where an annotation overlaps with the
// spec text, annotations for elements with p ancestors are handled
// separately here. The annotations are inserted after the p-element
// ancestor of the element being annotated.
if ((CurrentVariant = vHTML) and InSplit) then
// MDN annotations have already been inserted in this case.
exit;
if (not(Element.HasAttribute('id'))) then
exit;
ID := Element.GetAttribute('id').AsString;
if (not(MDNJSONData[ID] is TJSONArray)) then
// No MDN article has a link to this ID.
exit;
MDNBox := E(eAside, ['class', 'mdn']);
Candidate := Element;
repeat
Candidate := Candidate.ParentNode;
if (not (Candidate is TElement)) then
exit;
until (TElement(Candidate).IsIdentity(nsHTML, eP));
PElement := TElement(Candidate);
if ((PElement.NextSibling is TElement)
and (TElement(PElement.NextSibling)
.GetAttribute('class').AsString = 'mdn')) then
// If there's already an MDN box at the point where we want this,
// then just re-use it (instead of creating another one).
MDNBox := TElement(PElement.NextSibling)
else
TElement(PElement.ParentNode)
.InsertBefore(MDNBox, PElement.NextSibling);
AddMDNBox(MDNBox, ID, Document);
end;

procedure WalkIn(const Element: TElement);
var
IsExcluder, Skip, NotFirstAttribute: Boolean;
Expand Down Expand Up @@ -2292,13 +2227,6 @@ procedure Save(const Document: TDocument; const FileName: AnsiString; const InSp
CurrentElement := TElement(Element.ParentNode)
else
CurrentElement := nil;
if (HasAncestor(Element, nsHTML, eP)) then
// Elements other than those with p ancestors are handled earlier.
// Elements with p ancestors are handled here because the annotations need
// to be inserted after the end tag for the p element has been emitted.
// Otherwise the annotation ends up getting inserted within the p element.
if (CurrentVariant <> vReview) then
InsertMDNAnnotationForElementWithPAncestor(Element);
end;

var
Expand Down

0 comments on commit e4fd2f0

Please sign in to comment.