Skip to content
Open
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
3,506 changes: 1,785 additions & 1,721 deletions src/nu/validator/htmlparser/impl/AttributeName.java

Large diffs are not rendered by default.

578 changes: 324 additions & 254 deletions src/nu/validator/htmlparser/impl/ElementName.java

Large diffs are not rendered by default.

22 changes: 21 additions & 1 deletion src/nu/validator/htmlparser/impl/Portability.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static int checkedAdd(int a, int b) throws SAXException {
}

public static String newStringFromBuffer(@NoLength char[] buf, int offset, int length
// CPPONLY: , TreeBuilder treeBuilder, boolean maybeAtomize
// CPPONLY: , TreeBuilder treeBuilder, Interner interner
) {
return new String(buf, offset, length);
}
Expand Down Expand Up @@ -151,6 +151,26 @@ public static boolean stringEqualsString(String one, String other) {
return one.equals(other);
}

// [NOCPP[
public static boolean bufferStartsWithLiteralAssumeSufficientLength(char[] buf, String literal) {
for (int i = 0; i < literal.length(); i++) {
if (buf[i] != literal.charAt(i)) {
return false;
}
}
return true;
}

public static boolean bufferStartsWithLiteralAtOffsetAssumeSufficientLength(char[] buf, String literal, int offset) {
for (int i = 0; i < literal.length(); i++) {
if (buf[offset + i] != literal.charAt(i)) {
return false;
}
}
return true;
}
// ]NOCPP]

public static void delete(Object o) {

}
Expand Down
28 changes: 10 additions & 18 deletions src/nu/validator/htmlparser/impl/Tokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -989,30 +989,22 @@ private void emitOrAppendCharRefBuf(int returnState) throws SAXException {
strBuf[strBufLen++] = c;
}

/**
* The buffer as a String. Currently only used for error reporting.
*
* <p>
* C++ memory note: The return value must be released.
*
* @return the buffer as a string
*/
@Inline protected String strBufToString() {
String str = Portability.newStringFromBuffer(strBuf, 0, strBufLen
// CPPONLY: , tokenHandler, null
);
clearStrBufAfterUse();
return str;
}

