Bug 1415940 Part 5: Change InspectorUtils::GetRelativeRuleLine to not remap line numbers if it introduces underflow. r=bz

MozReview-Commit-ID: 8ZhzPWubBg7

--HG--
extra : rebase_source : e3edb4a9f9449dc6c760a71cd61a3ffb08c9fcfb
This commit is contained in:
Brad Werth 2018-01-17 14:40:45 -08:00
Родитель dad1ec8175
Коммит 150230fd83
2 изменённых файлов: 33 добавлений и 1 удалений

Просмотреть файл

@ -9,6 +9,9 @@
window.onload = function () {
let x = document.styleSheets[0];
x.insertRule("div { color: seagreen; }", 1);
// Add a rule with a leading newline, to test that inspector can handle it.
x.insertRule("\n\ndiv { font-weight: bold; }", 1);
};
</script>

Просмотреть файл

@ -275,6 +275,28 @@ InspectorUtils::GetRuleColumn(GlobalObject& aGlobal, css::Rule& aRule)
InspectorUtils::GetRelativeRuleLine(GlobalObject& aGlobal, css::Rule& aRule)
{
uint32_t lineNumber = aRule.GetLineNumber();
// If aRule was parsed along with its stylesheet, then it will
// have an absolute lineNumber that we need to remap to its
// containing node. But if aRule was added via CSSOM after parsing,
// then it has a sort-of relative line number already:
// Gecko gives all rules a 0 lineNumber.
// Servo gives the first line of a rule a 0 lineNumber, and then
// counts up from there.
// The Servo behavior is arguably more correct, but harder to
// interpret for purposes of deciding whether a lineNumber is
// relative or absolute.
// Since most of the time, inserted rules are single line and
// therefore have 0 lineNumbers in both Gecko and Servo, we use
// that to detect that a lineNumber is already relative.
// There is one ugly edge case that we avoid: if an inserted rule
// is multi-line, then Servo will give it 0+ lineNumbers. If we
// do relative number mapping on those line numbers, we could get
// negative underflow. So we check for underflow and instead report
// a 0 lineNumber.
StyleSheet* sheet = aRule.GetStyleSheet();
if (sheet && lineNumber != 0) {
nsINode* owningNode = sheet->GetOwnerNode();
@ -282,7 +304,14 @@ InspectorUtils::GetRelativeRuleLine(GlobalObject& aGlobal, css::Rule& aRule)
nsCOMPtr<nsIStyleSheetLinkingElement> link =
do_QueryInterface(owningNode);
if (link) {
lineNumber -= link->GetLineNumber() - 1;
// Check for underflow, which is one indication that we're
// trying to remap an already relative lineNumber.
uint32_t linkLineIndex0 = link->GetLineNumber() - 1;
if (linkLineIndex0 > lineNumber ) {
lineNumber = 0;
} else {
lineNumber -= linkLineIndex0;
}
}
}
}