Bug 1288084 - Expose a line number for each attribute, v2; r=wchen

This commit is contained in:
Henri Sivonen 2016-09-20 16:31:57 +02:00
parent 7dc66b85b3
commit eea6b5c9c8
12 changed files with 87 additions and 27 deletions

View File

@ -4452,7 +4452,7 @@ class MOZ_STACK_CLASS Debugger::ScriptQuery
return;
}
if (hasSource && !(source.is<ScriptSourceObject*>() &&
source.as<ScriptSourceObject*>() == script->sourceObject()))
source.as<ScriptSourceObject*>()->source() == script->scriptSource()))
{
return;
}

View File

@ -63,6 +63,8 @@ public final class HtmlAttributes implements Attributes {
private @Auto String[] values; // XXX perhaps make this @NoLength?
// CPPONLY: private @Auto int[] lines; // XXX perhaps make this @NoLength?
// [NOCPP[
private String idValue;
@ -80,10 +82,12 @@ public final class HtmlAttributes implements Attributes {
this.length = 0;
/*
* The length of 5 covers covers 98.3% of elements
* according to Hixie
* according to Hixie, but lets round to the next power of two for
* jemalloc.
*/
this.names = new AttributeName[5];
this.values = new String[5];
this.names = new AttributeName[8];
this.values = new String[8];
// CPPONLY: this.lines = new int[8];
// [NOCPP[
@ -199,6 +203,16 @@ public final class HtmlAttributes implements Attributes {
return names[index];
}
// CPPONLY: /**
// CPPONLY: * Obtains a line number without bounds check.
// CPPONLY: * @param index a valid attribute index
// CPPONLY: * @return the line number at index or -1 if unknown
// CPPONLY: */
// CPPONLY: public int getLineNoBoundsCheck(int index) {
// CPPONLY: assert index < length && index >= 0: "Index out of bounds";
// CPPONLY: return lines[index];
// CPPONLY: }
// [NOCPP[
/**
@ -393,7 +407,8 @@ public final class HtmlAttributes implements Attributes {
void addAttribute(AttributeName name, String value
// [NOCPP[
, XmlViolationPolicy xmlnsPolicy
// ]NOCPP]
// ]NOCPP]
// CPPONLY: , int line
) throws SAXException {
// [NOCPP[
if (name == AttributeName.ID) {
@ -436,9 +451,13 @@ public final class HtmlAttributes implements Attributes {
String[] newValues = new String[newLen];
System.arraycopy(values, 0, newValues, 0, values.length);
values = newValues;
// CPPONLY: int[] newLines = new int[newLen];
// CPPONLY: System.arraycopy(lines, 0, newLines, 0, lines.length);
// CPPONLY: lines = newLines;
}
names[length] = name;
values[length] = value;
// CPPONLY: lines[length] = line;
length++;
}
@ -520,6 +539,7 @@ public final class HtmlAttributes implements Attributes {
// [NOCPP[
, XmlViolationPolicy.ALLOW
// ]NOCPP]
// CPPONLY: , lines[i]
);
}
// [NOCPP[

View File

@ -493,6 +493,13 @@ public class Tokenizer implements Locator {
private int line;
/*
* The line number of the current attribute. First set to the line of the
* attribute name and if there is a value, set to the line the value
* started on.
*/
// CPPONLY: private int attributeLine;
private Interner interner;
// CPPONLY: private boolean viewingXmlSource;
@ -747,6 +754,7 @@ public class Tokenizer implements Locator {
* For C++ use only.
*/
public void setLineNumber(int line) {
// CPPONLY: this.attributeLine = line; // XXX is this needed?
this.line = line;
}
@ -1175,6 +1183,7 @@ public class Tokenizer implements Locator {
// [NOCPP[
, xmlnsPolicy
// ]NOCPP]
// CPPONLY: , attributeLine
);
// [NOCPP[
}
@ -1207,6 +1216,7 @@ public class Tokenizer implements Locator {
// [NOCPP[
, xmlnsPolicy
// ]NOCPP]
// CPPONLY: , attributeLine
);
attributeName = null; // attributeName has been adopted by the
// |attributes| object
@ -1753,6 +1763,7 @@ public class Tokenizer implements Locator {
*/
c += 0x20;
}
// CPPONLY: attributeLine = line;
/*
* Set that attribute's name to the current
* input character,
@ -1902,6 +1913,7 @@ public class Tokenizer implements Locator {
* U+0022 QUOTATION MARK (") Switch to the
* attribute value (double-quoted) state.
*/
// CPPONLY: attributeLine = line;
clearStrBuf();
state = transition(state, Tokenizer.ATTRIBUTE_VALUE_DOUBLE_QUOTED, reconsume, pos);
break beforeattributevalueloop;
@ -1912,6 +1924,7 @@ public class Tokenizer implements Locator {
* value (unquoted) state and reconsume this
* input character.
*/
// CPPONLY: attributeLine = line;
clearStrBuf();
reconsume = true;
state = transition(state, Tokenizer.ATTRIBUTE_VALUE_UNQUOTED, reconsume, pos);
@ -1922,6 +1935,7 @@ public class Tokenizer implements Locator {
* U+0027 APOSTROPHE (') Switch to the attribute
* value (single-quoted) state.
*/
// CPPONLY: attributeLine = line;
clearStrBuf();
state = transition(state, Tokenizer.ATTRIBUTE_VALUE_SINGLE_QUOTED, reconsume, pos);
continue stateloop;
@ -1965,6 +1979,7 @@ public class Tokenizer implements Locator {
* Anything else Append the current input
* character to the current attribute's value.
*/
// CPPONLY: attributeLine = line;
clearStrBufAndAppend(c);
/*
* Switch to the attribute value (unquoted)
@ -6737,6 +6752,7 @@ public class Tokenizer implements Locator {
confident = false;
strBuf = null;
line = 1;
// CPPONLY: attributeLine = 1;
// [NOCPP[
html4 = false;
metaBoundaryPassed = false;

View File

@ -2346,6 +2346,7 @@ public abstract class TreeBuilder<T> implements TokenHandler,
// [NOCPP[
, XmlViolationPolicy.ALLOW
// ]NOCPP]
// CPPONLY: , attributes.getLineNoBoundsCheck(actionIndex)
);
}
appendToCurrentNodeAndPushFormElementMayFoster(formAttrs);
@ -2371,6 +2372,7 @@ public abstract class TreeBuilder<T> implements TokenHandler,
// [NOCPP[
, XmlViolationPolicy.ALLOW
// ]NOCPP]
// CPPONLY: , tokenizer.getLineNumber()
);
for (int i = 0; i < attributes.getLength(); i++) {
AttributeName attributeQName = attributes.getAttributeNameNoBoundsCheck(i);
@ -2384,7 +2386,7 @@ public abstract class TreeBuilder<T> implements TokenHandler,
// [NOCPP[
, XmlViolationPolicy.ALLOW
// ]NOCPP]
// CPPONLY: , attributes.getLineNoBoundsCheck(i)
);
}
}

View File

@ -106,7 +106,7 @@ nsHtml5Highlighter::Start(const nsAutoString& aTitle)
nsHtml5HtmlAttributes* preAttrs = new nsHtml5HtmlAttributes(0);
nsString* preId = new nsString(NS_LITERAL_STRING("line1"));
preAttrs->addAttribute(nsHtml5AttributeName::ATTR_ID, preId);
preAttrs->addAttribute(nsHtml5AttributeName::ATTR_ID, preId, -1);
Push(nsGkAtoms::pre, preAttrs);
StartCharacters();

View File

@ -60,8 +60,9 @@ nsHtml5HtmlAttributes* nsHtml5HtmlAttributes::EMPTY_ATTRIBUTES = nullptr;
nsHtml5HtmlAttributes::nsHtml5HtmlAttributes(int32_t mode)
: mode(mode),
length(0),
names(jArray<nsHtml5AttributeName*,int32_t>::newJArray(5)),
values(jArray<nsString*,int32_t>::newJArray(5))
names(jArray<nsHtml5AttributeName*,int32_t>::newJArray(8)),
values(jArray<nsString*,int32_t>::newJArray(8)),
lines(jArray<int32_t,int32_t>::newJArray(8))
{
MOZ_COUNT_CTOR(nsHtml5HtmlAttributes);
}
@ -136,8 +137,15 @@ nsHtml5HtmlAttributes::getAttributeNameNoBoundsCheck(int32_t index)
return names[index];
}
int32_t
nsHtml5HtmlAttributes::getLineNoBoundsCheck(int32_t index)
{
MOZ_ASSERT(index < length && index >= 0, "Index out of bounds");
return lines[index];
}
void
nsHtml5HtmlAttributes::addAttribute(nsHtml5AttributeName* name, nsString* value)
nsHtml5HtmlAttributes::addAttribute(nsHtml5AttributeName* name, nsString* value, int32_t line)
{
if (names.length == length) {
int32_t newLen = length << 1;
@ -147,9 +155,13 @@ nsHtml5HtmlAttributes::addAttribute(nsHtml5AttributeName* name, nsString* value)
jArray<nsString*,int32_t> newValues = jArray<nsString*,int32_t>::newJArray(newLen);
nsHtml5ArrayCopy::arraycopy(values, newValues, values.length);
values = newValues;
jArray<int32_t,int32_t> newLines = jArray<int32_t,int32_t>::newJArray(newLen);
nsHtml5ArrayCopy::arraycopy(lines, newLines, lines.length);
lines = newLines;
}
names[length] = name;
values[length] = value;
lines[length] = line;
length++;
}
@ -211,7 +223,7 @@ nsHtml5HtmlAttributes::cloneAttributes(nsHtml5AtomTable* interner)
MOZ_ASSERT((!length) || !mode || mode == 3);
nsHtml5HtmlAttributes* clone = new nsHtml5HtmlAttributes(0);
for (int32_t i = 0; i < length; i++) {
clone->addAttribute(names[i]->cloneAttributeName(interner), nsHtml5Portability::newStringFromString(values[i]));
clone->addAttribute(names[i]->cloneAttributeName(interner), nsHtml5Portability::newStringFromString(values[i]), lines[i]);
}
return clone;
}

View File

@ -65,6 +65,7 @@ class nsHtml5HtmlAttributes
int32_t length;
autoJArray<nsHtml5AttributeName*,int32_t> names;
autoJArray<nsString*,int32_t> values;
autoJArray<int32_t,int32_t> lines;
public:
explicit nsHtml5HtmlAttributes(int32_t mode);
~nsHtml5HtmlAttributes();
@ -76,7 +77,8 @@ class nsHtml5HtmlAttributes
nsIAtom* getPrefixNoBoundsCheck(int32_t index);
nsString* getValueNoBoundsCheck(int32_t index);
nsHtml5AttributeName* getAttributeNameNoBoundsCheck(int32_t index);
void addAttribute(nsHtml5AttributeName* name, nsString* value);
int32_t getLineNoBoundsCheck(int32_t index);
void addAttribute(nsHtml5AttributeName* name, nsString* value, int32_t line);
void clear(int32_t m);
void releaseValue(int32_t i);
void clearWithoutReleasingContents();

View File

@ -15,12 +15,12 @@ nsHtml5PlainTextUtils::NewLinkAttributes()
{
nsHtml5HtmlAttributes* linkAttrs = new nsHtml5HtmlAttributes(0);
nsString* rel = new nsString(NS_LITERAL_STRING("alternate stylesheet"));
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_REL, rel);
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_REL, rel, -1);
nsString* type = new nsString(NS_LITERAL_STRING("text/css"));
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_TYPE, type);
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_TYPE, type, -1);
nsString* href = new nsString(
NS_LITERAL_STRING("resource://gre-resources/plaintext.css"));
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_HREF, href);
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_HREF, href, -1);
nsresult rv;
nsCOMPtr<nsIStringBundleService> bundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
@ -35,6 +35,6 @@ nsHtml5PlainTextUtils::NewLinkAttributes()
}
nsString* titleCopy = new nsString(title);
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_TITLE, titleCopy);
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_TITLE, titleCopy, -1);
return linkAttrs;
}

View File

@ -199,6 +199,7 @@ nsHtml5Tokenizer::endTagExpectationToArray()
void
nsHtml5Tokenizer::setLineNumber(int32_t line)
{
this->attributeLine = line;
this->line = line;
}
@ -335,7 +336,7 @@ nsHtml5Tokenizer::addAttributeWithoutValue()
{
if (attributeName) {
attributes->addAttribute(attributeName, nsHtml5Portability::newEmptyString());
attributes->addAttribute(attributeName, nsHtml5Portability::newEmptyString(), attributeLine);
attributeName = nullptr;
}
}
@ -348,7 +349,7 @@ nsHtml5Tokenizer::addAttributeWithValue()
if (mViewSource) {
mViewSource->MaybeLinkifyAttributeValue(attributeName, val);
}
attributes->addAttribute(attributeName, val);
attributes->addAttribute(attributeName, val, attributeLine);
attributeName = nullptr;
}
}
@ -620,6 +621,7 @@ nsHtml5Tokenizer::stateLoop(int32_t state, char16_t c, int32_t pos, char16_t* bu
if (c >= 'A' && c <= 'Z') {
c += 0x20;
}
attributeLine = line;
clearStrBufAndAppend(c);
state = P::transition(mViewSource, NS_HTML5TOKENIZER_ATTRIBUTE_NAME, reconsume, pos);
NS_HTML5_BREAK(beforeattributenameloop);
@ -712,11 +714,13 @@ nsHtml5Tokenizer::stateLoop(int32_t state, char16_t c, int32_t pos, char16_t* bu
continue;
}
case '\"': {
attributeLine = line;
clearStrBuf();
state = P::transition(mViewSource, NS_HTML5TOKENIZER_ATTRIBUTE_VALUE_DOUBLE_QUOTED, reconsume, pos);
NS_HTML5_BREAK(beforeattributevalueloop);
}
case '&': {
attributeLine = line;
clearStrBuf();
reconsume = true;
state = P::transition(mViewSource, NS_HTML5TOKENIZER_ATTRIBUTE_VALUE_UNQUOTED, reconsume, pos);
@ -724,6 +728,7 @@ nsHtml5Tokenizer::stateLoop(int32_t state, char16_t c, int32_t pos, char16_t* bu
NS_HTML5_CONTINUE(stateloop);
}
case '\'': {
attributeLine = line;
clearStrBuf();
state = P::transition(mViewSource, NS_HTML5TOKENIZER_ATTRIBUTE_VALUE_SINGLE_QUOTED, reconsume, pos);
NS_HTML5_CONTINUE(stateloop);
@ -750,6 +755,7 @@ nsHtml5Tokenizer::stateLoop(int32_t state, char16_t c, int32_t pos, char16_t* bu
}
}
default: {
attributeLine = line;
clearStrBufAndAppend(c);
state = P::transition(mViewSource, NS_HTML5TOKENIZER_ATTRIBUTE_VALUE_UNQUOTED, reconsume, pos);
@ -4043,6 +4049,7 @@ nsHtml5Tokenizer::initializeWithoutStarting()
confident = false;
strBuf = nullptr;
line = 1;
attributeLine = 1;
resetToDataState();
}

View File

@ -135,6 +135,7 @@ class nsHtml5Tokenizer
bool confident;
private:
int32_t line;
int32_t attributeLine;
nsHtml5AtomTable* interner;
bool viewingXmlSource;
public:

View File

@ -1243,7 +1243,7 @@ nsHtml5TreeBuilder::startTag(nsHtml5ElementName* elementName, nsHtml5HtmlAttribu
nsHtml5HtmlAttributes* formAttrs = new nsHtml5HtmlAttributes(0);
int32_t actionIndex = attributes->getIndex(nsHtml5AttributeName::ATTR_ACTION);
if (actionIndex > -1) {
formAttrs->addAttribute(nsHtml5AttributeName::ATTR_ACTION, attributes->getValueNoBoundsCheck(actionIndex));
formAttrs->addAttribute(nsHtml5AttributeName::ATTR_ACTION, attributes->getValueNoBoundsCheck(actionIndex), attributes->getLineNoBoundsCheck(actionIndex));
}
appendToCurrentNodeAndPushFormElementMayFoster(formAttrs);
appendVoidElementToCurrentMayFoster(nsHtml5ElementName::ELT_HR, nsHtml5HtmlAttributes::EMPTY_ATTRIBUTES);
@ -1256,13 +1256,13 @@ nsHtml5TreeBuilder::startTag(nsHtml5ElementName* elementName, nsHtml5HtmlAttribu
appendIsindexPrompt(stack[currentPtr]->node);
}
nsHtml5HtmlAttributes* inputAttributes = new nsHtml5HtmlAttributes(0);
inputAttributes->addAttribute(nsHtml5AttributeName::ATTR_NAME, nsHtml5Portability::newStringFromLiteral("isindex"));
inputAttributes->addAttribute(nsHtml5AttributeName::ATTR_NAME, nsHtml5Portability::newStringFromLiteral("isindex"), tokenizer->getLineNumber());
for (int32_t i = 0; i < attributes->getLength(); i++) {
nsHtml5AttributeName* attributeQName = attributes->getAttributeNameNoBoundsCheck(i);
if (nsHtml5AttributeName::ATTR_NAME == attributeQName || nsHtml5AttributeName::ATTR_PROMPT == attributeQName) {
attributes->releaseValue(i);
} else if (nsHtml5AttributeName::ATTR_ACTION != attributeQName) {
inputAttributes->addAttribute(attributeQName, attributes->getValueNoBoundsCheck(i));
inputAttributes->addAttribute(attributeQName, attributes->getValueNoBoundsCheck(i), attributes->getLineNoBoundsCheck(i));
}
}
attributes->clearWithoutReleasingContents();

View File

@ -14,7 +14,7 @@ nsHtml5ViewSourceUtils::NewBodyAttributes()
{
nsHtml5HtmlAttributes* bodyAttrs = new nsHtml5HtmlAttributes(0);
auto id = MakeUnique<nsString>(NS_LITERAL_STRING("viewsource"));
bodyAttrs->addAttribute(nsHtml5AttributeName::ATTR_ID, id.release());
bodyAttrs->addAttribute(nsHtml5AttributeName::ATTR_ID, id.release(), -1);
auto klass = MakeUnique<nsString>();
if (mozilla::Preferences::GetBool("view_source.wrap_long_lines", true)) {
@ -24,14 +24,14 @@ nsHtml5ViewSourceUtils::NewBodyAttributes()
klass->Append(NS_LITERAL_STRING("highlight"));
}
if (!klass->IsEmpty()) {
bodyAttrs->addAttribute(nsHtml5AttributeName::ATTR_CLASS, klass.release());
bodyAttrs->addAttribute(nsHtml5AttributeName::ATTR_CLASS, klass.release(), -1);
}
int32_t tabSize = mozilla::Preferences::GetInt("view_source.tab_size", 4);
if (tabSize > 0) {
auto style = MakeUnique<nsString>(NS_LITERAL_STRING("-moz-tab-size: "));
style->AppendInt(tabSize);
bodyAttrs->addAttribute(nsHtml5AttributeName::ATTR_STYLE, style.release());
bodyAttrs->addAttribute(nsHtml5AttributeName::ATTR_STYLE, style.release(), -1);
}
return bodyAttrs;
@ -43,11 +43,11 @@ nsHtml5ViewSourceUtils::NewLinkAttributes()
{
nsHtml5HtmlAttributes* linkAttrs = new nsHtml5HtmlAttributes(0);
nsString* rel = new nsString(NS_LITERAL_STRING("stylesheet"));
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_REL, rel);
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_REL, rel, -1);
nsString* type = new nsString(NS_LITERAL_STRING("text/css"));
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_TYPE, type);
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_TYPE, type, -1);
nsString* href = new nsString(
NS_LITERAL_STRING("resource://gre-resources/viewsource.css"));
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_HREF, href);
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_HREF, href, -1);
return linkAttrs;
}