@Inline protected String strBufToAttributeValueString() {
// CPPONLY: String digitAtom = TryAtomizeForSingleDigit();
// CPPONLY: if (digitAtom) {
// CPPONLY: return digitAtom;
// CPPONLY: }
// CPPONLY:
// CPPONLY: boolean maybeAtomize = false;
// CPPONLY: if (!newAttributesEachTime) {
// CPPONLY: if (attributeName == AttributeName.CLASS ||
// CPPONLY: attributeName == AttributeName.TYPE) {
// CPPONLY: maybeAtomize = true;
// CPPONLY: }
// CPPONLY: }
// CPPONLY:
String str = Portability.newStringFromBuffer(strBuf, 0, strBufLen
// CPPONLY: , tokenHandler, maybeAtomize
// CPPONLY: , tokenHandler, attributeName.isUseAtom() ? interner : null
);
clearStrBufAfterUse();
return str;
Expand Down Expand Up @@ -1397,7 +1389,7 @@ private void addAttributeWithValue() throws SAXException {
}
// ]NOCPP]
if (attributeName != null) {
String val = strBufToString(); // Ownership transferred to
String val = strBufToAttributeValueString(); // Ownership transferred to
// HtmlAttributes
// CPPONLY: if (mViewSource) {
// CPPONLY: mViewSource.MaybeLinkifyAttributeValue(attributeName, val);
Expand Down
82 changes: 51 additions & 31 deletions src/nu/validator/htmlparser/impl/TreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2967,7 +2967,7 @@ private void startTagScriptInHead(ElementName elementName, HtmlAttributes attrib
}

private void startTagTemplateInHead(ElementName elementName, HtmlAttributes attributes) throws SAXException {
appendToCurrentNodeAndPushElement(elementName, attributes);
appendToCurrentNodeAndPushTemplateElement(attributes);
insertMarker();
framesetOk = false;
originalMode = mode;
Expand All @@ -2990,26 +2990,6 @@ private boolean isSpecialParentInForeign(StackNode<T> stackNode) {
|| (("http://www.w3.org/1998/Math/MathML" == ns) && (stackNode.getGroup() == MI_MO_MN_MS_MTEXT));
}

private T getDeclarativeShadowRoot(T currentNode, T templateNode, HtmlAttributes attributes) {
if (!isAllowDeclarativeShadowRoots()) {
return null;
}

String shadowRootMode = attributes.getValue(AttributeName.SHADOWROOTMODE);
if (shadowRootMode == null) {
return null;
}

boolean shadowRootIsClonable = attributes.contains(AttributeName.SHADOWROOTCLONABLE);
boolean shadowRootIsSerializable = attributes.contains(AttributeName.SHADOWROOTSERIALIZABLE);
boolean shadowRootDelegatesFocus = attributes.contains(AttributeName.SHADOWROOTDELEGATESFOCUS);
boolean shadowRootCustomElementRegistry = attributes.contains(AttributeName.SHADOWROOTCUSTOMELEMENTREGISTRY);
String shadowRootReferenceTarget = attributes.getValue(AttributeName.SHADOWROOTREFERENCETARGET);
String shadowRootSlotAssignment = attributes.getValue(AttributeName.SHADOWROOTSLOTASSIGNMENT);

return getShadowRootFromHost(currentNode, templateNode, shadowRootMode, shadowRootIsClonable, shadowRootIsSerializable, shadowRootDelegatesFocus, shadowRootCustomElementRegistry, shadowRootSlotAssignment, shadowRootReferenceTarget);
}

/**
*
* <p>
Expand Down Expand Up @@ -3177,7 +3157,7 @@ public static String extractCharsetFromContent(String attributeValue
}
return Portability.newStringFromBuffer(buffer, start, end
- start
// CPPONLY: , tb, false
// CPPONLY: , tb, null
);
}
return null;
Expand Down Expand Up @@ -5286,19 +5266,59 @@ private void appendToCurrentNodeAndPushElement(ElementName elementName,
T elt = createElement("http://www.w3.org/1999/xhtml", elementName.getName(), attributes, currentNode
// CPPONLY: , htmlCreator(elementName.getHtmlCreator())
);
if (ElementName.TEMPLATE == elementName) {
T root = getDeclarativeShadowRoot(currentNode, elt, attributes);
if (root != null) {
setDocumentFragmentForTemplate(elt, root);
elt = root;
} else {
appendElement(elt, currentNode);
elt = getDocumentFragmentForTemplate(elt);
appendElement(elt, currentNode);
StackNode<T> node = createStackNode(elementName, elt
// [NOCPP[
, errorHandler == null ? null : new TaintableLocatorImpl(tokenizer)
// ]NOCPP]
);
push(node);
}

private void appendToCurrentNodeAndPushTemplateElement(HtmlAttributes attributes)
throws SAXException {
// [NOCPP[
checkAttributes(attributes, "http://www.w3.org/1999/xhtml");
// ]NOCPP]
// This method can't be called for custom elements
T currentNode = nodeFromStackWithBlinkCompat(currentPtr);
// All accesses to `attributes` must happen before `createElement`.
String shadowRootMode = null;
boolean shadowRootIsClonable = false;
boolean shadowRootIsSerializable = false;
boolean shadowRootDelegatesFocus = false;
boolean shadowRootCustomElementRegistry = false;
String shadowRootReferenceTarget = null;
String shadowRootSlotAssignment = null;
if (isAllowDeclarativeShadowRoots()) {
shadowRootMode = Portability.newStringFromString(attributes.getValue(AttributeName.SHADOWROOTMODE));
if (shadowRootMode != null) {
shadowRootIsClonable = attributes.contains(AttributeName.SHADOWROOTCLONABLE);
shadowRootIsSerializable = attributes.contains(AttributeName.SHADOWROOTSERIALIZABLE);
shadowRootDelegatesFocus = attributes.contains(AttributeName.SHADOWROOTDELEGATESFOCUS);
shadowRootCustomElementRegistry = attributes.contains(AttributeName.SHADOWROOTCUSTOMELEMENTREGISTRY);
shadowRootReferenceTarget = Portability.newStringFromString(attributes.getValue(AttributeName.SHADOWROOTREFERENCETARGET));
shadowRootSlotAssignment = Portability.newStringFromString(attributes.getValue(AttributeName.SHADOWROOTSLOTASSIGNMENT));
}
}
T elt = createElement("http://www.w3.org/1999/xhtml", "template", attributes, currentNode
// CPPONLY: , htmlCreator(NS_NewHTMLTemplateElement)
);
T root = null;
if (shadowRootMode != null) {
root = getShadowRootFromHost(currentNode, elt, shadowRootMode, shadowRootIsClonable, shadowRootIsSerializable, shadowRootDelegatesFocus, shadowRootCustomElementRegistry, shadowRootSlotAssignment, shadowRootReferenceTarget);
Portability.releaseString(shadowRootMode);
Portability.releaseString(shadowRootReferenceTarget);
Portability.releaseString(shadowRootSlotAssignment);
}
if (root != null) {
setDocumentFragmentForTemplate(elt, root);
elt = root;
} else {
appendElement(elt, currentNode);
elt = getDocumentFragmentForTemplate(elt);
}
StackNode<T> node = createStackNode(elementName, elt
StackNode<T> node = createStackNode(ElementName.TEMPLATE, elt
// [NOCPP[
, errorHandler == null ? null : new TaintableLocatorImpl(tokenizer)
// ]NOCPP]
Expand Down
37 changes: 35 additions & 2 deletions translator-src/nu/validator/htmlparser/cpptranslate/CppTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public class CppTypes {
PseudoElementAtom or NonInheritingAnonBoxAtom or InheritingAnonBoxAtom */
private static final Pattern ATOM_DEF = Pattern.compile("^\\s*Atom\\(\"([^,]+)\",\\s*\"([^\"]*)\"\\).*$");

private static final Pattern GKATOM = Pattern.compile("^.*nsGkAtoms::([a-zA-Z_]+)[^a-zA-Z_].*$");

private static Set<String> reservedWords = new HashSet<String>();

static {
Expand All @@ -81,6 +83,8 @@ public class CppTypes {
reservedWords.add("unicode");
}

private static Set<String> atomAttributes = new HashSet<String>();

private static Map<String, String> methodRenames = new HashMap<String, String>();

static {
Expand Down Expand Up @@ -128,7 +132,7 @@ public class CppTypes {
private static final String[] FORWARD_DECLARATIONS = { "nsHtml5StreamParser" };

private static final String[] CLASSES_THAT_NEED_SUPPLEMENT = {
"Tokenizer", "TreeBuilder", "UTF16Buffer", };
"Portability", "Tokenizer", "TreeBuilder", "UTF16Buffer" };

private static final String[] STATE_LOOP_POLICIES = {
"nsHtml5ViewSourcePolicy", "nsHtml5LineColPolicy", "nsHtml5FastestPolicy" };
Expand All @@ -137,12 +141,13 @@ public class CppTypes {

private final Writer atomWriter;

public CppTypes(File atomList, File generatedAtomFile) {
public CppTypes(File atomList, File generatedAtomFile, File atomAttributeFile) {
if (atomList == null) {
atomWriter = null;
} else {
try {
ingestAtoms(atomList);
ingestAtomAttributes(atomAttributeFile);
atomWriter = new OutputStreamWriter(new FileOutputStream(
generatedAtomFile), "utf-8");
this.start();
Expand Down Expand Up @@ -186,6 +191,23 @@ private void ingestAtoms(File atomList) throws IOException {
}
}

private void ingestAtomAttributes(File atomAttributeFile) throws IOException {
// This doesn't need to be efficient, so let's make it easy to write.
BufferedReader atomReader = new BufferedReader(
new InputStreamReader(new FileInputStream(atomAttributeFile), "utf-8"));
try {
String line;
while ((line = atomReader.readLine()) != null) {
Matcher m = GKATOM.matcher(line);
if (m.matches()) {
atomAttributes.add(m.group(1));
}
}
} finally {
atomReader.close();
}
}

public void start() {
try {

Expand All @@ -209,6 +231,9 @@ public void finished() {
} catch (IOException e) {
throw new RuntimeException(e);
}
for (String atomAttr : atomAttributes) {
System.err.println("Missing atom attribute: " + atomAttr);
}
}

public String classPrefix() {
Expand Down Expand Up @@ -377,6 +402,10 @@ public String stringForLiteral(String literal) {
return '"' + literal + '"';
}

public String utf16StringForLiteral(String literal) {
return "u\"" + literal + '"';
}

public String staticArrayTemplate() {
return "staticJArray";
}
Expand Down Expand Up @@ -555,4 +584,8 @@ public String releaseAssertionMacro() {
public String crashMacro() {
return "MOZ_CRASH";
}

public boolean useAtom(String name) {
return atomAttributes.remove(atomMap.get(name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,8 @@ public void visit(StringLiteralExpr n, LocalSymbolTable arg) {
printer.print(cppTypes.stringForLiteral(val));
} else if ("isQuirky".equals(currentMethod) && "html".equals(val)) {
printer.print(cppTypes.stringForLiteral(val));
} else if ("nameByBuffer".equals(currentMethod) || "elementNameByBuffer".equals(currentMethod)) {
printer.print(cppTypes.utf16StringForLiteral(val));
} else {
printer.print(cppTypes.localForLiteral(val));
}
Expand Down Expand Up @@ -1442,6 +1444,7 @@ public void visit(ObjectCreationExpr n, LocalSymbolTable arg) {
// Remove flags, and htmlLang from AttributeName constructor.
args.remove(6);
args.remove(4);
args.add(new BooleanLiteralExpr(cppTypes.useAtom(((StringLiteralExpr)args.get(1)).getValue())));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class Main {
* @throws IOException
*/
public static void main(String[] args) throws ParseException, IOException {
CppTypes cppTypes = new CppTypes(new File(args[2]), new File(args[3]));
CppTypes cppTypes = new CppTypes(new File(args[2]), new File(args[3]), new File(args[4]));
SymbolTable symbolTable = new SymbolTable();

File javaDirectory = new File(args[0]);
Expand Down
Loading